frontend_nyla/src/utils/mandateDisplayUtils.ts

42 lines
1.2 KiB
TypeScript

/**
* UI display helpers for Mandate `label` (Voller Name) vs `name` (Kurzzeichen / Slug).
* Mirrors semantics in `wiki/c-work/1-plan/2026-04-mandate-name-label-logic.md`.
*/
function _trimOrEmpty(value: unknown): string {
if (value === null || value === undefined) return '';
if (typeof value === 'string') return value.trim();
return String(value).trim();
}
/**
* Primary string for lists, dropdowns, breadcrumbs: Voller Name, then Kurzzeichen,
* then id (defensive).
*/
export function mandateDisplayLabel(m: {
label?: string | null;
name?: string | null;
id?: string;
}): string {
const lab = _trimOrEmpty(m.label);
if (lab) return lab;
const slug = _trimOrEmpty(m.name);
if (slug) return slug;
return typeof m.id === 'string' ? m.id : '';
}
/**
* One line: `"Voller Name (kurzzeichen)"` when both differ; otherwise the single value.
* Use where users should see the human name first and the technical slug second.
*/
export function mandateDisplayLineLabelThenSlug(m: {
label?: string | null;
name?: string | null;
}): string {
const lab = _trimOrEmpty(m.label);
const slug = _trimOrEmpty(m.name);
if (lab && slug && lab !== slug) {
return `${lab} (${slug})`;
}
return lab || slug;
}