@marktiderman/genesis-core 1.11.0 → 1.13.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.
Files changed (80) hide show
  1. package/README.md +34 -0
  2. package/dist/chunk-3B4IFDMK.js +117 -0
  3. package/dist/chunk-3B4IFDMK.js.map +1 -0
  4. package/dist/chunk-7FVTGKTX.js +8 -0
  5. package/dist/chunk-7FVTGKTX.js.map +1 -0
  6. package/dist/chunk-AUVKRXRC.js +81 -0
  7. package/dist/chunk-AUVKRXRC.js.map +1 -0
  8. package/dist/chunk-FARKPY6R.js +66 -0
  9. package/dist/chunk-FARKPY6R.js.map +1 -0
  10. package/dist/{chunk-OSPLIKSH.js → chunk-GEF5W2LO.js} +19 -9
  11. package/dist/chunk-GEF5W2LO.js.map +1 -0
  12. package/dist/{chunk-NMFABW4Y.js → chunk-JKZ2FVWK.js} +5 -5
  13. package/dist/{chunk-NMFABW4Y.js.map → chunk-JKZ2FVWK.js.map} +1 -1
  14. package/dist/{chunk-RCAP3Q4T.js → chunk-OYIUIW7C.js} +3 -2
  15. package/dist/chunk-OYIUIW7C.js.map +1 -0
  16. package/dist/{chunk-S2ZXUNXB.js → chunk-REYTMXAA.js} +3 -3
  17. package/dist/{chunk-S2ZXUNXB.js.map → chunk-REYTMXAA.js.map} +1 -1
  18. package/dist/{chunk-BGTXN2E6.js → chunk-RF6YVWHX.js} +4 -4
  19. package/dist/{chunk-BGTXN2E6.js.map → chunk-RF6YVWHX.js.map} +1 -1
  20. package/dist/{chunk-ALOP5MZM.js → chunk-TCL27Q5W.js} +171 -59
  21. package/dist/chunk-TCL27Q5W.js.map +1 -0
  22. package/dist/{chunk-NZPJI7FC.js → chunk-UGGMJUFQ.js} +45 -13
  23. package/dist/chunk-UGGMJUFQ.js.map +1 -0
  24. package/dist/{chunk-KXVHDK3Q.js → chunk-UV3ROHQA.js} +27 -5
  25. package/dist/chunk-UV3ROHQA.js.map +1 -0
  26. package/dist/chunk-YDDPFSQ6.js +35 -0
  27. package/dist/chunk-YDDPFSQ6.js.map +1 -0
  28. package/dist/csv.d.ts +41 -0
  29. package/dist/csv.js +77 -0
  30. package/dist/csv.js.map +1 -0
  31. package/dist/dates.js +1 -115
  32. package/dist/dates.js.map +1 -1
  33. package/dist/hooks/index.d.ts +7 -43
  34. package/dist/hooks/index.js +14 -12
  35. package/dist/hooks/live-cache.d.ts +15 -0
  36. package/dist/hooks/live-cache.js +3 -0
  37. package/dist/hooks/live-cache.js.map +1 -0
  38. package/dist/hooks/mutation-mode.d.ts +2 -2
  39. package/dist/hooks/mutation-mode.js +2 -2
  40. package/dist/hooks/related-keys.d.ts +2 -2
  41. package/dist/hooks/use-capabilities.d.ts +1 -1
  42. package/dist/hooks/use-infinite-resource.d.ts +18 -2
  43. package/dist/hooks/use-infinite-resource.js +4 -3
  44. package/dist/hooks/use-live.d.ts +15 -3
  45. package/dist/hooks/use-live.js +2 -1
  46. package/dist/hooks/use-one.d.ts +2 -2
  47. package/dist/hooks/use-one.js +4 -3
  48. package/dist/hooks/use-resource-form.d.ts +9 -2
  49. package/dist/hooks/use-resource-form.js +7 -6
  50. package/dist/hooks/use-resource.d.ts +2 -2
  51. package/dist/hooks/use-resource.js +5 -4
  52. package/dist/hooks/use-write.d.ts +2 -2
  53. package/dist/index.d.ts +13 -6
  54. package/dist/index.js +19 -15
  55. package/dist/provider/index.d.ts +17 -6
  56. package/dist/provider/index.js +2 -2
  57. package/dist/resource/index.d.ts +86 -0
  58. package/dist/resource/index.js +85 -0
  59. package/dist/resource/index.js.map +1 -0
  60. package/dist/resource-view/index.d.ts +285 -0
  61. package/dist/resource-view/index.js +320 -0
  62. package/dist/resource-view/index.js.map +1 -0
  63. package/dist/schema/index.d.ts +4 -4
  64. package/dist/schema/index.js +1 -0
  65. package/dist/testing/index.d.ts +3 -3
  66. package/dist/{types-B9DDfvWa.d.ts → types-CXhqXPN1.d.ts} +21 -4
  67. package/dist/{types-C7FJtFXV.d.ts → types-o8j7g6O6.d.ts} +38 -1
  68. package/dist/urls.d.ts +18 -0
  69. package/dist/urls.js +3 -0
  70. package/dist/urls.js.map +1 -0
  71. package/dist/viewer-scope-0dR-_o0f.d.ts +76 -0
  72. package/dist/{write-outcome-B2fE29yZ.d.ts → write-outcome-CFnpciUI.d.ts} +1 -1
  73. package/package.json +17 -1
  74. package/dist/chunk-5ORRAXIV.js +0 -30
  75. package/dist/chunk-5ORRAXIV.js.map +0 -1
  76. package/dist/chunk-ALOP5MZM.js.map +0 -1
  77. package/dist/chunk-KXVHDK3Q.js.map +0 -1
  78. package/dist/chunk-NZPJI7FC.js.map +0 -1
  79. package/dist/chunk-OSPLIKSH.js.map +0 -1
  80. package/dist/chunk-RCAP3Q4T.js.map +0 -1
