'use client' import { InfoIcon, PlusIcon } from 'lucide-react' 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 { CampaignWizardNavLink } from '@/components/campaign/shared/CampaignWizardNavLink' 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 { recordLastActedMunicipality } from '@/lib/campaignLastActedMunicipality' 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, wizardFlowTitleForSlug, } from '@/lib/campaignWizardCopy' import { truncateNameAtWordBoundary } from '@/lib/leadershipNameTruncate' import { cn } from '@/lib/utils' import { resolveWizardChainEntry, wizardChainContinueHref, wizardChainEndHref, } from '@/lib/wizardActionChain' import { resolveWizardLeadershipSkip, type WizardLeadershipTileViewModel, } from '@/lib/wizardLeadershipContract' type WizardLeadershipStepProps = { actionSlug: string municipalityId: number municipalityName: string municipalitySlug: string entryAction?: CampaignWizardActionId initialTiles: WizardLeadershipTileViewModel[] initialLeadershipId?: number returnPath?: string } 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, initialLeadershipId, returnPath, }: WizardLeadershipStepProps) => { const router = useRouter() const [isContinuing, startContinueTransition] = useTransition() const initialMode = useMemo((): WizardLeadershipMode => { if (initialLeadershipId === undefined) { return { kind: 'grid' } } const leadership = initialTiles.find((tile) => tile.id === initialLeadershipId) return leadership ? { kind: 'form', leadership } : { kind: 'grid' } }, [initialLeadershipId, initialTiles]) const [mode, setMode] = useState(initialMode) const [dirty, setDirty] = useState(false) const [infoTile, setInfoTile] = useState(null) const skipConfig = resolveWizardLeadershipSkip(entryAction, municipalitySlug, returnPath) const chainContinueHref = wizardChainContinueHref( resolveWizardChainEntry(entryAction, 'update-leadership'), 'update-leadership', municipalitySlug, returnPath, ) const stepTitle = mode.kind === 'grid' ? WIZARD_LEADERSHIP_GRID_TITLE : mode.leadership ? WIZARD_LEADERSHIP_FORM_EDIT_TITLE : WIZARD_LEADERSHIP_FORM_CREATE_TITLE const trailingAction = skipConfig ? ( ) : undefined const handleSaved = useCallback(() => { recordLastActedMunicipality(municipalitySlug) setDirty(true) setMode({ kind: 'grid' }) router.refresh() toast.success(WIZARD_LEADERSHIP_SAVED_TOAST) }, [municipalitySlug, router]) const handleContinue = () => { startContinueTransition(() => { router.replace(chainContinueHref) }) } return ( {mode.kind === 'form' ? ( setMode({ kind: 'grid' })} /> ) : (
{initialTiles.length === 0 ? (

{WIZARD_LEADERSHIP_EMPTY_GRID}

) : null}
    {initialTiles.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}
) }