teqo/src/lib/campaignQuickActionDemands.ts
Francisco Ignácio Fontoura Solla 4a90fb19ec C86+ — pathname de lista das ações rápidas em fonte única (isListPath)
Extrai isListPath + normalizePathname exportados em campaignQuickActionPaths.ts
e migra os 9 wrappers (7 públicos + 2 privados) + a 3a cópia de normalize em
campaignPageChrome.ts. Refactor estrutural sem mudança de comportamento;
derivação de regex de detalhe numérico adiada (gatilho: 3o vertical [id]).
Inclui docs/plans/escala-dry-pos-acoes-rapidas-impl.md e o fix de format
pré-existente de docs/plans/liderancas-coluna-municipios.md (bloqueava format:check).
2026-08-04 01:14:35 -03:00

104 lines
3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {
CAMPAIGN_WIZARD_ACTION_SLUGS,
wizardActionHref,
type CampaignWizardActionId,
} from '@/lib/campaignActionRoutes'
import { homeActionsForRole } from '@/lib/campaignHomeActions'
import { CAMPAIGN_DEMANDS_HOME } from '@/lib/campaignPaths'
import type { CampaignQuickActionContext } from '@/lib/campaignQuickActionContext'
import { isListPath } from '@/lib/campaignQuickActionPaths'
import type { CampaignQuickAction } from '@/lib/campaignQuickActionTypes'
import { isStaffCampaignRole, type CampaignRole } from '@/lib/campaignRoles'
export const CAMPAIGN_DEMANDS_CREATE_HREF = `${CAMPAIGN_DEMANDS_HOME}/nova` as const
const DETAIL_WIZARD_ACTION_IDS: readonly CampaignWizardActionId[] = [
'update-votes',
'register-signal',
'change-trend',
'update-leadership',
'register-demand',
]
export const demandCreateHref = (municipalityId?: number): string => {
if (municipalityId === undefined) {
return CAMPAIGN_DEMANDS_CREATE_HREF
}
return `${CAMPAIGN_DEMANDS_CREATE_HREF}?municipality=${municipalityId}`
}
export const isDemandsListPath = (pathname: string): boolean =>
isListPath(pathname, CAMPAIGN_DEMANDS_HOME)
export const isDemandDetailPath = (pathname: string): boolean => {
if (!pathname.startsWith(`${CAMPAIGN_DEMANDS_HOME}/`)) {
return false
}
const rest = pathname.slice(`${CAMPAIGN_DEMANDS_HOME}/`.length)
if (!rest || rest.includes('/')) {
return false
}
return rest !== 'nova'
}
const registerDemandListAction = (role: CampaignRole): CampaignQuickAction | null => {
const source = homeActionsForRole(role).find((action) => action.id === 'register-demand')
if (!source) {
return null
}
return {
...source,
href: CAMPAIGN_DEMANDS_CREATE_HREF,
}
}
/** B85 list: single "Registrar pedido" launcher (form route — wizard A5 is still a stub). */
export const resolveDemandsListQuickActions = (
role: CampaignRole,
): readonly CampaignQuickAction[] => {
if (!isStaffCampaignRole(role)) {
return []
}
const action = registerDemandListAction(role)
return action ? [action] : []
}
/** B85 detail: staff Início A1–A5 with municipality prefill when linked. */
export const resolveDemandDetailQuickActions = (
role: CampaignRole,
context: CampaignQuickActionContext,
returnPath: string,
): readonly CampaignQuickAction[] => {
if (!isStaffCampaignRole(role)) {
return []
}
const { municipalitySlug, municipalityId } = context
if (!municipalitySlug) {
return []
}
const byId = new Map(homeActionsForRole(role).map((action) => [action.id, action]))
return DETAIL_WIZARD_ACTION_IDS.flatMap((id) => {
const action = byId.get(id)
if (!action) {
return []
}
if (id === 'register-demand') {
return [
{
...action,
href: demandCreateHref(municipalityId),
},
]
}
return [
{
...action,
href: wizardActionHref(CAMPAIGN_WIZARD_ACTION_SLUGS[id], municipalitySlug, {
returnPath,
}),
},
]
})
}