Skip to content

Commit a3e07df

Browse files
perf(web): the theme editor compiles with the React Compiler
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 77a4083 commit a3e07df

2 files changed

Lines changed: 119 additions & 122 deletions

File tree

‎apps/web/reactCompiledFiles.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@ export const REACT_COMPILED_FILES = [
3333
"apps/web/src/state/queries.ts",
3434
"apps/web/src/state/sourceControlActions.ts",
3535
"apps/web/src/components/settings/ProjectFaviconPickerDialog.tsx",
36+
"apps/web/src/components/settings/ThemeEditorPanel.tsx",
3637
] as const;

‎apps/web/src/components/settings/ThemeEditorPanel.tsx‎

Lines changed: 118 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { MousePointer2Icon, PaintbrushIcon, PlusIcon, XIcon } from "lucide-react";
22
import { ChevronDown, ChevronUp } from "lucide";
3-
import {
4-
useCallback,
5-
useEffect,
6-
useRef,
7-
useState,
8-
type PointerEvent as ReactPointerEvent,
9-
} from "react";
3+
import { useEffect, useRef, useState, type PointerEvent as ReactPointerEvent } from "react";
104
import {
115
applyThemeColorPreview,
126
THEME_COLOR_ROLES,
@@ -277,6 +271,37 @@ function getManagedEditorColors(
277271
);
278272
}
279273

