"use client"; import React from "react"; import Link from "next/link"; import { useAuth } from "@/components/auth/authContext"; import { getBranches } from "@/lib/frontend/apiClient"; import { branchPath, searchPath } from "@/lib/frontend/routes"; import { isValidBranchParam } from "@/lib/frontend/params"; import { Button } from "@/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuLabel, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; const STORAGE_KEY_LAST_BRANCH = "rhl_last_branch"; const BRANCH_LIST_STATE = Object.freeze({ IDLE: "idle", LOADING: "loading", READY: "ready", ERROR: "error", }); function readRouteBranchFromPathname(pathname) { if (typeof pathname !== "string" || !pathname.startsWith("/")) return null; const seg = pathname.split("/").filter(Boolean)[0] || null; return seg && isValidBranchParam(seg) ? seg : null; } function safeReadLocalStorageBranch() { if (typeof window === "undefined") return null; try { const raw = window.localStorage.getItem(STORAGE_KEY_LAST_BRANCH); return raw && isValidBranchParam(raw) ? raw : null; } catch { return null; } } function safeWriteLocalStorageBranch(branch) { if (typeof window === "undefined") return; try { window.localStorage.setItem(STORAGE_KEY_LAST_BRANCH, String(branch)); } catch { // ignore } } export default function QuickNav() { const { status, user } = useAuth(); const isAuthenticated = status === "authenticated" && user; const isAdminDev = isAuthenticated && (user.role === "admin" || user.role === "dev"); const isBranchUser = isAuthenticated && user.role === "branch"; const [selectedBranch, setSelectedBranch] = React.useState(null); const [branchList, setBranchList] = React.useState({ status: BRANCH_LIST_STATE.IDLE, branches: null, }); // Determine a good default branch: // - branch users: always their own // - admin/dev: prefer current route branch, else last stored branch, else first fetched branch React.useEffect(() => { if (!isAuthenticated) return; if (isBranchUser) { const own = user.branchId; setSelectedBranch(own && isValidBranchParam(own) ? own : null); return; } // admin/dev const fromRoute = readRouteBranchFromPathname(window.location.pathname); const fromStorage = safeReadLocalStorageBranch(); const initial = fromRoute || fromStorage || null; if (initial) setSelectedBranch(initial); }, [isAuthenticated, isBranchUser, user?.branchId]); // Fetch branches for admin/dev dropdown (fail-open; used only for convenience). React.useEffect(() => { if (!isAdminDev) return; let cancelled = false; setBranchList({ status: BRANCH_LIST_STATE.LOADING, branches: null }); (async () => { try { const res = await getBranches(); if (cancelled) return; const branches = Array.isArray(res?.branches) ? res.branches : []; setBranchList({ status: BRANCH_LIST_STATE.READY, branches }); // If nothing selected yet, pick the first available branch as a convenience default. if (!selectedBranch && branches.length > 0) { setSelectedBranch(branches[0]); safeWriteLocalStorageBranch(branches[0]); } } catch (err) { if (cancelled) return; console.error("[QuickNav] getBranches failed:", err); setBranchList({ status: BRANCH_LIST_STATE.ERROR, branches: null }); } })(); return () => { cancelled = true; }; }, [isAdminDev, selectedBranch]); React.useEffect(() => { // Persist selection for admin/dev convenience. if (!isAdminDev) return; if (!selectedBranch) return; safeWriteLocalStorageBranch(selectedBranch); }, [isAdminDev, selectedBranch]); if (!isAuthenticated) return null; const effectiveBranch = isBranchUser ? user.branchId : selectedBranch; const canNavigate = Boolean( effectiveBranch && isValidBranchParam(effectiveBranch) ); // Keep TopNav clean on small screens. return (