'use client' import { startTransition, useActionState, useEffect, useRef, type FormEvent } from 'react' import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage' import { Badge } from '@/components/ui/Badge' import { Button } from '@/components/ui/button' import { NativeSelect, NativeSelectOption } from '@/components/ui/native-select' import { Spinner } from '@/components/ui/Spinner' import { Textarea } from '@/components/ui/textarea' import { MUNICIPALITY_UPDATE_COMMENT_MAX_LENGTH } from '@/lib/schemas/municipalityUpdate' import type { MunicipalityUpdateDeliberationCapabilities } from '@/utilities/campaignAccess' import type { CampaignFormActionState } from '@/utilities/campaignFormActionError' import type { EligibleUpdateStaffMember, MunicipalityUpdateCommentViewModel, } from '@/utilities/municipality/municipalityUpdatePageData' /** C88 — the four deliberation server actions, injected by the server feeds. */ export type MunicipalityUpdateDeliberationFormActions = { assign: (state: CampaignFormActionState, formData: FormData) => Promise comment: (state: CampaignFormActionState, formData: FormData) => Promise resolve: (state: CampaignFormActionState, formData: FormData) => Promise reopen: (state: CampaignFormActionState, formData: FormData) => Promise } const dateTimeFormatter = new Intl.DateTimeFormat('pt-BR', { dateStyle: 'short', timeStyle: 'short', }) const staffRoleLabels: Record<'coordinator' | 'advisor' | 'candidate', string> = { coordinator: 'Coordenador Geral', advisor: 'Assessor', candidate: 'Candidato', } type MunicipalityUpdateDeliberationProps = { updateId: number responsibleId: number | null responsibleName: string | null resolvedAt: string | null resolvedByName: string | null comments: MunicipalityUpdateCommentViewModel[] eligibleStaff: EligibleUpdateStaffMember[] capabilities: MunicipalityUpdateDeliberationCapabilities formActions: MunicipalityUpdateDeliberationFormActions } /** * C88 — the deliberation island on an update card: assignee select, comment * thread and the Resolvido/Reabrir transition. The server passes the actor's * capabilities; the client only renders what the actor may do. Leader lockdown * is upstream — this card never renders for a leader. */ export const MunicipalityUpdateDeliberation = ({ updateId, responsibleId, responsibleName, resolvedAt, resolvedByName, comments, eligibleStaff, capabilities, formActions, }: MunicipalityUpdateDeliberationProps) => { const isResolved = resolvedAt !== null const inDeliberation = !isResolved && (responsibleId !== null || comments.length > 0) return (
{isResolved || inDeliberation ? (
{isResolved ? ( Resolvido ) : ( Em deliberação )}
) : null} {isResolved ? (

Resolvido por {resolvedByName} {resolvedAt ? ` em ${dateTimeFormatter.format(new Date(resolvedAt))}` : ''}

) : null} {capabilities.canAssign || responsibleId !== null ? ( ) : null} {comments.length > 0 || capabilities.canComment ? ( ) : null} {capabilities.canResolve ? (
) : null}
) } const ResponsibleBlock = ({ updateId, responsibleId, responsibleName, eligibleStaff, canAssign, formAction, }: { updateId: number responsibleId: number | null responsibleName: string | null eligibleStaff: EligibleUpdateStaffMember[] canAssign: boolean formAction: MunicipalityUpdateDeliberationFormActions['assign'] }) => { const [state, submitAction, isPending] = useActionState(formAction, {}) if (!canAssign) { return (

Responsável:{' '} {responsibleName}

) } return (
Sem responsável {eligibleStaff.map((staff) => ( {staff.name} ({staffRoleLabels[staff.role]}) ))} {state.message ? ( ) : null} ) } const CommentThread = ({ updateId, comments, canComment, formAction, }: { updateId: number comments: MunicipalityUpdateCommentViewModel[] canComment: boolean formAction: MunicipalityUpdateDeliberationFormActions['comment'] }) => { const [state, submitAction, isPending] = useActionState(formAction, {}) const formRef = useRef(null) useEffect(() => { if (state.status === 'success' && formRef.current) formRef.current.reset() }, [state]) // C139 — manual dispatch: React 19 resets uncontrolled fields after any // settled form action, wiping the typed comment on a validation error. // The success reset above stays explicit; `startTransition` keeps `pending`. const handleSubmit = (event: FormEvent) => { event.preventDefault() startTransition(() => submitAction(new FormData(event.currentTarget))) } return (

Discussão

{comments.length > 0 ? (
    {comments.map((comment) => (
  • {comment.authorName} {comment.createdAt ? ( <> {dateTimeFormatter.format(new Date(comment.createdAt))} ) : null}

    {comment.body}

  • ))}
) : null} {canComment ? (