fix: improvements
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
* with initial graph from recollection storage (or example). Save is explicit via save().
|
||||
*/
|
||||
|
||||
import { useCallback, useMemo, useRef, useState } from 'react'
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
||||
import { useGraphStateWithHistory } from '@/hooks/useGraphStateWithHistory'
|
||||
import { getInitialGraph } from '@/app/canvas/canvasGraphUtils'
|
||||
import { saveGraphToStorage, RECOLLECTION_VERSION } from '@/app/recollections/state/recollectionGraphStorage'
|
||||
@@ -59,5 +59,16 @@ export function useCanvasGraph(recollectionId: string | undefined): UseCanvasGra
|
||||
}, SAVING_DISPLAY_MS)
|
||||
}, [recollectionId])
|
||||
|
||||
// Auto-save after 5 seconds of inactivity when there are unsaved changes.
|
||||
// Prevents data loss if the user closes the tab without pressing Ctrl+S.
|
||||
const AUTO_SAVE_DELAY_MS = 5000
|
||||
const saveRef = useRef(save)
|
||||
saveRef.current = save
|
||||
useEffect(() => {
|
||||
if (!isDirty || !recollectionId) return
|
||||
const timer = setTimeout(() => saveRef.current(), AUTO_SAVE_DELAY_MS)
|
||||
return () => clearTimeout(timer)
|
||||
}, [isDirty, recollectionId, currentSerialized])
|
||||
|
||||
return { ...result, save, saveStatus }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user