This commit is contained in:
DrSmoothl committed 2026-10-12 15:43:34 +08:00
1 parent effeb66554
commit 7c0519f75f
977 files changed
+198426 -158324

No files matched your search

+63 -38
View File
@@ -8,57 +8,82 @@ import { createContext, useCallback, useContext, useEffect, useState, type React
const KIOSK_KEY = 'snowluma_kiosk';
function readInitial(): boolean {
try {
const params = new URLSearchParams(window.location.search);
if (params.get('kiosk') === '1') {
sessionStorage.setItem(KIOSK_KEY, '1');
// Strip the param so a later reload (after exiting) doesn't re-enter.
const url = new URL(window.location.href);
url.searchParams.delete('kiosk');
window.history.replaceState({}, '', url.pathname + url.search + url.hash);
return true;
try {
const params = new URLSearchParams(window.location.search);
if (params.get('kiosk') === '1') {
sessionStorage.setItem(KIOSK_KEY, '1');
// Strip the param so a later reload (after exiting) doesn't re-enter.
const url = new URL(window.location.href);
url.searchParams.delete('kiosk');
window.history.replaceState({}, '', url.pathname + url.search + url.hash);
return true;
}
return sessionStorage.getItem(KIOSK_KEY) === '1';
} catch {
return false;
}
return sessionStorage.getItem(KIOSK_KEY) === '1';
} catch {
return false;
}
}
interface KioskValue {
kiosk: boolean;
enter: () => void;
exit: () => void;
kiosk: boolean;
enter: () => void;
exit: () => void;
}
const KioskContext = createContext<KioskValue | null>(null);
export function KioskProvider({ children }: { children: ReactNode }) {
const [kiosk, setKiosk] = useState(readInitial);
const [kiosk, setKiosk] = useState(readInitial);
const enter = useCallback(() => {
try { sessionStorage.setItem(KIOSK_KEY, '1'); } catch { /* private mode */ }
setKiosk(true);
}, []);
const exit = useCallback(() => {
try { sessionStorage.removeItem(KIOSK_KEY); } catch { /* private mode */ }
setKiosk(false);
}, []);
const enter = useCallback(() => {
try {
sessionStorage.setItem(KIOSK_KEY, '1');
} catch {
/* private mode */
}
// Reflect onto <html data-kiosk> so CSS can hide chrome (e.g. the overview
// edit entry), and bind Esc-to-exit only while active.
useEffect(() => {
document.documentElement.setAttribute('data-kiosk', kiosk ? '1' : '0');
if (!kiosk) return;
const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') exit(); };
window.addEventListener('keydown', onKey);
return () => window.removeEventListener('keydown', onKey);
}, [kiosk, exit]);
setKiosk(true);
}, []);
const exit = useCallback(() => {
try {
sessionStorage.removeItem(KIOSK_KEY);
} catch {
/* private mode */
}
return <KioskContext.Provider value={{ kiosk, enter, exit }}>{children}</KioskContext.Provider>;
setKiosk(false);
}, []);
// Reflect onto <html data-kiosk> so CSS can hide chrome (e.g. the overview
// edit entry), and bind Esc-to-exit only while active.
useEffect(() => {
document.documentElement.setAttribute('data-kiosk', kiosk ? '1' : '0');
if (!kiosk) {
return;
}
const onKey = (e: KeyboardEvent) => {
if (e.key === 'Escape') {
exit();
}
};
window.addEventListener('keydown', onKey);
return () => window.removeEventListener('keydown', onKey);
}, [kiosk, exit]);
return <KioskContext.Provider value={{ kiosk, enter, exit }}>{children}</KioskContext.Provider>;
}
export function useKiosk(): KioskValue {
const ctx = useContext(KioskContext);
if (!ctx) throw new Error('useKiosk must be used within KioskProvider');
return ctx;
const ctx = useContext(KioskContext);
if (!ctx) {
throw new Error('useKiosk must be used within KioskProvider');
}
return ctx;
}