import { type ComponentType, type KeyboardEvent, useEffect, useRef } from "react"; import { useTranslation } from "react-i18next"; import { Layers3Icon, type LucideProps, MonitorSmartphoneIcon } from "lucide-react"; import { cn } from "@/lib/cn"; import { useNavSection, type NavSection } from "@/contexts/NavSectionContext"; import { useStatus } from "@/contexts/StatusContext"; import { useRestrictions } from "@/contexts/RestrictionsContext"; type TabEntry = { value: NavSection; label: string; icon: ComponentType; }; export const Navigation = () => { const { t } = useTranslation(); const { section, setSection } = useNavSection(); const { status } = useStatus(); const { features } = useRestrictions(); const isConnected = status?.status === "Connected"; // Reset back to peers tab if mdm or feature flag flipped it useEffect(() => { if (features.disableNetworks && section === "networks") { setSection("peers"); } }, [features.disableNetworks, section, setSection]); const tabs: TabEntry[] = [ { value: "peers", label: t("nav.peers.title"), icon: MonitorSmartphoneIcon, }, ]; if (!features.disableNetworks) { tabs.push({ value: "networks", label: t("nav.resources.title"), icon: Layers3Icon, }); } const tabRefs = useRef>({}); const focusTab = (value: NavSection) => { setSection(value); requestAnimationFrame(() => tabRefs.current[value]?.focus()); }; const handleKeyDown = (e: KeyboardEvent) => { const enabled = tabs.filter((t) => isConnected || t.value === section); if (enabled.length < 2) return; const currentIndex = enabled.findIndex((t) => t.value === section); if (currentIndex === -1) return; let nextIndex: number; switch (e.key) { case "ArrowRight": nextIndex = (currentIndex + 1) % enabled.length; break; case "ArrowLeft": nextIndex = (currentIndex - 1 + enabled.length) % enabled.length; break; case "Home": nextIndex = 0; break; case "End": nextIndex = enabled.length - 1; break; default: return; } e.preventDefault(); focusTab(enabled[nextIndex].value); }; return (
{tabs.map((tab, index) => { const isActive = tab.value === section; const isDisabled = !isConnected && !isActive; const isFirst = index === 0; const isLast = index === tabs.length - 1; const Icon = tab.icon; return ( ); })}
); }; export type { NavSection } from "@/contexts/NavSectionContext";