-
-
Notifications
You must be signed in to change notification settings - Fork 17.4k
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
Feature: Autosave and Undo/Redo #3687
Open
0xi4o
wants to merge
30
commits into
main
Choose a base branch
from
feature/autosave
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
a6d9062
Implement autosave and undo/redo for chatflow and agentflow
0xi4o 718349d
Update saving indicator and show last saved timestamp
0xi4o 1079c4e
Fix console error on first load
0xi4o 9110187
Fix issues in undo/redo - intermediate steps, pre-existing history on…
0xi4o 0188e14
Fix console errors in configuration and api code dialogs
0xi4o 0bbe431
Trigger autosave when deleting nodes/edges and when duplicating nodes
0xi4o 370a6fe
Track chatflow/agentflow name change in undo/redo history
0xi4o d0a9890
Update autosave indicator text
0xi4o 63d67ea
Handle keyboard shortcuts for undo and redo
0xi4o e37ae82
Update node actions - remove tooltip and use css to hide/show
0xi4o 9c75040
Prevent save indicator and autosaved showing for new chatflows before…
0xi4o 4b3d50b
Fix onDragNodeStop triggering on clicking inside nodes and fix consol…
0xi4o 43bb823
Trigger SET_DIRTY and SET_CHATFLOW when node inputs data changes
0xi4o e63f625
Fix issue where exiting canvas doesn't clear canvas data and history
0xi4o baaf56b
Fix issue with undo/redo keybindings not working
0xi4o 4623387
Fix undo/redo not updating input fields in nodes
0xi4o 6aa1ce0
Detect changes in additional params and format prompt values dialogs …
0xi4o 7a0f3f4
Fix autosave on dropping nodes into the canvas
0xi4o 0825738
Highlight condition dialog button if inputs inside condition dialog c…
0xi4o 9c0a6e4
Enable redux devtools extension with tracing
0xi4o 73c211a
Update reactflow controls styles in marketplace canvas
0xi4o 8478285
Fix conflicts
0xi4o d1afba7
Update add nodes and sync versions buttons
0xi4o 4368ca0
Optimize add nodes component
0xi4o f20d136
Avoid autosave on initial load
0xi4o 6d06392
Go to agentflows page when pressing back in agent canvas
0xi4o 1b43638
Memoize canvas node and sticky note components
0xi4o 1ac0b0b
Fix conflicts
0xi4o 8350552
Update styles for controls in flow and marketplace canvas
0xi4o edff7a8
Trigger autosave only on blur for input fields - string, password, nu…
0xi4o 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 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 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,98 @@ | ||
import { useCallback, useEffect, useRef, useState } from 'react' | ||
import { useSelector } from 'react-redux' | ||
import { isEqual } from 'lodash' | ||
|
||
const useAutoSave = ({ onAutoSave, interval = 60000, debounce = 1000 }) => { | ||
const canvas = useSelector((state) => state.canvas.present) | ||
const [isSaving, setIsSaving] = useState(false) | ||
const timeoutRef = useRef(null) | ||
const previousSaveRef = useRef(null) | ||
const onAutoSaveRef = useRef(onAutoSave) | ||
const initialLoadRef = useRef(true) | ||
|
||
useEffect(() => { | ||
onAutoSaveRef.current = onAutoSave | ||
}, [onAutoSave]) | ||
|
||
// debounced save function | ||
const debouncedSave = useCallback(() => { | ||
if (initialLoadRef.current) { | ||
initialLoadRef.current = false | ||
return | ||
} | ||
|
||
setIsSaving(true) | ||
|
||
if (timeoutRef.current) { | ||
clearTimeout(timeoutRef.current) | ||
} | ||
|
||
timeoutRef.current = setTimeout(() => { | ||
if (canvas.chatflow && canvas.chatflow.flowData) { | ||
const currentData = { | ||
chatflowId: canvas.chatflow.id, | ||
chatflowName: canvas.chatflow.name, | ||
flowData: canvas.chatflow.flowData | ||
} | ||
|
||
const hasChanged = !previousSaveRef.current || !isEqual(currentData, previousSaveRef.current) | ||
|
||
if (hasChanged) { | ||
onAutoSaveRef.current(currentData) | ||
previousSaveRef.current = currentData | ||
} | ||
} | ||
setIsSaving(false) | ||
}, debounce) | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [debounce, canvas.chatflow?.id, canvas.chatflow?.name, canvas.chatflow?.flowData]) | ||
|
||
// watch for changes to trigger debounced save | ||
useEffect(() => { | ||
if (canvas.chatflow?.flowData || canvas.chatflow?.name) { | ||
debouncedSave() | ||
} | ||
}, [canvas.chatflow?.flowData, canvas.chatflow?.name, debouncedSave]) | ||
|
||
// periodic saves | ||
useEffect(() => { | ||
const intervalId = setInterval(() => { | ||
if (canvas.chatflow && !isEqual(canvas.chatflow, previousSaveRef.current)) { | ||
onAutoSaveRef.current({ | ||
chatflowId: canvas.chatflow.id, | ||
chatflowName: canvas.chatflow.name, | ||
flowData: canvas.chatflow.flowData | ||
}) | ||
previousSaveRef.current = canvas.chatflow | ||
} | ||
}, interval) | ||
|
||
return () => { | ||
clearInterval(intervalId) | ||
if (timeoutRef.current) { | ||
clearTimeout(timeoutRef.current) | ||
} | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [interval]) | ||
|
||
// force an immediate save | ||
const forceSave = useCallback(() => { | ||
if (timeoutRef.current) { | ||
clearTimeout(timeoutRef.current) | ||
} | ||
|
||
if (canvas.chatflow && !isEqual(canvas.chatflow, previousSaveRef.current)) { | ||
onAutoSaveRef.current({ | ||
chatflowId: canvas.chatflow.id, | ||
chatflowName: canvas.chatflow.name, | ||
flowData: canvas.chatflow.flowData | ||
}) | ||
previousSaveRef.current = canvas.chatflow | ||
} | ||
}, [canvas.chatflow]) | ||
|
||
return [canvas.chatflow, isSaving, forceSave] | ||
} | ||
|
||
export default useAutoSave |
This file contains 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 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 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 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 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 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 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 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 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 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 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there are other places that uses AsyncDropdown, for example when creating custom assistant. In those cases, there will be no nodeData, then it will throw error: