33 lines
956 B
TypeScript
33 lines
956 B
TypeScript
import type { SubjectOption } from '../../cms-client/contact';
|
|
|
|
interface NamedPackage {
|
|
slug: string;
|
|
title: string;
|
|
}
|
|
|
|
/**
|
|
* Subject options of the contact form. The value is the readable label (e.g. "Website"), because
|
|
* that is what ends up in the e-mail and in the CMS message list. The slug only stays local, for
|
|
* the preselect (see `subjectForSlug`).
|
|
*/
|
|
export function buildSubjectOptions(
|
|
packages: readonly NamedPackage[] | undefined,
|
|
otherLabel: string,
|
|
): SubjectOption[] {
|
|
return [
|
|
...(packages ?? []).map((pkg) => ({ value: pkg.title, label: pkg.title })),
|
|
{ value: otherLabel, label: otherLabel },
|
|
];
|
|
}
|
|
|
|
/** Subject value (label) for a package slug, or null when the slug is unknown. */
|
|
export function subjectForSlug(
|
|
packages: readonly NamedPackage[] | undefined,
|
|
slug: string | null,
|
|
): string | null {
|
|
if (!slug) {
|
|
return null;
|
|
}
|
|
return packages?.find((pkg) => pkg.slug === slug)?.title ?? null;
|
|
}
|