TypeScript
perf(web): useTheme compiles with the React Compiler (#20473)
Julius Marminge9730e044cad1
diff --git a/apps/web/reactCompiledFiles.ts b/apps/web/reactCompiledFiles.tsindex 4f66229b535..b02df0f940e 100644--- a/apps/web/reactCompiledFiles.ts+++ b/apps/web/reactCompiledFiles.ts@@ -48,4 +48,5 @@ export const REACT_COMPILED_FILES = ["apps/web/src/hooks/useThreadActions.ts","apps/web/src/components/pullRequest/PullRequestCodeTab.tsx","apps/web/src/hooks/useLocalStorage.ts",+ "apps/web/src/hooks/useTheme.ts",] as const;diff --git a/apps/web/src/hooks/useTheme.ts b/apps/web/src/hooks/useTheme.tsindex 442b5b6a05a..7b147766ea8 100644--- a/apps/web/src/hooks/useTheme.ts+++ b/apps/web/src/hooks/useTheme.ts@@ -1,7 +1,7 @@import type { DesktopBridge } from "@t3tools/contracts";import { safeErrorLogAttributes } from "@t3tools/client-runtime/errors";import * as Schema from "effect/Schema";-import { useCallback, useEffect, useSyncExternalStore } from "react";+import { useEffect, useSyncExternalStore } from "react";import {applyThemePalette,CUSTOM_THEMES_STORAGE_KEY,@@ -492,157 +492,153 @@ function subscribe(listener: () => void): () => void {};}-export function useTheme() {- const snapshot = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);- const { theme, resolvedTheme } = snapshot;-- const setTheme = useCallback((next: Theme): boolean => {- if (typeof window === "undefined") return false;- try {- // Preserve the current mode before replacing a legacy or inferred theme- // preference. Otherwise a fresh System preference is re-inferred from- // the new theme's base appearance, which can switch a dark UI to light.- writeAppearanceModePreference(readAppearanceModePreference(getStored()));- // Choosing a whole theme replaces any automatic-mode mix. The mix is- // captured first so a failed preference write can put it back instead- // of erasing it or leaving it attached to the new theme.- const previousHalvesRaw = window.localStorage.getItem(THEME_HALVES_STORAGE_KEY);- window.localStorage.removeItem(THEME_HALVES_STORAGE_KEY);- try {- writeThemePreference(next);- } catch (cause) {- if (previousHalvesRaw !== null) {- try {- window.localStorage.setItem(THEME_HALVES_STORAGE_KEY, previousHalvesRaw);- } catch {}- }- throw cause;- }- } catch (cause) {- const error = isThemeStorageError(cause)- ? cause- : new ThemeStorageError({- operation: "write",- storageKey: STORAGE_KEY,- theme: next,- cause,- });- console.error(error.message, {- operation: error.operation,- storageKey: error.storageKey,- theme: next,- ...safeErrorLogAttributes(error),- });- return false;- }- applyTheme(next, { suppressTransitions: true });- emitChange();- return true;- }, []);-- const setAppearanceMode = useCallback((nextAppearanceMode: ThemePreferenceMode): boolean => {- if (typeof window === "undefined") return false;+function setTheme(next: Theme): boolean {+ if (typeof window === "undefined") return false;+ try {+ // Preserve the current mode before replacing a legacy or inferred theme+ // preference. Otherwise a fresh System preference is re-inferred from+ // the new theme's base appearance, which can switch a dark UI to light.+ writeAppearanceModePreference(readAppearanceModePreference(getStored()));+ // Choosing a whole theme replaces any automatic-mode mix. The mix is+ // captured first so a failed preference write can put it back instead+ // of erasing it or leaving it attached to the new theme.+ const previousHalvesRaw = window.localStorage.getItem(THEME_HALVES_STORAGE_KEY);+ window.localStorage.removeItem(THEME_HALVES_STORAGE_KEY);try {- writeAppearanceModePreference(nextAppearanceMode);+ writeThemePreference(next);} catch (cause) {- const error = isThemeStorageError(cause)- ? cause- : new ThemeStorageError({- operation: "write",- storageKey: THEME_APPEARANCE_MODE_STORAGE_KEY,- cause,- });- console.error(error.message, {- operation: error.operation,- storageKey: error.storageKey,- ...safeErrorLogAttributes(error),- });- return false;+ if (previousHalvesRaw !== null) {+ try {+ window.localStorage.setItem(THEME_HALVES_STORAGE_KEY, previousHalvesRaw);+ } catch {}+ }+ throw cause;}- themeStorageReadFailure = null;- applyTheme(getStored(), { suppressTransitions: true });- emitChange();- return true;- }, []);-- const setFollowSystem = useCallback(- (nextFollowSystem: boolean): boolean => {- const currentMode = readAppearanceModePreference(theme);- const nextMode = nextFollowSystem- ? "system"- : currentMode === "system"- ? (getThemePreferenceMode(theme) ?? "light")- : currentMode;- return setAppearanceMode(nextMode);- },- [setAppearanceMode, theme],- );-- const setThemeHalf = useCallback(- (appearance: ThemeAppearance, themeId: string | null): boolean => {- if (typeof window === "undefined") return false;- try {- const current = readStoredThemeHalvesRaw();- const next: { light?: string; dark?: string } = { ...current };- if (themeId === null) delete next[appearance];- else next[appearance] = themeId;- if (next.light === undefined && next.dark === undefined) {- window.localStorage.removeItem(THEME_HALVES_STORAGE_KEY);- } else {- window.localStorage.setItem(THEME_HALVES_STORAGE_KEY, JSON.stringify(next));- }- } catch (cause) {- const error = new ThemeStorageError({+ } catch (cause) {+ const error = isThemeStorageError(cause)+ ? cause+ : new ThemeStorageError({operation: "write",- storageKey: THEME_HALVES_STORAGE_KEY,+ storageKey: STORAGE_KEY,+ theme: next,cause,});- console.error(error.message, {- operation: error.operation,- storageKey: error.storageKey,- ...safeErrorLogAttributes(error),+ console.error(error.message, {+ operation: error.operation,+ storageKey: error.storageKey,+ theme: next,+ ...safeErrorLogAttributes(error),+ });+ return false;+ }+ applyTheme(next, { suppressTransitions: true });+ emitChange();+ return true;+}++function setAppearanceMode(nextAppearanceMode: ThemePreferenceMode): boolean {+ if (typeof window === "undefined") return false;+ try {+ writeAppearanceModePreference(nextAppearanceMode);+ } catch (cause) {+ const error = isThemeStorageError(cause)+ ? cause+ : new ThemeStorageError({+ operation: "write",+ storageKey: THEME_APPEARANCE_MODE_STORAGE_KEY,+ cause,});- return false;- }- applyTheme(getStored(), { suppressTransitions: true });- emitChange();- return true;- },- [],- );+ console.error(error.message, {+ operation: error.operation,+ storageKey: error.storageKey,+ ...safeErrorLogAttributes(error),+ });+ return false;+ }+ themeStorageReadFailure = null;+ applyTheme(getStored(), { suppressTransitions: true });+ emitChange();+ return true;+}- const clearThemeHalves = useCallback((): boolean => {- if (typeof window === "undefined") return false;- try {+function setThemeHalf(appearance: ThemeAppearance, themeId: string | null): boolean {+ if (typeof window === "undefined") return false;+ try {+ const current = readStoredThemeHalvesRaw();+ const next: { light?: string; dark?: string } = { ...current };+ if (themeId === null) delete next[appearance];+ else next[appearance] = themeId;+ if (next.light === undefined && next.dark === undefined) {window.localStorage.removeItem(THEME_HALVES_STORAGE_KEY);- } catch (cause) {- const error = new ThemeStorageError({- operation: "write",- storageKey: THEME_HALVES_STORAGE_KEY,- cause,- });- console.error(error.message, {- operation: error.operation,- storageKey: error.storageKey,- ...safeErrorLogAttributes(error),- });- return false;+ } else {+ window.localStorage.setItem(THEME_HALVES_STORAGE_KEY, JSON.stringify(next));}- applyTheme(getStored(), { suppressTransitions: true });- emitChange();- return true;- }, []);+ } catch (cause) {+ const error = new ThemeStorageError({+ operation: "write",+ storageKey: THEME_HALVES_STORAGE_KEY,+ cause,+ });+ console.error(error.message, {+ operation: error.operation,+ storageKey: error.storageKey,+ ...safeErrorLogAttributes(error),+ });+ return false;+ }+ applyTheme(getStored(), { suppressTransitions: true });+ emitChange();+ return true;+}- const refreshTheme = useCallback(({ preservePreview = false } = {}) => {- if (typeof window === "undefined") return;- lastAppliedTheme = null;- applyTheme(getStored(), { suppressTransitions: true, preservePreview });- emitChange();- }, []);+function clearThemeHalves(): boolean {+ if (typeof window === "undefined") return false;+ try {+ window.localStorage.removeItem(THEME_HALVES_STORAGE_KEY);+ } catch (cause) {+ const error = new ThemeStorageError({+ operation: "write",+ storageKey: THEME_HALVES_STORAGE_KEY,+ cause,+ });+ console.error(error.message, {+ operation: error.operation,+ storageKey: error.storageKey,+ ...safeErrorLogAttributes(error),+ });+ return false;+ }+ applyTheme(getStored(), { suppressTransitions: true });+ emitChange();+ return true;+}++function refreshTheme({ preservePreview = false } = {}): void {+ if (typeof window === "undefined") return;+ lastAppliedTheme = null;+ applyTheme(getStored(), { suppressTransitions: true, preservePreview });+ emitChange();+}++export function useTheme() {+ const snapshot = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);+ const { theme, resolvedTheme } = snapshot;++ const setFollowSystem = (nextFollowSystem: boolean): boolean => {+ const currentMode = readAppearanceModePreference(theme);+ const nextMode = nextFollowSystem+ ? "system"+ : currentMode === "system"+ ? (getThemePreferenceMode(theme) ?? "light")+ : currentMode;+ return setAppearanceMode(nextMode);+ };+ // applyTheme reads the appearance mode and halves from storage, so it keys on the+ // whole snapshot; getSnapshot keeps one object until one of those changes.useEffect(() => {- applyTheme(theme);- }, [snapshot.appearanceMode, theme]);+ applyTheme(snapshot.theme);+ }, [snapshot]);return {theme,