MCPcopy Create free account
hub / github.com/heygen-com/hyperframes / useEditorSave

Function useEditorSave

packages/studio/src/hooks/useEditorSave.ts:23–82  ·  view source on GitHub ↗
({
  editingPathRef,
  projectIdRef,
  readProjectFile,
  writeProjectFile,
  recordEdit,
  domEditSaveTimestampRef,
  setRefreshKey,
}: UseEditorSaveOptions)

Source from the content-addressed store, hash-verified

21}
22
23export function useEditorSave({
24 editingPathRef,
25 projectIdRef,
26 readProjectFile,
27 writeProjectFile,
28 recordEdit,
29 domEditSaveTimestampRef,
30 setRefreshKey,
31}: UseEditorSaveOptions) {
32 const saveRafRef = useRef<number | null>(null);
33 const refreshRafRef = useRef<number | null>(null);
34
35 const handleContentChange = useCallback(
36 (content: string) => {
37 const pid = projectIdRef.current;
38 if (!pid) return;
39 const path = editingPathRef.current;
40 if (!path) return;
41
42 if (saveRafRef.current != null) cancelAnimationFrame(saveRafRef.current);
43 saveRafRef.current = requestAnimationFrame(() => {
44 domEditSaveTimestampRef.current = Date.now();
45 saveProjectFilesWithHistory({
46 projectId: pid,
47 label: "Edit source",
48 kind: "source",
49 coalesceKey: `source:${path}`,
50 files: { [path]: content },
51 readFile: readProjectFile,
52 writeFile: writeProjectFile,
53 recordEdit,
54 })
55 .then(() => {
56 if (refreshRafRef.current != null) cancelAnimationFrame(refreshRafRef.current);
57 refreshRafRef.current = requestAnimationFrame(() => setRefreshKey((k) => k + 1));
58 })
59 .catch((error) => {
60 trackStudioEvent("save_failure", {
61 source: "code_editor",
62 error_message: error instanceof Error ? error.message : "unknown",
63 });
64 });
65 });
66 },
67 [
68 domEditSaveTimestampRef,
69 editingPathRef,
70 projectIdRef,
71 readProjectFile,
72 recordEdit,
73 setRefreshKey,
74 writeProjectFile,
75 ],
76 );
77
78 return {
79 saveRafRef,
80 handleContentChange,

Callers 1

useFileManagerFunction · 0.90

Calls 3

trackStudioEventFunction · 0.90
nowMethod · 0.80

Tested by

no test coverage detected