'use client'
import { useState, type FormEvent } from 'react'
import Link from 'next/link'
import { usePathname } from 'next/navigation'
import { logoutCampaign } from '@/app/(campaign)/campanha/actions/auth'
import { CampaignLogo } from '@/components/campaign/campaign-logo'
import { CampaignScopeBadge } from '@/components/campaign/CampaignScopeBadge'
import { CampaignUserAvatar } from '@/components/campaign/CampaignUserAvatar'
import {
getCampaignNav,
getCampaignSecondaryNav,
isCampaignNavActive,
type CampaignNavItem,
} from '@/components/campaign/nav'
import { Button } from '@/components/ui/button'
import { Spinner } from '@/components/ui/Spinner'
import {
Sidebar,
SidebarContent,
SidebarFooter,
SidebarGroup,
SidebarGroupContent,
SidebarHeader,
SidebarMenu,
SidebarMenuButton,
SidebarMenuItem,
useSidebar,
} from '@/components/ui/Sidebar'
import type { CampaignUserShellView } from '@/utilities/campaignUserProfile'
import { campaignRoleLabels } from '@/utilities/campaignUserProfile'
import { clearCampaignPwaCaches } from '@/utilities/campaignPwaClient'
import { clearRecentVisits } from '@/utilities/recentVisits'
export type CampaignSidebarUser = CampaignUserShellView
const CampaignSidebarLink = ({
item,
isActive,
onNavigate,
}: {
item: CampaignNavItem
isActive: boolean
onNavigate: () => void
}) => (
{item.title}
)
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()
await clearCampaignPwaCaches()
// logoutCampaign redirects — no need to reset the pending flag on success.
await logoutCampaign()
}
return (
{campaignRoleLabels[user.role]}
{getCampaignNav(user.role).map((item) => (
))}
{/* 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
)
}