feat(web): add persistent theme toggle

This commit is contained in:
dahyeon committed 2026-06-08 01:46:59 -07:00
1 parent edfb960e97
commit 326e0c66b6
5 files changed
+89 -3

No files matched your search

+16 -1
View File
@@ -1,5 +1,5 @@
<!doctype html> <!doctype html>
<html lang="en"> <html lang="ko" data-theme="light">
<head> <head>
<meta charset="UTF-8" /> <meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" /> <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
@@ -7,6 +7,21 @@
<title>롤림푸스 - 내전 관리</title> <title>롤림푸스 - 내전 관리</title>
</head> </head>
<body> <body>
<script>
(() => {
let theme = "light";
try {
theme =
window.localStorage.getItem("lolympus.theme") === "dark"
? "dark"
: "light";
} catch {
theme = "light";
}
document.documentElement.dataset.theme = theme;
document.documentElement.style.colorScheme = theme;
})();
</script>
<div id="root"></div> <div id="root"></div>
<script type="module" src="/src/main.tsx"></script> <script type="module" src="/src/main.tsx"></script>
</body> </body>
+23 -1
View File
@@ -1,7 +1,8 @@
import { BrowserRouter, Link, Route, Routes } from "react-router"; import { BrowserRouter, Link, Route, Routes } from "react-router";
import { useMsal } from "@azure/msal-react"; import { useMsal } from "@azure/msal-react";
import { Moon, Sun } from "lucide-react";
import DevAuthSwitcher from "./components/DevAuthSwitcher"; 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 { useCommunity } from "./api/hooks";
import { isMsalConfigured } from "./lib/auth/msal.config"; import { isMsalConfigured } from "./lib/auth/msal.config";
import AuthCallbackPage from "./pages/AuthCallback"; 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() { function App() {
const communityQuery = useCommunity(DEFAULT_COMMUNITY_ID); const communityQuery = useCommunity(DEFAULT_COMMUNITY_ID);
const communityLabel = communityQuery.data?.name ?? "LOLYMPUS"; const communityLabel = communityQuery.data?.name ?? "LOLYMPUS";
@@ -66,6 +87,7 @@ function App() {
Set `VITE_DEFAULT_COMMUNITY_ID` Set `VITE_DEFAULT_COMMUNITY_ID`
</span> </span>
)} */} )} */}
<ThemeToggle />
{isMsalConfigured && <MsalHeaderActions />} {isMsalConfigured && <MsalHeaderActions />}
</div> </div>
</div> </div>
+46
View File
@@ -3,8 +3,12 @@ import { useSyncExternalStore } from 'react';
const AUTH_USER_ID_STORAGE_KEY = 'lolympus.authUserId'; const AUTH_USER_ID_STORAGE_KEY = 'lolympus.authUserId';
const DEV_CAPTAIN_TEAM_INDEX_STORAGE_KEY = 'lolympus.devCaptainTeamIndex'; const DEV_CAPTAIN_TEAM_INDEX_STORAGE_KEY = 'lolympus.devCaptainTeamIndex';
const LEGACY_DEV_CAPTAIN_SIDE_STORAGE_KEY = 'lolympus.devCaptainSide'; 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 AUTH_USER_CHANGED_EVENT = 'lolympus:auth-user-changed';
const CAPTAIN_SIDE_CHANGED_EVENT = 'lolympus:captain-side-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 = ( export const API_BASE_URL = (
import.meta.env.VITE_API_BASE_URL ?? 'http://localhost:3000' 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); 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 { export function useAuthUserId(): string | null {
return useSyncExternalStore<string | null>( return useSyncExternalStore<string | null>(
(callback) => subscribeToWindowChanges(AUTH_USER_CHANGED_EVENT, callback), (callback) => subscribeToWindowChanges(AUTH_USER_CHANGED_EVENT, callback),
@@ -95,3 +133,11 @@ export function useDevCaptainTeamIndex(): number | null {
() => null, () => null,
); );
} }
export function useTheme(): AppTheme {
return useSyncExternalStore<AppTheme>(
(callback) => subscribeToWindowChanges(THEME_CHANGED_EVENT, callback),
getTheme,
() => 'light',
);
}
+1 -1
View File
@@ -4,7 +4,7 @@
@plugin "daisyui" { @plugin "daisyui" {
themes: themes:
light --default, light --default,
dark --prefersdark; dark;
} }
body { body {
+3
View File
@@ -4,6 +4,9 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import './index.css'; import './index.css';
import App from './App.tsx'; import App from './App.tsx';
import { MsalAppProvider } from './lib/auth/MsalAppProvider.tsx'; import { MsalAppProvider } from './lib/auth/MsalAppProvider.tsx';
import { initializeTheme } from './config.ts';
initializeTheme();
const queryClient = new QueryClient({ const queryClient = new QueryClient({
defaultOptions: { defaultOptions: {