- bookmark management with dnd-kit reordering, bulk edit, search, category filter/rename, and meta auto-fetch - migrate /bookmarks → /dashboard/bookmarks under (main) layout - homepage redesign with category grid, /-key search, dock tooltips - theme toggle + use-theme, sonner toasts, alert-dialog/skeleton, visual refresh of auth pages Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
type Theme = "light" | "dark";
|
|
|
|
function readTheme(): Theme {
|
|
if (typeof window === "undefined") return "light";
|
|
const stored = localStorage.getItem("theme");
|
|
if (stored === "light" || stored === "dark") return stored;
|
|
return window.matchMedia("(prefers-color-scheme: dark)").matches
|
|
? "dark"
|
|
: "light";
|
|
}
|
|
|
|
function applyTheme(theme: Theme) {
|
|
const root = document.documentElement;
|
|
if (theme === "dark") root.classList.add("dark");
|
|
else root.classList.remove("dark");
|
|
}
|
|
|
|
export function useTheme() {
|
|
const [theme, setThemeState] = useState<Theme>("light");
|
|
const [mounted, setMounted] = useState(false);
|
|
|
|
useEffect(() => {
|
|
setThemeState(readTheme());
|
|
setMounted(true);
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (typeof window === "undefined") return;
|
|
const onStorage = (e: StorageEvent) => {
|
|
if (e.key === "theme" && (e.newValue === "light" || e.newValue === "dark")) {
|
|
setThemeState(e.newValue);
|
|
applyTheme(e.newValue);
|
|
}
|
|
};
|
|
window.addEventListener("storage", onStorage);
|
|
return () => window.removeEventListener("storage", onStorage);
|
|
}, []);
|
|
|
|
function setTheme(next: Theme) {
|
|
setThemeState(next);
|
|
try {
|
|
localStorage.setItem("theme", next);
|
|
} catch {}
|
|
applyTheme(next);
|
|
}
|
|
|
|
function toggle() {
|
|
setTheme(theme === "dark" ? "light" : "dark");
|
|
}
|
|
|
|
return { theme, setTheme, toggle, mounted };
|
|
}
|