@devries-cameron20 / t3code-1 / commits / 275e55cfca
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

perf(web): the theme editor compiles with the React Compiler (#20458)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 16, 2026 at 18:46 UTC 275e55cfca59ead9ea39ab1fb514535c91bc4414
2 files changed +119 -122
apps/web/reactCompiledFiles.ts
+1
@@ -33,4 +33,5 @@ export const REACT_COMPILED_FILES = [
33 "apps/web/src/state/queries.ts",
34 "apps/web/src/state/sourceControlActions.ts",
35 "apps/web/src/components/settings/ProjectFaviconPickerDialog.tsx",
36 + "apps/web/src/components/settings/ThemeEditorPanel.tsx",
37 ] as const;
apps/web/src/components/settings/ThemeEditorPanel.tsx
+118 -122
@@ -1,12 +1,6 @@
1 import { MousePointer2Icon, PaintbrushIcon, PlusIcon, XIcon } from "lucide-react";
2 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";
4 import {
5 applyThemeColorPreview,
6 THEME_COLOR_ROLES,
@@ -277,6 +271,37 @@ function getManagedEditorColors(
271 );
272 }
273
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 +
305 export function ThemeEditorPanel({
306 open,
307 onOpenChange,
@@ -345,7 +370,7 @@ export function ThemeEditorPanel({
370 });
371 setPosition((current) => {
372 if (!current) return current;
348 - const clamped = clampPosition(current.x, current.y, clampedWidth);
373 + const clamped = clampPanelPosition(panelRef.current, current.x, current.y, clampedWidth);
374 const height = isMinimized
375 ? (panelRef.current?.offsetHeight ?? 0)
376 : (clampedHeight ?? panelRef.current?.offsetHeight ?? 0);
@@ -357,14 +382,13 @@ export function ThemeEditorPanel({
382 return () => window.removeEventListener("resize", clamp);
383 }, [isMinimized, open]);
384
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) {
392 const sourceTheme = editingTheme ?? seedTheme ?? null;
393 const nextColors = getThemeEditorColorsByAppearance();
394 const nextAppearance = sourceTheme
@@ -393,11 +417,8 @@ export function ThemeEditorPanel({
417 setUsageCount(null);
418 setIsInspecting(false);
419 setError(null);
396 - setIsDraftSeeded(true);
420 }
398 - if (!open && isDraftSeeded) setIsDraftSeeded(false);
399 - previousOpenRef.current = open;
400 - }, [editingTheme, initialAppearance, isDraftSeeded, open, seedName, seedTheme]);
421 + }
422
423 const nameTargetId = themeIdFromName(name);
424 const normalizedName = name.trim().toLowerCase();
@@ -422,22 +443,25 @@ export function ThemeEditorPanel({
443 return null;
444 };
445
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 + }
460
461 useEffect(() => {
438 - if (!open || !isDraftSeeded) return;
462 + if (!open) return;
463 applyThemeColorPreview(colorsByAppearance[activeAppearance], activeAppearance);
440 - }, [activeAppearance, colorsByAppearance, isDraftSeeded, open]);
464 + }, [activeAppearance, colorsByAppearance, open]);
465
466 useEffect(() => {
467 if (!open) return;
@@ -446,34 +470,31 @@ export function ThemeEditorPanel({
470 };
471 }, [open, restoreTheme]);
472
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);
478
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 + };
496
476 - const selectThemeRole = useCallback((role: ThemeColorRole, reveal = false) => {
497 + const selectThemeRole = (role: ThemeColorRole, reveal = false) => {
498 const visibleRole = getThemeEditorColorFamily(role)?.role ?? role;
499 setSelectedRole(visibleRole);
500 if (!THEME_EDITOR_SIMPLE_ROLES.includes(visibleRole)) {
@@ -487,17 +508,17 @@ export function ThemeEditorPanel({
508 ?.querySelector(`[data-theme-color-role="${visibleRole}"]`)
509 ?.scrollIntoView({ behavior: "smooth", block: "nearest" });
510 });
490 - }, []);
511 + };
512
492 - const toggleThemeRole = useCallback((role: ThemeColorRole) => {
513 + const toggleThemeRole = (role: ThemeColorRole) => {
514 setSelectedRole((current) => (current === role ? null : role));
494 - }, []);
515 + };
516
496 - const clearInspectorSelection = useCallback(() => {
517 + const clearInspectorSelection = () => {
518 setSelectedRole(null);
519 setUsageCount(null);
520 setIsInspecting(false);
500 - }, []);
521 + };
522
523 const selectedHighlightRoles = selectedRole
524 ? isAdvanced
@@ -514,11 +535,7 @@ export function ThemeEditorPanel({
535
536 useEffect(() => {
537 clearThemeInspectorHighlights();
517 - if (!open || selectedRole === null) {
518 - setUsageCount(null);
519 - return;
520 - }
521 - if (isInspecting) return;
538 + if (!open || selectedRole === null || isInspecting) return;
539
540 const highlightedRoles = selectedHighlightRolesKey.split(",") as Array<ThemeColorRole>;
541 const refreshHighlights = () => setUsageCount(highlightThemeRoleUsage(highlightedRoles));
@@ -559,7 +576,8 @@ export function ThemeEditorPanel({
576 observer.observe(document.body, { childList: true, subtree: true });
577 let spotlightFrame: number | null = null;
578 const scheduleSpotlightRefresh = () => {
562 - spotlightFrame ??= requestAnimationFrame(() => {
579 + if (spotlightFrame !== null) return;
580 + spotlightFrame = requestAnimationFrame(() => {
581 spotlightFrame = null;
582 refreshThemeInspectorSpotlight();
583 });
@@ -574,6 +592,7 @@ export function ThemeEditorPanel({
592 window.removeEventListener("resize", scheduleSpotlightRefresh);
593 window.removeEventListener("scroll", scheduleSpotlightRefresh, true);
594 clearThemeInspectorHighlights();
595 + setUsageCount(null);
596 };
597 }, [isInspecting, open, selectedHighlightRolesKey, selectedRole]);
598
@@ -663,8 +682,8 @@ export function ThemeEditorPanel({
682 clearInspectorSelection();
683 };
684 const refreshHover = () => {
666 - if (!hoverInspection) return;
667 - hoverFrame ??= requestAnimationFrame(() => {
685 + if (!hoverInspection || hoverFrame !== null) return;
686 + hoverFrame = requestAnimationFrame(() => {
687 hoverFrame = null;
688 if (hoverInspection) {
689 showThemeInspectorHover(
@@ -698,35 +717,32 @@ export function ThemeEditorPanel({
717 };
718 }, [clearInspectorSelection, isInspecting, open, selectThemeRole]);
719
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;
727
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 + };
746
747 const handleSubmit = () => {
748 if (!name.trim()) {
@@ -773,17 +789,7 @@ export function ThemeEditorPanel({
789 ...(mergeTarget.collection ? { collection: mergeTarget.collection } : {}),
790 });
791 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);
793 } else if (editingTheme) {
794 const baseAppearance = editingTheme.appearance;
795 const variantAppearance = baseAppearance === "light" ? "dark" : "light";
@@ -1015,17 +1021,7 @@ export function ThemeEditorPanel({
1021 );
1022 };
1023
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);
1025
1026 const handleDragPointerDown = (event: ReactPointerEvent<HTMLDivElement>) => {
1027 if ((event.target as HTMLElement).closest("button, input, a")) return;