@@ -2,6 +2,10 @@ import { useCallback, useRef } from "react";
22import { saveProjectFilesWithHistory } from "../utils/studioFileHistory" ;
33import type { EditHistoryKind } from "../utils/editHistory" ;
44import { trackStudioEvent } from "../utils/studioTelemetry" ;
5+ import {
6+ StudioFileConflictError ,
7+ type StudioSaveDrainResult ,
8+ } from "../utils/studioSaveDiagnostics" ;
59
610interface RecordEditInput {
711 label : string ;
@@ -21,6 +25,25 @@ interface UseEditorSaveOptions {
2125 showToast : ( message : string , tone ?: "error" | "info" ) => void ;
2226}
2327
28+ export interface EditorSaveCandidate {
29+ projectId : string ;
30+ path : string ;
31+ content : string ;
32+ }
33+
34+ export type EditorSaveDrainResult = StudioSaveDrainResult ;
35+
36+ export interface EditorSaveHandle {
37+ saveRafRef : React . MutableRefObject < number | null > ;
38+ handleContentChange : ( content : string ) => void ;
39+ /** Read by the external-reload reconciliation introduced in stack PR #2993. */
40+ getPendingCandidate : ( ) => EditorSaveCandidate | null ;
41+ /** Wired into the external-reload drain by stack PR #2993. */
42+ flushPendingSave : ( ) => Promise < EditorSaveDrainResult > ;
43+ /** Used by PR #2993 when the external version wins. */
44+ discardPendingSave : ( ) => void ;
45+ }
46+
2447export function useEditorSave ( {
2548 editingPathRef,
2649 projectIdRef,
@@ -30,12 +53,70 @@ export function useEditorSave({
3053 domEditSaveTimestampRef,
3154 setRefreshKey,
3255 showToast,
33- } : UseEditorSaveOptions ) {
56+ } : UseEditorSaveOptions ) : EditorSaveHandle {
3457 const saveRafRef = useRef < number | null > ( null ) ;
3558 const refreshRafRef = useRef < number | null > ( null ) ;
3659 // One error toast per burst of failures — every keystroke retries the save,
3760 // and error toasts persist until dismissed, so don't stack duplicates.
3861 const lastFailureToastAtRef = useRef ( 0 ) ;
62+ const pendingCandidateRef = useRef < EditorSaveCandidate | null > ( null ) ;
63+ const inFlightRef = useRef < Promise < EditorSaveDrainResult > | null > ( null ) ;
64+ const inFlightCandidateRef = useRef < EditorSaveCandidate | null > ( null ) ;
65+
66+ const reportFailure = useCallback (
67+ ( path : string , error : unknown ) => {
68+ trackStudioEvent ( "save_failure" , {
69+ source : "code_editor" ,
70+ error_message : error instanceof Error ? error . message : "unknown" ,
71+ } ) ;
72+ const now = Date . now ( ) ;
73+ if ( now - lastFailureToastAtRef . current > 5000 ) {
74+ lastFailureToastAtRef . current = now ;
75+ showToast (
76+ `Couldn't save ${ path } — your latest edits are NOT persisted. Check the preview server; editing again retries the save.` ,
77+ "error" ,
78+ ) ;
79+ }
80+ } ,
81+ [ showToast ] ,
82+ ) ;
83+
84+ const persistCandidate = useCallback (
85+ ( candidate : EditorSaveCandidate ) : Promise < EditorSaveDrainResult > => {
86+ const task = saveProjectFilesWithHistory ( {
87+ projectId : candidate . projectId ,
88+ label : "Edit source" ,
89+ kind : "source" ,
90+ coalesceKey : `source:${ candidate . path } ` ,
91+ files : { [ candidate . path ] : candidate . content } ,
92+ readFile : readProjectFile ,
93+ writeFile : writeProjectFile ,
94+ recordEdit,
95+ } )
96+ . then < EditorSaveDrainResult > ( ( ) => {
97+ if ( pendingCandidateRef . current === candidate ) pendingCandidateRef . current = null ;
98+ if ( refreshRafRef . current != null ) cancelAnimationFrame ( refreshRafRef . current ) ;
99+ refreshRafRef . current = requestAnimationFrame ( ( ) => setRefreshKey ( ( k ) => k + 1 ) ) ;
100+ return { status : "clean" } ;
101+ } )
102+ . catch < EditorSaveDrainResult > ( ( error : unknown ) => {
103+ reportFailure ( candidate . path , error ) ;
104+ return error instanceof StudioFileConflictError
105+ ? { status : "conflict" , error }
106+ : { status : "failed" , error } ;
107+ } )
108+ . finally ( ( ) => {
109+ if ( inFlightRef . current === task ) {
110+ inFlightRef . current = null ;
111+ inFlightCandidateRef . current = null ;
112+ }
113+ } ) ;
114+ inFlightRef . current = task ;
115+ inFlightCandidateRef . current = candidate ;
116+ return task ;
117+ } ,
118+ [ readProjectFile , recordEdit , reportFailure , setRefreshKey , writeProjectFile ] ,
119+ ) ;
39120
40121 const handleContentChange = useCallback (
41122 ( content : string ) => {
@@ -44,53 +125,46 @@ export function useEditorSave({
44125 const path = editingPathRef . current ;
45126 if ( ! path ) return ;
46127
128+ const candidate = { projectId : pid , path, content } ;
129+ pendingCandidateRef . current = candidate ;
130+
47131 if ( saveRafRef . current != null ) cancelAnimationFrame ( saveRafRef . current ) ;
48132 saveRafRef . current = requestAnimationFrame ( ( ) => {
133+ saveRafRef . current = null ;
49134 domEditSaveTimestampRef . current = Date . now ( ) ;
50- saveProjectFilesWithHistory ( {
51- projectId : pid ,
52- label : "Edit source" ,
53- kind : "source" ,
54- coalesceKey : `source:${ path } ` ,
55- files : { [ path ] : content } ,
56- readFile : readProjectFile ,
57- writeFile : writeProjectFile ,
58- recordEdit,
59- } )
60- . then ( ( ) => {
61- if ( refreshRafRef . current != null ) cancelAnimationFrame ( refreshRafRef . current ) ;
62- refreshRafRef . current = requestAnimationFrame ( ( ) => setRefreshKey ( ( k ) => k + 1 ) ) ;
63- } )
64- . catch ( ( error ) => {
65- trackStudioEvent ( "save_failure" , {
66- source : "code_editor" ,
67- error_message : error instanceof Error ? error . message : "unknown" ,
68- } ) ;
69- const now = Date . now ( ) ;
70- if ( now - lastFailureToastAtRef . current > 5000 ) {
71- lastFailureToastAtRef . current = now ;
72- showToast (
73- `Couldn't save ${ path } — your latest edits are NOT persisted. Check the preview server; editing again retries the save.` ,
74- "error" ,
75- ) ;
76- }
77- } ) ;
135+ void persistCandidate ( candidate ) ;
78136 } ) ;
79137 } ,
80- [
81- domEditSaveTimestampRef ,
82- editingPathRef ,
83- projectIdRef ,
84- readProjectFile ,
85- recordEdit ,
86- setRefreshKey ,
87- showToast ,
88- writeProjectFile ,
89- ] ,
138+ [ domEditSaveTimestampRef , editingPathRef , projectIdRef , persistCandidate ] ,
90139 ) ;
91140
141+ const flushPendingSave = useCallback ( async ( ) : Promise < EditorSaveDrainResult > => {
142+ if ( saveRafRef . current != null ) {
143+ cancelAnimationFrame ( saveRafRef . current ) ;
144+ saveRafRef . current = null ;
145+ }
146+ const candidate = pendingCandidateRef . current ;
147+ if ( candidate && candidate === inFlightCandidateRef . current && inFlightRef . current ) {
148+ return inFlightRef . current ;
149+ }
150+ if ( candidate ) {
151+ domEditSaveTimestampRef . current = Date . now ( ) ;
152+ return persistCandidate ( candidate ) ;
153+ }
154+ return ( await inFlightRef . current ) ?? { status : "clean" } ;
155+ } , [ domEditSaveTimestampRef , persistCandidate ] ) ;
156+
157+ const discardPendingSave = useCallback ( ( ) => {
158+ if ( saveRafRef . current != null ) cancelAnimationFrame ( saveRafRef . current ) ;
159+ saveRafRef . current = null ;
160+ pendingCandidateRef . current = null ;
161+ } , [ ] ) ;
162+
92163 return {
93164 saveRafRef,
94165 handleContentChange,
166+ getPendingCandidate : ( ) => pendingCandidateRef . current ,
167+ flushPendingSave,
168+ discardPendingSave,
95169 } ;
96170}
0 commit comments