- footer: dark #050e11 band with an Instrument Serif display CTA, an inverted Download button, link columns (tracked download hrefs preserved) and copyright - structure: move icon-mark to brand/, theme provider+toggle to theme/, site-constants to lib/; sections/layout/ui/brand/theme now fully separated - button: add an inverted variant for dark surfaces - metadata: point OpenGraph/Twitter at a 1200x630 og.jpg and the JSON-LD screenshot at dashboard.webp; drop the old /screenshots references - remove orphans: framer-motion (replaced by CSS + IntersectionObserver), ascii-background, and the seven superseded product screenshots - add @playwright/test for the e2e suite
77 lines
2.0 KiB
TypeScript
77 lines
2.0 KiB
TypeScript
"use client";
|
|
|
|
import { createContext, useCallback, useContext, useEffect, useState } from "react";
|
|
|
|
const storageKey = "agentcompany-theme";
|
|
|
|
type ThemeMode = "light" | "dark";
|
|
|
|
type ThemeContextValue = {
|
|
theme: ThemeMode;
|
|
setTheme: (theme: ThemeMode) => void;
|
|
toggleTheme: () => void;
|
|
};
|
|
|
|
const ThemeContext = createContext<ThemeContextValue | undefined>(undefined);
|
|
|
|
function resolveTheme(): ThemeMode {
|
|
if (typeof window === "undefined") {
|
|
return "light";
|
|
}
|
|
|
|
const stored = localStorage.getItem(storageKey);
|
|
if (stored === "light" || stored === "dark") {
|
|
return stored;
|
|
}
|
|
|
|
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
}
|
|
|
|
export function ThemeProvider({ children }: { children: React.ReactNode }) {
|
|
const [theme, setThemeState] = useState<ThemeMode>(resolveTheme);
|
|
|
|
useEffect(() => {
|
|
const root = document.documentElement;
|
|
if (theme === "dark") {
|
|
root.classList.add("dark");
|
|
} else {
|
|
root.classList.remove("dark");
|
|
}
|
|
localStorage.setItem(storageKey, theme);
|
|
}, [theme]);
|
|
|
|
useEffect(() => {
|
|
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
|
const handleChange = (event: MediaQueryListEvent) => {
|
|
if (!localStorage.getItem(storageKey)) {
|
|
setThemeState(event.matches ? "dark" : "light");
|
|
}
|
|
};
|
|
|
|
mediaQuery.addEventListener("change", handleChange);
|
|
return () => mediaQuery.removeEventListener("change", handleChange);
|
|
}, []);
|
|
|
|
const setTheme = useCallback((nextTheme: ThemeMode) => {
|
|
setThemeState(nextTheme);
|
|
}, []);
|
|
|
|
const toggleTheme = useCallback(() => {
|
|
setThemeState((currentTheme) => (currentTheme === "light" ? "dark" : "light"));
|
|
}, []);
|
|
|
|
return (
|
|
<ThemeContext.Provider value={{ theme, setTheme, toggleTheme }}>
|
|
{children}
|
|
</ThemeContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useTheme() {
|
|
const context = useContext(ThemeContext);
|
|
if (!context) {
|
|
throw new Error("useTheme must be used within a ThemeProvider");
|
|
}
|
|
return context;
|
|
}
|