WORKFLOW-APP/lib/format.ts
SJCoetz bb97fef453 feat: initialize full ysterbaard frontend application
Add complete production-ready Ysterbaard PocketBase registry browser frontend:
- Next.js 14 App Router with TypeScript, Tailwind CSS, and TanStack React Query
- Secure server-side PocketBase API proxy to avoid exposing superuser tokens
- Read-only directors and companies registry browse pages with search, filtering, and pagination
- Client onboarding workflow for creating and managing client submissions
- Docker container configuration for easy deployment
- All supporting UI components, utility functions, and configuration files
2026-07-23 19:10:47 +02:00

62 lines
2.5 KiB
TypeScript

// lib/format.ts
// Defensive formatters. All PocketBase text fields may come back as
// null, undefined, or — for some columns historically stored as
// numerics — actual numbers. String() coerce first.
const PLACEHOLDER = "—";
export function text(value: unknown, fallback: string = PLACEHOLDER): string {
if (value === null || value === undefined) return fallback;
const s = String(value).trim();
return s.length === 0 ? fallback : s;
}
export function number(value: unknown, fallback: number = 0): number {
if (value === null || value === undefined || value === "") return fallback;
const n = Number(value);
return Number.isFinite(n) ? n : fallback;
}
export function formatDate(iso: string | null | undefined): string {
if (!iso) return PLACEHOLDER;
const d = new Date(iso);
if (Number.isNaN(d.getTime())) return PLACEHOLDER;
// SA-style dd MMM yyyy (e.g. "08 Jul 2026"). Uses user locale-stable
// English month names to avoid I18n drift in screenshots.
const day = String(d.getDate()).padStart(2, "0");
const month = d.toLocaleString("en-US", { month: "short", timeZone: "UTC" });
const year = d.getUTCFullYear();
return `${day} ${month} ${year}`;
}
export function formatDateTime(iso: string | null | undefined): string {
if (!iso) return PLACEHOLDER;
const d = new Date(iso);
if (Number.isNaN(d.getTime())) return PLACEHOLDER;
const day = String(d.getDate()).padStart(2, "0");
const month = d.toLocaleString("en-US", { month: "short", timeZone: "UTC" });
const year = d.getUTCFullYear();
const hh = String(d.getUTCHours()).padStart(2, "0");
const mm = String(d.getUTCMinutes()).padStart(2, "0");
return `${day} ${month} ${year} ${hh}:${mm}`;
}
/** Map raw `enterprise_status` text into the three buckets we render. */
export function deriveCompanyStatus(
enterpriseStatus: string | null,
isActive: boolean,
): "active" | "inactive" | "in_process" | "deregistered" {
const s = (enterpriseStatus ?? "").toLowerCase();
if (s.includes("deregist")) return "deregistered";
if (s.includes("in process") || s.includes("process")) return "in_process";
if (s.includes("in business") || s.includes("active")) return "active";
if (s.includes("inactive")) return "inactive";
// Fall back to the boolean if text status is ambiguous.
return isActive ? "active" : "inactive";
}
/** Build a PocketBase filter string safely. Escapes single quotes
* by doubling them (PB's own escape convention). */
export function quoteFilter(input: string): string {
return input.replace(/'/g, "''");
}