Files
ipal-kit/src/modules/notifications/resolveFormMessage.ts
T

72 lines
3.1 KiB
TypeScript

import type { SubmitFormResult } from '../forms/index.js'
import type { FormNotificationTexts } from './types.js'
/**
* The texts resolveFormMessage needs. The File-field texts are optional, so an
* object written before they existed still fits (the field or general text is used).
*/
type FileTextKey = 'fileCount' | 'fileNotSent' | 'fileOptional' | 'fileRefused' | 'fileRemove' | 'fileTooLarge' | 'fileType'
export type FormMessageTexts = { [K in FileTextKey]?: string } & Omit<FormNotificationTexts, FileTextKey>
/** The texts of the File field problems, by result code. */
const FILE_TEXT: Partial<Record<string, FileTextKey>> = {
file_count: 'fileCount',
file_refused: 'fileRefused',
file_too_large: 'fileTooLarge',
file_type: 'fileType',
}
/**
* Maps a submitForm result to the user-facing message, interpolating {field}
* with the field's label (its key when the form has no label) for validation
* errors. This is the bridge the frontend uses: it gets a result
* code from submitForm and the resolved texts from getNotificationTexts, and
* this turns them into one string to display. Keeping the mapping here means the
* frontend never hard-codes messages or knows about result codes.
*
* Never surfaces raw backend/exception detail — 'error' maps to a friendly
* generic message, not the thrown error's text (which could leak internals).
*/
export function resolveFormMessage(result: SubmitFormResult, texts: FormMessageTexts): string {
if (result.success) {return texts.success}
switch (result.reason) {
case 'consent':
return texts.consent
case 'delivery': {
// The file could not be e-mailed: say it is the file (at its field), never the mail server's own words.
if (!texts.fileNotSent) {return texts.error}
return withoutFile(texts.fileNotSent.replace('{file}', result.file ?? ''), result.optional, texts)
}
case 'not_found':
return texts.notFound
case 'rate_limited':
return texts.rateLimited
case 'turnstile':
return texts.turnstile
case 'validation': {
// {field} → the label the visitor sees; no single field at fault → the generic error.
const field = result.label || result.field
// A problem with a file says so: which file, why, and what to do next.
const fileText = texts[FILE_TEXT[result.kind ?? ''] ?? 'fileRemove']
if (FILE_TEXT[result.kind ?? ''] && fileText) {
const message = fileText
.replace('{file}', result.file ?? '')
.replace('{max}', result.max === undefined ? '' : String(result.max))
.replace('{types}', result.types ?? '')
.replace('{field}', field ?? '')
return withoutFile(message, result.optional, texts)
}
return field ? texts.validation.replace('{field}', field) : texts.error
}
case 'error':
default:
return texts.error
}
}
/** On a field that is not required, a file message ends with "…send the form without the file". */
function withoutFile(message: string, optional: boolean | undefined, texts: FormMessageTexts): string {
return optional && texts.fileOptional ? `${message} ${texts.fileOptional}` : message
}