mirror of
https://github.com/fsolla/teqo.git
synced 2026-11-15 18:53:06 +00:00
Propagate return path from quick actions through the B98 wizard chain so dismiss, skip, and chain end navigate back to the originating page instead of always redirecting to Início. Co-authored-by: Francisco Solla <fsolla@users.noreply.github.com>
153 lines
4.9 KiB
TypeScript
153 lines
4.9 KiB
TypeScript
import type { CampaignWizardActionId } from '@/lib/campaignActionRoutes'
|
|
import { WIZARD_CHAIN_SKIP_LABEL } from '@/lib/campaignWizardCopy'
|
|
import { politicalTrendStatuses, type PoliticalTrendStatusValue } from '@/lib/schemas/municipality'
|
|
import {
|
|
municipalitySignalTypeLabels,
|
|
type MunicipalitySignalType,
|
|
} from '@/lib/schemas/municipalityUpdate'
|
|
import { resolveWizardChainEntry, wizardChainContinueHref } from '@/lib/wizardActionChain'
|
|
|
|
const politicalTrendDisplayLabels: Record<PoliticalTrendStatusValue, string> = {
|
|
favoravel: 'Favorável',
|
|
neutra: 'Neutra',
|
|
desfavoravel: 'Desfavorável',
|
|
}
|
|
|
|
export const WIZARD_TREND_SAVE_LABEL = 'Salvar' as const
|
|
|
|
export const WIZARD_TREND_CLEAR_LABEL = 'Limpar' as const
|
|
|
|
export const WIZARD_TREND_SAVED_MESSAGE = 'Tendência política registrada.' as const
|
|
|
|
export const WIZARD_TREND_UNREGISTERED_TITLE = 'Tendência não registrada' as const
|
|
|
|
export type WizardTrendSkipAction = {
|
|
label: string
|
|
href: string
|
|
}
|
|
|
|
export const wizardTrendChoiceStepTitle = (
|
|
currentStatus: PoliticalTrendStatusValue | null,
|
|
): string =>
|
|
currentStatus
|
|
? `Tendência ${politicalTrendDisplayLabels[currentStatus]}`
|
|
: WIZARD_TREND_UNREGISTERED_TITLE
|
|
|
|
export const selectablePoliticalTrendStatuses = (
|
|
currentStatus: PoliticalTrendStatusValue | null,
|
|
): PoliticalTrendStatusValue[] =>
|
|
currentStatus
|
|
? politicalTrendStatuses.filter((status) => status !== currentStatus)
|
|
: [...politicalTrendStatuses]
|
|
|
|
/** Where the change-trend wizard should land when `tendencia=` is present. */
|
|
export type WizardTrendNoteDestination = 'note' | 'choice' | 'home'
|
|
|
|
/**
|
|
* `home` when the query trend already matches the persisted status — covers
|
|
* post-save RSC refresh (B97) and stale deep-links; never loops back to choice.
|
|
*/
|
|
export const resolveWizardTrendNoteDestination = (
|
|
trendStatus: PoliticalTrendStatusValue,
|
|
currentStatus: PoliticalTrendStatusValue | null,
|
|
): WizardTrendNoteDestination => {
|
|
if (trendStatus === currentStatus) {
|
|
return 'home'
|
|
}
|
|
|
|
if (!selectablePoliticalTrendStatuses(currentStatus).includes(trendStatus)) {
|
|
return 'choice'
|
|
}
|
|
|
|
return 'note'
|
|
}
|
|
|
|
export const shouldShowWizardTrendSkip = (
|
|
entryAction: CampaignWizardActionId | undefined,
|
|
): boolean => entryAction != null && entryAction !== 'change-trend'
|
|
|
|
export const resolveWizardTrendSkip = (
|
|
entryAction: CampaignWizardActionId | undefined,
|
|
municipalitySlug: string,
|
|
returnPath?: string,
|
|
): WizardTrendSkipAction | undefined =>
|
|
shouldShowWizardTrendSkip(entryAction)
|
|
? {
|
|
label: WIZARD_CHAIN_SKIP_LABEL,
|
|
href: wizardChainContinueHref(
|
|
resolveWizardChainEntry(entryAction, 'change-trend'),
|
|
'change-trend',
|
|
municipalitySlug,
|
|
returnPath,
|
|
),
|
|
}
|
|
: undefined
|
|
|
|
export type PoliticalTrendNotePrefillSource =
|
|
| { kind: 'none' }
|
|
| { kind: 'signal'; signalType: MunicipalitySignalType; description: string }
|
|
| { kind: 'voteAdjustment'; previousValue: number; newValue: number }
|
|
| { kind: 'custom'; text: string }
|
|
|
|
export const buildPoliticalTrendNotePrefill = (source: PoliticalTrendNotePrefillSource): string => {
|
|
switch (source.kind) {
|
|
case 'none':
|
|
return ''
|
|
case 'signal':
|
|
return `Sinal de ${municipalitySignalTypeLabels[source.signalType].toLowerCase()}: ${source.description}`
|
|
case 'voteAdjustment':
|
|
return `Ajuste de votos: ${source.previousValue} → ${source.newValue}`
|
|
case 'custom':
|
|
return source.text
|
|
default: {
|
|
const exhaustive: never = source
|
|
return exhaustive
|
|
}
|
|
}
|
|
}
|
|
|
|
const parseOptionalIntParam = (value: string | string[] | undefined): number | undefined => {
|
|
const raw = Array.isArray(value) ? value[0] : value
|
|
const trimmed = raw?.trim()
|
|
if (!trimmed) return undefined
|
|
const parsed = Number.parseInt(trimmed, 10)
|
|
return Number.isFinite(parsed) ? parsed : undefined
|
|
}
|
|
|
|
export const resolvePoliticalTrendNotePrefillSource = ({
|
|
entryAction,
|
|
notePrefill,
|
|
signalType,
|
|
signalBody,
|
|
voteFrom,
|
|
voteTo,
|
|
}: {
|
|
entryAction?: CampaignWizardActionId
|
|
notePrefill?: string | string[] | undefined
|
|
signalType?: MunicipalitySignalType
|
|
signalBody?: string | string[] | undefined
|
|
voteFrom?: string | string[] | undefined
|
|
voteTo?: string | string[] | undefined
|
|
}): PoliticalTrendNotePrefillSource => {
|
|
const customText = Array.isArray(notePrefill) ? notePrefill[0] : notePrefill
|
|
if (customText?.trim()) {
|
|
return { kind: 'custom', text: customText.trim() }
|
|
}
|
|
|
|
if (entryAction === 'register-signal' && signalType) {
|
|
const body = Array.isArray(signalBody) ? signalBody[0] : signalBody
|
|
if (body?.trim()) {
|
|
return { kind: 'signal', signalType, description: body.trim() }
|
|
}
|
|
}
|
|
|
|
if (entryAction === 'update-votes') {
|
|
const previousValue = parseOptionalIntParam(voteFrom)
|
|
const newValue = parseOptionalIntParam(voteTo)
|
|
if (previousValue !== undefined && newValue !== undefined) {
|
|
return { kind: 'voteAdjustment', previousValue, newValue }
|
|
}
|
|
}
|
|
|
|
return { kind: 'none' }
|
|
}
|