'use client'
import Link from 'next/link'
import { usePathname } from 'next/navigation'
import { useState, type FormEvent, type ReactNode } from 'react'
import { logoutCampaign } from '@/app/(campaign)/campanha/actions/auth'
import { CampaignScopeBadge } from '@/components/campaign/shared/CampaignScopeBadge'
import { CampaignUserAvatar } from '@/components/campaign/shared/CampaignUserAvatar'
import { CampaignLogo } from '@/components/campaign/shell/campaign-logo'
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,
SidebarHeader,
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 that sit beside the link — B18's disclosure and sub-list. */
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 (
{campaignRoleLabels[user.role]}
{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}
Meu perfil
)
}