-
Notifications
You must be signed in to change notification settings - Fork 3.3k
feat(code): undo-redo state #3018
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
17b2091
feat(code): undo-redo state
icecrasher321 ae5c1bb
address greptile
icecrasher321 bd827d4
address bugbot comments
icecrasher321 865a5bb
fix debounce flush
icecrasher321 b0338d3
inc debounce time
icecrasher321 be556b9
fix wand case
icecrasher321 7d1a1b8
address comments
icecrasher321 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,239 @@ | ||
| import { useCallback, useEffect, useMemo, useRef } from 'react' | ||
| import { createLogger } from '@sim/logger' | ||
| import { useShallow } from 'zustand/react/shallow' | ||
| import { useCollaborativeWorkflow } from '@/hooks/use-collaborative-workflow' | ||
| import { useCodeUndoRedoStore } from '@/stores/undo-redo' | ||
| import { useWorkflowDiffStore } from '@/stores/workflow-diff/store' | ||
| import { useWorkflowRegistry } from '@/stores/workflows/registry/store' | ||
|
|
||
| const logger = createLogger('CodeUndoRedo') | ||
|
|
||
| interface UseCodeUndoRedoOptions { | ||
| blockId: string | ||
| subBlockId: string | ||
| value: string | ||
| enabled?: boolean | ||
| isReadOnly?: boolean | ||
| isStreaming?: boolean | ||
| debounceMs?: number | ||
| } | ||
|
|
||
| export function useCodeUndoRedo({ | ||
| blockId, | ||
| subBlockId, | ||
| value, | ||
| enabled = true, | ||
| isReadOnly = false, | ||
| isStreaming = false, | ||
| debounceMs = 500, | ||
| }: UseCodeUndoRedoOptions) { | ||
| const { collaborativeSetSubblockValue } = useCollaborativeWorkflow() | ||
| const activeWorkflowId = useWorkflowRegistry((state) => state.activeWorkflowId) | ||
| const { isShowingDiff, hasActiveDiff } = useWorkflowDiffStore( | ||
| useShallow((state) => ({ | ||
| isShowingDiff: state.isShowingDiff, | ||
| hasActiveDiff: state.hasActiveDiff, | ||
| })) | ||
| ) | ||
|
|
||
| const isBaselineView = hasActiveDiff && !isShowingDiff | ||
| const isEnabled = useMemo( | ||
| () => Boolean(enabled && activeWorkflowId && !isReadOnly && !isStreaming && !isBaselineView), | ||
| [enabled, activeWorkflowId, isReadOnly, isStreaming, isBaselineView] | ||
| ) | ||
| const isReplaceEnabled = useMemo( | ||
| () => Boolean(enabled && activeWorkflowId && !isReadOnly && !isBaselineView), | ||
| [enabled, activeWorkflowId, isReadOnly, isBaselineView] | ||
| ) | ||
|
|
||
| const lastCommittedValueRef = useRef<string>(value ?? '') | ||
| const pendingBeforeRef = useRef<string | null>(null) | ||
| const pendingAfterRef = useRef<string | null>(null) | ||
| const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null) | ||
| const isApplyingRef = useRef(false) | ||
|
|
||
| const clearTimer = useCallback(() => { | ||
| if (timeoutRef.current) { | ||
| clearTimeout(timeoutRef.current) | ||
| timeoutRef.current = null | ||
| } | ||
| }, []) | ||
|
|
||
| const resetPending = useCallback(() => { | ||
| pendingBeforeRef.current = null | ||
| pendingAfterRef.current = null | ||
| }, []) | ||
|
|
||
| const commitPending = useCallback(() => { | ||
| if (!isEnabled || !activeWorkflowId) { | ||
| clearTimer() | ||
| resetPending() | ||
| return | ||
| } | ||
|
|
||
| const before = pendingBeforeRef.current | ||
| const after = pendingAfterRef.current | ||
| if (before === null || after === null) return | ||
|
|
||
| if (before === after) { | ||
| lastCommittedValueRef.current = after | ||
| clearTimer() | ||
| resetPending() | ||
| return | ||
| } | ||
|
|
||
| useCodeUndoRedoStore.getState().push({ | ||
| id: crypto.randomUUID(), | ||
| createdAt: Date.now(), | ||
| workflowId: activeWorkflowId, | ||
| blockId, | ||
| subBlockId, | ||
| before, | ||
| after, | ||
| }) | ||
|
|
||
| lastCommittedValueRef.current = after | ||
| clearTimer() | ||
| resetPending() | ||
| }, [activeWorkflowId, blockId, clearTimer, isEnabled, resetPending, subBlockId]) | ||
|
|
||
| const recordChange = useCallback( | ||
| (nextValue: string) => { | ||
| if (!isEnabled || isApplyingRef.current) return | ||
|
|
||
| if (pendingBeforeRef.current === null) { | ||
| pendingBeforeRef.current = lastCommittedValueRef.current ?? '' | ||
| } | ||
|
|
||
| pendingAfterRef.current = nextValue | ||
| clearTimer() | ||
| timeoutRef.current = setTimeout(commitPending, debounceMs) | ||
| }, | ||
| [clearTimer, commitPending, debounceMs, isEnabled] | ||
| ) | ||
|
|
||
| const recordReplace = useCallback( | ||
| (nextValue: string) => { | ||
| if (!isReplaceEnabled || isApplyingRef.current || !activeWorkflowId) return | ||
|
|
||
| if (pendingBeforeRef.current !== null) { | ||
| commitPending() | ||
| } | ||
|
|
||
| const before = lastCommittedValueRef.current ?? '' | ||
| if (before === nextValue) { | ||
| lastCommittedValueRef.current = nextValue | ||
| resetPending() | ||
| return | ||
| } | ||
|
|
||
| useCodeUndoRedoStore.getState().push({ | ||
| id: crypto.randomUUID(), | ||
| createdAt: Date.now(), | ||
| workflowId: activeWorkflowId, | ||
| blockId, | ||
| subBlockId, | ||
| before, | ||
| after: nextValue, | ||
| }) | ||
|
|
||
| lastCommittedValueRef.current = nextValue | ||
| clearTimer() | ||
| resetPending() | ||
| }, | ||
| [ | ||
| activeWorkflowId, | ||
| blockId, | ||
| clearTimer, | ||
| commitPending, | ||
| isReplaceEnabled, | ||
| resetPending, | ||
| subBlockId, | ||
| ] | ||
| ) | ||
|
|
||
| const flushPending = useCallback(() => { | ||
| if (pendingBeforeRef.current === null) return | ||
| clearTimer() | ||
| commitPending() | ||
| }, [clearTimer, commitPending]) | ||
|
|
||
| const startSession = useCallback( | ||
| (currentValue: string) => { | ||
| clearTimer() | ||
| resetPending() | ||
| lastCommittedValueRef.current = currentValue ?? '' | ||
| }, | ||
| [clearTimer, resetPending] | ||
| ) | ||
|
|
||
| const applyValue = useCallback( | ||
| (nextValue: string) => { | ||
| if (!isEnabled) return | ||
| isApplyingRef.current = true | ||
| try { | ||
| collaborativeSetSubblockValue(blockId, subBlockId, nextValue) | ||
| } finally { | ||
| isApplyingRef.current = false | ||
| } | ||
| lastCommittedValueRef.current = nextValue | ||
| clearTimer() | ||
| resetPending() | ||
| }, | ||
| [blockId, clearTimer, collaborativeSetSubblockValue, isEnabled, resetPending, subBlockId] | ||
| ) | ||
|
|
||
| const undo = useCallback(() => { | ||
| if (!activeWorkflowId || !isEnabled) return | ||
| if (pendingBeforeRef.current !== null) { | ||
| flushPending() | ||
| } | ||
| const entry = useCodeUndoRedoStore.getState().undo(activeWorkflowId, blockId, subBlockId) | ||
| if (!entry) return | ||
| logger.debug('Undo code edit', { blockId, subBlockId }) | ||
| applyValue(entry.before) | ||
| }, [activeWorkflowId, applyValue, blockId, flushPending, isEnabled, subBlockId]) | ||
|
|
||
| const redo = useCallback(() => { | ||
| if (!activeWorkflowId || !isEnabled) return | ||
| if (pendingBeforeRef.current !== null) { | ||
| flushPending() | ||
| } | ||
| const entry = useCodeUndoRedoStore.getState().redo(activeWorkflowId, blockId, subBlockId) | ||
| if (!entry) return | ||
| logger.debug('Redo code edit', { blockId, subBlockId }) | ||
| applyValue(entry.after) | ||
| }, [activeWorkflowId, applyValue, blockId, flushPending, isEnabled, subBlockId]) | ||
|
|
||
| useEffect(() => { | ||
| if (isApplyingRef.current || isStreaming) return | ||
|
|
||
| const nextValue = value ?? '' | ||
|
|
||
| if (pendingBeforeRef.current !== null) { | ||
| if (pendingAfterRef.current !== nextValue) { | ||
| clearTimer() | ||
| resetPending() | ||
| lastCommittedValueRef.current = nextValue | ||
| } | ||
| return | ||
| } | ||
|
|
||
| lastCommittedValueRef.current = nextValue | ||
| }, [clearTimer, isStreaming, resetPending, value]) | ||
|
|
||
| useEffect(() => { | ||
| return () => { | ||
| flushPending() | ||
| } | ||
| }, [flushPending]) | ||
|
|
||
| return { | ||
| recordChange, | ||
| recordReplace, | ||
| flushPending, | ||
| startSession, | ||
| undo, | ||
| redo, | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.