'use client' import { InfoIcon, PlusIcon } from 'lucide-react' import Link from 'next/link' import { useRouter } from 'next/navigation' import { useCallback, useMemo, useState, useTransition } from 'react' import { toast } from 'sonner' import { SupportStatusBadge } from '@/components/campaign/leadership/SupportStatusBadge' import { WizardLeadershipForm } from '@/components/campaign/leadership/WizardLeadershipForm' import { CampaignWizardShell } from '@/components/campaign/shared/CampaignWizardShell' import { Badge } from '@/components/ui/Badge' import { Button } from '@/components/ui/button' import { Drawer, DrawerContent, DrawerDescription, DrawerHeader, DrawerTitle, } from '@/components/ui/Drawer' import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip' import type { CampaignWizardActionId } from '@/lib/campaignActionRoutes' import { wizardActionHref } from '@/lib/campaignActionRoutes' import { CAMPAIGN_HOME } from '@/lib/campaignPaths' import { WIZARD_LEADERSHIP_ADD_TILE_LABEL, WIZARD_LEADERSHIP_CONTINUE_LABEL, WIZARD_LEADERSHIP_EMPTY_GRID, WIZARD_LEADERSHIP_EMPTY_NOTES, WIZARD_LEADERSHIP_FORM_CREATE_TITLE, WIZARD_LEADERSHIP_FORM_EDIT_TITLE, WIZARD_LEADERSHIP_GRID_TITLE, WIZARD_LEADERSHIP_SAVED_TOAST, WIZARD_LEADERSHIP_SKIP_LABEL, wizardFlowTitleForSlug, } from '@/lib/campaignWizardCopy' import { truncateNameAtWordBoundary } from '@/lib/leadershipNameTruncate' import { cn } from '@/lib/utils' import { showLeadershipWizardSkip, type WizardLeadershipTileViewModel, } from '@/lib/wizardLeadershipContract' type WizardLeadershipStepProps = { actionSlug: string municipalityId: number municipalityName: string municipalitySlug: string entryAction?: CampaignWizardActionId initialTiles: WizardLeadershipTileViewModel[] } type WizardLeadershipMode = | { kind: 'grid' } | { kind: 'form'; leadership: WizardLeadershipTileViewModel | null } const notesLabel = (notes: string | null): string => notes?.trim() ? notes.trim() : WIZARD_LEADERSHIP_EMPTY_NOTES const secondaryContact = (tile: WizardLeadershipTileViewModel): string | null => tile.phone?.trim() || tile.email?.trim() || null export const WizardLeadershipStep = ({ actionSlug, municipalityId, municipalityName, municipalitySlug, entryAction, initialTiles, }: WizardLeadershipStepProps) => { const router = useRouter() const [isContinuing, startContinueTransition] = useTransition() const [mode, setMode] = useState({ kind: 'grid' }) const [dirty, setDirty] = useState(false) const [infoTile, setInfoTile] = useState(null) const showSkip = showLeadershipWizardSkip(entryAction) const skip = showSkip ? { label: WIZARD_LEADERSHIP_SKIP_LABEL, href: CAMPAIGN_HOME } : undefined const stepTitle = mode.kind === 'grid' ? WIZARD_LEADERSHIP_GRID_TITLE : mode.leadership ? WIZARD_LEADERSHIP_FORM_EDIT_TITLE : WIZARD_LEADERSHIP_FORM_CREATE_TITLE const trailingAction = showSkip ? ( ) : undefined const handleSaved = useCallback(() => { setDirty(true) setMode({ kind: 'grid' }) router.refresh() toast.success(WIZARD_LEADERSHIP_SAVED_TOAST) }, [router]) const handleContinue = () => { startContinueTransition(() => { router.push(CAMPAIGN_HOME) }) } const sortedTiles = useMemo( () => [...initialTiles].sort((left, right) => left.name.localeCompare(right.name, 'pt-BR')), [initialTiles], ) return ( {mode.kind === 'form' ? ( setMode({ kind: 'grid' })} /> ) : (
{sortedTiles.length === 0 ? (

{WIZARD_LEADERSHIP_EMPTY_GRID}

) : null}
    {sortedTiles.map((tile) => (
  • setMode({ kind: 'form', leadership: tile })} onInfo={() => setInfoTile(tile)} />
  • ))}
)} {dirty && mode.kind === 'grid' ? (
) : null} !open && setInfoTile(null)}> {infoTile?.name ?? 'Observação'} Observação interna da liderança
{notesLabel(infoTile?.notes ?? null)}
) } type LeadershipTileButtonProps = { tile: WizardLeadershipTileViewModel onOpen: () => void onInfo: () => void } const LeadershipTileButton = ({ tile, onOpen, onInfo }: LeadershipTileButtonProps) => { const secondary = secondaryContact(tile) const noteText = notesLabel(tile.notes) return ( {noteText} ) }