Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(autosave): store idle-reset timer ref to prevent status corruptio…
…n on rapid saves
  • Loading branch information
waleedlatif1 committed Mar 18, 2026
commit 2f13d0a7939b284e6042718323db391fa582605e
5 changes: 4 additions & 1 deletion apps/sim/hooks/use-autosave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export function useAutosave({
}: UseAutosaveOptions): UseAutosaveReturn {
const [saveStatus, setSaveStatus] = useState<SaveStatus>('idle')
const timerRef = useRef<ReturnType<typeof setTimeout>>(undefined)
const idleTimerRef = useRef<ReturnType<typeof setTimeout>>(undefined)
const savingRef = useRef(false)
const onSaveRef = useRef(onSave)
onSaveRef.current = onSave
Expand Down Expand Up @@ -59,7 +60,8 @@ export function useAutosave({
const remaining = Math.max(0, MIN_SAVING_DISPLAY_MS - elapsed)
setTimeout(() => {
setSaveStatus(nextStatus)
setTimeout(() => setSaveStatus('idle'), 2000)
clearTimeout(idleTimerRef.current)
idleTimerRef.current = setTimeout(() => setSaveStatus('idle'), 2000)
savingRef.current = false
if (nextStatus !== 'error' && contentRef.current !== savedContentRef.current) {
save()
Expand All @@ -78,6 +80,7 @@ export function useAutosave({
useEffect(() => {
return () => {
clearTimeout(timerRef.current)
clearTimeout(idleTimerRef.current)
if (contentRef.current !== savedContentRef.current && !savingRef.current) {
onSaveRef.current().catch(() => {})
}
Expand Down
Loading