From 326e0c66b673eab34a642bedc7313fce1bc331a5 Mon Sep 17 00:00:00 2001 From: dahyeon Date: Mon, 8 Jun 2026 01:46:59 -0700 Subject: [PATCH] feat(web): add persistent theme toggle --- apps/web/index.html | 17 +++++++++++++++- apps/web/src/App.tsx | 24 +++++++++++++++++++++- apps/web/src/config.ts | 46 ++++++++++++++++++++++++++++++++++++++++++ apps/web/src/index.css | 2 +- apps/web/src/main.tsx | 3 +++ 5 files changed, 89 insertions(+), 3 deletions(-) diff --git a/apps/web/index.html b/apps/web/index.html index ae9502bc..77cbd64a 100644 --- a/apps/web/index.html +++ b/apps/web/index.html @@ -1,5 +1,5 @@ - + @@ -7,6 +7,21 @@ 롤림푸스 - 내전 관리 +
diff --git a/apps/web/src/App.tsx b/apps/web/src/App.tsx index 93ff7eab..107b7c87 100644 --- a/apps/web/src/App.tsx +++ b/apps/web/src/App.tsx @@ -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 ( + + + + ); +} + 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` )} */} + {isMsalConfigured && } diff --git a/apps/web/src/config.ts b/apps/web/src/config.ts index 2e0381ca..bad4ac61 100644 --- a/apps/web/src/config.ts +++ b/apps/web/src/config.ts @@ -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( (callback) => subscribeToWindowChanges(AUTH_USER_CHANGED_EVENT, callback), @@ -95,3 +133,11 @@ export function useDevCaptainTeamIndex(): number | null { () => null, ); } + +export function useTheme(): AppTheme { + return useSyncExternalStore( + (callback) => subscribeToWindowChanges(THEME_CHANGED_EVENT, callback), + getTheme, + () => 'light', + ); +} diff --git a/apps/web/src/index.css b/apps/web/src/index.css index 45ade146..e931b224 100644 --- a/apps/web/src/index.css +++ b/apps/web/src/index.css @@ -4,7 +4,7 @@ @plugin "daisyui" { themes: light --default, - dark --prefersdark; + dark; } body { diff --git a/apps/web/src/main.tsx b/apps/web/src/main.tsx index 5487c4df..af11ac47 100644 --- a/apps/web/src/main.tsx +++ b/apps/web/src/main.tsx @@ -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: {