From e9d9b815584bc3b8f20a5f9b4d238f3464628844 Mon Sep 17 00:00:00 2001 From: shuv1337 Date: Sat, 14 Nov 2026 21:44:12 -0800 Subject: [PATCH 1/3] feat(branding): Ocean dark default theme, drop starry sidebar header --- .../AppearancePreferencesProvider.tsx | 8 +- apps/mobile/src/lib/mobileTheme.test.ts | 14 ++- apps/mobile/src/lib/mobileTheme.ts | 18 +++- apps/web/index.html | 40 +++++---- apps/web/src/components/AppSidebarLayout.tsx | 13 +-- .../src/components/SidebarStageBackdrop.tsx | 27 ++---- .../settings/AppearanceSettingsPanel.tsx | 2 +- .../src/components/sidebar/SidebarChrome.tsx | 50 +++-------- apps/web/src/hooks/useTheme.ts | 27 +++--- apps/web/src/index.css | 31 ------- apps/web/src/themeBoot.test.ts | 87 ++++++++++++------- 11 files changed, 152 insertions(+), 165 deletions(-) diff --git a/apps/mobile/src/features/settings/appearance/AppearancePreferencesProvider.tsx b/apps/mobile/src/features/settings/appearance/AppearancePreferencesProvider.tsx index 26e17f45afb13..6b7c160dcfbb4 100644 --- a/apps/mobile/src/features/settings/appearance/AppearancePreferencesProvider.tsx +++ b/apps/mobile/src/features/settings/appearance/AppearancePreferencesProvider.tsx @@ -31,7 +31,7 @@ import type { MobileThemeVariables } from "../../../lib/mobileTheme"; import { createMobileThemePairPatch, createMobileThemeSelectionPatch, - normalizeMobileThemeMode, + resolveMobileThemeMode, resolveMobileThemeIds, type MobileThemeAppearance, type MobileThemeId, @@ -120,7 +120,7 @@ function resolveThemeVariablesByAppearance( /** The appearance whose theme is on screen, read from the native scheme. */ export function resolveThemeAppearance(stored: Preferences | null): MobileThemeAppearance { - const themeMode = normalizeMobileThemeMode(stored?.themeMode); + const themeMode = resolveMobileThemeMode(stored ?? {}); if (themeMode !== "system") return themeMode; return Appearance.getColorScheme() === "dark" ? "dark" : "light"; } @@ -128,7 +128,7 @@ export function resolveThemeAppearance(stored: Preferences | null): MobileThemeA function runtimeStateFor(stored: Preferences | null): MobileThemeRuntimeState { return { baseFontSize: resolveAppearancePreferences(stored).baseFontSize, - themeMode: normalizeMobileThemeMode(stored?.themeMode), + themeMode: resolveMobileThemeMode(stored ?? {}), themeAppearance: resolveThemeAppearance(stored), }; } @@ -201,7 +201,7 @@ export function AppearancePreferencesProvider(props: { readonly children: ReactN manufacturer, process.env.EXPO_PUBLIC_T3_FLOATING_TABS === "1", ); - const themeMode = normalizeMobileThemeMode(storedPreferences?.themeMode); + const themeMode = resolveMobileThemeMode(storedPreferences ?? {}); const themeAppearance = themeMode === "system" ? systemColorScheme : themeMode; const resolvedThemeIds = resolveMobileThemeIds(storedPreferences ?? {}); const themeIds = useMemo( diff --git a/apps/mobile/src/lib/mobileTheme.test.ts b/apps/mobile/src/lib/mobileTheme.test.ts index 0b35a5ba27d31..efcc7fe337e44 100644 --- a/apps/mobile/src/lib/mobileTheme.test.ts +++ b/apps/mobile/src/lib/mobileTheme.test.ts @@ -21,6 +21,7 @@ import { flattenThemeColor, getMobileThemeVariables, resolveMobileThemeIds, + resolveMobileThemeMode, themeColorWithAlpha, themeColorToNativeColor, } from "./mobileTheme"; @@ -225,6 +226,17 @@ describe("mobile themes", () => { }); it("migrates one theme choice to both appearances and preserves independent choices", () => { + expect(resolveMobileThemeIds({})).toEqual({ light: "ocean", dark: "ocean" }); + expect(resolveMobileThemeMode({})).toBe("dark"); + expect(resolveMobileThemeMode({ themeId: "grove" })).toBe("system"); + for (const themeMode of ["light", "dark", "system"] as const) { + expect(resolveMobileThemeMode({ themeMode })).toBe(themeMode); + expect(resolveMobileThemeMode({ themeId: "t3-code", themeMode })).toBe(themeMode); + } + expect(resolveMobileThemeIds({ themeId: "t3-code" })).toEqual({ + light: "t3-code", + dark: "t3-code", + }); expect(resolveMobileThemeIds({ themeId: "grove" })).toEqual({ light: "grove", dark: "grove", @@ -233,7 +245,7 @@ describe("mobile themes", () => { resolveMobileThemeIds({ themeId: "grove", lightThemeId: "iris", darkThemeId: "ocean" }), ).toEqual({ light: "iris", dark: "ocean" }); expect(resolveMobileThemeIds({ themeId: "grove", lightThemeId: "missing" })).toEqual({ - light: DEFAULT_MOBILE_THEME_ID, + light: "ocean", dark: "grove", }); }); diff --git a/apps/mobile/src/lib/mobileTheme.ts b/apps/mobile/src/lib/mobileTheme.ts index cdbe844265ad0..acf2ae1aad962 100644 --- a/apps/mobile/src/lib/mobileTheme.ts +++ b/apps/mobile/src/lib/mobileTheme.ts @@ -37,11 +37,23 @@ export type MobileThemeVariables = Readonly> function normalizeMobileThemeId(value: unknown): MobileThemeId { return typeof value === "string" && (MOBILE_THEME_IDS as readonly string[]).includes(value) ? (value as MobileThemeId) - : DEFAULT_MOBILE_THEME_ID; + : "ocean"; } -export function normalizeMobileThemeMode(value: unknown): MobileThemeMode { - return value === "light" || value === "dark" || value === "system" ? value : "system"; +export function resolveMobileThemeMode(preferences: { + readonly themeMode?: unknown; + readonly themeId?: unknown; + readonly lightThemeId?: unknown; + readonly darkThemeId?: unknown; +}): MobileThemeMode { + const value = preferences.themeMode; + if (value === "light" || value === "dark" || value === "system") return value; + const hasThemeChoice = [ + preferences.themeId, + preferences.lightThemeId, + preferences.darkThemeId, + ].some((id) => typeof id === "string" && (MOBILE_THEME_IDS as readonly string[]).includes(id)); + return hasThemeChoice ? "system" : "dark"; } export function resolveMobileThemeIds(preferences: { diff --git a/apps/web/index.html b/apps/web/index.html index 0f3f4e009c6fd..15d98c3ca2f57 100644 --- a/apps/web/index.html +++ b/apps/web/index.html @@ -219,7 +219,7 @@ }; try { - const storedTheme = window.localStorage.getItem("t3code:theme"); + let storedTheme = window.localStorage.getItem("t3code:theme"); const prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches; const LEGACY_THEME_IDS = { "t3-chat-dark": "t3-chat", @@ -228,9 +228,16 @@ "t3-ember": "ember", "t3-iris": "iris", }; + if ( + storedTheme !== null && + !RESERVED_THEME_IDS.includes(storedTheme) && + !findCustomTheme(storedTheme) + ) { + storedTheme = null; + } const isLegacyT3ChatDark = storedTheme === "t3-chat-dark"; const themeId = - storedTheme === null ? "" : (LEGACY_THEME_IDS[storedTheme] ?? storedTheme); + storedTheme === null ? "ocean" : (LEGACY_THEME_IDS[storedTheme] ?? storedTheme); const legacyMode = isLegacyT3ChatDark ? "dark" : null; const builtInModes = BUILT_IN_THEME_MODES[themeId] ?? null; const customTheme = builtInModes ? null : findCustomTheme(themeId); @@ -246,9 +253,13 @@ storedTheme === "dark" || storedTheme === "system" || themeModes !== null; - const theme = isKnownTheme && storedTheme ? storedTheme : "system"; + const theme = isKnownTheme && storedTheme ? storedTheme : "ocean"; const isThemed = isKnownTheme && themeModes !== null; - const baseAppearance = customTheme ? customTheme.appearance : "light"; + const baseAppearance = customTheme + ? customTheme.appearance + : storedTheme === null + ? "dark" + : "light"; const systemMode = prefersDark ? "dark" : "light"; const storedAppearanceMode = window.localStorage.getItem( THEME_APPEARANCE_MODE_STORAGE_KEY, @@ -354,7 +365,7 @@ } // A resolved half is a selection even when the base theme is gone // (deleting the base leaves the mix pointing at a real theme). - const hasSelection = halfThemeId !== null || (isKnownTheme && storedTheme !== null); + const hasSelection = halfThemeId !== null || isKnownTheme; if (hasSelection) { document.documentElement.dataset.themeSelected = "true"; } else { @@ -379,16 +390,15 @@ } setThemeColor(chromeColor); } catch { - let prefersDark = true; - try { - prefersDark = window.matchMedia("(prefers-color-scheme: dark)").matches; - } catch {} - delete document.documentElement.dataset.themeId; - delete document.documentElement.dataset.themeSelected; - document.documentElement.classList.toggle("dark", prefersDark); - const fallbackColor = prefersDark ? DARK_BACKGROUND : LIGHT_BACKGROUND; - document.documentElement.style.backgroundColor = fallbackColor; - setThemeColor(fallbackColor); + const splash = BUILT_IN_THEME_PALETTES.ocean.dark; + document.documentElement.dataset.themeId = "ocean"; + document.documentElement.dataset.themeSelected = "true"; + document.documentElement.classList.add("dark"); + document.documentElement.style.backgroundColor = splash.chrome; + for (const [name, value] of Object.entries(splash)) { + document.documentElement.style.setProperty(`--boot-${name}`, value); + } + setThemeColor(splash.chrome); } })(); diff --git a/apps/web/src/components/AppSidebarLayout.tsx b/apps/web/src/components/AppSidebarLayout.tsx index 8e4baf20451c8..6749a28712493 100644 --- a/apps/web/src/components/AppSidebarLayout.tsx +++ b/apps/web/src/components/AppSidebarLayout.tsx @@ -31,7 +31,6 @@ import { selectActiveRightPanel, useRightPanelStore } from "../rightPanelStore"; import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore"; import { cn, isMacPlatform } from "../lib/utils"; import { primaryServerKeybindingsAtom } from "../state/server"; -import { useEnvironmentIdentificationMode } from "../hooks/useSettings"; import { PanelAnimationSuppressionProvider, usePanelAnimationSettings, @@ -43,7 +42,6 @@ import ThreadSidebar from "./Sidebar"; import { SidebarBrandWidthProbe } from "./sidebar/SidebarChrome"; import { isAppModalPage } from "./sidebar/mainAppLocation"; import type { SidebarRouteReveal } from "./sidebar/routeReveal"; -import { useSidebarStageBackdropVariant } from "./SidebarStageBackdrop"; import { clampThreadSidebarWidth, resolveInitialThreadSidebarWidth, @@ -59,7 +57,6 @@ import { SidebarTrigger, useMobileSidebarSheetMounted, useSidebar, - useSidebarVisibility, } from "./ui/sidebar"; import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip"; import { appAtomRegistry } from "../rpc/atomRegistry"; @@ -319,9 +316,6 @@ function SidebarControl({ showSidebar }: { readonly showSidebar: boolean }) { const keybindings = useAtomValue(primaryServerKeybindingsAtom); const { toggleSidebar } = useSidebar(); const mobileSidebarSheetMounted = useMobileSidebarSheetMounted(); - const isSidebarVisible = useSidebarVisibility(); - const environmentIdentificationMode = useEnvironmentIdentificationMode(); - const stageBackdropVariant = useSidebarStageBackdropVariant(environmentIdentificationMode); const shortcutLabel = shortcutLabelForCommand(keybindings, "sidebar.toggle", { context: { usagePageOpen }, }); @@ -393,11 +387,8 @@ function SidebarControl({ showSidebar }: { readonly showSidebar: boolean }) { } diff --git a/apps/web/src/components/SidebarStageBackdrop.tsx b/apps/web/src/components/SidebarStageBackdrop.tsx index b5c9d34ac6670..52105cbfaa43c 100644 --- a/apps/web/src/components/SidebarStageBackdrop.tsx +++ b/apps/web/src/components/SidebarStageBackdrop.tsx @@ -9,8 +9,6 @@ import { primaryServerConfigAtom } from "../state/server"; export type SidebarStageBackdropVariant = "nightly" | "dev"; export type EnvironmentIdentificationPillLabel = "Dev" | "Nightly"; -const STAGE_BACKDROP_VIEW_BOX = "0 0 8192 96"; - export function resolveSidebarStageBackdropVariant( stageLabel: string, mode: EnvironmentIdentificationMode = "artwork", @@ -49,23 +47,8 @@ export function useSidebarStageBackdropVariant( return resolveSidebarStageBackdropVariant(useEnvironmentStageLabel(), mode); } -export function SidebarStageBackdrop({ variant }: { variant: SidebarStageBackdropVariant }) { - return ( -
- -
- ); -} - -function StageBackdropArt({ variant }: { variant: SidebarStageBackdropVariant }) { - return variant === "nightly" ? : ; -} - export function StageBackdropButtonArt({ variant }: { variant: SidebarStageBackdropVariant }) { - return variant === "nightly" ? : ; + return variant === "nightly" ? : ; } const NIGHTLY_STARS: ReadonlyArray<{ @@ -99,7 +82,7 @@ const NIGHTLY_SPARKLES: ReadonlyArray<{ x: number; y: number }> = [ { x: 246, y: 26 }, ]; -function NightlySkyArt({ compact = false }: { compact?: boolean }) { +function NightlySkyArt() { const idPrefix = useId().replaceAll(":", ""); const skyId = `${idPrefix}-stage-night-sky`; const glowId = `${idPrefix}-stage-night-glow`; @@ -114,7 +97,7 @@ function NightlySkyArt({ compact = false }: { compact?: boolean }) { className="h-full w-full" fill="none" preserveAspectRatio="xMinYMin slice" - viewBox={compact ? "96 0 8192 96" : STAGE_BACKDROP_VIEW_BOX} + viewBox="96 0 8192 96" xmlns="http://www.w3.org/2000/svg" > @@ -211,7 +194,7 @@ function NightlySkyArt({ compact = false }: { compact?: boolean }) { ); } -function DevBlueprintArt({ compact = false }: { compact?: boolean }) { +function DevBlueprintArt() { const idPrefix = useId().replaceAll(":", ""); const paperId = `${idPrefix}-stage-bp-paper`; const glowId = `${idPrefix}-stage-bp-glow`; @@ -229,7 +212,7 @@ function DevBlueprintArt({ compact = false }: { compact?: boolean }) { className="h-full w-full" fill="none" preserveAspectRatio="xMinYMin slice" - viewBox={compact ? "64 0 8192 96" : STAGE_BACKDROP_VIEW_BOX} + viewBox="64 0 8192 96" xmlns="http://www.w3.org/2000/svg" > diff --git a/apps/web/src/components/settings/AppearanceSettingsPanel.tsx b/apps/web/src/components/settings/AppearanceSettingsPanel.tsx index 00783b085d609..ef6018f2d0739 100644 --- a/apps/web/src/components/settings/AppearanceSettingsPanel.tsx +++ b/apps/web/src/components/settings/AppearanceSettingsPanel.tsx @@ -120,7 +120,7 @@ export function AppearanceSettingsPanel() { : "Only built-in themes show artwork." : environmentStageVariant ? "Choose how Dev and Nightly environments are identified." - : "Show Dev or Nightly artwork in the sidebar header."; + : "Show Dev or Nightly artwork on the send button."; const glassOpacityRatio = (settings.glassOpacity - MIN_GLASS_OPACITY) / (MAX_GLASS_OPACITY - MIN_GLASS_OPACITY); const glassOpacitySliderStyle = { diff --git a/apps/web/src/components/sidebar/SidebarChrome.tsx b/apps/web/src/components/sidebar/SidebarChrome.tsx index 07909049dd23d..5bf7f89720015 100644 --- a/apps/web/src/components/sidebar/SidebarChrome.tsx +++ b/apps/web/src/components/sidebar/SidebarChrome.tsx @@ -17,8 +17,6 @@ import { usePullRequestsSupported } from "../../state/environments"; import { T3Wordmark } from "../T3Wordmark"; import { resolveEnvironmentIdentificationPillLabel, - resolveSidebarStageBackdropVariant, - SidebarStageBackdrop, useEnvironmentStageLabel, } from "../SidebarStageBackdrop"; import { Badge } from "../ui/badge"; @@ -47,10 +45,6 @@ export const SidebarChromeHeader = memo(function SidebarChromeHeader({ }) { const stageLabel = useEnvironmentStageLabel(); const environmentIdentificationMode = useEnvironmentIdentificationMode(); - const backdropVariant = resolveSidebarStageBackdropVariant( - stageLabel, - environmentIdentificationMode, - ); const pillLabel = environmentIdentificationMode === "pill" ? resolveEnvironmentIdentificationPillLabel(stageLabel) @@ -63,21 +57,20 @@ export const SidebarChromeHeader = memo(function SidebarChromeHeader({ isElectron && "drag-region", )} > - {backdropVariant ? : null}
- + {isElectron ? (
- - + +
) : null}
@@ -113,20 +106,14 @@ export function SidebarBrandWidthProbe({ ref={observeWidth} >
- + {isElectron ?
: null}
); } -function HistoryNavigationButton({ - direction, - onBackdrop, -}: { - direction: HistoryDirection; - onBackdrop: boolean; -}) { +function HistoryNavigationButton({ direction }: { direction: HistoryDirection }) { const keybindings = useAtomValue(primaryServerKeybindingsAtom); const enabled = useCanNavigateHistory(direction); const label = direction === "back" ? "Go back" : "Go forward"; @@ -141,14 +128,11 @@ function HistoryNavigationButton({ render={ + {changedSettingLabels.join(", ") || "None"} + + {theme} {appearanceMode} + + + ); +} + +describe("device theme defaults", () => { + it("retains the stored preferences when clearing a theme key fails", async () => { + const storage = { + "t3code:theme": "grove", + [THEME_APPEARANCE_MODE_STORAGE_KEY]: "light", + [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "false", + [THEME_HALVES_STORAGE_KEY]: JSON.stringify({ dark: "ember" }), + }; + using _storage = setup(storage); + await using screen = await render(); + const removeItem = Storage.prototype.removeItem; + using _failure = vi.spyOn(Storage.prototype, "removeItem").mockImplementation(function ( + this: Storage, + key, + ) { + if (key === THEME_APPEARANCE_MODE_STORAGE_KEY) throw new Error("storage blocked"); + removeItem.call(this, key); + }); + using _log = vi.spyOn(console, "error").mockImplementation(() => {}); + await screen.getByRole("button", { name: "Restore device defaults" }).click(); + for (const [key, value] of Object.entries(storage)) + expect(localStorage.getItem(key)).toBe(value); + await expect.element(screen.getByLabelText("Theme")).toHaveTextContent("grove light"); + await expect + .element(screen.getByRole("button", { name: "Restore device defaults" })) + .toBeEnabled(); + }); + + it("has nothing to restore on a fresh install", async () => { + using _storage = setup(); + await using screen = await render(); + await expect + .element(screen.getByRole("button", { name: "Restore device defaults" })) + .toBeDisabled(); + await expect.element(screen.getByLabelText("Changed settings")).toHaveTextContent("None"); + await expect.element(screen.getByLabelText("Theme")).toHaveTextContent("ocean dark"); + }); + + it.each([ + { + name: "a different palette and light appearance", + storage: { "t3code:theme": "grove", [THEME_APPEARANCE_MODE_STORAGE_KEY]: "light" }, + labels: ["Theme", "Appearance"], + }, + { + name: "legacy System and an automatic theme mix", + storage: { + "t3code:theme": "system", + [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "true", + [THEME_HALVES_STORAGE_KEY]: JSON.stringify({ dark: "grove" }), + }, + labels: ["Theme", "Appearance", "Theme mix"], + }, + { + name: "explicit Ocean dark preferences", + storage: { "t3code:theme": "ocean", [THEME_APPEARANCE_MODE_STORAGE_KEY]: "dark" }, + labels: ["Theme", "Appearance"], + }, + ])("clears $name back to implicit Ocean dark", async ({ storage, labels }) => { + using _storage = setup(storage); + await using screen = await render(); + const button = screen.getByRole("button", { name: "Restore device defaults" }); + await expect.element(button).toBeEnabled(); + await expect + .element(screen.getByLabelText("Changed settings")) + .toHaveTextContent(labels.join(", ")); + await button.click(); + expect(mocks.confirm).toHaveBeenCalledWith( + `Restore default settings?\nThis will reset: ${labels.join(", ")}.`, + { variant: "destructive" }, + ); + for (const key of themeKeys) expect(localStorage.getItem(key), key).toBeNull(); + await expect.element(button).toBeDisabled(); + await expect.element(screen.getByLabelText("Theme")).toHaveTextContent("ocean dark"); + await act(() => refreshTheme()); + await expect.element(screen.getByLabelText("Theme")).toHaveTextContent("ocean dark"); + }); +}); diff --git a/apps/web/src/components/settings/SettingsPanels.tsx b/apps/web/src/components/settings/SettingsPanels.tsx index a6fb40306baaa..feea94bf46f7a 100644 --- a/apps/web/src/components/settings/SettingsPanels.tsx +++ b/apps/web/src/components/settings/SettingsPanels.tsx @@ -44,12 +44,7 @@ import { ProviderModelPicker } from "../chat/ProviderModelPicker"; import { TraitsPicker } from "../chat/TraitsPicker"; import { isElectron } from "../../env"; import { buildHostedChannelSelectionUrl, type HostedAppChannel } from "../../hostedPairing"; -import { - readAppearanceModePreference, - readThemeHalves, - readThemePreference, - useTheme, -} from "../../hooks/useTheme"; +import { resetThemePreferences, useTheme } from "../../hooks/useTheme"; import { useScopedSettings, useScopedSettingsMixed, @@ -341,15 +336,7 @@ function AboutVersionSection() { } export function useSettingsRestore(onRestored?: () => void) { - const { - theme, - setTheme, - followSystem, - setFollowSystem, - setThemeHalf, - clearThemeHalves, - themeHalves, - } = useTheme(); + const { theme, appearanceMode, themeHalves, themeOverrides } = useTheme(); const settings = useScopedSettings(); const updateSettings = useUpdateScopedSettings(); @@ -360,9 +347,9 @@ export function useSettingsRestore(onRestored?: () => void) { const isBackgroundActivityDirty = hasChangedBackgroundActivitySettings(settings); const changedSettingLabels = [ - ...(theme !== "system" ? ["Theme"] : []), - ...(!followSystem ? ["Follow system"] : []), - ...(themeHalves !== null ? ["Theme mix"] : []), + ...(themeOverrides.theme || theme !== "ocean" ? ["Theme"] : []), + ...(themeOverrides.appearance || appearanceMode !== "dark" ? ["Appearance"] : []), + ...(themeOverrides.halves || themeHalves !== null ? ["Theme mix"] : []), ...(settings.appearanceContrast !== DEFAULT_UNIFIED_SETTINGS.appearanceContrast ? ["Contrast"] : []), @@ -515,17 +502,7 @@ export function useSettingsRestore(onRestored?: () => void) { ); if (!confirmed) return; - // Reset only dirty theme keys so storage failures cannot block other settings; re-read after confirmation - // for safe rollback. - let previousTheme = theme; - try { - previousTheme = readThemePreference(); - } catch {} - const liveHalves = readThemeHalves(); - const needsThemeReset = previousTheme !== "system"; - const needsMixReset = liveHalves !== null; - const needsFollowSystemReset = readAppearanceModePreference(previousTheme) !== "system"; - const notifyThemeRestoreFailure = () => { + if (!resetThemePreferences()) { toastManager.add( stackedThreadToast({ type: "error", @@ -533,26 +510,6 @@ export function useSettingsRestore(onRestored?: () => void) { description: "Try again.", }), ); - }; - // Restore the base preference before the mix because restoring the base clears the mix. - const previousHalves = liveHalves; - const rollbackThemeState = () => { - if (needsThemeReset) setTheme(previousTheme); - if (previousHalves?.light) setThemeHalf("light", previousHalves.light); - if (previousHalves?.dark) setThemeHalf("dark", previousHalves.dark); - }; - if (needsThemeReset && !setTheme("system")) { - notifyThemeRestoreFailure(); - return; - } - if (needsMixReset && !clearThemeHalves()) { - rollbackThemeState(); - notifyThemeRestoreFailure(); - return; - } - if (needsFollowSystemReset && !setFollowSystem(true)) { - rollbackThemeState(); - notifyThemeRestoreFailure(); return; } updateSettings({ diff --git a/apps/web/src/hooks/useTheme.ts b/apps/web/src/hooks/useTheme.ts index a2d211a459a3b..1d1e18953c5de 100644 --- a/apps/web/src/hooks/useTheme.ts +++ b/apps/web/src/hooks/useTheme.ts @@ -34,11 +34,18 @@ type ThemeSnapshot = { followSystem: boolean; appearanceMode: ThemePreferenceMode; themeHalves: ThemeHalves | null; + themeOverrides: { theme: boolean; appearance: boolean; halves: boolean }; }; type DesktopThemeBridge = Pick; const STORAGE_KEY = "t3code:theme"; +const THEME_PREFERENCE_KEYS = { + theme: STORAGE_KEY, + appearance: THEME_APPEARANCE_MODE_STORAGE_KEY, + followSystem: THEME_FOLLOW_SYSTEM_STORAGE_KEY, + halves: THEME_HALVES_STORAGE_KEY, +} as const; const MEDIA_QUERY = "(prefers-color-scheme: dark)"; const DEFAULT_THEME_SNAPSHOT: ThemeSnapshot = { theme: "ocean", @@ -47,6 +54,7 @@ const DEFAULT_THEME_SNAPSHOT: ThemeSnapshot = { followSystem: false, appearanceMode: "dark", themeHalves: null, + themeOverrides: { theme: false, appearance: false, halves: false }, }; export function readThemeHalves(): ThemeHalves | null { @@ -127,7 +135,7 @@ let listeners: Array<() => void> = []; let lastSnapshot: ThemeSnapshot | null = null; let snapshotStale = true; let lastDesktopTheme: "light" | "dark" | "system" | null = null; -let lastAppliedTheme: Omit | null = null; +let lastAppliedTheme: Omit | null = null; let themeStorageReadFailure: ThemeStorageError | null = null; function emitChange() { @@ -173,7 +181,7 @@ export function readAppearanceModePreference(theme: Theme): ThemePreferenceMode } catch { return "dark"; } - return getThemePreferenceMode(theme) ?? "dark"; + return getThemePreferenceMode(theme) ?? "light"; } function writeAppearanceModePreference(appearanceMode: ThemePreferenceMode): void { @@ -405,6 +413,7 @@ function getSnapshot(): ThemeSnapshot { // Cache localStorage reads until signalled; useTheme calls this every render. if (!snapshotStale && lastSnapshot) return lastSnapshot; snapshotStale = false; + const themeOverrides = readThemeOverrides(); const theme = getStored(); const appearanceMode = readAppearanceModePreference(theme); const followSystem = appearanceMode === "system"; @@ -425,12 +434,23 @@ function getSnapshot(): ThemeSnapshot { lastSnapshot.systemDark === systemDark && lastSnapshot.followSystem === followSystem && lastSnapshot.appearanceMode === appearanceMode && + lastSnapshot.themeOverrides.theme === themeOverrides.theme && + lastSnapshot.themeOverrides.appearance === themeOverrides.appearance && + lastSnapshot.themeOverrides.halves === themeOverrides.halves && themeHalvesSignature(lastSnapshot.themeHalves) === themeHalvesSignature(themeHalves) ) { return lastSnapshot; } - lastSnapshot = { theme, resolvedTheme, systemDark, followSystem, appearanceMode, themeHalves }; + lastSnapshot = { + theme, + resolvedTheme, + systemDark, + followSystem, + appearanceMode, + themeHalves, + themeOverrides, + }; return lastSnapshot; } @@ -637,6 +657,56 @@ function clearThemeHalves(): boolean { return true; } +function readThemeOverrides() { + const hasPreference = (key: string) => { + try { + return typeof window !== "undefined" && window.localStorage.getItem(key) !== null; + } catch { + return false; + } + }; + return { + theme: hasPreference(THEME_PREFERENCE_KEYS.theme), + appearance: + hasPreference(THEME_PREFERENCE_KEYS.appearance) || + hasPreference(THEME_PREFERENCE_KEYS.followSystem), + halves: hasPreference(THEME_PREFERENCE_KEYS.halves), + }; +} + +export function resetThemePreferences(): boolean { + if (typeof window === "undefined") return false; + if (!Object.values(readThemeOverrides()).some(Boolean)) return true; + const cleared: Array = []; + let storageKey: string = STORAGE_KEY; + try { + for (const key of Object.values(THEME_PREFERENCE_KEYS)) { + storageKey = key; + const value = window.localStorage.getItem(key); + if (value !== null) { + window.localStorage.removeItem(key); + cleared.push([key, value]); + } + } + } catch (cause) { + for (const [key, value] of cleared) { + try { + window.localStorage.setItem(key, value); + } catch {} + } + const error = new ThemeStorageError({ operation: "write", storageKey, cause }); + console.error(error.message, { + operation: error.operation, + storageKey, + ...safeErrorLogAttributes(error), + }); + return false; + } + themeStorageReadFailure = null; + refreshTheme(); + return true; +} + export function refreshTheme({ preservePreview = false } = {}): void { if (typeof window === "undefined") return; lastAppliedTheme = null; @@ -670,5 +740,6 @@ export function useTheme() { appearanceMode: snapshot.appearanceMode, resolvedTheme, themeHalves: snapshot.themeHalves, + themeOverrides: snapshot.themeOverrides, } as const; } diff --git a/apps/web/src/themeBoot.test.ts b/apps/web/src/themeBoot.test.ts index 4392c598df99b..bad573a0bd1b6 100644 --- a/apps/web/src/themeBoot.test.ts +++ b/apps/web/src/themeBoot.test.ts @@ -149,6 +149,16 @@ describe("index.html boot script", () => { storage: { [THEME_STORAGE_KEY]: "system" }, prefersDark: false, }, + { + name: "legacy System with follow-system off on a dark OS", + storage: { [THEME_STORAGE_KEY]: "system", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "false" }, + prefersDark: true, + }, + { + name: "legacy System with follow-system off on a light OS", + storage: { [THEME_STORAGE_KEY]: "system", [THEME_FOLLOW_SYSTEM_STORAGE_KEY]: "false" }, + prefersDark: false, + }, { name: "explicit Light on a dark OS", storage: { [THEME_STORAGE_KEY]: "light" }, From 580d637e829611df14b35c482217b78c347b7862 Mon Sep 17 00:00:00 2001 From: shuv1337 Date: Sat, 14 Nov 2026 22:52:42 -0800 Subject: [PATCH 3/3] ci: give Test Web headroom after the browser worker cap --- .github/workflows/ci-full.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci-full.yml b/.github/workflows/ci-full.yml index f7e19f78c2ac7..bd750d74a488e 100644 --- a/.github/workflows/ci-full.yml +++ b/.github/workflows/ci-full.yml @@ -295,7 +295,7 @@ jobs: needs: changes if: ${{ !cancelled() && needs.changes.outputs.test_web != 'false' }} runs-on: ubuntu-24.04 - timeout-minutes: 15 + timeout-minutes: 30 steps: - name: Checkout uses: actions/checkout@v6