- add French-keyed i18n with an English dictionary, plus theme and locale helpers, applied across all pages and components - account page: theme and language selectors and a two-column layout - browser: per-folder media type filters and unread chat badges - admin: audit log filtered by user and action through dropdowns - global API and type updates to match the new backend endpoints
39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import { useEffect, type ReactNode } from "react";
|
|
import type { Theme } from "./api.js";
|
|
|
|
const STORAGE_KEY = "cohaise-theme";
|
|
|
|
const prefersDark = (): boolean =>
|
|
typeof window !== "undefined" &&
|
|
window.matchMedia("(prefers-color-scheme: dark)").matches;
|
|
|
|
const isDark = (theme: Theme): boolean =>
|
|
theme === "dark" || (theme === "system" && prefersDark());
|
|
|
|
/**
|
|
* Applique le theme choisi dans le compte sur la racine du document. Le meme
|
|
* choix est recopie dans `localStorage` pour que `public/theme.js` reproduise
|
|
* le rendu des le premier pixel, avant que React ne demarre.
|
|
*/
|
|
export const ThemeProvider = ({ theme, children }: { theme: Theme; children: ReactNode }) => {
|
|
useEffect(() => {
|
|
const apply = () => {
|
|
document.documentElement.classList.toggle("dark", isDark(theme));
|
|
};
|
|
apply();
|
|
try {
|
|
localStorage.setItem(STORAGE_KEY, theme);
|
|
} catch {
|
|
/* stockage indisponible : le rendu reste correct */
|
|
}
|
|
|
|
if (theme !== "system") return undefined;
|
|
const media = window.matchMedia("(prefers-color-scheme: dark)");
|
|
const onChange = () => apply();
|
|
media.addEventListener("change", onChange);
|
|
return () => media.removeEventListener("change", onChange);
|
|
}, [theme]);
|
|
|
|
return <>{children}</>;
|
|
};
|