Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(theme): apply dark mode synchronously on launch

Akarshanebe77f9ff7c3
diff --git a/web-app/index.html b/web-app/index.html
index d424456ff..a4fefe495 100644
--- a/web-app/index.html
+++ b/web-app/index.html
@@ -22,6 +22,29 @@
/>
<title>Jan</title>
<link rel="stylesheet" href="/src/loader.css" />
+ <script>
+ // Apply dark class synchronously before first paint to avoid a
+ // light-mode flash when the user's effective theme is dark. Truth is
+ // re-resolved by ThemeProvider once React mounts (and, on Linux, after
+ // the XDG portal read completes).
+ (function () {
+ try {
+ var raw = localStorage.getItem('theme')
+ var active = 'auto'
+ if (raw) {
+ var parsed = JSON.parse(raw)
+ var s = parsed && parsed.state
+ if (s && typeof s.activeTheme === 'string') active = s.activeTheme
+ }
+ var dark =
+ active === 'dark' ||
+ (active === 'auto' &&
+ window.matchMedia &&
+ window.matchMedia('(prefers-color-scheme: dark)').matches)
+ if (dark) document.documentElement.classList.add('dark')
+ } catch (_) {}
+ })()
+ </script>
</head>
<body>
<div id="initial-loader">
diff --git a/web-app/src/hooks/useTheme.ts b/web-app/src/hooks/useTheme.ts
index 76d0fbfa5..0af1658fa 100644
--- a/web-app/src/hooks/useTheme.ts
+++ b/web-app/src/hooks/useTheme.ts
@@ -56,6 +56,15 @@ export const useTheme = create<ThemeState>()(
{
name: localStorageKey.theme,
storage: createJSONStorage(() => localStorage),
+ // Persisted isDark from the previous session can be stale (user changed
+ // OS theme while the app was closed). Re-derive on hydration so the
+ // initial render matches reality; the Tauri portal listener corrects
+ // Linux where matchMedia is unreliable.
+ onRehydrateStorage: () => (state) => {
+ if (!state) return
+ if (state.activeTheme === 'auto') state.isDark = checkOSDarkMode()
+ else state.isDark = state.activeTheme === 'dark'
+ },
}
)
)