mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-23 06:33:06 +00:00
nextStateDeputyIdsAfterMunicipalityMembership was the third byte-for-byte copy of the same single-id delta — nextAdvisorIdsAfterMembership and nextStateDeputyIdsAfterMembership differ from it only in which cap they read — so the algorithm now lives once in nextIdsAfterMembership with the cap as data, and the three named wrappers just bind their own. The three cloned spec files became one describe.each suite: coverage went up rather than down, since every relation now gets the "can go to zero" and no-mutation cases only B37's clone had, plus an explicit assertion that each wrapper refuses at ITS cap with ITS message. That pairing matters because mapCampaignFormActionError matches thrown messages against safeMessages by exact string, and two of those cap messages were bare literals in three src places each (thrown, plus two independent allowlists): rewording any one would have silently collapsed a real refusal into "tente novamente". Both are constants now, next to their caps, following the MUNICIPALITY_STATE_DEPUTIES_CAP_MESSAGE precedent already in those same files. The dobradinha staff message had the same exposure in four places — and actions/stateDeputy.ts already imported the constant while restating it as a literal one line below — so it is STATE_DEPUTY_STAFF_MESSAGE (renamed: it guards create/update too, not just the batch), and the conflict message it shares with the create route is named too. The batch action stopped resolving the dobradinha slug before its loop: the slug exists only to target the revalidate, which the caller already skips when nothing changed, so on a no-op batch that read was fetched and thrown away — the guard setLeadershipMunicipalitiesMembershipRecord already had. Its redundant second dedup is gone (the schema's transform already deduped; the sort stays, for deadlock-free lock ordering), and the bare 435 shared with the advisor batch is MAX_MUNICIPALITIES_PER_BATCH. Two memoization gaps closed, both restoring invariants the codebase had already established elsewhere. An advisor's scoped .filter() returned a fresh array per row, defeating the WeakMap that exists precisely to share derivations across rows, so scopedPortfolioIndex memoizes on (index, addableIds) and every row gets one array. And LeadershipStateDeputyRelationCell called matchesAtWordStart per candidate, paying an NFD pass plus three Unicode regexes on both sides per keystroke over the unpaginated leadership catalog — the cost wordStartFilter's own doc comment warns about; it now normalizes the query once against labels cached on options, like municipalityPortfolio. Rejected with reason: maxItems on the dobradinhas cell. A dobradinha has no product ceiling on municípios (that is why the bound is the catalog), so a "generous 30" would invent a rule that refuses a legitimate statewide dobradinha, and the honest 435 is unreachable when ids are unique. Also dropped the dead addPlaceholder knob no caller ever set.
216 lines
7.4 KiB
TypeScript
216 lines
7.4 KiB
TypeScript
'use client'
|
|
|
|
import { useCallback, useMemo } from 'react'
|
|
|
|
import {
|
|
RelationChipCell,
|
|
type RelationChip,
|
|
type RelationChipCellCopy,
|
|
type RelationSearchHit,
|
|
} from '@/components/campaign/shared/RelationChipCell'
|
|
import { matchesNormalizedAtWordStart, normalizeSearchPhrase } from '@/lib/wordStartFilter'
|
|
import type { CampaignFormActionState } from '@/utilities/campaignFormActionError'
|
|
|
|
export type RelationCellItem = {
|
|
id: number
|
|
label: string
|
|
href: string
|
|
party?: string
|
|
}
|
|
|
|
export type RelationCellOption = {
|
|
id: number
|
|
searchLabel: string
|
|
item: RelationCellItem
|
|
}
|
|
|
|
type LeadershipStateDeputyRelationDirection = 'fromLeadership' | 'fromStateDeputy'
|
|
|
|
type LeadershipStateDeputyRelationCellProps = {
|
|
direction: LeadershipStateDeputyRelationDirection
|
|
fixedId: number
|
|
/** Whose relation this is — spoken in the aria-labels and the Drawer. */
|
|
ownerName: string
|
|
items: RelationCellItem[]
|
|
options: RelationCellOption[]
|
|
membershipAction: (
|
|
state: CampaignFormActionState,
|
|
formData: FormData,
|
|
) => Promise<CampaignFormActionState>
|
|
/** Clamp rest chips to 3 rows + "Ver mais…". Default true; pass false when few chips are expected. */
|
|
measureOverflow?: boolean
|
|
}
|
|
|
|
const itemLabel = (item: RelationCellItem): string =>
|
|
item.party ? `${item.label} (${item.party})` : item.label
|
|
|
|
/**
|
|
* Normalized search labels, once per `options` array rather than per row: the
|
|
* RSC builds one array and every row of the table gets that same reference, so a
|
|
* `WeakMap` shares the work where a per-row `useMemo` would repeat it (same
|
|
* reasoning as `municipalityPortfolio`'s derivation cache).
|
|
*/
|
|
const normalizedLabelsByOptions = new WeakMap<readonly RelationCellOption[], Map<number, string>>()
|
|
|
|
const normalizedOptionLabels = (options: readonly RelationCellOption[]): Map<number, string> => {
|
|
const cached = normalizedLabelsByOptions.get(options)
|
|
if (cached) return cached
|
|
|
|
const normalized = new Map(
|
|
options.map((option) => [option.id, normalizeSearchPhrase(option.searchLabel)] as const),
|
|
)
|
|
normalizedLabelsByOptions.set(options, normalized)
|
|
return normalized
|
|
}
|
|
|
|
const DIRECTION_COPY: Record<
|
|
LeadershipStateDeputyRelationDirection,
|
|
{
|
|
drawerTitle: string
|
|
triggerVerb: string
|
|
emptyDrawerMessage: string
|
|
searchPlaceholder: string
|
|
searchLabel: string
|
|
suggestionsLabel: string
|
|
savingMessage: string
|
|
savedMessage: string
|
|
removedMessage: (count: number) => string
|
|
updateErrorMessage: string
|
|
}
|
|
> = {
|
|
fromLeadership: {
|
|
drawerTitle: 'Dobradinhas da liderança',
|
|
triggerVerb: 'dobradinhas',
|
|
emptyDrawerMessage: 'Nenhuma dobradinha vinculada.',
|
|
searchPlaceholder: 'Buscar deputado estadual…',
|
|
searchLabel: 'Buscar deputado estadual',
|
|
suggestionsLabel: 'Sugestões de deputados estaduais',
|
|
savingMessage: 'Salvando dobradinhas.',
|
|
savedMessage: 'Dobradinhas salvas.',
|
|
removedMessage: (count) =>
|
|
count === 1 ? 'Dobradinha removida.' : `${count} dobradinhas removidas.`,
|
|
updateErrorMessage: 'Não foi possível atualizar as dobradinhas. Tente novamente.',
|
|
},
|
|
fromStateDeputy: {
|
|
drawerTitle: 'Lideranças da dobradinha',
|
|
triggerVerb: 'lideranças',
|
|
emptyDrawerMessage: 'Nenhuma liderança vinculada.',
|
|
searchPlaceholder: 'Buscar liderança…',
|
|
searchLabel: 'Buscar liderança',
|
|
suggestionsLabel: 'Sugestões de lideranças',
|
|
savingMessage: 'Salvando lideranças.',
|
|
savedMessage: 'Lideranças salvas.',
|
|
removedMessage: (count) =>
|
|
count === 1 ? 'Liderança removida.' : `${count} lideranças removidas.`,
|
|
updateErrorMessage: 'Não foi possível atualizar as lideranças. Tente novamente.',
|
|
},
|
|
}
|
|
|
|
/**
|
|
* Bidirectional `leadership.stateDeputies` edge, on the same `RelationChipCell`
|
|
* that serves município portfolios (B37). Both directions write
|
|
* `leadershipId`/`stateDeputyId`/`assigned` through the same membership action;
|
|
* every chip here is a single item — no batch, no floor/ceiling.
|
|
*/
|
|
export const LeadershipStateDeputyRelationCell = ({
|
|
direction,
|
|
fixedId,
|
|
ownerName,
|
|
items,
|
|
options,
|
|
membershipAction,
|
|
measureOverflow = true,
|
|
}: LeadershipStateDeputyRelationCellProps) => {
|
|
const copy = DIRECTION_COPY[direction]
|
|
|
|
const itemById = useMemo(() => new Map(items.map((item) => [item.id, item])), [items])
|
|
// Additions resolve through `options` (the addable catalog), same asymmetry
|
|
// as the município cell's `optionById`: only what can still be added needs to
|
|
// be looked up there, and a removal always has its item in `items` already.
|
|
const optionItemById = useMemo(
|
|
() => new Map(options.map((option) => [option.id, option.item])),
|
|
[options],
|
|
)
|
|
|
|
const buildChips = useCallback(
|
|
(ids: number[]): RelationChip[] => {
|
|
const chips: RelationChip[] = []
|
|
for (const id of ids) {
|
|
const item = itemById.get(id) ?? optionItemById.get(id)
|
|
if (!item) continue
|
|
chips.push({ key: String(id), label: itemLabel(item), href: item.href, ids: [id] })
|
|
}
|
|
return chips
|
|
},
|
|
[itemById, optionItemById],
|
|
)
|
|
|
|
const searchHits = useCallback(
|
|
(query: string, assignedIds: ReadonlySet<number>): RelationSearchHit[] => {
|
|
// Normalize the query ONCE and read pre-normalized labels, instead of an
|
|
// NFD pass plus three Unicode-property regexes per candidate per
|
|
// keystroke — the cost `wordStartFilter`'s own doc comment warns about,
|
|
// and which matters here because `options` in the `fromStateDeputy`
|
|
// direction is the whole (unpaginated) leadership catalog.
|
|
const normalizedQuery = normalizeSearchPhrase(query)
|
|
const normalizedLabels = normalizedOptionLabels(options)
|
|
const hits: RelationSearchHit[] = []
|
|
for (const option of options) {
|
|
if (assignedIds.has(option.id)) continue
|
|
if (!matchesNormalizedAtWordStart(normalizedLabels.get(option.id) ?? '', normalizedQuery)) {
|
|
continue
|
|
}
|
|
hits.push({ key: String(option.id), label: option.searchLabel, ids: [option.id] })
|
|
}
|
|
return hits
|
|
},
|
|
[options],
|
|
)
|
|
|
|
const buildFormData = useCallback(
|
|
(changedIds: number[], assigned: boolean) => {
|
|
const itemId = changedIds[0]
|
|
const formData = new FormData()
|
|
if (direction === 'fromLeadership') {
|
|
formData.set('leadershipId', String(fixedId))
|
|
formData.set('stateDeputyId', String(itemId))
|
|
} else {
|
|
formData.set('leadershipId', String(itemId))
|
|
formData.set('stateDeputyId', String(fixedId))
|
|
}
|
|
formData.set('assigned', assigned ? 'true' : 'false')
|
|
return formData
|
|
},
|
|
[direction, fixedId],
|
|
)
|
|
|
|
const chipCellCopy = useMemo<RelationChipCellCopy>(
|
|
() => ({
|
|
searchPlaceholder: copy.searchPlaceholder,
|
|
searchLabel: copy.searchLabel,
|
|
suggestionsLabel: copy.suggestionsLabel,
|
|
emptyDrawerMessage: copy.emptyDrawerMessage,
|
|
savingMessage: copy.savingMessage,
|
|
savedMessage: copy.savedMessage,
|
|
removedMessage: copy.removedMessage,
|
|
}),
|
|
[copy],
|
|
)
|
|
|
|
return (
|
|
<RelationChipCell
|
|
ownerId={fixedId}
|
|
ownerName={ownerName}
|
|
ids={items.map((item) => item.id)}
|
|
buildChips={buildChips}
|
|
searchHits={searchHits}
|
|
buildFormData={buildFormData}
|
|
commitAction={membershipAction}
|
|
drawerTitle={copy.drawerTitle}
|
|
triggerLabel={`Editar ${copy.triggerVerb} de ${ownerName}`}
|
|
updateErrorMessage={copy.updateErrorMessage}
|
|
copy={chipCellCopy}
|
|
measureOverflow={measureOverflow}
|
|
/>
|
|
)
|
|
}
|