/ hooks / useFileHistorySnapshotInit.ts
useFileHistorySnapshotInit.ts
 1  import { useEffect, useRef } from 'react'
 2  import {
 3    type FileHistorySnapshot,
 4    type FileHistoryState,
 5    fileHistoryEnabled,
 6    fileHistoryRestoreStateFromLog,
 7  } from '../utils/fileHistory.js'
 8  
 9  export function useFileHistorySnapshotInit(
10    initialFileHistorySnapshots: FileHistorySnapshot[] | undefined,
11    fileHistoryState: FileHistoryState,
12    onUpdateState: (newState: FileHistoryState) => void,
13  ): void {
14    const initialized = useRef(false)
15  
16    useEffect(() => {
17      if (!fileHistoryEnabled() || initialized.current) {
18        return
19      }
20      initialized.current = true
21      if (initialFileHistorySnapshots) {
22        fileHistoryRestoreStateFromLog(initialFileHistorySnapshots, onUpdateState)
23      }
24    }, [fileHistoryState, initialFileHistorySnapshots, onUpdateState])
25  }