mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-23 11:29:54 +00:00
* fix(studio): undo waits for in-flight hand edits before consuming history * docs(studio): trim comments to the 3-line house rule * feat(studio): export timeline editing, undo/redo, conflict banner and TimelinePane for a host * docs(studio): trim comments to the 3-line house rule * refactor(studio): simplify exports pass * docs(studio): trim comments to the 3-line house rule * fix(studio): resolve canEdit targets against expanded timeline rows * docs(studio): trim comment to the 3-line house rule * fix(studio): guard razor-split-all and the audio-group attribute write * fix(studio): gate the audio-group attribute for sub-composition-only members * fix(studio): keep blocked timeline edits truthful Refresh cached permission checks from the current host policy, gate auto-grouping before live patches, and restore optimistic attribute previews when a guarded commit is refused. * fix(studio): publish desktop compatibility exports Expose range selection and the canonical loader from the built Studio package while retaining timeline editing seams. * fix(studio): gate timeline drop writes Route asset, file, and composition drops through the shared edit permission gate before any side effect. * fix(studio): preserve release-owned style exports * fix(studio): clear file and Fallow gates * fix(studio): keep the carve gate and range slice under the size budgets * style(studio): format playerStore
151 lines
5.7 KiB
TypeScript
151 lines
5.7 KiB
TypeScript
import { useCallback, useMemo } from "react";
|
|
import {
|
|
createStudioSaveHttpError,
|
|
retryStudioSave,
|
|
StudioFileConflictError,
|
|
StudioSaveNetworkError,
|
|
} from "../utils/studioSaveDiagnostics";
|
|
import { studioExpectedFileVersion, studioWriteHeaders } from "../utils/studioFileVersion";
|
|
|
|
export interface UseProjectFileWriterOptions {
|
|
projectId: string | null;
|
|
}
|
|
|
|
// The etag-guarded project-file read/write pair every Studio save path
|
|
// shares. A host mounting hand editing outside Studio needs only this, not
|
|
// the full file manager (file tree, editor tabs, uploads, font assets).
|
|
export function useProjectFileWriter({ projectId }: UseProjectFileWriterOptions) {
|
|
const fileVersionScope = useMemo(
|
|
() => ({ projectId, versions: new Map<string, string | null>() }),
|
|
[projectId],
|
|
);
|
|
const fileVersions = fileVersionScope.versions;
|
|
|
|
const observeProjectFileVersion = useCallback(
|
|
(path: string, version: string | null) => {
|
|
fileVersions.set(path, version);
|
|
},
|
|
[fileVersions],
|
|
);
|
|
|
|
const readProjectFile = useCallback(
|
|
async (path: string): Promise<string> => {
|
|
if (!projectId) throw new Error("No active project");
|
|
const response = await fetch(
|
|
`/api/projects/${encodeURIComponent(projectId)}/files/${encodeURIComponent(path)}`,
|
|
);
|
|
if (!response.ok) throw new Error(`Failed to read ${path}`);
|
|
const data = (await response.json()) as { content?: string; version?: string };
|
|
if (typeof data.content !== "string") throw new Error(`Missing file contents for ${path}`);
|
|
fileVersions.set(path, data.version ?? response.headers.get("etag"));
|
|
return data.content;
|
|
},
|
|
[fileVersions, projectId],
|
|
);
|
|
|
|
const readOptionalProjectFile = useCallback(
|
|
async (path: string): Promise<string> => {
|
|
if (!projectId) throw new Error("No active project");
|
|
const response = await fetch(
|
|
`/api/projects/${encodeURIComponent(projectId)}/files/${encodeURIComponent(path)}?optional=1`,
|
|
);
|
|
if (!response.ok) throw new Error(`Failed to read ${path}`);
|
|
const data = (await response.json()) as { content?: string; version?: string };
|
|
fileVersions.set(path, data.version ?? response.headers.get("etag"));
|
|
return typeof data.content === "string" ? data.content : "";
|
|
},
|
|
[fileVersions, projectId],
|
|
);
|
|
|
|
const writeProjectFile = useCallback(
|
|
async (path: string, content: string, expectedContent?: string): Promise<void> => {
|
|
if (!projectId) throw new Error("No active project");
|
|
const writeProjectId = projectId;
|
|
let expectedVersion = await studioExpectedFileVersion(fileVersions, path, expectedContent);
|
|
if (expectedVersion === undefined) {
|
|
const preflight = await fetch(
|
|
`/api/projects/${encodeURIComponent(writeProjectId)}/files/${encodeURIComponent(path)}`,
|
|
);
|
|
if (preflight.ok) {
|
|
const data = (await preflight.json()) as { content?: string; version?: string };
|
|
throw new StudioFileConflictError({
|
|
filePath: path,
|
|
currentVersion: data.version ?? preflight.headers.get("etag"),
|
|
currentContent: data.content ?? null,
|
|
attemptedContent: content,
|
|
});
|
|
} else if (preflight.status === 404) {
|
|
expectedVersion = null;
|
|
} else {
|
|
throw await createStudioSaveHttpError(preflight, `Failed to read ${path} before save`);
|
|
}
|
|
}
|
|
// fallow-ignore-next-line complexity
|
|
await retryStudioSave(async () => {
|
|
// Each request gets its own receipt identity. If a committed request loses its response,
|
|
// the retry can produce a second filesystem receipt that must be suppressed independently.
|
|
let response: Response;
|
|
try {
|
|
response = await fetch(
|
|
`/api/projects/${encodeURIComponent(writeProjectId)}/files/${encodeURIComponent(path)}`,
|
|
{
|
|
method: "PUT",
|
|
headers: {
|
|
"Content-Type": "text/plain",
|
|
...studioWriteHeaders(),
|
|
...(expectedVersion ? { "If-Match": expectedVersion } : { "If-None-Match": "*" }),
|
|
},
|
|
body: content,
|
|
},
|
|
);
|
|
} catch (error) {
|
|
throw new StudioSaveNetworkError(`Failed to save ${path}: network error`, {
|
|
cause: error,
|
|
});
|
|
}
|
|
if (response.status === 409) {
|
|
const conflict = (await response.json().catch(() => null)) as {
|
|
currentVersion?: string | null;
|
|
currentContent?: string | null;
|
|
} | null;
|
|
const currentVersion = conflict?.currentVersion ?? null;
|
|
if (currentVersion && conflict?.currentContent === content) {
|
|
fileVersions.set(path, currentVersion);
|
|
return;
|
|
}
|
|
throw new StudioFileConflictError({
|
|
filePath: path,
|
|
currentVersion,
|
|
currentContent: conflict?.currentContent ?? null,
|
|
attemptedContent: content,
|
|
});
|
|
}
|
|
if (!response.ok) throw await createStudioSaveHttpError(response, `Failed to save ${path}`);
|
|
const result = (await response.json()) as { version?: string };
|
|
const version = result.version ?? response.headers.get("etag");
|
|
if (!version)
|
|
throw new Error(`Save response for ${path} did not include a content version`);
|
|
fileVersions.set(path, version);
|
|
});
|
|
},
|
|
[fileVersions, projectId],
|
|
);
|
|
|
|
return useMemo(
|
|
() => ({
|
|
fileVersions,
|
|
readProjectFile,
|
|
writeProjectFile,
|
|
readOptionalProjectFile,
|
|
observeProjectFileVersion,
|
|
}),
|
|
[
|
|
fileVersions,
|
|
readProjectFile,
|
|
writeProjectFile,
|
|
readOptionalProjectFile,
|
|
observeProjectFileVersion,
|
|
],
|
|
);
|
|
}
|