'use client' import { ExternalLinkIcon } from 'lucide-react' import Link from 'next/link' import { startTransition, useActionState, useMemo, useState, type FormEvent } from 'react' import { Alert, AlertDescription, AlertTitle } from '@/components/ui/Alert' import { Badge } from '@/components/ui/Badge' import { Button } from '@/components/ui/button' import { Checkbox } from '@/components/ui/Checkbox' import { Field, FieldDescription, FieldError, FieldLabel } from '@/components/ui/field' import { Input } from '@/components/ui/input' import { Spinner } from '@/components/ui/Spinner' import { Textarea } from '@/components/ui/textarea' import type { CampaignFormActionState } from '@/utilities/campaignFormActionError' import { fieldError } from '@/utilities/campaignFormFields' import type { TourStopRole } from '@/utilities/visit/visitPlannerViews' import { MAX_TOUR_NAME_LENGTH, MAX_TOUR_STOPS, tourStopRoleDescriptions, tourStopRoleLabels, } from '@/utilities/visit/visitPlannerViews' /** * One candidate stop, already reduced to what the decision needs. Numbers arrive * formatted and the dossier href arrives built: the canonical serializers drag * the município catalog with them, and this is a client component (the B14 * lesson — 21 kB of First Load JS for one link). */ export type TourStopOption = { municipalityID: number slug: string name: string role: TourStopRole suggested: boolean /** Activity tags the stop generates, derived from its role. */ tags: string[] metCount: number conditionCount: number unmetConditionLabels: string[] deficitLabel: string contraindicationReason: string | null dossierHref: string } const StopRow = ({ stop, checked, onToggle, }: { stop: TourStopOption checked: boolean onToggle: (checked: boolean) => void }) => { const checkboxId = `tour-stop-${stop.slug}` return (
  • onToggle(next === true)} className="mt-1.5" />
    {tourStopRoleLabels[stop.role]} {stop.metCount} de {stop.conditionCount} condições

    {tourStopRoleDescriptions[stop.role]}

    Faltam {stop.deficitLabel} votos para a meta. {stop.unmetConditionLabels.length > 0 ? ` Ainda não atende: ${stop.unmetConditionLabels.join(', ')}.` : ' Atende as cinco condições.'}

    {stop.contraindicationReason ? (

    {stop.contraindicationReason}

    ) : null}
    Ver dossiê
  • ) } /** * Interactions 2 and 3 of the composer: review the proposed composition * (âncora + satélites + semente, all pre-selected) and generate the drafts. * * It is a SUGGESTION and it says so — the numbers rank, the coordination * decides, and nothing is written until this form is submitted (P25: no * automatic allocation without a human in the loop). */ export const TourComposerForm = ({ region, stops, defaultTourName, formAction, }: { region: string stops: TourStopOption[] defaultTourName: string formAction: ( state: CampaignFormActionState, formData: FormData, ) => Promise }) => { const [state, submitAction, isPending] = useActionState(formAction, {}) const [selectedSlugs, setSelectedSlugs] = useState(() => stops.filter((stop) => stop.suggested).map((stop) => stop.slug), ) const selectedSet = useMemo(() => new Set(selectedSlugs), [selectedSlugs]) const stopsJson = useMemo( () => JSON.stringify( stops .filter((stop) => selectedSet.has(stop.slug)) .map((stop) => ({ municipality: stop.municipalityID, tags: stop.tags, })), ), [stops, selectedSet], ) const overCap = selectedSlugs.length > MAX_TOUR_STOPS const canSubmit = selectedSlugs.length > 0 && !overCap const toggleStop = (slug: string, checked: boolean) => setSelectedSlugs((current) => checked ? [...current, slug] : current.filter((entry) => entry !== slug), ) // C140 — manual dispatch (no `action={submitAction}`): React 19 resets // uncontrolled fields after any settled form action, wiping typed values // on a validation error — the page stays visible, so the wipe showed. const handleSubmit = (event: FormEvent) => { event.preventDefault() startTransition(() => submitAction(new FormData(event.currentTarget))) } return (

    Paradas propostas em {region}

    {selectedSlugs.length} selecionada{selectedSlugs.length === 1 ? '' : 's'}
      {stops.map((stop) => ( toggleStop(stop.slug, checked)} /> ))}
    Nome do giro Cada compromisso recebe este nome mais o município — assim o giro se lê como um conjunto na lista de Atividades. {fieldError(state.fieldErrors, 'tourName') ? ( {fieldError(state.fieldErrors, 'tourName')} ) : null} Nota de janela política