diff --git a/packages/studio/src/captions/hooks/useCaptionSync.ts b/packages/studio/src/captions/hooks/useCaptionSync.ts new file mode 100644 index 0000000000..34c68bcbc1 --- /dev/null +++ b/packages/studio/src/captions/hooks/useCaptionSync.ts @@ -0,0 +1,49 @@ +import { useRef, useCallback } from "react"; +import { useCaptionStore } from "../store"; +import { generateCaptionHtml } from "../generator"; +import { useMountEffect } from "../../hooks/useMountEffect"; + +export function useCaptionSync(projectId: string | null) { + const debounceRef = useRef | null>(null); + + const writeToSource = useCallback( + (filePath: string, html: string) => { + if (!projectId || !filePath) return; + fetch(`/api/projects/${projectId}/files/${encodeURIComponent(filePath)}`, { + method: "PUT", + headers: { "Content-Type": "text/plain" }, + body: html, + }).catch(() => {}); + }, + [projectId], + ); + + useMountEffect(() => { + let prevModel = useCaptionStore.getState().model; + + const unsubscribe = useCaptionStore.subscribe((state) => { + if (!state.isEditMode) return; + if (state.model === prevModel) return; + + prevModel = state.model; + + if (!state.model) return; + + const model = state.model; + const filePath = state.sourceFilePath; + + if (!filePath) return; + + if (debounceRef.current) clearTimeout(debounceRef.current); + debounceRef.current = setTimeout(() => { + const html = generateCaptionHtml(model); + writeToSource(filePath, html); + }, 400); + }); + + return () => { + unsubscribe(); + if (debounceRef.current) clearTimeout(debounceRef.current); + }; + }); +}