'use client' import Link from 'next/link' import { usePathname } from 'next/navigation' import { useState, type FormEvent, type ReactNode } from 'react' import { LogOutIcon } from 'lucide-react' import { logoutCampaign } from '@/app/(campaign)/campanha/actions/auth' import { CampaignUserAvatar } from '@/components/campaign/shared/CampaignUserAvatar' import { MunicipalityNavSavedFilters } from '@/components/campaign/shell/MunicipalityNavSavedFilters' import { getCampaignNav, getCampaignSecondaryNav, isCampaignNavActive, MUNICIPALITY_NAV_HREF, type CampaignNavItem, } from '@/components/campaign/shell/nav' import { Button } from '@/components/ui/button' import { Sidebar, SidebarContent, SidebarFooter, SidebarGroup, SidebarGroupContent, SidebarMenu, SidebarMenuButton, SidebarMenuItem, useSidebar, } from '@/components/ui/Sidebar' import { Spinner } from '@/components/ui/Spinner' import { clearLastActedMunicipality } from '@/lib/campaignLastActedMunicipality' import { clearCampaignPwaCaches } from '@/utilities/campaignPwaClient' import type { CampaignUserShellView } from '@/utilities/campaignUserProfile' import { campaignRoleLabels } from '@/utilities/campaignUserProfile' import { clearMunicipalitySavedFilters } from '@/utilities/municipality/municipalitySavedFilters' import { clearRecentVisits } from '@/utilities/recentVisits' export type CampaignSidebarUser = CampaignUserShellView const CampaignSidebarLink = ({ item, isActive, onNavigate, children, }: { item: CampaignNavItem isActive: boolean onNavigate: () => void /** Row affordances below the link — B18's saved-filter sub-list (B124: always on). */ children?: ReactNode }) => ( {item.title} {children} ) export const CampaignSidebar = ({ user }: { user: CampaignSidebarUser }) => { const pathname = usePathname() const { isMobile, setOpenMobile } = useSidebar() const [isLoggingOut, setIsLoggingOut] = useState(false) const secondaryNav = getCampaignSecondaryNav(user.role) const closeMobileSidebar = () => { if (isMobile) setOpenMobile(false) } const handleLogout = async (event: FormEvent) => { event.preventDefault() if (isLoggingOut) return setIsLoggingOut(true) clearRecentVisits() clearLastActedMunicipality() clearMunicipalitySavedFilters() await clearCampaignPwaCaches() // logoutCampaign redirects — no need to reset the pending flag on success. await logoutCampaign() } return ( {getCampaignNav(user.role).map((item) => ( {item.href === MUNICIPALITY_NAV_HREF ? ( ) : null} ))} {/* Reference, not a destination: pinned to the foot of the nav so it never competes with the places the mesa works every day. */} {secondaryNav.length > 0 ? ( {secondaryNav.map((item) => ( ))} ) : null}
{user.name} {campaignRoleLabels[user.role]}
) }