@@ -0,0 +1,85 @@
1
+ // src/resource/status.ts
2
+ var SAVE_STATE_LABEL = {
3
+ saved: "Changes saved",
4
+ saving: "Saving\u2026",
5
+ unsaved: "Unsaved changes",
6
+ failed: "Couldn't save; trying again",
7
+ stopped: "Couldn't save",
8
+ conflict: "Changed elsewhere; reload"
9
+ };
10
+ function readinessOf(findings, options = {}) {
11
+ const { emptyCode, pending } = options;
12
+ let errors = 0;
13
+ let warnings = 0;
14
+ for (const f of findings) {
15
+ if (emptyCode !== void 0 && f.code === emptyCode) return { state: "empty", count: 0 };
16
+ if (f.level === "error") errors++;
17
+ else if (f.level === "warning") warnings++;
18
+ }
19
+ if (errors) return { state: "problems", count: errors };
20
+ if (pending) return { state: "pending", count: 0 };
21
+ if (warnings) return { state: "warnings", count: warnings };
22
+ return { state: "ready", count: 0 };
23
+ }
24
+ function readinessChip(readiness, labels = {}) {
25
+ switch (readiness.state) {
26
+ case "ready":
27
+ return { label: labels.ready ?? "Ready to publish", tone: "success" };
28
+ case "empty":
29
+ return { label: labels.empty ?? "Nothing to publish yet", tone: "muted" };
30
+ case "pending":
31
+ return { label: labels.pending ?? "In progress", tone: "muted" };
32
+ case "problems":
33
+ return {
34
+ label: labels.problems?.(readiness.count) ?? `${readiness.count} ${readiness.count === 1 ? "problem" : "problems"} to fix`,
35
+ tone: "danger"
36
+ };
37
+ case "warnings":
38
+ return { label: labels.warnings?.(readiness.count) ?? `${readiness.count} to check`, tone: "warning" };
39
+ }
40
+ }
41
+
42
+ // src/resource/days.ts
43
+ var MONTH_DAY = new Intl.DateTimeFormat("en-US", { month: "short", day: "numeric", timeZone: "UTC" });
44
+ var MONTH_DAY_YEAR = new Intl.DateTimeFormat("en-US", {
45
+ month: "short",
46
+ day: "numeric",
47
+ year: "numeric",
48
+ timeZone: "UTC"
49
+ });
50
+ function dayLabel(day, today) {
51
+ const t = Date.parse(`${day}T00:00:00Z`);
52
+ if (Number.isNaN(t)) return day;
53
+ if (today) {
54
+ const diff = Math.round((Date.parse(`${today}T00:00:00Z`) - t) / 864e5);
55
+ if (diff === 0) return "Today";
56
+ if (diff === 1) return "Yesterday";
57
+ if (day.slice(0, 4) !== today.slice(0, 4)) return MONTH_DAY_YEAR.format(t);
58
+ }
59
+ return MONTH_DAY.format(t);
60
+ }
61
+
62
+ // src/resource/share.ts
63
+ var RELATIVE_BASE = "http://relative.invalid";
64
+ function prefilledUrl(url, values) {
65
+ let absolute = true;
66
+ let u;
67
+ try {
68
+ u = new URL(url);
69
+ } catch {
70
+ absolute = false;
71
+ u = new URL(url, RELATIVE_BASE);
72
+ }
73
+ for (const [k, v] of Object.entries(values)) if (v) u.searchParams.set(k, v);
74
+ if (absolute) return u.toString();
75
+ const path = url.startsWith("/") ? u.pathname : u.pathname.replace(/^\//, "");
76
+ return `${path}${u.search}${u.hash}`;
77
+ }
78
+ var attr = (s) => s.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
79
+ function embedSnippet(url, title) {
80
+ return `<iframe src="${attr(url)}" title="${attr(title)}" width="100%" height="720" style="border:0;max-width:100%" loading="lazy"></iframe>`;
81
+ }
82
+
83
+ export { SAVE_STATE_LABEL, dayLabel, embedSnippet, prefilledUrl, readinessChip, readinessOf };
84
+ //# sourceMappingURL=index.js.map
85
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/resource/status.ts","../../src/resource/days.ts","../../src/resource/share.ts"],"names":[],"mappings":";AAOO,IAAM,gBAAA,GAA8C;AAAA,EACzD,KAAA,EAAO,eAAA;AAAA,EACP,MAAA,EAAQ,cAAA;AAAA,EACR,OAAA,EAAS,iBAAA;AAAA,EACT,MAAA,EAAQ,6BAAA;AAAA,EACR,OAAA,EAAS,eAAA;AAAA,EACT,QAAA,EAAU;AACZ;AAgCO,SAAS,WAAA,CACd,QAAA,EACA,OAAA,GAA4B,EAAC,EAClB;AACX,EAAA,MAAM,EAAE,SAAA,EAAW,OAAA,EAAQ,GAAI,OAAA;AAC/B,EAAA,IAAI,MAAA,GAAS,CAAA;AACb,EAAA,IAAI,QAAA,GAAW,CAAA;AACf,EAAA,KAAA,MAAW,KAAK,QAAA,EAAU;AACxB,IAAA,IAAI,SAAA,KAAc,MAAA,IAAa,CAAA,CAAE,IAAA,KAAS,SAAA,SAAkB,EAAE,KAAA,EAAO,OAAA,EAAS,KAAA,EAAO,CAAA,EAAE;AACvF,IAAA,IAAI,CAAA,CAAE,UAAU,OAAA,EAAS,MAAA,EAAA;AAAA,SAAA,IAChB,CAAA,CAAE,UAAU,SAAA,EAAW,QAAA,EAAA;AAAA,EAClC;AACA,EAAA,IAAI,QAAQ,OAAO,EAAE,KAAA,EAAO,UAAA,EAAY,OAAO,MAAA,EAAO;AACtD,EAAA,IAAI,SAAS,OAAO,EAAE,KAAA,EAAO,SAAA,EAAW,OAAO,CAAA,EAAE;AACjD,EAAA,IAAI,UAAU,OAAO,EAAE,KAAA,EAAO,UAAA,EAAY,OAAO,QAAA,EAAS;AAC1D,EAAA,OAAO,EAAE,KAAA,EAAO,OAAA,EAAS,KAAA,EAAO,CAAA,EAAE;AACpC;AAiBO,SAAS,aAAA,CACd,SAAA,EACA,MAAA,GAA0B,E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AAU,OAAO,CAAA,CAAE,QAAA,EAAS;AAChC,EAAA,MAAM,IAAA,GAAO,GAAA,CAAI,UAAA,CAAW,GAAG,CAAA,GAAI,CAAA,CAAE,QAAA,GAAW,CAAA,CAAE,QAAA,CAAS,OAAA,CAAQ,KAAA,EAAO,EAAE,CAAA;AAC5E,EAAA,OAAO,GAAG,IAAI,CAAA,EAAG,EAAE,MAAM,CAAA,EAAG,EAAE,IAAI,CAAA,CAAA;AACpC;AAEA,IAAM,OAAO,CAAC,CAAA,KACZ,EAAE,OAAA,CAAQ,IAAA,EAAM,OAAO,CAAA,CAAE,OAAA,CAAQ,IAAA,EAAM,QAAQ,EAAE,OAAA,CAAQ,IAAA,EAAM,MAAM,CAAA,CAAE,OAAA,CAAQ,MAAM,MAAM,CAAA;AAGtF,SAAS,YAAA,CAAa,KAAa,KAAA,EAAuB;AAC/D,EAAA,OAAO,gBAAgB,IAAA,CAAK,GAAG,CAAC,CAAA,SAAA,EAAY,IAAA,CAAK,KAAK,CAAC,CAAA,oFAAA,CAAA;AACzD","file":"index.js","sourcesContent":["// Words for a resource page's chips (save state, readiness). Pure: no React, safe on a\n// server. Not about any one kind of resource: forms, courses, lessons and worksheets use\n// the same page pattern.\n\n/** Where the host's draft saving stands. */\nexport type SaveState = \"saved\" | \"saving\" | \"unsaved\" | \"failed\" | \"stopped\" | \"conflict\";\n\nexport const SAVE_STATE_LABEL: Record<SaveState, string> = {\n saved: \"Changes saved\",\n saving: \"Saving…\",\n unsaved: \"Unsaved changes\",\n failed: \"Couldn't save; trying again\",\n stopped: \"Couldn't save\",\n conflict: \"Changed elsewhere; reload\",\n};\n\n/** A chip's colour: the tinted, AA-safe Genesis badge tones. */\nexport type ChipTone = \"success\" | \"warning\" | \"danger\" | \"info\" | \"muted\";\n\n/** Whether something can be published, from an editor's checks. */\nexport interface Readiness {\n state: \"ready\" | \"problems\" | \"warnings\" | \"empty\" | \"pending\";\n /** Problems or warnings to look at (0 when ready, empty or pending). */\n count: number;\n}\n\n/** One of an editor's findings, as readinessOf reads it. */\nexport interface ReadinessFinding {\n level: \"error\" | \"warning\" | \"info\";\n code?: string | undefined;\n}\n\nexport interface ReadinessOptions {\n /** Errors are held back while something new is being written: \"pending\", not \"problems\". */\n pending?: boolean | undefined;\n /**\n * The finding code that means \"nothing to publish yet\" (for example a form builder's\n * `no-questions`): it makes the result \"empty\", not broken. None by default.\n */\n emptyCode?: string | undefined;\n}\n\n/**\n * Readiness from an editor's findings (any list of `{ level, code? }`): errors block\n * publishing, warnings ask for a look. One pass over the findings.\n */\nexport function readinessOf(\n findings: readonly ReadinessFinding[],\n options: ReadinessOptions = {},\n): Readiness {\n const { emptyCode, pending } = options;\n let errors = 0;\n let warnings = 0;\n for (const f of findings) {\n if (emptyCode !== undefined && f.code === emptyCode) return { state: \"empty\", count: 0 };\n if (f.level === \"error\") errors++;\n else if (f.level === \"warning\") warnings++;\n }\n if (errors) return { state: \"problems\", count: errors };\n if (pending) return { state: \"pending\", count: 0 };\n if (warnings) return { state: \"warnings\", count: warnings };\n return { state: \"ready\", count: 0 };\n}\n\n/** The readiness chip's words, each optional; the defaults are resource-neutral. */\nexport interface ReadinessLabels {\n /** Default \"Ready to publish\". */\n ready?: string | undefined;\n /** When there is nothing yet. Default \"Nothing to publish yet\"; a form says \"Add a question\". */\n empty?: string | undefined;\n /** Default \"In progress\". */\n pending?: string | undefined;\n /** Default \"1 problem to fix\" / \"3 problems to fix\". */\n problems?: ((count: number) => string) | undefined;\n /** Default \"2 to check\". */\n warnings?: ((count: number) => string) | undefined;\n}\n\n/** The readiness chip's label and tone. */\nexport function readinessChip(\n readiness: Readiness,\n labels: ReadinessLabels = {},\n): { label: string; tone: ChipTone } {\n switch (readiness.state) {\n case \"ready\":\n return { label: labels.ready ?? \"Ready to publish\", tone: \"success\" };\n case \"empty\":\n return { label: labels.empty ?? \"Nothing to publish yet\", tone: \"muted\" };\n case \"pending\":\n return { label: labels.pending ?? \"In progress\", tone: \"muted\" };\n case \"problems\":\n return {\n label:\n labels.problems?.(readiness.count) ??\n `${readiness.count} ${readiness.count === 1 ? \"problem\" : \"problems\"} to fix`,\n tone: \"danger\",\n };\n case \"warnings\":\n return { label: labels.warnings?.(readiness.count) ?? `${readiness.count} to check`, tone: \"warning\" };\n }\n}\n","// A day (YYYY-MM-DD) in plain words. Pure.\n\nconst MONTH_DAY = new Intl.DateTimeFormat(\"en-US\", { month: \"short\", day: \"numeric\", timeZone: \"UTC\" });\nconst MONTH_DAY_YEAR = new Intl.DateTimeFormat(\"en-US\", {\n month: \"short\",\n day: \"numeric\",\n year: \"numeric\",\n timeZone: \"UTC\",\n});\n\n/** A day (YYYY-MM-DD) in words, given today on the same clock: \"Today\", \"Yesterday\", \"Oct 21\", \"Oct 21, 2025\". */\nexport function dayLabel(day: string, today?: string): string {\n const t = Date.parse(`${day}T00:00:00Z`);\n if (Number.isNaN(t)) return day;\n if (today) {\n const diff = Math.round((Date.parse(`${today}T00:00:00Z`) - t) / 86_400_000);\n if (diff === 0) return \"Today\";\n if (diff === 1) return \"Yesterday\";\n if (day.slice(0, 4) !== today.slice(0, 4)) return MONTH_DAY_YEAR.format(t);\n }\n return MONTH_DAY.format(t);\n}\n","// Sharing a published page (ShareDialog): prefilled links and the embed snippet. Pure.\n\n/** One search param a prefilled link may set, and the values staff pick from. */\nexport interface PrefillChoice {\n key: string;\n /** The picker's accessible name, e.g. \"Location\". */\n label: string;\n /** The picker's \"leave it out\" choice, e.g. \"Any location\". */\n anyLabel: string;\n options: readonly { value: string; label: string }[];\n}\n\n// Resolves a relative link without inventing an origin for it.\nconst RELATIVE_BASE = \"http://relative.invalid\";\n\n/**\n * `url` with `values` set as search params (empty values left out), keeping what it has.\n * An absolute link stays absolute; a relative one (`/f/a?x=1`) stays relative, so it never\n * throws while a page is still working out its origin.\n */\nexport function prefilledUrl(url: string, values: Record<string, string | undefined>): string {\n let absolute = true;\n let u: URL;\n try {\n u = new URL(url);\n } catch {\n absolute = false;\n u = new URL(url, RELATIVE_BASE);\n }\n for (const [k, v] of Object.entries(values)) if (v) u.searchParams.set(k, v);\n if (absolute) return u.toString();\n const path = url.startsWith(\"/\") ? u.pathname : u.pathname.replace(/^\\//, \"\");\n return `${path}${u.search}${u.hash}`;\n}\n\nconst attr = (s: string) =>\n s.replace(/&/g, \"&amp;\").replace(/\"/g, \"&quot;\").replace(/</g, \"&lt;\").replace(/>/g, \"&gt;\");\n\n/** An `<iframe>` that shows the page on another website, full width. */\nexport function embedSnippet(url: string, title: string): string {\n return `<iframe src=\"${attr(url)}\" title=\"${attr(title)}\" width=\"100%\" height=\"720\" style=\"border:0;max-width:100%\" loading=\"lazy\"></iframe>`;\n}\n"]}
@@ -0,0 +1,285 @@
1
+ import { D as DataRow, e as ResourceContract, F as FieldSchema, i as RowAction, j as RowOf } from '../types-o8j7g6O6.js';
2
+ import { R as ResourceScope } from '../viewer-scope-0dR-_o0f.js';
3
+ import '../form-types.js';
4
+ import 'react';
5
+ import '@tanstack/react-query';
6
+ import '../dates.js';
7
+
8
+ /** A chip's colour: the tinted, AA-safe Genesis badge tones (genesis-ui's `ChipTone`). */
9
+ type ViewTone = "success" | "warning" | "danger" | "info" | "muted";
10
+ /**
11
+ * How a value is shown. A hint: each platform registers one renderer per hint, so the shared view
12
+ * never imports a platform. Derived from the field when the view doesn't say.
13
+ */
14
+ type DisplayHint = "text" | "longText" | "number" | "currency" | "percent" | "boolean" | "badge" | "date" | "datetime" | "relation" | "url" | "email" | "image" | "json";
15
+ /** The field names a contract declares; `string` when the contract isn't a literal. */
16
+ type FieldNameOf<C extends ResourceContract> = C["fields"][number]["name"];
17
+ /** What a view says about one field. Every key is optional; the contract fills the rest. */
18
+ interface ViewField<Row extends DataRow = DataRow> {
19
+ /** The field's label. Default: the contract's label, then the name in title case. */
20
+ label?: string;
21
+ display?: DisplayHint;
22
+ /** A badge's tone per value, e.g. `{ active: "success", archived: "muted" }`. */
23
+ tones?: Readonly<Record<string, ViewTone>>;
24
+ /** Longest text a form accepts. Feeds the derived validation. */
25
+ max?: number;
26
+ /** Shown read-only even where the server would accept a write. Never widens. */
27
+ readOnly?: boolean;
28
+ /**
29
+ * Show the cell only when the row says so: for values a server function masks by policy
30
+ * (`(row) => row.author_visible`). The masking itself is the server's.
31
+ */
32
+ when?: (row: Row) => boolean;
33
+ /** In a form: show the field only while this holds for the form's values. A hidden field isn't saved. */
34
+ visible?: (values: Readonly<Record<string, unknown>>) => boolean;
35
+ /** A timestamp's clock: the viewer's (default), the practice's, or a fixed IANA zone. */
36
+ timeZone?: "viewer" | "practice" | (string & {});
37
+ }
38
+ /** A column computed from the row, never stored or sent. */
39
+ interface ComputedColumn<Row extends DataRow = DataRow> {
40
+ /** The column's key; must not clash with a field name. */
41
+ key: string;
42
+ label: string;
43
+ value: (row: Row) => unknown;
44
+ display?: DisplayHint;
45
+ }
46
+ /** One entry in a column list: a field by name, or a computed column. */
47
+ type ViewColumn<Name extends string, Row extends DataRow> = Name | ComputedColumn<Row>;
48
+ /** A form laid out in order: fields, and fields under a heading. */
49
+ type ViewFormItem<Name extends string> = Name | {
50
+ section: string;
51
+ title?: string;
52
+ fields: readonly Name[];
53
+ };
54
+ /**
55
+ * A named tab of the list: which columns, and which rows (`{ status: "open" }` is an equality
56
+ * filter, sent to the server, never applied to loaded rows).
57
+ */
58
+ interface ViewPreset<Name extends string> {
59
+ label?: string;
60
+ columns?: string;
61
+ filter?: Readonly<Partial<Record<Name, string | number | boolean | null>>>;
62
+ }
63
+ /** `true` when allowed, or the reason it isn't, in one sentence for the disabled control. */
64
+ type Allowed = true | string;
65
+ /**
66
+ * Row rules the server can't cheaply express ("the default brand can't be deleted"). They only
67
+ * narrow: the effective right is the server's AND these. An invariant goes in the database too.
68
+ */
69
+ interface ViewGuards<Row extends DataRow> {
70
+ create?: () => Allowed;
71
+ update?: (row: Row) => Allowed;
72
+ delete?: (row: Row) => Allowed;
73
+ }
74
+ /**
75
+ * A named operation on one row, shaped like core's {@link RowAction} (which a provider reports),
76
+ * plus a guard. The guard only narrows: the server still decides whether the action runs.
77
+ *
78
+ * ```ts
79
+ * actions: [{ name: "archive", label: "Archive", confirm: "Archive this brand?", can: (r) => (r.is_default ? "The default brand stays." : true) }]
80
+ * ```
81
+ */
82
+ interface ViewAction<Row extends DataRow = DataRow> extends RowAction {
83
+ can?: (row: Row) => Allowed;
84
+ }
85
+ /** An action as a renderer lists it: the words only. Ask `view.canAction` whether a row allows it. */
86
+ type ResolvedViewAction = RowAction;
87
+ /** A row in one line, for pickers, notifications and page bars. */
88
+ interface RowSummary {
89
+ title: string;
90
+ subtitle?: string;
91
+ chips?: readonly {
92
+ label: string;
93
+ tone?: ViewTone;
94
+ }[];
95
+ /**
96
+ * A picture beside the row: `src` is shown only when `isSafeUrl` accepts it; otherwise the
97
+ * initials of `name`. `alt` defaults to empty (decorative). Resolve it with `rowPicture`.
98
+ */
99
+ image?: RowImage;
100
+ }
101
+ /** A row's picture, as a view's `summary` gives it. */
102
+ interface RowImage {
103
+ src?: string;
104
+ /** Whose picture: the initials come from it when there is no safe `src`. */
105
+ name?: string;
106
+ /** Alternative text. Default empty: the picture is decorative (the row's title names it). */
107
+ alt?: string;
108
+ }
109
+ /** A row's picture, ready to draw: an image (with initials for when it fails to load), or initials. */
110
+ type RowPicture = {
111
+ readonly kind: "image";
112
+ readonly src: string;
113
+ readonly alt: string;
114
+ readonly fallback: string;
115
+ } | {
116
+ readonly kind: "initials";
117
+ readonly text: string;
118
+ };
119
+ /** The words a view shows. Each defaults from the contract (`noun`, labels). */
120
+ interface ViewCopy {
121
+ /** One row, in a sentence: "brand". */
122
+ noun?: string;
123
+ /** Several: "brands". Default `noun` + "s". */
124
+ nounPlural?: string;
125
+ /** The page title. Default: the contract's label, then the plural noun in title case. */
126
+ title?: string;
127
+ subtitle?: string;
128
+ empty?: {
129
+ title?: string;
130
+ body?: string;
131
+ };
132
+ create?: string;
133
+ }
134
+ /** What `defineView` accepts. */
135
+ interface ViewDefinition<C extends ResourceContract, Row extends DataRow = RowOf<C>> {
136
+ copy?: ViewCopy;
137
+ fields?: {
138
+ readonly [K in FieldNameOf<C>]?: ViewField<Row>;
139
+ };
140
+ /**
141
+ * Column lists by preset name. `default` is the full list; others (`compact`) are what a screen
142
+ * picks. Without `default`: every field that isn't sensitive, generated-only or the key.
143
+ */
144
+ columns?: {
145
+ readonly default?: readonly ViewColumn<FieldNameOf<C>, Row>[];
146
+ } & {
147
+ readonly [preset: string]: readonly ViewColumn<FieldNameOf<C>, Row>[] | undefined;
148
+ };
149
+ /** Tabs: columns plus filters. */
150
+ presets?: Readonly<Record<string, ViewPreset<FieldNameOf<C>>>>;
151
+ /** The record form. Default: every writable field, in contract order. */
152
+ form?: readonly ViewFormItem<FieldNameOf<C>>[];
153
+ can?: ViewGuards<Row>;
154
+ /** Named operations on one row, in the order a menu shows them. Names are unique. */
155
+ actions?: readonly ViewAction<Row>[];
156
+ summary?: (row: Row) => RowSummary;
157
+ /** The scope keys this view needs above it (`["practice_id"]`). They stay out of forms. */
158
+ scope?: readonly FieldNameOf<C>[];
159
+ /** Group rows under headers by this field. */
160
+ groupBy?: FieldNameOf<C>;
161
+ /** Default order; `manual: true` turns on drag to reorder, writing this field. */
162
+ orderBy?: {
163
+ field: FieldNameOf<C>;
164
+ direction?: "asc" | "desc";
165
+ manual?: boolean;
166
+ };
167
+ /** Bumped when stored preferences (saved views, column order) change shape. */
168
+ version?: number;
169
+ }
170
+ /** A column as a renderer draws it: everything resolved. */
171
+ interface ResolvedColumn<Row extends DataRow = DataRow> {
172
+ key: string;
173
+ label: string;
174
+ display: DisplayHint;
175
+ /** The field behind it; absent for a computed column. */
176
+ field?: FieldSchema;
177
+ value: (row: Row) => unknown;
178
+ tones?: Readonly<Record<string, ViewTone>>;
179
+ when?: (row: Row) => boolean;
180
+ timeZone?: ViewField["timeZone"];
181
+ }
182
+ /** A form field as a renderer draws it. */
183
+ interface ResolvedFormField {
184
+ field: FieldSchema;
185
+ label: string;
186
+ display: DisplayHint;
187
+ required: boolean;
188
+ readOnly: boolean;
189
+ max?: number;
190
+ /** The heading it sits under, or null for none. */
191
+ section: string | null;
192
+ sectionTitle: string | null;
193
+ visible?: (values: Readonly<Record<string, unknown>>) => boolean;
194
+ }
195
+ /** What `defineView` returns: frozen, with everything the renderers need resolved. */
196
+ interface ResourceView<C extends ResourceContract = ResourceContract, Row extends DataRow = RowOf<C>> {
197
+ readonly contract: C;
198
+ readonly definition: Readonly<ViewDefinition<C, Row>>;
199
+ /** The resource name the provider addresses. */
200
+ readonly resource: string;
201
+ readonly noun: string;
202
+ readonly nounPlural: string;
203
+ readonly title: string;
204
+ /** The field that names a row, for links, pickers and the peek's title. */
205
+ readonly labelField: string | undefined;
206
+ /** The column presets this view has, `default` first. */
207
+ readonly presets: readonly string[];
208
+ field(name: string): FieldSchema | undefined;
209
+ label(name: string): string;
210
+ display(name: string): DisplayHint;
211
+ columns(preset?: string): readonly ResolvedColumn<Row>[];
212
+ form(mode: "create" | "edit"): readonly ResolvedFormField[];
213
+ /** Fields a writer may send: the form's, never generated, read-only, key or scope fields. */
214
+ writable(mode: "create" | "edit"): readonly string[];
215
+ can(action: "create"): Allowed;
216
+ can(action: "update" | "delete", row: Row): Allowed;
217
+ /** The view's actions, in order, frozen. */
218
+ actions(): readonly ResolvedViewAction[];
219
+ /**
220
+ * Whether this row allows the named action: `true`, or the guard's reason. Only narrows what the
221
+ * server allows. Throws {@link ViewDefinitionError} for a name the view doesn't define.
222
+ */
223
+ canAction(name: string, row: Row): Allowed;
224
+ summary(row: Row): RowSummary;
225
+ }
226
+
227
+ /** A view that names a field, preset or section its contract doesn't have. */
228
+ declare class ViewDefinitionError extends Error {
229
+ readonly problems: readonly string[];
230
+ constructor(resource: string, problems: readonly string[]);
231
+ }
232
+ /** The display a field gets when the view doesn't name one. */
233
+ declare function deriveDisplay(field: FieldSchema): DisplayHint;
234
+ /**
235
+ * Describe what a resource means to people. The result is frozen and platform-neutral:
236
+ *
237
+ * ```ts
238
+ * export const brandsView = defineView(contracts["public.brands"], {
239
+ * fields: { status: { display: "badge", tones: { active: "success" } } },
240
+ * columns: { default: ["name", "status", "created_at"], compact: ["name"] },
241
+ * can: { delete: (row) => (row.is_default ? "The default brand stays." : true) },
242
+ * actions: [{ name: "archive", label: "Archive", can: (row) => (row.is_default ? "The default brand stays." : true) }],
243
+ * });
244
+ * ```
245
+ *
246
+ * Throws {@link ViewDefinitionError} when it names something the contract doesn't have, so a
247
+ * renamed column fails at start-up, not as a blank cell.
248
+ */
249
+ declare function defineView<C extends ResourceContract, Row extends DataRow = RowOf<C>>(contract: C, definition?: ViewDefinition<C, Row>): ResourceView<C, Row>;
250
+
251
+ /**
252
+ * First and last initial of a name, from letters only: "Ada Lovelace" → "AL", "Ana-María López"
253
+ * → "AL", "李 小龙" → "李小", "Cher" → "C". Empty when the name has no letters (" ", "🎉🎉").
254
+ */
255
+ declare function initialsOf(name: string | undefined): string;
256
+ /**
257
+ * What to draw for a summary's picture:
258
+ *
259
+ * ```ts
260
+ * rowPicture({ title: "Ada", image: { src: "https://…/ada.png", name: "Ada Lovelace" } });
261
+ * // { kind: "image", src: "https://…/ada.png", alt: "", fallback: "AL" }
262
+ * rowPicture({ title: "Ada", image: { src: "javascript:x", name: "Ada Lovelace" } });
263
+ * // { kind: "initials", text: "AL" }
264
+ * ```
265
+ *
266
+ * `null` when the summary has no `image`, or neither a safe `src` nor a `name` with letters.
267
+ */
268
+ declare function rowPicture(summary: RowSummary): RowPicture | null;
269
+
270
+ type ScopeValue = string | number | boolean | null;
271
+ /**
272
+ * The view's declared scope keys with their values from the viewer's scope, ready to pass as a
273
+ * screen's `filter`: `scope: ["practice_id"]` reads `scope.practice_id`, then `scope.practiceId`.
274
+ *
275
+ * ```tsx
276
+ * <ResourceView view={brandsView} filter={scopeValues(brandsView, useViewerScope().scope)} />
277
+ * ```
278
+ *
279
+ * In development a declared key with no single value (missing, `undefined`, or a list) throws
280
+ * {@link ViewDefinitionError}, so a screen mounted outside its scope fails loudly. In production
281
+ * it is left out; row-level security still decides what the viewer reads.
282
+ */
283
+ declare function scopeValues(view: Pick<ResourceView<any, any>, "resource" | "definition">, scope: ResourceScope | null | undefined): Readonly<Record<string, ScopeValue>>;
284
+
285
+ export { type Allowed, type ComputedColumn, type DisplayHint, type FieldNameOf, type ResolvedColumn, type ResolvedFormField, type ResolvedViewAction, type ResourceView, type RowImage, RowOf, type RowPicture, type RowSummary, type ViewAction, type ViewColumn, type ViewCopy, type ViewDefinition, ViewDefinitionError, type ViewField, type ViewFormItem, type ViewGuards, type ViewPreset, type ViewTone, defineView, deriveDisplay, initialsOf, rowPicture, scopeValues };