2
0

🐛 (editor) Fix saving typebot after undoing changes

This commit is contained in:
Baptiste Arnaud
2023-02-23 08:17:07 +01:00
parent be4c8e0760
commit 671c2cb101

View File

@@ -50,7 +50,7 @@ const reducer = <T extends { updatedAt: Date } | undefined>(
switch (action.type) { switch (action.type) {
case ActionType.Undo: { case ActionType.Undo: {
if (past.length === 0) { if (past.length === 0 || !present) {
return state return state
} }
@@ -59,7 +59,7 @@ const reducer = <T extends { updatedAt: Date } | undefined>(
return { return {
past: newPast, past: newPast,
present: previous, present: { ...previous, updatedAt: present.updatedAt },
future: [present, ...future], future: [present, ...future],
} }
} }
@@ -79,7 +79,7 @@ const reducer = <T extends { updatedAt: Date } | undefined>(
} }
case ActionType.Set: { case ActionType.Set: {
const { newPresent, updateDate } = action const { newPresent } = action
if ( if (
isNotDefined(newPresent) || isNotDefined(newPresent) ||
(present && (present &&
@@ -90,19 +90,10 @@ const reducer = <T extends { updatedAt: Date } | undefined>(
) { ) {
return state return state
} }
// Uncomment to debug history ⬇️
// console.log(
// diff(
// JSON.parse(JSON.stringify(newPresent)),
// present ? JSON.parse(JSON.stringify(present)) : {}
// )
// )
return { return {
past: [...past, present].filter(isDefined), past: [...past, present].filter(isDefined),
present: { present: newPresent,
...newPresent,
updatedAt: updateDate ? new Date() : newPresent.updatedAt,
},
future: [], future: [],
} }
} }
@@ -136,21 +127,17 @@ const useUndo = <T extends { updatedAt: Date } | undefined>(
} }
}, [canRedo]) }, [canRedo])
const set = useCallback( const set = useCallback((newPresent: T | ((current: T) => T)) => {
(newPresent: T | ((current: T) => T), options = { updateDate: true }) => { const updatedTypebot =
const updatedTypebot = newPresent && typeof newPresent === 'function'
newPresent && typeof newPresent === 'function' ? newPresent(presentRef.current)
? newPresent(presentRef.current) : newPresent
: newPresent presentRef.current = updatedTypebot
presentRef.current = updatedTypebot dispatch({
dispatch({ type: ActionType.Set,
type: ActionType.Set, newPresent: updatedTypebot,
newPresent: updatedTypebot, })
updateDate: options.updateDate, }, [])
})
},
[]
)
const flush = useCallback(() => { const flush = useCallback(() => {
dispatch({ type: ActionType.Flush }) dispatch({ type: ActionType.Flush })