"use client"; // components/Sidebar.jsx — the Open Workspace shell: brand, primary nav, a live // library folder tree grouped by subject, and a pinned theme toggle + New button. import Link from "next/link"; import { usePathname } from "next/navigation"; import { useCallback, useEffect, useMemo, useState } from "react"; import { IconPencil, IconPlus, IconSettings, IconMoon, IconSun, IconChevronDown, IconChevronRight, IconFileText, IconPencilPlus, } from "@tabler/icons-react"; import { groupBySubject } from "@/lib/group"; const NAV = [ { href: "/", label: "Create", icon: IconPlus }, { href: "/settings", label: "Settings", icon: IconSettings }, ]; export default function Sidebar() { const pathname = usePathname(); const [theme, setTheme] = useState(null); const [items, setItems] = useState([]); const [collapsed, setCollapsed] = useState({}); // subject -> true when collapsed useEffect(() => { setTheme(document.documentElement.dataset.theme === "dark" ? "dark" : "light"); }, []); const load = useCallback(() => { fetch("/api/assignments") .then((r) => r.json()) .then((d) => setItems(d.assignments || [])) .catch(() => {}); }, []); // Reload the tree whenever the route changes (covers create / delete / rename). useEffect(() => { load(); }, [load, pathname]); function toggleTheme() { const next = document.documentElement.dataset.theme === "dark" ? "light" : "dark"; if (next === "dark") document.documentElement.dataset.theme = "dark"; else delete document.documentElement.dataset.theme; try { localStorage.setItem("theme", next); } catch {} setTheme(next); } const groups = useMemo(() => groupBySubject(items), [items]); const activeId = pathname.startsWith("/editor/") ? decodeURIComponent(pathname.split("/editor/")[1] || "") : ""; const isNavActive = (href) => (href === "/" ? pathname === "/" : pathname.startsWith(href)); return ( ); }