'use client' import { useRouter } from 'next/navigation' import { startTransition, useActionState, useState, type FormEvent } from 'react' import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage' import { useCampaignFormSuccessToast } from '@/components/campaign/shared/useCampaignFormSuccessToast' import { Button } from '@/components/ui/button' import { Field, FieldError, FieldLabel } from '@/components/ui/field' import { Spinner } from '@/components/ui/Spinner' import { Textarea } from '@/components/ui/textarea' import { CAMPAIGN_DEMAND_BODY_LABEL, CAMPAIGN_DEMAND_BODY_MAX_LENGTH, } from '@/lib/schemas/campaignDemand' import type { CampaignFormActionState } from '@/utilities/campaignFormActionError' type DemandDescriptionEditorProps = { demandId: number initialDescription: string formAction: ( state: CampaignFormActionState, formData: FormData, ) => Promise /** C142 — read-only presentation (advisor with Edição `somente_leitura`): the description renders with no edit affordance. */ readOnly?: boolean } /** * Inline edit of the single demand free-text field (B195): saving re-derives * the AI title server-side (previous title kept on AI failure) and the * canonical slug never changes — the page refreshes to the new title. */ export const DemandDescriptionEditor = ({ demandId, initialDescription, formAction, readOnly = false, }: DemandDescriptionEditorProps) => { const router = useRouter() const [editing, setEditing] = useState(false) const [state, submitAction, isPending] = useActionState(formAction, {}) useCampaignFormSuccessToast(state, () => { setEditing(false) router.refresh() }) if (!editing) { return (

{initialDescription}

{readOnly ? null : ( )}
) } // C140 — manual dispatch (no `action={submitAction}`): React 19 resets // uncontrolled fields after any settled form action, wiping typed values // on a validation error — the editor stays open, so the wipe showed. const handleSubmit = (event: FormEvent) => { event.preventDefault() startTransition(() => submitAction(new FormData(event.currentTarget))) } return (
{CAMPAIGN_DEMAND_BODY_LABEL}