2024-03-21 04:46:45 +03:00
|
|
|
import { createContext, useContext, useEffect, useMemo, useState } from "react";
|
2023-12-08 16:33:22 +03:00
|
|
|
|
|
|
|
|
type Theme = "dark" | "light" | "system";
|
|
|
|
|
type ColorScheme =
|
|
|
|
|
| "theme-blue"
|
|
|
|
|
| "theme-green"
|
|
|
|
|
| "theme-nord"
|
|
|
|
|
| "theme-red"
|
2024-07-10 15:04:02 +03:00
|
|
|
| "theme-high-contrast"
|
2023-12-08 16:33:22 +03:00
|
|
|
| "theme-default";
|
|
|
|
|
|
2024-05-18 19:36:13 +03:00
|
|
|
// eslint-disable-next-line react-refresh/only-export-components
|
2023-12-08 16:33:22 +03:00
|
|
|
export const colorSchemes: ColorScheme[] = [
|
|
|
|
|
"theme-blue",
|
|
|
|
|
"theme-green",
|
|
|
|
|
"theme-nord",
|
|
|
|
|
"theme-red",
|
2024-07-10 15:04:02 +03:00
|
|
|
"theme-high-contrast",
|
2023-12-08 16:33:22 +03:00
|
|
|
"theme-default",
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Helper function to generate friendly color scheme names
|
2024-05-18 19:36:13 +03:00
|
|
|
// eslint-disable-next-line react-refresh/only-export-components
|
2025-12-20 15:08:24 +03:00
|
|
|
export const friendlyColorSchemeName = (
|
|
|
|
|
className: string,
|
2025-12-21 11:04:03 +03:00
|
|
|
t?: (key: string, options?: any) => string,
|
2025-12-20 15:08:24 +03:00
|
|
|
): string => {
|
|
|
|
|
const words = className.split("-").slice(1);
|
|
|
|
|
const key = "menu.theme." + words.join("");
|
|
|
|
|
|
|
|
|
|
if (!t) {
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fallback = words
|
|
|
|
|
.join(" ")
|
|
|
|
|
.replace(/\b\w/g, (char) => char.toUpperCase());
|
|
|
|
|
|
|
|
|
|
return t(key, { defaultValue: fallback });
|
2023-12-08 16:33:22 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type ThemeProviderProps = {
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
defaultTheme?: Theme;
|
|
|
|
|
defaultColorScheme?: ColorScheme;
|
|
|
|
|
storageKey?: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type ThemeProviderState = {
|
|
|
|
|
theme: Theme;
|
2024-03-21 04:46:45 +03:00
|
|
|
systemTheme?: Theme;
|
2023-12-08 16:33:22 +03:00
|
|
|
colorScheme: ColorScheme;
|
|
|
|
|
setTheme: (theme: Theme) => void;
|
|
|
|
|
setColorScheme: (colorScheme: ColorScheme) => void;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const initialState: ThemeProviderState = {
|
|
|
|
|
theme: "system",
|
2024-03-21 04:46:45 +03:00
|
|
|
systemTheme: undefined,
|
2023-12-08 16:33:22 +03:00
|
|
|
colorScheme: "theme-default",
|
|
|
|
|
setTheme: () => null,
|
|
|
|
|
setColorScheme: () => null,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const ThemeProviderContext = createContext<ThemeProviderState>(initialState);
|
|
|
|
|
|
|
|
|
|
export function ThemeProvider({
|
|
|
|
|
children,
|
|
|
|
|
defaultTheme = "system",
|
|
|
|
|
defaultColorScheme = "theme-default",
|
|
|
|
|
storageKey = "frigate-ui-theme",
|
|
|
|
|
...props
|
|
|
|
|
}: ThemeProviderProps) {
|
|
|
|
|
const [theme, setTheme] = useState<Theme>(() => {
|
|
|
|
|
try {
|
|
|
|
|
const storedData = JSON.parse(localStorage.getItem(storageKey) || "{}");
|
|
|
|
|
return storedData.theme || defaultTheme;
|
|
|
|
|
} catch (error) {
|
2024-02-29 01:23:56 +03:00
|
|
|
// eslint-disable-next-line no-console
|
2023-12-08 16:33:22 +03:00
|
|
|
console.error("Error parsing theme data from storage:", error);
|
|
|
|
|
return defaultTheme;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const [colorScheme, setColorScheme] = useState<ColorScheme>(() => {
|
|
|
|
|
try {
|
|
|
|
|
const storedData = JSON.parse(localStorage.getItem(storageKey) || "{}");
|
|
|
|
|
return storedData.colorScheme === "default"
|
|
|
|
|
? defaultColorScheme
|
|
|
|
|
: storedData.colorScheme || defaultColorScheme;
|
|
|
|
|
} catch (error) {
|
2024-02-29 01:23:56 +03:00
|
|
|
// eslint-disable-next-line no-console
|
2023-12-08 16:33:22 +03:00
|
|
|
console.error("Error parsing color scheme data from storage:", error);
|
|
|
|
|
return defaultColorScheme;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2024-03-21 04:46:45 +03:00
|
|
|
const systemTheme = useMemo<Theme | undefined>(() => {
|
|
|
|
|
if (theme != "system") {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return window.matchMedia("(prefers-color-scheme: dark)").matches
|
|
|
|
|
? "dark"
|
|
|
|
|
: "light";
|
|
|
|
|
}, [theme]);
|
|
|
|
|
|
2023-12-08 16:33:22 +03:00
|
|
|
useEffect(() => {
|
|
|
|
|
//localStorage.removeItem(storageKey);
|
|
|
|
|
//console.log(localStorage.getItem(storageKey));
|
|
|
|
|
const root = window.document.documentElement;
|
|
|
|
|
|
2025-12-20 15:08:24 +03:00
|
|
|
if (!(window as any).__frigateThemesLoaded) {
|
|
|
|
|
(window as any).__frigateThemesLoaded = true;
|
|
|
|
|
|
|
|
|
|
fetch("/api/config/themes")
|
|
|
|
|
.then((res) => (res.ok ? res.json() : []))
|
|
|
|
|
.then((files: string[]) => {
|
|
|
|
|
files.forEach((file) => {
|
|
|
|
|
if (!file.endsWith(".css")) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const baseName = file.replace(/\.css$/, "");
|
|
|
|
|
const className = baseName.startsWith("theme-")
|
|
|
|
|
? baseName
|
|
|
|
|
: `theme-${baseName}`;
|
|
|
|
|
|
|
|
|
|
if (!colorSchemes.includes(className as ColorScheme)) {
|
|
|
|
|
// runtime extension is intentional
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
colorSchemes.push(className);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-21 11:04:03 +03:00
|
|
|
if (!document.querySelector(`link[data-theme="${className}"]`)) {
|
2025-12-20 15:08:24 +03:00
|
|
|
const link = document.createElement("link");
|
|
|
|
|
link.rel = "stylesheet";
|
|
|
|
|
link.href = `/config/themes/${file}`;
|
|
|
|
|
link.dataset.theme = className;
|
|
|
|
|
document.head.appendChild(link);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.catch(() => {
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-08 16:33:22 +03:00
|
|
|
root.classList.remove("light", "dark", "system", ...colorSchemes);
|
|
|
|
|
root.classList.add(theme, colorScheme);
|
|
|
|
|
|
2024-03-21 04:46:45 +03:00
|
|
|
if (systemTheme) {
|
2023-12-08 16:33:22 +03:00
|
|
|
root.classList.add(systemTheme);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
root.classList.add(theme);
|
2024-03-21 04:46:45 +03:00
|
|
|
}, [theme, colorScheme, systemTheme]);
|
2023-12-08 16:33:22 +03:00
|
|
|
|
|
|
|
|
const value = {
|
|
|
|
|
theme,
|
2024-03-21 04:46:45 +03:00
|
|
|
systemTheme,
|
2023-12-08 16:33:22 +03:00
|
|
|
colorScheme,
|
|
|
|
|
setTheme: (theme: Theme) => {
|
|
|
|
|
localStorage.setItem(storageKey, JSON.stringify({ theme, colorScheme }));
|
|
|
|
|
setTheme(theme);
|
|
|
|
|
},
|
|
|
|
|
setColorScheme: (colorScheme: ColorScheme) => {
|
|
|
|
|
localStorage.setItem(storageKey, JSON.stringify({ theme, colorScheme }));
|
|
|
|
|
setColorScheme(colorScheme);
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<ThemeProviderContext.Provider {...props} value={value}>
|
|
|
|
|
{children}
|
|
|
|
|
</ThemeProviderContext.Provider>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-18 19:36:13 +03:00
|
|
|
// eslint-disable-next-line react-refresh/only-export-components
|
2023-12-08 16:33:22 +03:00
|
|
|
export const useTheme = () => {
|
|
|
|
|
const context = useContext(ThemeProviderContext);
|
|
|
|
|
|
|
|
|
|
if (context === undefined)
|
|
|
|
|
throw new Error("useTheme must be used within a ThemeProvider");
|
|
|
|
|
|
|
|
|
|
return context;
|
|
|
|
|
};
|