Files
hyperframes/packages/studio/src/hooks/useStudioExternalFileChanges.ts
T
Vance IngallsandClaude Sonnet 5 c59326d72e fix(studio): refresh the file tree on external change, and say when a composition is gone (#4370)
The fix the cutover-dashboard investigation this week was building toward.
Proven 2026-09-23: `refreshFileTree` (useFileTree) is only ever called from
Studio's OWN file operations (create/delete/rename/upload/block install) —
never on an external change. An agent removing or replacing a composition
updates the preview and the SDK session (useExternalFileChangeCoordinator
already reloads both) but never the tree, so the file stays listed and
clickable forever. Opening it opens a session that can never resolve
(`reason: "absent"`), every edit through it silently takes the (also
failing) server path, and nothing tells the user why.

Two refresh triggers, belt and braces:

- `useExternalFileChangeCoordinator` gains an optional `refreshFileTree`
  collaborator, called alongside `reloadPreview`/`reloadSdkSession` on
  every accepted external change — closes staleness at the source, for
  every consumer of the tree, not just the SDK read. Wired through
  `useStudioExternalFileChanges` from `fileManager.refreshFileTree`
  (already in scope there).

- `useSdkSession` gains `onAbsentRead`, called once per (projectId, path)
  when a read reports `reason: "absent"` — the fallback for a change the
  SSE never delivered (server restart, a missed watcher event). Guarded by
  a ref-tracked set so a genuinely-gone file can't loop, cleared on
  project change so a different project's identical path can still fire.

New: `sdkHandle.compositionMissing`, true for exactly that read result on
the currently active path, and `CompositionMissingBanner` — sibling of
`ProjectUnreachableBanner` (that one fires when the whole PROJECT is
unreachable; this one when the project is fine and this one file inside
it is not) — shown in App.tsx alongside it. The read failure alone is
silent; this is what tells the user their edits stopped landing.

Deliberately not touched: `activeCompPath` is never cleared when the path
leaves the tree — the graveyard page (`clearing-activecomppath-on-tree-
absence`) is binding, and its reasons still hold (`useAutoOpenRootComposition`
latches once; a null path never recovers).

Tests: coordinator refresh fires once per accepted change, not for a
self-write echo, and no-ops when the collaborator is absent (4 new).
useSdkSession: compositionMissing set/cleared, onAbsentRead once per path,
fires again for a different path, guard resets on project change, no-ops
without the collaborator (5 new). Banner render test (1 new). 25/25 in the
touched suites; 2830/2831 across studio hooks+components overall (the one
failure is a pre-existing happy-dom `window.confirm` gap in
ExternalFileConflictBanner.test.tsx, confirmed unrelated via git stash).
oxlint, oxfmt, tsc --noEmit clean.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-23 20:13:21 -07:00

92 lines
3.4 KiB
TypeScript

import { useCallback, type MutableRefObject } from "react";
import { usePlayerStore } from "../player/store/playerStore";
import {
deleteExternalConflictSnapshot,
loadExternalConflictSnapshot,
persistExternalConflictSnapshot,
persistExternalFailureSnapshot,
} from "../utils/externalConflictStorage";
import { notifyExternalFileReload } from "./externalFileReloadBus";
import { useExternalFileChangeCoordinator } from "./useExternalFileChangeCoordinator";
import type { useFileManager } from "./useFileManager";
import type { usePreviewPersistence } from "./usePreviewPersistence";
type FileManager = Pick<
ReturnType<typeof useFileManager>,
| "editingFile"
| "flushPendingSourceSave"
| "discardPendingSourceSave"
| "getPendingSourceCandidate"
| "overwriteExternalConflict"
| "readProjectFile"
| "updateEditingFileContent"
| "refreshFileTree"
>;
type PreviewPersistence = Pick<
ReturnType<typeof usePreviewPersistence>,
"drainPendingDomEditSaves" | "resetDomEditSaveQueueBreaker"
>;
interface UseStudioExternalFileChangesOptions {
projectId: string | null;
activeCompPath: string | null;
masterCompPath: string | null;
fileManager: FileManager;
previewPersistence: PreviewPersistence;
pendingTimelineEditPathRef: MutableRefObject<Set<string>>;
reloadPreview: () => void;
}
/** Connects the app's save queues, recovery storage, and reload surfaces to one owner. */
export function useStudioExternalFileChanges({
projectId,
activeCompPath,
masterCompPath,
fileManager,
previewPersistence,
pendingTimelineEditPathRef,
reloadPreview,
}: UseStudioExternalFileChangesOptions) {
const { flushPendingSourceSave, discardPendingSourceSave } = fileManager;
const { drainPendingDomEditSaves, resetDomEditSaveQueueBreaker } = previewPersistence;
const bumpThumbnailRevisions = usePlayerStore((state) => state.bumpThumbnailRevisions);
const onAcceptedPersistedFileChange = useCallback(
(_path: string, affectedCompositions: readonly string[] | null) =>
bumpThumbnailRevisions(affectedCompositions),
[bumpThumbnailRevisions],
);
const drainPendingChanges = useCallback(async () => {
const source = await flushPendingSourceSave();
if (source.status !== "clean") return source;
return drainPendingDomEditSaves();
}, [drainPendingDomEditSaves, flushPendingSourceSave]);
const discardPendingChanges = useCallback(() => {
discardPendingSourceSave();
resetDomEditSaveQueueBreaker();
}, [discardPendingSourceSave, resetDomEditSaveQueueBreaker]);
return useExternalFileChangeCoordinator({
projectId,
activeCompPath,
recoveryFilePath: fileManager.editingFile?.path ?? activeCompPath ?? masterCompPath,
pendingTimelineEditPathRef,
drainPendingChanges,
getPendingCandidate: fileManager.getPendingSourceCandidate,
discardPendingChanges,
reloadPreview,
reloadSdkSession: notifyExternalFileReload,
persistConflictSnapshot: persistExternalConflictSnapshot,
persistFailureSnapshot: persistExternalFailureSnapshot,
loadConflictSnapshot: loadExternalConflictSnapshot,
deleteConflictSnapshot: deleteExternalConflictSnapshot,
overwriteConflict: fileManager.overwriteExternalConflict,
readProjectFile: fileManager.readProjectFile,
onUseExternalFile: fileManager.updateEditingFileContent,
resetSaveQueues: resetDomEditSaveQueueBreaker,
onAcceptedPersistedFileChange,
refreshFileTree: fileManager.refreshFileTree,
});
}