274+
function clampPanelPosition(
275+
panel: HTMLElement | null,
276+
x: number,
277+
y: number,
278+
widthOverride?: number,
279+
) {
280+
const margin = 8;
281+
// The caller passes a width when it has just shrunk the panel: the DOM
282+
// still reports the old one until React commits.
283+
const width = widthOverride ?? panel?.offsetWidth ?? 0;
284+
return {
285+
x: Math.min(Math.max(x, margin), Math.max(margin, window.innerWidth - width - margin)),
286+
y: Math.min(Math.max(y, margin), Math.max(margin, window.innerHeight - 48)),
287+
};
288+
}
289+
290+
// Kept out of the component: the compiler cannot lower a throw inside try/catch.
291+
function retireMergedTheme(retired: ThemeDefinition, mergeTarget: ThemeDefinition) {
292+
try {
293+
removeCustomTheme(retired.id);
294+
} catch (cause) {
295+
// The merge already persisted. Leaving it while the edited theme
296+
// survives would collide on every retry, so the target goes back to
297+
// its pre-merge palettes before the failure surfaces.
298+
try {
299+
updateCustomTheme(mergeTarget);
300+
} catch {}
301+
throw cause;
302+
}
303+
}
304+
280305
export function ThemeEditorPanel({
281306
open,
282307
onOpenChange,
@@ -345,7 +370,7 @@ export function ThemeEditorPanel({
345370
});
346371
setPosition((current) => {
347372
if (!current) return current;
348-
const clamped = clampPosition(current.x, current.y, clampedWidth);
373+
const clamped = clampPanelPosition(panelRef.current, current.x, current.y, clampedWidth);
349374
const height = isMinimized
350375
? (panelRef.current?.offsetHeight ?? 0)
351376
: (clampedHeight ?? panelRef.current?.offsetHeight ?? 0);
@@ -357,14 +382,13 @@ export function ThemeEditorPanel({
357382
return () => window.removeEventListener("resize", clamp);
358383
}, [isMinimized, open]);
359384

360-
// The draft only reaches the live app once this open has been seeded;
361-
// previewing in the seeding commit would paint the previous session's
362-
// colors for a frame.
363-
const [isDraftSeeded, setIsDraftSeeded] = useState(false);
364-
const previousOpenRef = useRef(false);
365-
366-
useEffect(() => {
367-
if (open && !previousOpenRef.current) {
385+
// Seeded during render so the first open commit already holds this
386+
// session's draft; seeding in an effect would preview the previous
387+
// session's colors for a frame.
388+
const [seededOpen, setSeededOpen] = useState(false);
389+
if (seededOpen !== open) {
390+
setSeededOpen(open);
391+
if (open) {
368392
const sourceTheme = editingTheme ?? seedTheme ?? null;
369393
const nextColors = getThemeEditorColorsByAppearance();
370394
const nextAppearance = sourceTheme
@@ -393,11 +417,8 @@ export function ThemeEditorPanel({
393417
setUsageCount(null);
394418
setIsInspecting(false);
395419
setError(null);
396-
setIsDraftSeeded(true);
397420
}
398-
if (!open && isDraftSeeded) setIsDraftSeeded(false);
399-
previousOpenRef.current = open;
400-
}, [editingTheme, initialAppearance, isDraftSeeded, open, seedName, seedTheme]);
421+
}
401422

402423
const nameTargetId = themeIdFromName(name);
403424
const normalizedName = name.trim().toLowerCase();
@@ -422,22 +443,25 @@ export function ThemeEditorPanel({
422443
return null;
423444
};
424445

425-
const mergeTargetId = mergeTarget?.id ?? null;
426-
const takenAppearancesKey = takenAppearances.join(",");
427-
useEffect(() => {
428-
if (isEditing || mergeTargetId === null) return;
429-
const taken = takenAppearancesKey.split(",").filter(Boolean) as ThemeAppearance[];
430-
if (taken.length !== 1) return;
431-
setActiveAppearance((current) => {
432-
if (!taken.includes(current)) return current;
433-
return taken[0] === "light" ? "dark" : "light";
434-
});
435-
}, [isEditing, mergeTargetId, takenAppearancesKey]);
446+
// Moves off a palette the merge target already has, once per target change.
447+
const mergeKey =
448+
isEditing || mergeTarget === null ? null : `${mergeTarget.id}:${takenAppearances.join(",")}`;
449+
const [appliedMergeKey, setAppliedMergeKey] = useState<string | null>(null);
450+
if (appliedMergeKey !== mergeKey) {
451+
setAppliedMergeKey(mergeKey);
452+
if (
453+
mergeKey !== null &&
454+
takenAppearances.length === 1 &&
455+
takenAppearances.includes(activeAppearance)
456+
) {
457+
setActiveAppearance(takenAppearances[0] === "light" ? "dark" : "light");
458+
}
459+
}
436460

437461
useEffect(() => {
438-
if (!open || !isDraftSeeded) return;
462+
if (!open) return;
439463
applyThemeColorPreview(colorsByAppearance[activeAppearance], activeAppearance);
440-
}, [activeAppearance, colorsByAppearance, isDraftSeeded, open]);
464+
}, [activeAppearance, colorsByAppearance, open]);
441465

442466
useEffect(() => {
443467
if (!open) return;
@@ -446,34 +470,31 @@ export function ThemeEditorPanel({
446470
};
447471
}, [open, restoreTheme]);
448472

449-
const updateColor = useCallback(
450-
(role: ThemeColorRole, value: string) => {
451-
setColorsByAppearance((current) => {
452-
const nextColors = { ...current[activeAppearance], [role]: value };
453-
const shouldManageColors =
454-
!isAdvanced && THEME_EDITOR_SIMPLE_ROLES.includes(role) && isThemeEditorColor(value);
473+
const updateColor = (role: ThemeColorRole, value: string) => {
474+
setColorsByAppearance((current) => {
475+
const nextColors = { ...current[activeAppearance], [role]: value };
476+
const shouldManageColors =
477+
!isAdvanced && THEME_EDITOR_SIMPLE_ROLES.includes(role) && isThemeEditorColor(value);
455478

456-
return {
457-
...current,
458-
[activeAppearance]: isAdvanced
459-
? updateThemeColorFamily(activeAppearance, current[activeAppearance], role, value)
460-
: shouldManageColors
461-
? getManagedEditorColors(activeAppearance, nextColors)
462-
: nextColors,
463-
};
464-
});
465-
if (!isAdvanced && THEME_EDITOR_SIMPLE_ROLES.includes(role) && isThemeEditorColor(value)) {
466-
setSimpleColorsDirtyByAppearance((current) => ({
467-
...current,
468-
[activeAppearance]: true,
469-
}));
470-
}
471-
if (isAdvanced) setShouldRegenerateGuidedColors(true);
472-
},
473-
[activeAppearance, isAdvanced],
474-
);
479+
return {
480+
...current,
481+
[activeAppearance]: isAdvanced
482+
? updateThemeColorFamily(activeAppearance, current[activeAppearance], role, value)
483+
: shouldManageColors
484+
? getManagedEditorColors(activeAppearance, nextColors)
485+
: nextColors,
486+
};
487+
});
488+
if (!isAdvanced && THEME_EDITOR_SIMPLE_ROLES.includes(role) && isThemeEditorColor(value)) {
489+
setSimpleColorsDirtyByAppearance((current) => ({
490+
...current,
491+
[activeAppearance]: true,
492+
}));
493+
}
494+
if (isAdvanced) setShouldRegenerateGuidedColors(true);
495+
};
475496

476-
const selectThemeRole = useCallback((role: ThemeColorRole, reveal = false) => {
497+
const selectThemeRole = (role: ThemeColorRole, reveal = false) => {
477498
const visibleRole = getThemeEditorColorFamily(role)?.role ?? role;
478499
setSelectedRole(visibleRole);
479500
if (!THEME_EDITOR_SIMPLE_ROLES.includes(visibleRole)) {
@@ -487,17 +508,17 @@ export function ThemeEditorPanel({
487508
?.querySelector(`[data-theme-color-role="${visibleRole}"]`)
488509
?.scrollIntoView({ behavior: "smooth", block: "nearest" });
489510
});
490-
}, []);
511+
};
491512

492-
const toggleThemeRole = useCallback((role: ThemeColorRole) => {
513+
const toggleThemeRole = (role: ThemeColorRole) => {
493514
setSelectedRole((current) => (current === role ? null : role));
494-
}, []);
515+
};
495516

496-
const clearInspectorSelection = useCallback(() => {
517+
const clearInspectorSelection = () => {
497518
setSelectedRole(null);
498519
setUsageCount(null);
499520
setIsInspecting(false);
500-
}, []);
521+
};
501522

502523
const selectedHighlightRoles = selectedRole
503524
? isAdvanced
@@ -514,11 +535,7 @@ export function ThemeEditorPanel({
514535

515536
useEffect(() => {
516537
clearThemeInspectorHighlights();
517-
if (!open || selectedRole === null) {
518-
setUsageCount(null);
519-
return;
520-
}
521-
if (isInspecting) return;
538+
if (!open || selectedRole === null || isInspecting) return;
522539

523540
const highlightedRoles = selectedHighlightRolesKey.split(",") as Array<ThemeColorRole>;
524541
const refreshHighlights = () => setUsageCount(highlightThemeRoleUsage(highlightedRoles));
@@ -559,7 +576,8 @@ export function ThemeEditorPanel({
559576
observer.observe(document.body, { childList: true, subtree: true });
560577
let spotlightFrame: number | null = null;
561578
const scheduleSpotlightRefresh = () => {
562-
spotlightFrame ??= requestAnimationFrame(() => {
579+
if (spotlightFrame !== null) return;
580+
spotlightFrame = requestAnimationFrame(() => {
563581
spotlightFrame = null;
564582
refreshThemeInspectorSpotlight();
565583
});
@@ -574,6 +592,7 @@ export function ThemeEditorPanel({
574592
window.removeEventListener("resize", scheduleSpotlightRefresh);
575593
window.removeEventListener("scroll", scheduleSpotlightRefresh, true);
576594
clearThemeInspectorHighlights();
595+
setUsageCount(null);
577596
};
578597
}, [isInspecting, open, selectedHighlightRolesKey, selectedRole]);
579598

@@ -663,8 +682,8 @@ export function ThemeEditorPanel({
663682
clearInspectorSelection();
664683
};
665684
const refreshHover = () => {
666-
if (!hoverInspection) return;
667-
hoverFrame ??= requestAnimationFrame(() => {
685+
if (!hoverInspection || hoverFrame !== null) return;
686+
hoverFrame = requestAnimationFrame(() => {
668687
hoverFrame = null;
669688
if (hoverInspection) {
670689
showThemeInspectorHover(
@@ -698,35 +717,32 @@ export function ThemeEditorPanel({
698717
};
699718
}, [clearInspectorSelection, isInspecting, open, selectThemeRole]);
700719

701-
const handleAdvancedChange = useCallback(
702-
(checked: boolean) => {
703-
setIsAdvanced(checked);
704-
if (checked) return;
705-
if (selectedRole && !THEME_EDITOR_SIMPLE_ROLES.includes(selectedRole)) {
706-
setSelectedRole(null);
707-
}
708-
if (!shouldRegenerateGuidedColors) return;
720+
const handleAdvancedChange = (checked: boolean) => {
721+
setIsAdvanced(checked);
722+
if (checked) return;
723+
if (selectedRole && !THEME_EDITOR_SIMPLE_ROLES.includes(selectedRole)) {
724+
setSelectedRole(null);
725+
}
726+
if (!shouldRegenerateGuidedColors) return;
709727

710-
const managedAppearances: ReadonlyArray<ThemeAppearance> =
711-
editingTheme && getThemeModes(editingTheme).length > 1
712-
? ["light", "dark"]
713-
: [activeAppearance];
714-
setSimpleColorsDirtyByAppearance((current) => {
715-
const next = { ...current };
716-
for (const appearance of managedAppearances) next[appearance] = true;
717-
return next;
718-
});
719-
setColorsByAppearance((current) => {
720-
const next = { ...current };
721-
for (const appearance of managedAppearances) {
722-
next[appearance] = getManagedEditorColors(appearance, current[appearance]);
723-
}
724-
return next;
725-
});
726-
setShouldRegenerateGuidedColors(false);
727-
},
728-
[activeAppearance, editingTheme, selectedRole, shouldRegenerateGuidedColors],
729-
);
728+
const managedAppearances: ReadonlyArray<ThemeAppearance> =
729+
editingTheme && getThemeModes(editingTheme).length > 1
730+
? ["light", "dark"]
731+
: [activeAppearance];
732+
setSimpleColorsDirtyByAppearance((current) => {
733+
const next = { ...current };
734+
for (const appearance of managedAppearances) next[appearance] = true;
735+
return next;
736+
});
737+
setColorsByAppearance((current) => {
738+
const next = { ...current };
739+
for (const appearance of managedAppearances) {
740+
next[appearance] = getManagedEditorColors(appearance, current[appearance]);
741+
}
742+
return next;
743+
});
744+
setShouldRegenerateGuidedColors(false);
745+
};
730746

731747
const handleSubmit = () => {
732748
if (!name.trim()) {
@@ -773,17 +789,7 @@ export function ThemeEditorPanel({
773789
...(mergeTarget.collection ? { collection: mergeTarget.collection } : {}),
774790
});
775791
retiredTheme = editingTheme;
776-
try {
777-
removeCustomTheme(editingTheme.id);
778-
} catch (cause) {
779-
// The merge already persisted. Leaving it while the edited theme
780-
// survives would collide on every retry, so the target goes back to
781-
// its pre-merge palettes before the failure surfaces.
782-
try {
783-
updateCustomTheme(mergeTarget);
784-
} catch {}
785-
throw cause;
786-
}
792+
retireMergedTheme(editingTheme, mergeTarget);
787793
} else if (editingTheme) {
788794
const baseAppearance = editingTheme.appearance;
789795
const variantAppearance = baseAppearance === "light" ? "dark" : "light";
@@ -1015,17 +1021,7 @@ export function ThemeEditorPanel({
10151021
);
10161022
};
10171023

1018-
const clampPosition = (x: number, y: number, widthOverride?: number) => {
1019-
const panel = panelRef.current;
1020-
const margin = 8;
1021-
// The caller passes a width when it has just shrunk the panel: the DOM
1022-
// still reports the old one until React commits.
1023-
const width = widthOverride ?? panel?.offsetWidth ?? 0;
1024-
return {
1025-
x: Math.min(Math.max(x, margin), Math.max(margin, window.innerWidth - width - margin)),
1026-
y: Math.min(Math.max(y, margin), Math.max(margin, window.innerHeight - 48)),
1027-
};
1028-
};
1024+
const clampPosition = (x: number, y: number) => clampPanelPosition(panelRef.current, x, y);
10291025

10301026
const handleDragPointerDown = (event: ReactPointerEvent<HTMLDivElement>) => {
10311027
if ((event.target as HTMLElement).closest("button, input, a")) return;

0 commit comments

Comments
 (0)