Skip to content
TypeScript

perf(web): project script settings compile with the React Compiler (#20502)

Julius Marmingeb618d53941b8
diff --git a/apps/web/reactCompiledFiles.ts b/apps/web/reactCompiledFiles.ts
index f4d8634255f..8b31df8fd7d 100644
--- a/apps/web/reactCompiledFiles.ts
+++ b/apps/web/reactCompiledFiles.ts
@@ -77,4 +77,5 @@ export const REACT_COMPILED_FILES = [
"apps/web/src/components/settings/UsageProviderSettings.tsx",
"apps/web/src/components/settings/TelemetryExportSettings.tsx",
"apps/web/src/components/settings/useHostConnectionChecks.ts",
+ "apps/web/src/components/settings/useProjectScriptSettings.ts",
] as const;
diff --git a/apps/web/src/components/settings/useProjectScriptSettings.ts b/apps/web/src/components/settings/useProjectScriptSettings.ts
index 52e09627c98..c9e64467e34 100644
--- a/apps/web/src/components/settings/useProjectScriptSettings.ts
+++ b/apps/web/src/components/settings/useProjectScriptSettings.ts
@@ -96,79 +96,84 @@ export function useProjectScriptSettings(
}
savingRef.current = true;
setSaving(true);
- try {
- for (const { environmentId, settings, keybindings, project } of targets) {
- const current = project
- ? resolveProjectScripts(settings, project)
- : settings.defaultProjectScripts;
- const nextScripts = transform(current);
- const effectiveScripts = nextScripts ?? settings.defaultProjectScripts;
- const result = await updateSettings({
- environmentId,
- input: {
- patch: project
- ? {
- projectSettingsOverrides: {
- [project.id]:
- nextScripts === null
- ? clearProjectSettingsOverrides(settings, project.id, [
- "defaultProjectScripts",
- ])
- : {
- ...settings.projectSettingsOverrides[project.id],
- defaultProjectScripts: nextScripts,
- },
- },
- }
- : { defaultProjectScripts: nextScripts ?? [] },
- },
- });
- if (result._tag === "Failure") return reportScriptFailure(result);
- const changedIds = scriptId
- ? [scriptId]
- : current
- .filter((script) => !effectiveScripts.some((next) => next.id === script.id))
- .map((script) => script.id);
- for (const id of changedIds) {
- const command = commandForProjectScript(id);
- const previousValue = keybindingValueForCommand(keybindings, command);
- const previous = previousValue
- ? decodeProjectScriptKeybindingRule({ keybinding: previousValue, command })
- : null;
- const next = decodeProjectScriptKeybindingRule({ keybinding, command });
- const retainedElsewhere =
- !nextScripts?.some((script) => script.id === id) &&
- ((project && settings.defaultProjectScripts.some((script) => script.id === id)) ||
- Object.entries(settings.projectSettingsOverrides).some(
- ([projectId, entry]) =>
- projectId !== project?.id &&
- entry.defaultProjectScripts?.some((script) => script.id === id),
- ) ||
- projects.some(
- (other) =>
- other.environmentId === environmentId &&
- other.id !== project?.id &&
- (project ? resolveProjectScripts(settings, other) : other.scripts).some(
- (script) => script.id === id,
- ),
- ));
- const bindingResult = next
- ? await upsertKeybinding({
- environmentId,
- input:
- previous && previous.key !== next.key ? { ...next, replace: previous } : next,
- })
- : previous && !retainedElsewhere
- ? await removeKeybinding({ environmentId, input: previous })
- : null;
- if (bindingResult?._tag === "Failure") return reportScriptFailure(bindingResult);
- }
- }
- return AsyncResult.success(undefined);
- } finally {
+ return writeTargets(transform, scriptId, keybinding).finally(() => {
savingRef.current = false;
setSaving(false);
+ });
+ }
+
+ async function writeTargets(
+ transform: (current: readonly ProjectScript[]) => readonly ProjectScript[] | null,
+ scriptId: string | undefined,
+ keybinding: string | null | undefined,
+ ): Promise<AtomCommandResult<void, unknown>> {
+ for (const { environmentId, settings, keybindings, project } of targets) {
+ const current = project
+ ? resolveProjectScripts(settings, project)
+ : settings.defaultProjectScripts;
+ const nextScripts = transform(current);
+ const effectiveScripts = nextScripts ?? settings.defaultProjectScripts;
+ const result = await updateSettings({
+ environmentId,
+ input: {
+ patch: project
+ ? {
+ projectSettingsOverrides: {
+ [project.id]:
+ nextScripts === null
+ ? clearProjectSettingsOverrides(settings, project.id, [
+ "defaultProjectScripts",
+ ])
+ : {
+ ...settings.projectSettingsOverrides[project.id],
+ defaultProjectScripts: nextScripts,
+ },
+ },
+ }
+ : { defaultProjectScripts: nextScripts ?? [] },
+ },
+ });
+ if (result._tag === "Failure") return reportScriptFailure(result);
+ const changedIds = scriptId
+ ? [scriptId]
+ : current
+ .filter((script) => !effectiveScripts.some((next) => next.id === script.id))
+ .map((script) => script.id);
+ for (const id of changedIds) {
+ const command = commandForProjectScript(id);
+ const previousValue = keybindingValueForCommand(keybindings, command);
+ const previous = previousValue
+ ? decodeProjectScriptKeybindingRule({ keybinding: previousValue, command })
+ : null;
+ const next = decodeProjectScriptKeybindingRule({ keybinding, command });
+ const retainedElsewhere =
+ !nextScripts?.some((script) => script.id === id) &&
+ ((project && settings.defaultProjectScripts.some((script) => script.id === id)) ||
+ Object.entries(settings.projectSettingsOverrides).some(
+ ([projectId, entry]) =>
+ projectId !== project?.id &&
+ entry.defaultProjectScripts?.some((script) => script.id === id),
+ ) ||
+ projects.some(
+ (other) =>
+ other.environmentId === environmentId &&
+ other.id !== project?.id &&
+ (project ? resolveProjectScripts(settings, other) : other.scripts).some(
+ (script) => script.id === id,
+ ),
+ ));
+ const bindingResult = next
+ ? await upsertKeybinding({
+ environmentId,
+ input: previous && previous.key !== next.key ? { ...next, replace: previous } : next,
+ })
+ : previous && !retainedElsewhere
+ ? await removeKeybinding({ environmentId, input: previous })
+ : null;
+ if (bindingResult?._tag === "Failure") return reportScriptFailure(bindingResult);
+ }
}
+ return AsyncResult.success(undefined);
}
function submit(scriptId: string | null, input: NewProjectScriptInput) {