From d73a8957a9c3647271be008d29cab0a1d78b192e Mon Sep 17 00:00:00 2001 From: Emir Karabeg Date: Thu, 30 Jan 2025 11:50:52 -0800 Subject: [PATCH] Fixed and improved version history --- .../components/history-dropdown-item.tsx | 41 ++++++++++++++++--- app/w/components/control-bar/control-bar.tsx | 33 ++++++++++++--- stores/workflow/history-middleware.ts | 21 ++++++++++ stores/workflow/history-types.ts | 1 + 4 files changed, 86 insertions(+), 10 deletions(-) diff --git a/app/w/components/control-bar/components/history-dropdown-item.tsx b/app/w/components/control-bar/components/history-dropdown-item.tsx index 2c76066f1..33d2640a3 100644 --- a/app/w/components/control-bar/components/history-dropdown-item.tsx +++ b/app/w/components/control-bar/components/history-dropdown-item.tsx @@ -8,6 +8,7 @@ interface HistoryDropdownItemProps { timestamp: number onClick?: () => void isCurrent?: boolean + isFuture?: boolean id?: string } @@ -16,25 +17,55 @@ export function HistoryDropdownItem({ timestamp, onClick, isCurrent = false, + isFuture = false, id, }: HistoryDropdownItemProps) { const timeAgo = formatDistanceToNow(timestamp, { addSuffix: true }) return ( - +
{isCurrent ? ( - Current + + Current + ) : ( - {timeAgo} + + {timeAgo} + )}
-

{action}

+

+ {action} +

) diff --git a/app/w/components/control-bar/control-bar.tsx b/app/w/components/control-bar/control-bar.tsx index 287865b8d..7f5279628 100644 --- a/app/w/components/control-bar/control-bar.tsx +++ b/app/w/components/control-bar/control-bar.tsx @@ -25,7 +25,7 @@ import { export function ControlBar() { const { notifications, getWorkflowNotifications } = useNotificationStore() - const { history, undo, redo } = useWorkflowStore() + const { history, undo, redo, revertToHistoryState } = useWorkflowStore() const [, forceUpdate] = useState({}) const { isExecuting, handleRunWorkflow } = useWorkflowExecution() const { workflows, removeWorkflow, activeWorkflowId } = useWorkflowRegistry() @@ -109,15 +109,33 @@ export function ControlBar() { - {history.past.length === 0 ? ( + {history.past.length === 0 && history.future.length === 0 ? ( No history available ) : ( - + <> + {[...history.future].reverse().map((entry, index) => ( + + revertToHistoryState( + history.past.length + + 1 + + (history.future.length - 1 - index) + ) + } + isFuture={true} + /> + ))} + revertToHistoryState(history.past.length - 1 - index) + } /> ))} @@ -153,7 +173,10 @@ export function ControlBar() { ) : ( - + {[...workflowNotifications] .sort((a, b) => b.timestamp - a.timestamp) .map((notification) => ( diff --git a/stores/workflow/history-middleware.ts b/stores/workflow/history-middleware.ts index cce9033d5..939c7f0cc 100644 --- a/stores/workflow/history-middleware.ts +++ b/stores/workflow/history-middleware.ts @@ -86,6 +86,27 @@ export const withHistory = ( set(newState) return newState }, + + revertToHistoryState: (index: number) => { + const { history, ...state } = get() + const allStates = [...history.past, history.present, ...history.future] + const targetState = allStates[index] + + if (!targetState) return + + const newPast = allStates.slice(0, index) + const newFuture = allStates.slice(index + 1) + + set({ + ...state, + ...targetState.state, + history: { + past: newPast, + present: targetState, + future: newFuture, + }, + }) + }, } } } diff --git a/stores/workflow/history-types.ts b/stores/workflow/history-types.ts index 94d292d33..fdb115b2b 100644 --- a/stores/workflow/history-types.ts +++ b/stores/workflow/history-types.ts @@ -17,4 +17,5 @@ export interface HistoryActions { redo: () => void canUndo: () => boolean canRedo: () => boolean + revertToHistoryState: (index: number) => void } \ No newline at end of file