Files
alamin-br 7d7e532e9d fix(builder): restore table columns, menu items and options on undo (#6739)
* fix: restore builder element columns, menu items and options on undo

The update action only snapshotted allowed_fields, so related rows saved in
after_update (table columns, menu items, choice and property options) were
left out and undo/redo had nothing to restore.

* fix: snapshot only updated element values and skip deleted page links on undo

Exporting every related collection on each update cost queries for unrelated
changes, and replaying a link to a page deleted since made the whole undo fail.

* fix: keep click actions and page links of builder elements through undo

Undo hard-deleted the click actions of removed columns and menu items, trashed
ones included, and replayed links to trashed pages or skipped style changes.

* fix: keep trashed page links and replayed click actions through undo

Clearing links to trashed pages unlinked them for good, click actions trashed
by an undo or redo were never restored, and Kuma's formulas leaked into the
undo snapshot through prefetched column configs.

* fix(builder): keep click actions consistent across element update undo

Undo could duplicate a restored action in the editor, leave it on the old page,
delete nested or other elements' actions, or fail on an unused page link.

* fix(builder): echo trashed click actions to the requester only on element updates

A direct delete already removed the action in the requester's tab, and an echo
could hide it again after a quick undo. Menu item uids kept by an update now come
from one helper.
2026-11-20 17:58:13 +01:00

372 lines
11 KiB
JavaScript

import WorkflowActionService from '@baserow/modules/builder/services/workflowAction'
import PublishedBuilderService from '@baserow/modules/builder/services/publishedBuilder'
import _ from 'lodash'
import {
markRealtimeMetadata,
realtimeMetadata,
} from '@baserow/modules/core/utils/realtime'
const updateContext = {
updateTimeout: null,
promiseResolve: null,
lastUpdatedValues: null,
valuesToUpdate: {},
}
export function populateWorkflowAction(workflowAction) {
return {
...workflowAction,
_: {
loading: false,
dispatching: false,
dispatchedById: null,
...realtimeMetadata(),
},
}
}
const state = () => ({})
const mutations = {
ADD_ITEM(state, { page, workflowAction }) {
// An undo restores a workflow action every tab hears about, the one that
// already has it included, so add it only once.
const index = page.workflowActions.findIndex(
({ id }) => id === workflowAction.id
)
if (index > -1) {
page.workflowActions.splice(
index,
1,
populateWorkflowAction(workflowAction)
)
} else {
page.workflowActions.push(populateWorkflowAction(workflowAction))
}
},
SET_ITEMS(state, { page, workflowActions }) {
page.workflowActions = workflowActions.map((workflowAction) =>
populateWorkflowAction(workflowAction)
)
},
DELETE_ITEM(state, { page, workflowActionId }) {
const index = page.workflowActions.findIndex(
(workflowAction) => workflowAction.id === workflowActionId
)
if (index > -1) {
page.workflowActions.splice(index, 1)
}
},
UPDATE_ITEM(
state,
{
page,
workflowAction: workflowActionToUpdate,
values,
overwrite = false,
viaRealtime = false,
}
) {
const index = page.workflowActions.findIndex(
(wa) => wa.id === workflowActionToUpdate.id
)
if (index === -1) {
// The action might have been deleted during the debounced update
return
}
const existing = page.workflowActions[index]
const {
id,
page_id: pageId,
element_id: elementId,
event,
order,
} = existing
if (overwrite) {
const newValue = populateWorkflowAction({
id,
page_id: pageId,
element_id: elementId,
event,
order,
...values,
})
// Carry the realtime version over the object replacement so a subsequent
// increment is still detected as a change by watchers.
newValue._.realtimeVersion = existing?._?.realtimeVersion || 0
markRealtimeMetadata(newValue, viaRealtime)
page.workflowActions.splice(index, 1, newValue)
} else {
Object.assign(existing, values)
markRealtimeMetadata(existing, viaRealtime)
}
},
SET_ITEM(state, { page, workflowAction: workflowActionToSet, values }) {
page.workflowActions = page.workflowActions.map((workflowAction) =>
workflowAction.id === workflowActionToSet.id
? populateWorkflowAction(values)
: workflowAction
)
},
ORDER_ITEMS(state, { page, order, elementId = null }) {
page.workflowActions.forEach((workflowAction) => {
// Only reorder the actions belonging to the targeted element. Without this,
// actions of other elements (not present in `order`) would fall into the
// `index === -1` branch and have their order reset to 0, visibly reshuffling
// them - which is what happened when reordering one element's actions
// reordered another's.
if (elementId !== null && workflowAction.element_id !== elementId) {
return
}
const index = order.findIndex((value) => value === workflowAction.id)
workflowAction.order = index === -1 ? 0 : index + 1
})
},
SET_LOADING(state, { workflowAction, value }) {
workflowAction._.loading = value
},
SET_DISPATCHING(state, { workflowAction, dispatchedById, isDispatching }) {
workflowAction._.dispatchedById = dispatchedById
workflowAction._.dispatching = isDispatching
},
}
const actions = {
forceCreate({ commit }, { page, workflowAction }) {
commit('ADD_ITEM', { page, workflowAction })
},
forceDelete({ commit }, { page, workflowActionId }) {
commit('DELETE_ITEM', { page, workflowActionId })
},
forceUpdate(
{ commit },
{ page, workflowAction, values, overwrite, viaRealtime }
) {
commit('UPDATE_ITEM', {
page,
workflowAction,
values,
overwrite,
viaRealtime,
})
},
forceSet({ commit }, { page, workflowAction, values }) {
commit('SET_ITEM', { page, workflowAction, values })
},
forceOrder({ commit }, { page, order, elementId = null }) {
commit('ORDER_ITEMS', { page, order, elementId })
},
async create(
{ dispatch },
{ page, workflowActionType, eventType, configuration = null }
) {
const { $registry, $i18n, $client, $config } = this
const { data: workflowAction } = await WorkflowActionService(
$client
).create(page.id, workflowActionType, eventType, configuration)
await dispatch('forceCreate', { page, workflowAction })
return workflowAction
},
async fetch({ commit }, { page }) {
const { $registry, $i18n, $client, $config } = this
const { data: workflowActions } = await WorkflowActionService(
$client
).fetchAll(page.id)
commit('SET_ITEMS', { page, workflowActions })
},
async fetchPublished({ commit, rootGetters }, { page }) {
const { $registry, $i18n, $client, $config } = this
const previewBuilderId = rootGetters['publicBuilder/getPreviewBuilderId']
const { data: workflowActions } = await PublishedBuilderService(
$client
).fetchWorkflowActions(page.id, previewBuilderId)
commit('SET_ITEMS', { page, workflowActions })
},
async delete({ dispatch }, { page, workflowAction }) {
const { $registry, $i18n, $client, $config } = this
dispatch('forceDelete', { page, workflowActionId: workflowAction.id })
try {
await WorkflowActionService($client).delete(workflowAction.id)
} catch (error) {
await dispatch('forceCreate', { page, workflowAction })
throw error
}
},
async updateDebounced(
{ dispatch, commit },
{ page, workflowAction, values }
) {
const { $registry, $i18n, $client, $config } = this
// These values should not be updated via a regular update request
const excludeValues = ['order']
const oldValues = {}
Object.keys(values).forEach((name) => {
if (
Object.prototype.hasOwnProperty.call(workflowAction, name) &&
!excludeValues.includes(name)
) {
oldValues[name] = workflowAction[name]
// Accumulate the changed values to send all the ongoing changes with the
// final request. Ensure that the values are cloned by calling `_.cloneDeep`,
// as there is a (currently) unexplained issue with an `OpenPage` workflow action
// `page_parameter` not being cloned properly. Using lodash instead of
// structuredClone because Vue reactive proxies can't be structuredClone'd.
updateContext.valuesToUpdate[name] = _.cloneDeep(values[name])
}
})
await dispatch('forceUpdate', {
page,
workflowAction,
values: updateContext.valuesToUpdate,
overwrite: !!updateContext.valuesToUpdate.type,
})
return new Promise((resolve, reject) => {
const fire = async () => {
commit('SET_LOADING', { workflowAction, value: true })
const toUpdate = updateContext.valuesToUpdate
updateContext.valuesToUpdate = {}
try {
const { data } = await WorkflowActionService($client).update(
workflowAction.id,
toUpdate
)
updateContext.lastUpdatedValues = null
excludeValues.forEach((name) => {
delete data[name]
})
await dispatch('forceUpdate', {
page,
workflowAction,
values: data,
})
resolve()
} catch (error) {
await dispatch('forceUpdate', {
page,
workflowAction,
values: updateContext.lastUpdatedValues,
})
updateContext.lastUpdatedValues = null
reject(error)
}
updateContext.lastUpdatedValues = null
commit('SET_LOADING', { workflowAction, value: false })
}
if (updateContext.promiseResolve) {
updateContext.promiseResolve()
updateContext.promiseResolve = null
}
clearTimeout(updateContext.updateTimeout)
if (!updateContext.lastUpdatedValues) {
updateContext.lastUpdatedValues = oldValues
}
updateContext.updateTimeout = setTimeout(fire, 500)
updateContext.promiseResolve = resolve
})
},
async dispatchAction(
{ dispatch, rootGetters },
{ workflowActionId, data, files }
) {
const { $registry, $i18n, $client, $config } = this
const { data: result } = await WorkflowActionService($client).dispatch(
workflowActionId,
data,
files,
rootGetters['publicBuilder/getPreviewBuilderId']
)
return result
},
async order({ commit, getters }, { page, order, element = null }) {
const { $registry, $i18n, $client, $config } = this
const workflowActions =
element !== null
? getters.getElementWorkflowActions(page, element.id)
: getters.getWorkflowActions(page)
const oldOrder = workflowActions.map(({ id }) => id)
const elementId = element !== null ? element.id : null
commit('ORDER_ITEMS', { page, order, elementId })
try {
await WorkflowActionService($client).order(page.id, order, element.id)
} catch (error) {
commit('ORDER_ITEMS', { page, order: oldOrder, elementId })
throw error
}
},
setDispatching(
{ commit },
{ workflowAction, dispatchedById, isDispatching }
) {
commit('SET_DISPATCHING', {
workflowAction,
dispatchedById,
isDispatching,
})
},
}
const getters = {
getWorkflowActions: (state) => (page) => {
return page.workflowActions.map((w) => w).sort((a, b) => a.order - b.order)
},
getWorkflowActionById: (state, getters) => (page, workflowActionId) => {
return getters
.getWorkflowActions(page)
.find((workflowAction) => workflowAction.id === workflowActionId)
},
getElementWorkflowActions: (state) => (page, elementId) => {
return page.workflowActions
.filter((workflowAction) => workflowAction.element_id === elementId)
.sort((a, b) => a.order - b.order)
},
getElementPreviousWorkflowActions:
(state, getters) => (page, elementId, workflowAction) => {
return _.takeWhile(
getters
.getElementWorkflowActions(page, elementId)
.filter(
(workflowActionToFilter) =>
workflowActionToFilter.event === workflowAction.event
),
(workflowActionAll) => workflowActionAll.id !== workflowAction.id
)
},
getLoading: (state) => (workflowAction) => {
return workflowAction._?.loading
},
getDispatching: (state) => (workflowAction, dispatchedById) => {
return (
workflowAction._.dispatching &&
workflowAction._.dispatchedById === dispatchedById
)
},
}
export default {
namespaced: true,
state,
mutations,
actions,
getters,
}