teqo-backup/src/components/campaign/municipality/MunicipalityListSignalControl.tsx
Francisco Ignácio Fontoura Solla 6a20e98047 fix(campanha): share one Drawer across municipalities mobile cards
Root cause of the prod-build React #130 on /campanha/municipios: the
md:hidden mobile-cards tree still hydrated ~25 rows x ~5 sheet controls
(~125 eager Drawer/DrawerCloseButton instances) even on wide viewports.
Filtered ?q=... routes failed more often only because they exercise the
list with filters active — the search param itself was not the bug.

CampaignListSheetProvider hosts a single bottom Drawer for the whole
mobile section; CampaignCellEditOverlay sheet cells portal their
body/footer into it while keeping per-cell open state and autosave
scaffolds. Footers with DrawerCloseButton are only created once a sheet
actually opens. Desktop popover path is unchanged; per-cell Drawer
remains the fallback for surfaces without the provider.

Also: DrawerCloseButton (function render prop, avoids base-ui element
render in prod bundles) replaces the DrawerClose render={<Button/>}
pattern across call sites; stale e2e selectors refreshed; e2e affected
script + playwright github/list reporters for CI; use-mobile reverted
(no consumers of the gate remain).

Guardrail filed as issue #52.
2026-07-30 17:37:20 -03:00

108 lines
3.8 KiB
TypeScript

'use client'
import { useActionState, useId, useState, type ReactNode } from 'react'
import { MunicipalitySignalFields } from '@/components/campaign/municipality/MunicipalitySignalFields'
import {
CampaignCellEditOverlay,
type CampaignCellEditOverlayVariant,
} from '@/components/campaign/shared/CampaignCellEditOverlay'
import { CampaignFormActionMessage } from '@/components/campaign/shared/CampaignFormActionMessage'
import { useCampaignFormSuccessToast } from '@/components/campaign/shared/useCampaignFormSuccessToast'
import { Button } from '@/components/ui/button'
import { DrawerCloseButton } from '@/components/ui/Drawer'
import { Spinner } from '@/components/ui/Spinner'
import type { CampaignFormActionState } from '@/utilities/campaignFormActionError'
import {
formatMunicipalitySignalAgeLabel,
municipalitySignalAgeInDays,
} from '@/utilities/municipality/municipalitySignal'
type MunicipalityStaffFormAction = (
state: CampaignFormActionState,
formData: FormData,
) => Promise<CampaignFormActionState>
type MunicipalityListSignalControlProps = {
municipalityID: number
municipalitySlug: string
municipalityName: string
lastSignalAt: string | null
variant: CampaignCellEditOverlayVariant
formAction: MunicipalityStaffFormAction
children: ReactNode
}
export const MunicipalityListSignalControl = ({
municipalityID,
municipalitySlug,
municipalityName,
lastSignalAt,
variant,
formAction,
children,
}: MunicipalityListSignalControlProps) => {
const [open, setOpen] = useState(false)
const [formKey, setFormKey] = useState(0)
const [state, submitAction, isPending] = useActionState(formAction, {})
const reactId = useId()
const idPrefix = `municipality-list-signal-${variant}-${municipalityID}-${reactId}`
const formId = `${idPrefix}-form`
const isSheet = variant === 'sheet'
const frescorLabel = formatMunicipalitySignalAgeLabel(municipalitySignalAgeInDays(lastSignalAt))
useCampaignFormSuccessToast(state, () => {
setOpen(false)
setFormKey((key) => key + 1)
})
const submitButton = (
// On the sheet this button lives in the footer, outside the `<form>` it
// submits: `form={formId}` is the standard association, and React's action
// still runs because the submit event fires on the form either way. That is
// what let this control drop the Drawer/Popover it used to hand-roll only
// because its `<form>` had to wrap the footer (B32+ F5).
<Button type="submit" form={formId} disabled={isPending} className="min-h-11 w-full">
{isPending ? <Spinner data-icon="inline-start" aria-hidden="true" /> : null}
Registrar sinal
</Button>
)
return (
<CampaignCellEditOverlay
variant={variant}
open={open}
onOpenChange={setOpen}
title="Registrar sinal"
description={municipalityName}
triggerLabel={`Registrar sinal em ${municipalityName} — ${frescorLabel}`}
triggerBusy={isPending}
statusMessage={isPending ? 'Registrando sinal…' : ''}
triggerClassName="text-left"
contentClassName="w-80"
trigger={children}
footer={
isSheet && open ? (
<>
{submitButton}
<DrawerCloseButton>Cancelar</DrawerCloseButton>
</>
) : undefined
}
>
<form
key={formKey}
id={formId}
action={submitAction}
className="flex flex-col gap-3"
aria-busy={isPending || undefined}
>
<input type="hidden" name="municipalityId" value={municipalityID} />
<input type="hidden" name="municipalitySlug" value={municipalitySlug} />
<MunicipalitySignalFields idPrefix={idPrefix} fieldErrors={state.fieldErrors} />
{state.status !== 'success' ? <CampaignFormActionMessage state={state} /> : null}
{isSheet ? null : submitButton}
</form>
</CampaignCellEditOverlay>
)
}