@aiaiai-pt/design-system 0.35.0 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host-agnostic display transforms (#499 criterion A) — the pure value→text +
|
|
3
|
+
* status-badge + geo helpers the list/detail surfaces of BOTH hosts (the
|
|
4
|
+
* citizen portal AND the staff admin) share. Previously duplicated: the portal
|
|
5
|
+
* kept them in `portal/src/lib/renderer/{cells,columns}.ts`; the admin re-rolled
|
|
6
|
+
* equivalents inline (`renderCell`/`formatValue`). Lifting them here makes the
|
|
7
|
+
* value-redaction boundary one tested function instead of two drifting ones.
|
|
8
|
+
*
|
|
9
|
+
* Two host forks are parameters, not separate copies (the reconciliation #499
|
|
10
|
+
* called out before sharing):
|
|
11
|
+
* - object fallback: the portal REDACTS an unknown object (never leak a blob
|
|
12
|
+
* into the public DOM — a hard rule); the admin JSON-dumps it for operators.
|
|
13
|
+
* → `formatScalar(value, { objectFallback: "redact" | "json" })`, default
|
|
14
|
+
* "redact" (the safe default; a caller opts INTO dumping).
|
|
15
|
+
* - date detection: the portal detects timestamps by VALUE shape (the public
|
|
16
|
+
* schema strips created_at/updated_at, so type-driven detection misses
|
|
17
|
+
* them); the admin knows the field is a datetime from its schema TYPE.
|
|
18
|
+
* → `displayCell(value, locale, { treatAsDate })` — `treatAsDate` forces
|
|
19
|
+
* date formatting (admin, from schema type); otherwise value-shape
|
|
20
|
+
* detection (`isIsoTimestamp`) applies (portal).
|
|
21
|
+
*
|
|
22
|
+
* Everything here is PURE and dependency-free.
|
|
23
|
+
*/
|
|
24
|
+
export type ObjectFallback = "redact" | "json";
|
|
25
|
+
/**
|
|
26
|
+
* Pure value → display string. Scalars stringify; arrays join their non-empty
|
|
27
|
+
* formatted members; a disclosed relationship object ({id,label}|{name}|{title})
|
|
28
|
+
* renders its human label then its id. An UNKNOWN object renders per
|
|
29
|
+
* `objectFallback`: "redact" (default) → "" (never dumps internals — the public
|
|
30
|
+
* security boundary); "json" → pretty JSON (the admin operator view).
|
|
31
|
+
*/
|
|
32
|
+
export declare function formatScalar(value: unknown, opts?: {
|
|
33
|
+
objectFallback?: ObjectFallback;
|
|
34
|
+
}): string;
|
|
35
|
+
/** True for an ISO-8601 timestamp STRING (not a bare date). Value-shape
|
|
36
|
+
* detection — used where the schema can't tell us the field is a datetime. */
|
|
37
|
+
export declare function isIsoTimestamp(value: unknown): value is string;
|
|
38
|
+
/** Locale date for an ISO timestamp (date-only, UTC-stable so SSR == hydrate);
|
|
39
|
+
* the raw string when unparseable. */
|
|
40
|
+
export declare function formatTimestamp(value: string, locale: string): string;
|
|
41
|
+
/**
|
|
42
|
+
* The single display boundary for list cells / detail values. Formats a value
|
|
43
|
+
* as a locale date when it's a timestamp — either because `opts.treatAsDate`
|
|
44
|
+
* forces it (the admin, from the schema field type) or because the value itself
|
|
45
|
+
* looks like an ISO timestamp (the portal, value-shape). Everything else goes
|
|
46
|
+
* through `formatScalar` (with the host's object-fallback policy).
|
|
47
|
+
*/
|
|
48
|
+
export declare function displayCell(value: unknown, locale: string, opts?: {
|
|
49
|
+
treatAsDate?: boolean;
|
|
50
|
+
objectFallback?: ObjectFallback;
|
|
51
|
+
}): string;
|
|
52
|
+
/** Operator copy for a status value (falls back to the raw value). `labels` is
|
|
53
|
+
* untrusted DATA — a non-object/array is ignored. */
|
|
54
|
+
export declare function statusLabel(value: unknown, labels: unknown): string;
|
|
55
|
+
/** DS Badge variant for a status value (neutral when unmapped). `variants` is
|
|
56
|
+
* untrusted DATA — a non-object/array is ignored. */
|
|
57
|
+
export declare function statusVariant(value: unknown, variants: unknown): string;
|
|
58
|
+
/** Resolve a status value to a `{ label, variant }` pair in one call. */
|
|
59
|
+
export declare function resolveStatusBadge(value: unknown, opts?: {
|
|
60
|
+
labels?: unknown;
|
|
61
|
+
variants?: unknown;
|
|
62
|
+
}): {
|
|
63
|
+
label: string;
|
|
64
|
+
variant: string;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* Extract a `[lon, lat]` pair from a GeoJSON-ish Point value (the shape both a
|
|
68
|
+
* detail row's location FK and a MapPicker value use), or null when it isn't a
|
|
69
|
+
* usable point. Pure — the map widget decides how to render the result.
|
|
70
|
+
*/
|
|
71
|
+
export declare function geoPoint(value: unknown): [number, number] | null;
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host-agnostic display transforms (#499 criterion A) — the pure value→text +
|
|
3
|
+
* status-badge + geo helpers the list/detail surfaces of BOTH hosts (the
|
|
4
|
+
* citizen portal AND the staff admin) share. Previously duplicated: the portal
|
|
5
|
+
* kept them in `portal/src/lib/renderer/{cells,columns}.ts`; the admin re-rolled
|
|
6
|
+
* equivalents inline (`renderCell`/`formatValue`). Lifting them here makes the
|
|
7
|
+
* value-redaction boundary one tested function instead of two drifting ones.
|
|
8
|
+
*
|
|
9
|
+
* Two host forks are parameters, not separate copies (the reconciliation #499
|
|
10
|
+
* called out before sharing):
|
|
11
|
+
* - object fallback: the portal REDACTS an unknown object (never leak a blob
|
|
12
|
+
* into the public DOM — a hard rule); the admin JSON-dumps it for operators.
|
|
13
|
+
* → `formatScalar(value, { objectFallback: "redact" | "json" })`, default
|
|
14
|
+
* "redact" (the safe default; a caller opts INTO dumping).
|
|
15
|
+
* - date detection: the portal detects timestamps by VALUE shape (the public
|
|
16
|
+
* schema strips created_at/updated_at, so type-driven detection misses
|
|
17
|
+
* them); the admin knows the field is a datetime from its schema TYPE.
|
|
18
|
+
* → `displayCell(value, locale, { treatAsDate })` — `treatAsDate` forces
|
|
19
|
+
* date formatting (admin, from schema type); otherwise value-shape
|
|
20
|
+
* detection (`isIsoTimestamp`) applies (portal).
|
|
21
|
+
*
|
|
22
|
+
* Everything here is PURE and dependency-free.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export type ObjectFallback = "redact" | "json";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Pure value → display string. Scalars stringify; arrays join their non-empty
|
|
29
|
+
* formatted members; a disclosed relationship object ({id,label}|{name}|{title})
|
|
30
|
+
* renders its human label then its id. An UNKNOWN object renders per
|
|
31
|
+
* `objectFallback`: "redact" (default) → "" (never dumps internals — the public
|
|
32
|
+
* security boundary); "json" → pretty JSON (the admin operator view).
|
|
33
|
+
*/
|
|
34
|
+
export function formatScalar(
|
|
35
|
+
value: unknown,
|
|
36
|
+
opts: { objectFallback?: ObjectFallback } = {},
|
|
37
|
+
): string {
|
|
38
|
+
// `== null` catches both null and undefined.
|
|
39
|
+
if (value == null) return "";
|
|
40
|
+
if (typeof value === "string") return value;
|
|
41
|
+
if (typeof value === "number" || typeof value === "boolean") {
|
|
42
|
+
return String(value);
|
|
43
|
+
}
|
|
44
|
+
if (Array.isArray(value)) {
|
|
45
|
+
return value
|
|
46
|
+
.map((v) => formatScalar(v, opts))
|
|
47
|
+
.filter(Boolean)
|
|
48
|
+
.join(", ");
|
|
49
|
+
}
|
|
50
|
+
if (typeof value === "object") {
|
|
51
|
+
const o = value as Record<string, unknown>;
|
|
52
|
+
for (const k of ["label", "name", "title"]) {
|
|
53
|
+
const v = o[k];
|
|
54
|
+
if (typeof v === "string" && v) return v;
|
|
55
|
+
}
|
|
56
|
+
const id = o.id;
|
|
57
|
+
if (typeof id === "string" || typeof id === "number") return String(id);
|
|
58
|
+
// No human handle and no id → fall back per host policy.
|
|
59
|
+
if (opts.objectFallback === "json") {
|
|
60
|
+
try {
|
|
61
|
+
return JSON.stringify(value, null, 2);
|
|
62
|
+
} catch {
|
|
63
|
+
return "";
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return "";
|
|
67
|
+
}
|
|
68
|
+
return "";
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const ISO_TIMESTAMP_RE =
|
|
72
|
+
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
73
|
+
|
|
74
|
+
/** True for an ISO-8601 timestamp STRING (not a bare date). Value-shape
|
|
75
|
+
* detection — used where the schema can't tell us the field is a datetime. */
|
|
76
|
+
export function isIsoTimestamp(value: unknown): value is string {
|
|
77
|
+
return typeof value === "string" && ISO_TIMESTAMP_RE.test(value);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Locale date for an ISO timestamp (date-only, UTC-stable so SSR == hydrate);
|
|
81
|
+
* the raw string when unparseable. */
|
|
82
|
+
export function formatTimestamp(value: string, locale: string): string {
|
|
83
|
+
const parsed = new Date(value);
|
|
84
|
+
if (Number.isNaN(parsed.getTime())) return value;
|
|
85
|
+
try {
|
|
86
|
+
return new Intl.DateTimeFormat(locale || "en", {
|
|
87
|
+
dateStyle: "medium",
|
|
88
|
+
timeZone: "UTC",
|
|
89
|
+
}).format(parsed);
|
|
90
|
+
} catch {
|
|
91
|
+
return value;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The single display boundary for list cells / detail values. Formats a value
|
|
97
|
+
* as a locale date when it's a timestamp — either because `opts.treatAsDate`
|
|
98
|
+
* forces it (the admin, from the schema field type) or because the value itself
|
|
99
|
+
* looks like an ISO timestamp (the portal, value-shape). Everything else goes
|
|
100
|
+
* through `formatScalar` (with the host's object-fallback policy).
|
|
101
|
+
*/
|
|
102
|
+
export function displayCell(
|
|
103
|
+
value: unknown,
|
|
104
|
+
locale: string,
|
|
105
|
+
opts: { treatAsDate?: boolean; objectFallback?: ObjectFallback } = {},
|
|
106
|
+
): string {
|
|
107
|
+
if (opts.treatAsDate && typeof value === "string" && value) {
|
|
108
|
+
return formatTimestamp(value, locale);
|
|
109
|
+
}
|
|
110
|
+
if (isIsoTimestamp(value)) return formatTimestamp(value, locale);
|
|
111
|
+
return formatScalar(value, { objectFallback: opts.objectFallback });
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Operator copy for a status value (falls back to the raw value). `labels` is
|
|
115
|
+
* untrusted DATA — a non-object/array is ignored. */
|
|
116
|
+
export function statusLabel(value: unknown, labels: unknown): string {
|
|
117
|
+
const raw = formatScalar(value);
|
|
118
|
+
if (labels && typeof labels === "object" && !Array.isArray(labels)) {
|
|
119
|
+
const mapped = (labels as Record<string, unknown>)[raw];
|
|
120
|
+
if (typeof mapped === "string" && mapped) return mapped;
|
|
121
|
+
}
|
|
122
|
+
return raw;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** DS Badge variant for a status value (neutral when unmapped). `variants` is
|
|
126
|
+
* untrusted DATA — a non-object/array is ignored. */
|
|
127
|
+
export function statusVariant(value: unknown, variants: unknown): string {
|
|
128
|
+
const raw = formatScalar(value);
|
|
129
|
+
if (variants && typeof variants === "object" && !Array.isArray(variants)) {
|
|
130
|
+
const mapped = (variants as Record<string, unknown>)[raw];
|
|
131
|
+
if (typeof mapped === "string" && mapped) return mapped;
|
|
132
|
+
}
|
|
133
|
+
return "neutral";
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Resolve a status value to a `{ label, variant }` pair in one call. */
|
|
137
|
+
export function resolveStatusBadge(
|
|
138
|
+
value: unknown,
|
|
139
|
+
opts: { labels?: unknown; variants?: unknown } = {},
|
|
140
|
+
): { label: string; variant: string } {
|
|
141
|
+
return {
|
|
142
|
+
label: statusLabel(value, opts.labels),
|
|
143
|
+
variant: statusVariant(value, opts.variants),
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Extract a `[lon, lat]` pair from a GeoJSON-ish Point value (the shape both a
|
|
149
|
+
* detail row's location FK and a MapPicker value use), or null when it isn't a
|
|
150
|
+
* usable point. Pure — the map widget decides how to render the result.
|
|
151
|
+
*/
|
|
152
|
+
export function geoPoint(value: unknown): [number, number] | null {
|
|
153
|
+
if (!value || typeof value !== "object") return null;
|
|
154
|
+
const coords = (value as { coordinates?: unknown }).coordinates;
|
|
155
|
+
if (!Array.isArray(coords) || coords.length < 2) return null;
|
|
156
|
+
const lon = coords[0];
|
|
157
|
+
const lat = coords[1];
|
|
158
|
+
if (typeof lon !== "number" || typeof lat !== "number") return null;
|
|
159
|
+
if (!Number.isFinite(lon) || !Number.isFinite(lat)) return null;
|
|
160
|
+
return [lon, lat];
|
|
161
|
+
}
|