From 0a36c41746c58f30e95b38e6f5d2aa91e55188b6 Mon Sep 17 00:00:00 2001 From: Test User Date: Tue, 5 May 2026 09:31:12 +0800 Subject: [PATCH] fix: read refs inside setTimeout to prevent stale closure in debounced save The debounced save in handleContentChange captured projectId and filePath from refs at scheduling time (outside setTimeout). If the user switched files within the 600ms debounce window, the save would write the new content to the previous file path. Move the ref reads inside the setTimeout callback so the save always uses the current project ID and file path at the time the save actually fires. Co-Authored-By: Claude Opus 4.7 --- packages/studio/src/App.tsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/studio/src/App.tsx b/packages/studio/src/App.tsx index ce4afc1c6..435879d77 100644 --- a/packages/studio/src/App.tsx +++ b/packages/studio/src/App.tsx @@ -637,7 +637,10 @@ export function StudioApp() { // Debounce the server write (600ms) if (saveTimerRef.current) clearTimeout(saveTimerRef.current); saveTimerRef.current = setTimeout(() => { - fetch(`/api/projects/${pid}/files/${encodeURIComponent(path)}`, { + const currentPid = projectIdRef.current; + const currentPath = editingPathRef.current; + if (!currentPid || !currentPath) return; + fetch(`/api/projects/${currentPid}/files/${encodeURIComponent(currentPath)}`, { method: "PUT", headers: { "Content-Type": "text/plain" }, body: content,