import React, { useEffect, useMemo, useState } from "react"; import { NavLink, useLocation } from "react-router-dom"; import { Button, Sidebar, SidebarContent, SidebarHeader, SidebarMenu, SidebarMenuButton, SidebarMenuItem, } from "@relume_io/relume-ui"; import { MdTune, MdFitnessCenter, MdMonitorWeight, MdAccountBalanceWallet, MdTrendingUp, MdChecklist, MdAdd, MdBook, MdFlag, MdStar, MdBolt, MdNoteAlt, MdAutoGraph, } from "react-icons/md"; import logo from "../../../assets/logo.png"; import AddCustomCategoryModal from "./AddCustomCategoryModal.jsx"; import { createCustomTrackingCategory, getCustomTrackingCategories, } from "../../../api/discipline.js"; const navItems = [ { title: "Control Center", to: "/dashboard/control-center", icon: MdTune }, { title: "Training", to: "/dashboard/training", icon: MdFitnessCenter }, { title: "Weight", to: "/dashboard/weight", icon: MdMonitorWeight }, { title: "Finance", to: "/dashboard/finance", icon: MdAccountBalanceWallet }, { title: "Investing", to: "/dashboard/investing", icon: MdTrendingUp }, { title: "Discipline", to: "/dashboard/discipline", icon: MdChecklist }, ]; const CUSTOM_ICONS = [MdBook, MdFlag, MdStar, MdBolt, MdNoteAlt, MdAutoGraph]; function stableIconForId(id) { const n = Number(id); if (!Number.isFinite(n)) return CUSTOM_ICONS[0]; return CUSTOM_ICONS[Math.abs(n) % CUSTOM_ICONS.length]; } const DashboardSidebar = () => { const location = useLocation(); const [customOpen, setCustomOpen] = useState(false); const [customCategories, setCustomCategories] = useState([]); useEffect(() => { let mounted = true; (async () => { try { const res = await getCustomTrackingCategories(); const items = res?.items ?? []; if (mounted) setCustomCategories(items); } catch { // If the user isn't authenticated yet or the endpoint fails, // keep the sidebar usable (no custom categories). if (mounted) setCustomCategories([]); } })(); return () => { mounted = false; }; }, []); const customNavItems = useMemo(() => { return (customCategories ?? []).map((c) => { const Icon = stableIconForId(c.customTrackingId); return { title: c.name, to: `/dashboard/custom/${c.customTrackingId}`, icon: Icon, }; }); }, [customCategories]); return ( Trekr {navItems.map((item) => ( {(() => { const isActive = location.pathname === item.to || location.pathname.startsWith(`${item.to}/`); return ( {item.title} ); })()} ))} {customNavItems.length > 0 ? (
) : null} {customNavItems.map((item) => ( {(() => { const isActive = location.pathname === item.to || location.pathname.startsWith(`${item.to}/`); return ( {item.title} ); })()} ))}
setCustomOpen(false)} onSubmit={async (payload) => { const created = await createCustomTrackingCategory(payload); setCustomCategories((prev) => [created, ...(prev ?? [])]); }} /> ); }; export default DashboardSidebar;