feat(web): add persistent theme toggle
This commit is contained in:
1 parent
edfb960e97
commit
326e0c66b6
5 files changed
+89
-3
No files matched your search
+23
-1
@@ -1,7 +1,8 @@
|
||||
import { BrowserRouter, Link, Route, Routes } from "react-router";
|
||||
import { useMsal } from "@azure/msal-react";
|
||||
import { Moon, Sun } from "lucide-react";
|
||||
import DevAuthSwitcher from "./components/DevAuthSwitcher";
|
||||
import { DEFAULT_COMMUNITY_ID } from "./config";
|
||||
import { DEFAULT_COMMUNITY_ID, setTheme, useTheme } from "./config";
|
||||
import { useCommunity } from "./api/hooks";
|
||||
import { isMsalConfigured } from "./lib/auth/msal.config";
|
||||
import AuthCallbackPage from "./pages/AuthCallback";
|
||||
@@ -41,6 +42,26 @@ function MsalHeaderActions() {
|
||||
);
|
||||
}
|
||||
|
||||
function ThemeToggle() {
|
||||
const theme = useTheme();
|
||||
const nextTheme = theme === "light" ? "dark" : "light";
|
||||
const label = nextTheme === "dark" ? "다크 테마로 전환" : "라이트 테마로 전환";
|
||||
const Icon = nextTheme === "dark" ? Moon : Sun;
|
||||
|
||||
return (
|
||||
<span className="tooltip tooltip-bottom" data-tip={label}>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-ghost btn-xs btn-square text-base-content/60 hover:text-base-content"
|
||||
aria-label={label}
|
||||
onClick={() => setTheme(nextTheme)}
|
||||
>
|
||||
<Icon className="h-3.5 w-3.5" aria-hidden="true" />
|
||||
</button>
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
function App() {
|
||||
const communityQuery = useCommunity(DEFAULT_COMMUNITY_ID);
|
||||
const communityLabel = communityQuery.data?.name ?? "LOLYMPUS";
|
||||
@@ -66,6 +87,7 @@ function App() {
|
||||
Set `VITE_DEFAULT_COMMUNITY_ID`
|
||||
</span>
|
||||
)} */}
|
||||
<ThemeToggle />
|
||||
{isMsalConfigured && <MsalHeaderActions />}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -3,8 +3,12 @@ import { useSyncExternalStore } from 'react';
|
||||
const AUTH_USER_ID_STORAGE_KEY = 'lolympus.authUserId';
|
||||
const DEV_CAPTAIN_TEAM_INDEX_STORAGE_KEY = 'lolympus.devCaptainTeamIndex';
|
||||
const LEGACY_DEV_CAPTAIN_SIDE_STORAGE_KEY = 'lolympus.devCaptainSide';
|
||||
const THEME_STORAGE_KEY = 'lolympus.theme';
|
||||
const AUTH_USER_CHANGED_EVENT = 'lolympus:auth-user-changed';
|
||||
const CAPTAIN_SIDE_CHANGED_EVENT = 'lolympus:captain-side-changed';
|
||||
const THEME_CHANGED_EVENT = 'lolympus:theme-changed';
|
||||
|
||||
export type AppTheme = 'light' | 'dark';
|
||||
|
||||
export const API_BASE_URL = (
|
||||
import.meta.env.VITE_API_BASE_URL ?? 'http://localhost:3000'
|
||||
@@ -80,6 +84,40 @@ export function setDevCaptainTeamIndex(teamIndex: number | null) {
|
||||
dispatchWindowEvent(CAPTAIN_SIDE_CHANGED_EVENT);
|
||||
}
|
||||
|
||||
function normalizeTheme(value: string | null | undefined): AppTheme {
|
||||
return value === 'dark' ? 'dark' : 'light';
|
||||
}
|
||||
|
||||
function applyTheme(theme: AppTheme) {
|
||||
if (typeof document === 'undefined') return;
|
||||
document.documentElement.dataset.theme = theme;
|
||||
document.documentElement.style.colorScheme = theme;
|
||||
}
|
||||
|
||||
export function getTheme(): AppTheme {
|
||||
if (typeof window === 'undefined') return 'light';
|
||||
try {
|
||||
return normalizeTheme(window.localStorage.getItem(THEME_STORAGE_KEY));
|
||||
} catch {
|
||||
return 'light';
|
||||
}
|
||||
}
|
||||
|
||||
export function setTheme(theme: AppTheme) {
|
||||
if (typeof window === 'undefined') return;
|
||||
try {
|
||||
window.localStorage.setItem(THEME_STORAGE_KEY, theme);
|
||||
} catch {
|
||||
// Keep applying the in-memory document theme even if persistence is blocked.
|
||||
}
|
||||
applyTheme(theme);
|
||||
dispatchWindowEvent(THEME_CHANGED_EVENT);
|
||||
}
|
||||
|
||||
export function initializeTheme() {
|
||||
applyTheme(getTheme());
|
||||
}
|
||||
|
||||
export function useAuthUserId(): string | null {
|
||||
return useSyncExternalStore<string | null>(
|
||||
(callback) => subscribeToWindowChanges(AUTH_USER_CHANGED_EVENT, callback),
|
||||
@@ -95,3 +133,11 @@ export function useDevCaptainTeamIndex(): number | null {
|
||||
() => null,
|
||||
);
|
||||
}
|
||||
|
||||
export function useTheme(): AppTheme {
|
||||
return useSyncExternalStore<AppTheme>(
|
||||
(callback) => subscribeToWindowChanges(THEME_CHANGED_EVENT, callback),
|
||||
getTheme,
|
||||
() => 'light',
|
||||
);
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
@plugin "daisyui" {
|
||||
themes:
|
||||
light --default,
|
||||
dark --prefersdark;
|
||||
dark;
|
||||
}
|
||||
|
||||
body {
|
||||
|
||||
@@ -4,6 +4,9 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import './index.css';
|
||||
import App from './App.tsx';
|
||||
import { MsalAppProvider } from './lib/auth/MsalAppProvider.tsx';
|
||||
import { initializeTheme } from './config.ts';
|
||||
|
||||
initializeTheme();
|
||||
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
|
||||
Reference in new issue
Block a user