-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathuseDomEditPositionPatchCommit.ts
More file actions
53 lines (50 loc) · 1.91 KB
/
Copy pathuseDomEditPositionPatchCommit.ts
File metadata and controls
53 lines (50 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { useCallback } from "react";
import type { DomEditSelection } from "../components/editor/domEditing";
import type { PatchOperation } from "../utils/sourcePatcher";
import { trackStudioSaveFailure } from "../utils/studioSaveDiagnostics";
import { DomEditSaveQueueOpenError } from "../utils/domEditSaveQueue";
import type { PersistDomEditOperations } from "./domEditCommitTypes";
interface UseDomEditPositionPatchCommitParams {
activeCompPath: string | null;
persistDomEditOperations: PersistDomEditOperations;
queueDomEditSave: (save: () => Promise<void>) => Promise<void>;
showToast: (message: string, tone?: "error" | "info") => void;
}
interface PositionPatchOptions {
label: string;
coalesceKey: string;
skipRefresh?: boolean;
}
export function useDomEditPositionPatchCommit({
activeCompPath,
persistDomEditOperations,
queueDomEditSave,
showToast,
}: UseDomEditPositionPatchCommitParams) {
return useCallback(
(selection: DomEditSelection, patches: PatchOperation[], options: PositionPatchOptions) => {
return queueDomEditSave(async () => {
await persistDomEditOperations(selection, patches, {
label: options.label,
coalesceKey: options.coalesceKey,
skipRefresh: options.skipRefresh ?? true,
});
}).catch((error) => {
if (error instanceof DomEditSaveQueueOpenError) return;
showToast(error instanceof Error ? error.message : "Failed to save position");
trackStudioSaveFailure({
source: "dom_edit",
error,
filePath: selection.sourceFile ?? activeCompPath ?? "index.html",
mutationType: "position",
label: options.label,
targetId: selection.id,
targetSelector: selection.selector,
targetSourceFile: selection.sourceFile,
});
throw error;
});
},
[activeCompPath, persistDomEditOperations, queueDomEditSave, showToast],
);
}