11import { useAtomValue } from "@effect/atom-react" ;
2- import { useEffect } from "react" ;
2+ import { useEffect , useSyncExternalStore } from "react" ;
33
44import { primaryEnvironmentIdAtom } from "../state/primaryEnvironment" ;
55import { primaryServerSettingsAtom } from "../state/server" ;
6- import { getThemeDefinition , singleAppearanceOf } from "../themePalette" ;
7- import { useEnvironmentThemeDefinitions } from "./useEnvironmentTheme" ;
6+ import { getThemeDefinition , singleAppearanceOf , subscribeToCustomThemes } from "../themePalette" ;
87import { useTheme } from "./useTheme" ;
98
109const APPLIED_DEFAULT_THEME_STORAGE_PREFIX = "t3code:default-theme-applied:v2:" ;
@@ -54,12 +53,16 @@ export function useDefaultThemeAdoption(): void {
5453 const settings = useAtomValue ( primaryServerSettingsAtom ) ;
5554 const { defaultTheme, defaultThemeSetAt } = settings ;
5655 const { setTheme, setAppearanceMode } = useTheme ( ) ;
57- const environmentThemes = useEnvironmentThemeDefinitions ( ) ;
56+ // Re-read when published or custom palettes change, so a theme that resolves late still applies.
57+ const definition = useSyncExternalStore (
58+ subscribeToCustomThemes ,
59+ ( ) => getThemeDefinition ( defaultTheme ) ,
60+ ( ) => null ,
61+ ) ;
5862
5963 useEffect ( ( ) => {
6064 if ( typeof window === "undefined" || environmentId === null ) return ;
6165 const storageKey = `${ APPLIED_DEFAULT_THEME_STORAGE_PREFIX } ${ environmentId } ` ;
62- const definition = getThemeDefinition ( defaultTheme ) ;
6366 const generation = defaultThemeToApply ( {
6467 environmentId,
6568 defaultTheme,
@@ -81,12 +84,5 @@ export function useDefaultThemeAdoption(): void {
8184 // light half with no retry.
8285 if ( half !== null && ! setAppearanceMode ( half ) ) return ;
8386 writeAppliedGeneration ( storageKey , generation ) ;
84- } , [
85- environmentId ,
86- defaultTheme ,
87- defaultThemeSetAt ,
88- environmentThemes ,
89- setTheme ,
90- setAppearanceMode ,
91- ] ) ;
87+ } , [ environmentId , defaultTheme , defaultThemeSetAt , definition , setTheme , setAppearanceMode ] ) ;
9288}
0 commit comments