-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathuseSafeGsapCommitMutation.ts
More file actions
53 lines (49 loc) · 1.77 KB
/
Copy pathuseSafeGsapCommitMutation.ts
File metadata and controls
53 lines (49 loc) · 1.77 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/domEditingTypes";
import { getStudioSaveErrorMessage, trackStudioSaveFailure } from "../utils/studioSaveDiagnostics";
import type { CommitMutation, CommitMutationOptions } from "./gsapScriptCommitTypes";
type TrackGsapSaveFailure = (
error: unknown,
selection: DomEditSelection,
mutation: Record<string, unknown>,
label?: string,
) => void;
function getGsapMutationType(mutation: Record<string, unknown>): string {
return typeof mutation.type === "string" ? mutation.type : "gsap";
}
export function useGsapSaveFailureTelemetry(activeCompPath: string | null): TrackGsapSaveFailure {
return useCallback(
(error, selection, mutation, label) => {
trackStudioSaveFailure({
source: "gsap_commit",
error,
filePath: selection.sourceFile ?? activeCompPath ?? "index.html",
mutationType: getGsapMutationType(mutation),
label,
targetId: selection.id,
targetSelector: selection.selector,
targetSourceFile: selection.sourceFile,
});
},
[activeCompPath],
);
}
export function useSafeGsapCommitMutation(
commitMutation: CommitMutation,
trackGsapSaveFailure: TrackGsapSaveFailure,
showToast?: (message: string, tone?: "error" | "info") => void,
) {
return useCallback(
(
selection: DomEditSelection,
mutation: Record<string, unknown>,
options: CommitMutationOptions,
) => {
void commitMutation(selection, mutation, options).catch((error) => {
trackGsapSaveFailure(error, selection, mutation, options.label);
showToast?.(`Couldn't save animation: ${getStudioSaveErrorMessage(error)}`, "error");
});
},
[commitMutation, trackGsapSaveFailure, showToast],
);
}