Files
hyperframes/packages/studio/src/hooks/useFileManager.ts
T
Miguel ÁngelandMiguel Ángel 7bff49ecf0 refactor(studio): simplify hooks, split contexts, remove dead code (#1416)
* fix(studio): guard Zustand no-op setters and fix useConsoleErrorCapture memory leak

- Guard setIsPlaying to skip set() when value unchanged (eliminates 60
  notifications/sec during reverse playback)
- Guard caption store selectGroup to bail before set() when group missing
  (prevents empty Zustand notifications)
- Guard clearSelection to skip when already empty
- Fix useConsoleErrorCapture: restore original console.error, remove error
  event listener, and delete __hfErrorCapture flag on cleanup

* fix(studio): delete dead files and unused exports

Remove 7 dead files (audioBeatDetection, keyframeSnapping,
timelineInspector, DopesheetStrip, StaggerControls,
TimelineLayerPanel, TimelineEditorNotice) and their test companions.

Delete unused computeFitToChildrenSize export from propertyPanelHelpers.

Fix re-export indirection: useDomEditCommits and studioMotionOps.test
now import patch builders directly from manualEditsDomPatches instead
of the re-export passthrough in manualEditsDom.

* fix(studio): eliminate effect-chain state mirroring for lint findings, hover, and GSAP fetch

Move lint findingsByElement sync from App.tsx into useLintModal where
the value is produced, removing the mirroring useEffect. Consolidate
4 hover-clearing effects in useDomSelection into 2 (one unconditional
on context change, one conditional combining caption mode, selection
match, and disconnected element checks). Fold the GSAP retry effect
into the fetch effect in useGsapTweenCache, scheduling a single retry
via setTimeout when the initial fetch returns 0 animations.

Eliminates 3 unnecessary render cycles from effect chains.

* fix(studio): memoize renderQueue, toolbar, and canvas rect to prevent re-render cascade

- Wrap renderQueue object in useMemo so StudioContext consumers don't
  re-render on every App render
- Memoize timelineToolbar JSX so NLELayout memo isn't defeated
- Move canvasRect getBoundingClientRect() from render-time IIFE to a
  useLayoutEffect-backed ref, eliminating layout thrashing
- Track and clear setTimeout handles in refreshPreviewDocumentVersion
  to prevent stale timer accumulation on rapid calls and unmount

* refactor(studio): consolidate GSAP shared primitives — defaults, iframe access, keyframe parsing

Extract duplicated PROPERTY_DEFAULTS, IframeGsap interface, iframe
accessors (getIframeGsap, queryIframeElement), percentage keyframe
parsing, and toAbsoluteTime into a single gsapShared.ts module.

Removes ~120 lines of copy-pasted logic across 8 hook files, reducing
drift risk between the duplicate implementations.

* fix(studio): remove dead store fields, dead file, duplicate helper, and unsafe assertions

* refactor(studio): deduplicate selector helpers, rounding utils, percentage computation, and iframe access

* fix(studio): split StudioContext into Shell + Playback to prevent cascade re-renders

* refactor(studio): decompose useGsapScriptCommits into focused mutation hooks

* refactor(studio): decompose useFileManager into focused file operation hooks

Extract useFileTree (tree loading, refresh, derived assets/compositions)
and useEditorSave (debounced save with history tracking) from the 508-LOC
useFileManager. The parent hook composes both and retains file I/O,
click-to-source, upload/import, and CRUD — preserving the same public
interface so no consumers change.

* refactor(studio): decompose useDomEditCommits into focused commit hooks

Extract geometry (path offset, box size, rotation) and element lifecycle
(delete, z-index reorder) into useDomGeometryCommits and
useElementLifecycleOps. Parent keeps persistDomEditOperations as core
and composes all sub-hooks — public interface unchanged.

* refactor(studio): simplify useAppHotkeys with declarative command table

* refactor(studio): simplify useAppHotkeys with declarative command table

Replace 15 individual useRef callback refs with a single cbRef object.
Extract keydown dispatch into pure dispatchModifierKey/dispatchPlainKey
functions. Merge duplicate undo/redo logic into shared applyHistory.
Extract cross-origin listener boilerplate into safeAddListener/safeRemoveListener.

Hook body: 204 LOC (down from 445). Public API unchanged.

* fix(studio): remove unused getDomEditTargetKey import

* refactor(studio): decompose useDomEditSession into focused editing hooks

Extract GSAP-aware geometry intercepts (move/resize/rotation) and
animated property commit into useGsapAwareEditing, and selection
wiring, GSAP cache management, preview sync, and selection handlers
into useDomEditWiring. The parent remains a pure composition shell.

* style(studio): fix formatting in 5 files

* fix(studio): trim App.tsx to 598 lines (under 600 limit)

---------

Co-authored-by: Miguel Ángel <miguel07alm@protonmail.com>
2026-06-13 18:25:11 -04:00

427 lines
13 KiB
TypeScript

import { useState, useCallback, useRef } from "react";
import type { EditingFile } from "../utils/studioHelpers";
import { FONT_EXT, isMediaFile } from "../utils/mediaTypes";
import { fontFamilyFromAssetPath, type ImportedFontAsset } from "../components/editor/fontAssets";
import type { EditHistoryKind } from "../utils/editHistory";
import { findTagByTarget, type PatchTarget } from "../utils/sourcePatcher";
import {
createStudioSaveHttpError,
retryStudioSave,
StudioSaveNetworkError,
} from "../utils/studioSaveDiagnostics";
import { useFileTree } from "./useFileTree";
import { useEditorSave } from "./useEditorSave";
// ── Types ──
interface RecordEditInput {
label: string;
kind: EditHistoryKind;
coalesceKey?: string;
files: Record<string, { before: string; after: string }>;
}
interface UseFileManagerOptions {
projectId: string | null;
showToast: (message: string, tone?: "error" | "info") => void;
recordEdit: (input: RecordEditInput) => Promise<void>;
domEditSaveTimestampRef: React.MutableRefObject<number>;
setRefreshKey: React.Dispatch<React.SetStateAction<number>>;
}
// ── Hook ──
export function useFileManager({
projectId,
showToast,
recordEdit,
domEditSaveTimestampRef,
setRefreshKey,
}: UseFileManagerOptions) {
// ── Shared refs ──
const [editingFile, setEditingFile] = useState<EditingFile | null>(null);
const [revealSourceOffset, setRevealSourceOffset] = useState<number | null>(null);
const editingPathRef = useRef(editingFile?.path);
editingPathRef.current = editingFile?.path;
const projectIdRef = useRef(projectId);
projectIdRef.current = projectId;
const importedFontAssetsRef = useRef<ImportedFontAsset[]>([]);
// ── File tree ──
const {
projectDir,
fileTree,
setFileTree,
fileTreeLoaded,
refreshFileTree,
compositions,
assets,
fontAssets,
} = useFileTree({ projectId, projectIdRef });
// ── Core file I/O ──
const readProjectFile = useCallback(async (path: string): Promise<string> => {
const pid = projectIdRef.current;
if (!pid) throw new Error("No active project");
const response = await fetch(`/api/projects/${pid}/files/${encodeURIComponent(path)}`);
if (!response.ok) throw new Error(`Failed to read ${path}`);
const data = (await response.json()) as { content?: string };
if (typeof data.content !== "string") throw new Error(`Missing file contents for ${path}`);
return data.content;
}, []);
const writeProjectFile = useCallback(async (path: string, content: string): Promise<void> => {
const pid = projectIdRef.current;
if (!pid) throw new Error("No active project");
await retryStudioSave(async () => {
let response: Response;
try {
response = await fetch(`/api/projects/${pid}/files/${encodeURIComponent(path)}`, {
method: "PUT",
headers: { "Content-Type": "text/plain" },
body: content,
});
} catch (error) {
throw new StudioSaveNetworkError(`Failed to save ${path}: network error`, {
cause: error,
});
}
if (!response.ok) throw await createStudioSaveHttpError(response, `Failed to save ${path}`);
});
if (editingPathRef.current === path) {
setEditingFile({ path, content });
}
}, []);
const updateEditingFileContent = useCallback((path: string, content: string) => {
if (editingPathRef.current === path) {
setEditingFile({ path, content });
}
}, []);
const readOptionalProjectFile = useCallback(async (path: string): Promise<string> => {
const pid = projectIdRef.current;
if (!pid) throw new Error("No active project");
const response = await fetch(
`/api/projects/${pid}/files/${encodeURIComponent(path)}?optional=1`,
);
if (!response.ok) throw new Error(`Failed to read ${path}`);
const data = (await response.json()) as { content?: string };
return typeof data.content === "string" ? data.content : "";
}, []);
// ── Editor save (debounced content change) ──
const { saveRafRef, handleContentChange } = useEditorSave({
editingPathRef,
projectIdRef,
readProjectFile,
writeProjectFile,
recordEdit,
domEditSaveTimestampRef,
setRefreshKey,
});
// ── File select ──
const revealRequestIdRef = useRef(0);
const revealAbortRef = useRef<AbortController | null>(null);
const handleFileSelect = useCallback((path: string) => {
const pid = projectIdRef.current;
if (!pid) return;
revealAbortRef.current?.abort();
revealAbortRef.current = null;
revealRequestIdRef.current++;
// Skip fetching binary content for media files — just set the path for preview
if (isMediaFile(path)) {
setEditingFile({ path, content: null });
return;
}
fetch(`/api/projects/${pid}/files/${encodeURIComponent(path)}`)
.then((r) => r.json())
.then((data: { content?: string }) => {
if (data.content != null) {
setEditingFile({ path, content: data.content });
}
})
.catch(() => {});
}, []);
// ── Click-to-source ──
const openSourceForSelection = useCallback(
(sourceFile: string, target: PatchTarget) => {
const pid = projectIdRef.current;
if (!pid || !sourceFile) return;
revealAbortRef.current?.abort();
revealAbortRef.current = null;
if (editingPathRef.current === sourceFile && editingFile?.content != null) {
const match = findTagByTarget(editingFile.content, target);
setRevealSourceOffset(match ? match.start : null);
return;
}
const requestId = ++revealRequestIdRef.current;
const controller = new AbortController();
revealAbortRef.current = controller;
fetch(`/api/projects/${pid}/files/${encodeURIComponent(sourceFile)}`, {
signal: controller.signal,
})
.then((r) => r.json())
.then((data: { content?: string }) => {
if (requestId !== revealRequestIdRef.current) return;
if (data.content != null) {
setEditingFile({ path: sourceFile, content: data.content });
const match = findTagByTarget(data.content, target);
setRevealSourceOffset(match ? match.start : null);
}
})
.catch(() => {});
},
[editingFile?.content],
);
// ── Upload ──
const uploadProjectFiles = useCallback(
async (files: Iterable<File>, dir?: string): Promise<string[]> => {
const pid = projectIdRef.current;
const fileList = Array.from(files);
if (!pid || fileList.length === 0) return [];
const formData = new FormData();
for (const file of fileList) {
formData.append("file", file);
}
const qs = dir ? `?dir=${encodeURIComponent(dir)}` : "";
try {
const res = await fetch(`/api/projects/${pid}/upload${qs}`, {
method: "POST",
body: formData,
});
if (res.ok) {
const data = await res.json();
if (data.skipped?.length) {
showToast(`Skipped (too large): ${data.skipped.join(", ")}`);
}
if (data.invalid?.length) {
const names = data.invalid.map((entry: { name: string }) => entry.name).join(", ");
showToast(`Unsupported media skipped: ${names}`);
}
await refreshFileTree();
setRefreshKey((k) => k + 1);
return Array.isArray(data.files) ? data.files : [];
} else if (res.status === 413) {
showToast("Upload rejected: payload too large");
} else {
showToast(`Upload failed (${res.status})`);
}
} catch {
showToast("Upload failed: network error");
}
return [];
},
[refreshFileTree, setRefreshKey, showToast],
);
// ── File CRUD ──
const handleCreateFile = useCallback(
async (path: string) => {
const pid = projectIdRef.current;
if (!pid) return;
let content = "";
if (path.endsWith(".html")) {
content =
'<!DOCTYPE html>\n<html>\n<head>\n <meta charset="UTF-8">\n</head>\n<body>\n\n</body>\n</html>\n';
}
const res = await fetch(`/api/projects/${pid}/files/${encodeURIComponent(path)}`, {
method: "POST",
headers: { "Content-Type": "text/plain" },
body: content,
});
if (res.ok) {
await refreshFileTree();
handleFileSelect(path);
} else {
const err = await res.json().catch(() => ({ error: "unknown" }));
console.error(`Create file failed: ${err.error}`);
}
},
[refreshFileTree, handleFileSelect],
);
const handleCreateFolder = useCallback(
async (path: string) => {
const pid = projectIdRef.current;
if (!pid) return;
const res = await fetch(
`/api/projects/${pid}/files/${encodeURIComponent(path + "/.gitkeep")}`,
{
method: "POST",
headers: { "Content-Type": "text/plain" },
body: "",
},
);
if (res.ok) {
await refreshFileTree();
} else {
const err = await res.json().catch(() => ({ error: "unknown" }));
console.error(`Create folder failed: ${err.error}`);
}
},
[refreshFileTree],
);
const handleDeleteFile = useCallback(
async (path: string) => {
const pid = projectIdRef.current;
if (!pid) return;
const res = await fetch(`/api/projects/${pid}/files/${encodeURIComponent(path)}`, {
method: "DELETE",
});
if (res.ok) {
if (editingPathRef.current === path) setEditingFile(null);
await refreshFileTree();
} else {
const err = await res.json().catch(() => ({ error: "unknown" }));
console.error(`Delete failed: ${err.error}`);
}
},
[refreshFileTree],
);
const handleRenameFile = useCallback(
async (oldPath: string, newPath: string) => {
const pid = projectIdRef.current;
if (!pid) return;
const res = await fetch(`/api/projects/${pid}/files/${encodeURIComponent(oldPath)}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ newPath }),
});
if (res.ok) {
if (editingPathRef.current === oldPath) {
handleFileSelect(newPath);
}
await refreshFileTree();
setRefreshKey((k) => k + 1);
} else {
const err = await res.json().catch(() => ({ error: "unknown" }));
console.error(`Rename failed: ${err.error}`);
}
},
[refreshFileTree, handleFileSelect, setRefreshKey],
);
const handleDuplicateFile = useCallback(
async (path: string) => {
const pid = projectIdRef.current;
if (!pid) return;
const res = await fetch(`/api/projects/${pid}/duplicate-file`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ path }),
});
if (res.ok) {
const data = await res.json();
await refreshFileTree();
if (data.path) handleFileSelect(data.path);
} else {
const err = await res.json().catch(() => ({ error: "unknown" }));
console.error(`Duplicate failed: ${err.error}`);
}
},
[refreshFileTree, handleFileSelect],
);
const handleMoveFile = handleRenameFile;
const handleImportFiles = useCallback(
async (files: FileList | File[], dir?: string) => {
return uploadProjectFiles(Array.from(files), dir);
},
[uploadProjectFiles],
);
const handleImportFonts = useCallback(
async (files: FileList | File[]): Promise<ImportedFontAsset[]> => {
const uploaded = await uploadProjectFiles(
Array.from(files).filter((file) => FONT_EXT.test(file.name)),
"assets/fonts",
);
const pid = projectIdRef.current;
const imported = uploaded
.filter((asset) => FONT_EXT.test(asset))
.map((asset) => ({
family: fontFamilyFromAssetPath(asset),
path: asset,
url: `/api/projects/${pid}/preview/${asset}`,
}));
importedFontAssetsRef.current = [
...imported,
...importedFontAssetsRef.current.filter(
(existing) =>
!imported.some((font) => font.family.toLowerCase() === existing.family.toLowerCase()),
),
];
return imported;
},
[uploadProjectFiles],
);
// ── Return ──
return {
// State
editingFile,
setEditingFile,
projectDir,
fileTree,
fileTreeLoaded,
setFileTree,
// Refs
editingPathRef,
projectIdRef,
saveRafRef,
importedFontAssetsRef,
// Core I/O
readProjectFile,
writeProjectFile,
readOptionalProjectFile,
updateEditingFileContent,
// Click-to-source
revealSourceOffset,
openSourceForSelection,
// Callbacks
handleFileSelect,
handleContentChange,
refreshFileTree,
uploadProjectFiles,
handleCreateFile,
handleCreateFolder,
handleDeleteFile,
handleRenameFile,
handleDuplicateFile,
handleMoveFile,
handleImportFiles,
handleImportFonts,
// Derived
compositions,
assets,
fontAssets,
};
}