import { useCallback, type MutableRefObject } from "react"; import type { Composition } from "@hyperframes/sdk"; import type { SlideshowManifest } from "@hyperframes/core/slideshow"; import type { EditHistoryKind } from "../utils/editHistory"; import { persistSlideshowManifest } from "../utils/setSlideshowManifest"; export interface UseSlideshowPersistParams { sdkSession: Composition | null; activeCompPath: string | null; readProjectFile: (path: string) => Promise; writeProjectFile: (path: string, content: string) => Promise; recordEdit: (entry: { label: string; kind: EditHistoryKind; files: Record; }) => Promise; reloadPreview: () => void; domEditSaveTimestampRef: MutableRefObject; /** * When provided, rapid writes with the same key coalesce through the * save-queue infra (via recordEdit's coalesceKey) so back-to-back persists * collapse to a single undo entry rather than polluting history. * Pass e.g. `"slideshow-notes:" + activeCompPath` for the notes path. */ coalesceKey?: string; } export function useSlideshowPersist({ sdkSession, activeCompPath, readProjectFile, writeProjectFile, recordEdit, reloadPreview, domEditSaveTimestampRef, coalesceKey, }: UseSlideshowPersistParams): (manifest: SlideshowManifest) => Promise { return useCallback( async (manifest: SlideshowManifest) => { if (!sdkSession) return; const path = activeCompPath ?? "index.html"; const originalContent = await readProjectFile(path); await persistSlideshowManifest({ manifest, sdkSession, originalContent, targetPath: path, deps: { editHistory: { recordEdit }, writeProjectFile, reloadPreview, domEditSaveTimestampRef, }, coalesceKey, }); }, [ sdkSession, activeCompPath, readProjectFile, writeProjectFile, recordEdit, reloadPreview, domEditSaveTimestampRef, coalesceKey, ], ); }