MCPcopy
hub / github.com/fccview/jotty / ThemeProvider

Function ThemeProvider

app/_providers/ThemeProvider.tsx:10–147  ·  view source on GitHub ↗
({
  children,
  user,
}: {
  children: React.ReactNode;
  user: Partial<User>;
})

Source from the content-addressed store, hash-verified

8import { useTranslations } from "next-intl";
9
10export const ThemeProvider = ({
11 children,
12 user,
13}: {
14 children: React.ReactNode;
15 user: Partial<User>;
16}) => {
17 const { isRwMarkable } = useAppMode();
18 const t = useTranslations();
19 const { theme: localStorageTheme, getResolvedTheme } = useSettings();
20 const [resolvedTheme, setResolvedTheme] = useState<string>(() =>
21 getResolvedTheme(isRwMarkable, user.preferredTheme)
22 );
23
24 const themeIDs = BUILT_IN_THEMES.map((theme) => theme.id);
25
26 const [customThemeColors, setCustomThemeColors] = useState<{
27 [key: string]: any;
28 }>({});
29
30 const [customCSS, setCustomCSS] = useState<string>("");
31
32 const loadCustomCSS = async () => {
33 try {
34 const timestamp = Date.now();
35 const response = await fetch(`/api/custom-css?t=${timestamp}`);
36 if (response.ok) {
37 const css = await response.text();
38 setCustomCSS(css);
39 }
40 } catch (error) {
41 console.error("Failed to load custom CSS:", error);
42 }
43 };
44
45 useEffect(() => {
46 const loadCustomColors = async () => {
47 try {
48 const colors = await getCustomThemeColors();
49 setCustomThemeColors(colors);
50 } catch (error) {
51 console.error("Failed to load custom theme colors:", error);
52 }
53 };
54
55 loadCustomColors();
56 loadCustomCSS();
57 }, []);
58
59 useEffect(() => {
60 const handleCssUpdate = () => {
61 loadCustomCSS();
62 };
63
64 window.addEventListener("css-updated", handleCssUpdate);
65
66 return () => {
67 window.removeEventListener("css-updated", handleCssUpdate);

Callers

nothing calls this directly

Calls 4

useAppModeFunction · 0.90
loadCustomColorsFunction · 0.85
updateResolvedThemeFunction · 0.85
loadCustomCSSFunction · 0.70

Tested by

no test coverage detected