@marktiderman/genesis-core 1.13.0 → 1.14.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.
- package/README.md +45 -0
- package/dist/{chunk-REYTMXAA.js → chunk-3LZAZ5KB.js} +3 -3
- package/dist/{chunk-REYTMXAA.js.map → chunk-3LZAZ5KB.js.map} +1 -1
- package/dist/{chunk-JKZ2FVWK.js → chunk-7YHGNPPO.js} +4 -4
- package/dist/{chunk-JKZ2FVWK.js.map → chunk-7YHGNPPO.js.map} +1 -1
- package/dist/{chunk-TCL27Q5W.js → chunk-K7K6NY3C.js} +4 -113
- package/dist/chunk-K7K6NY3C.js.map +1 -0
- package/dist/chunk-LFWSTJWI.js +108 -0
- package/dist/chunk-LFWSTJWI.js.map +1 -0
- package/dist/chunk-OZYF55E3.js +6 -0
- package/dist/chunk-OZYF55E3.js.map +1 -0
- package/dist/chunk-PA6WJE5E.js +115 -0
- package/dist/chunk-PA6WJE5E.js.map +1 -0
- package/dist/chunk-PP3XFFQM.js +85 -0
- package/dist/chunk-PP3XFFQM.js.map +1 -0
- package/dist/{chunk-GEF5W2LO.js → chunk-YFTGLQDQ.js} +4 -4
- package/dist/{chunk-GEF5W2LO.js.map → chunk-YFTGLQDQ.js.map} +1 -1
- package/dist/chunk-ZIMQUFXD.js +154 -0
- package/dist/chunk-ZIMQUFXD.js.map +1 -0
- package/dist/errors.d.ts +69 -0
- package/dist/errors.js +7 -0
- package/dist/errors.js.map +1 -0
- package/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/index.js +12 -11
- package/dist/hooks/mutation-mode.js +2 -2
- package/dist/hooks/use-resource-form.js +6 -6
- package/dist/hooks/use-resource.js +4 -4
- package/dist/index.d.ts +2 -1
- package/dist/index.js +18 -15
- package/dist/provider/index.js +4 -3
- package/dist/resource/index.d.ts +2 -49
- package/dist/resource/index.js +1 -83
- package/dist/resource/index.js.map +1 -1
- package/dist/resource-view/index.d.ts +200 -17
- package/dist/resource-view/index.js +234 -12
- package/dist/resource-view/index.js.map +1 -1
- package/dist/status-BXRokZD9.d.ts +50 -0
- package/dist/{viewer-scope-0dR-_o0f.d.ts → viewer-scope-77MqxvmZ.d.ts} +51 -2
- package/package.json +5 -1
- package/dist/chunk-FARKPY6R.js +0 -66
- package/dist/chunk-FARKPY6R.js.map +0 -1
- package/dist/chunk-TCL27Q5W.js.map +0 -1
|
@@ -1,12 +1,15 @@
|
|
|
1
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
|
|
2
|
+
import { C as ChipTone, R as Readiness } from '../status-BXRokZD9.js';
|
|
3
|
+
import { V as Viewer, R as ResourceScope } from '../viewer-scope-77MqxvmZ.js';
|
|
4
|
+
export { G as GenesisViewerRoles, N as NO_VIEWER } from '../viewer-scope-77MqxvmZ.js';
|
|
5
|
+
import { a as Identifier } from '../types-CXhqXPN1.js';
|
|
3
6
|
import '../form-types.js';
|
|
4
7
|
import 'react';
|
|
5
8
|
import '@tanstack/react-query';
|
|
6
9
|
import '../dates.js';
|
|
7
10
|
|
|
8
|
-
/** A chip's colour: the
|
|
9
|
-
type ViewTone =
|
|
11
|
+
/** A chip's colour: the one Genesis tone set (`ChipTone`, genesis-core/resource). */
|
|
12
|
+
type ViewTone = ChipTone;
|
|
10
13
|
/**
|
|
11
14
|
* How a value is shown. A hint: each platform registers one renderer per hint, so the shared view
|
|
12
15
|
* never imports a platform. Derived from the field when the view doesn't say.
|
|
@@ -26,10 +29,12 @@ interface ViewField<Row extends DataRow = DataRow> {
|
|
|
26
29
|
/** Shown read-only even where the server would accept a write. Never widens. */
|
|
27
30
|
readOnly?: boolean;
|
|
28
31
|
/**
|
|
29
|
-
* Show the cell only when the row
|
|
30
|
-
* (`(row) => row.author_visible`). The masking itself is the server's.
|
|
32
|
+
* Show the cell only when the row (and the viewer) say so: for values a server function masks
|
|
33
|
+
* by policy (`(row) => row.author_visible`). The masking itself is the server's. It hides
|
|
34
|
+
* table cells and the peek only: not workspace settings, an edit form's prefilled values, a
|
|
35
|
+
* view's `summary` or header, or sorting and searching on the column.
|
|
31
36
|
*/
|
|
32
|
-
when?: (row: Row) => boolean;
|
|
37
|
+
when?: (row: Row, viewer: Viewer) => boolean;
|
|
33
38
|
/** In a form: show the field only while this holds for the form's values. A hidden field isn't saved. */
|
|
34
39
|
visible?: (values: Readonly<Record<string, unknown>>) => boolean;
|
|
35
40
|
/** A timestamp's clock: the viewer's (default), the practice's, or a fixed IANA zone. */
|
|
@@ -65,11 +70,15 @@ type Allowed = true | string;
|
|
|
65
70
|
/**
|
|
66
71
|
* Row rules the server can't cheaply express ("the default brand can't be deleted"). They only
|
|
67
72
|
* narrow: the effective right is the server's AND these. An invariant goes in the database too.
|
|
73
|
+
*
|
|
74
|
+
* Each gets the viewer (`useViewer()`: id, scope, roles). Outside a ViewerScope it is
|
|
75
|
+
* `{ id: null, scope: {} }`, so a guard that needs it refuses with its reason. Pure and
|
|
76
|
+
* synchronous; never a security check (row-level security is the boundary).
|
|
68
77
|
*/
|
|
69
78
|
interface ViewGuards<Row extends DataRow> {
|
|
70
|
-
create?: () => Allowed;
|
|
71
|
-
update?: (row: Row) => Allowed;
|
|
72
|
-
delete?: (row: Row) => Allowed;
|
|
79
|
+
create?: (viewer: Viewer) => Allowed;
|
|
80
|
+
update?: (row: Row, viewer: Viewer) => Allowed;
|
|
81
|
+
delete?: (row: Row, viewer: Viewer) => Allowed;
|
|
73
82
|
}
|
|
74
83
|
/**
|
|
75
84
|
* A named operation on one row, shaped like core's {@link RowAction} (which a provider reports),
|
|
@@ -80,10 +89,14 @@ interface ViewGuards<Row extends DataRow> {
|
|
|
80
89
|
* ```
|
|
81
90
|
*/
|
|
82
91
|
interface ViewAction<Row extends DataRow = DataRow> extends RowAction {
|
|
83
|
-
|
|
92
|
+
/** Drawn as destructive (red, the menu's destructive style). */
|
|
93
|
+
destructive?: boolean;
|
|
94
|
+
can?: (row: Row, viewer: Viewer) => Allowed;
|
|
84
95
|
}
|
|
85
96
|
/** An action as a renderer lists it: the words only. Ask `view.canAction` whether a row allows it. */
|
|
86
|
-
type ResolvedViewAction = RowAction
|
|
97
|
+
type ResolvedViewAction = RowAction & {
|
|
98
|
+
readonly destructive?: boolean;
|
|
99
|
+
};
|
|
87
100
|
/** A row in one line, for pickers, notifications and page bars. */
|
|
88
101
|
interface RowSummary {
|
|
89
102
|
title: string;
|
|
@@ -176,7 +189,8 @@ interface ResolvedColumn<Row extends DataRow = DataRow> {
|
|
|
176
189
|
field?: FieldSchema;
|
|
177
190
|
value: (row: Row) => unknown;
|
|
178
191
|
tones?: Readonly<Record<string, ViewTone>>;
|
|
179
|
-
|
|
192
|
+
/** Whether to show the cell. Without a viewer it answers for no one (fail closed). */
|
|
193
|
+
when?: (row: Row, viewer?: Viewer) => boolean;
|
|
180
194
|
timeZone?: ViewField["timeZone"];
|
|
181
195
|
}
|
|
182
196
|
/** A form field as a renderer draws it. */
|
|
@@ -212,15 +226,19 @@ interface ResourceView<C extends ResourceContract = ResourceContract, Row extend
|
|
|
212
226
|
form(mode: "create" | "edit"): readonly ResolvedFormField[];
|
|
213
227
|
/** Fields a writer may send: the form's, never generated, read-only, key or scope fields. */
|
|
214
228
|
writable(mode: "create" | "edit"): readonly string[];
|
|
215
|
-
|
|
216
|
-
|
|
229
|
+
/**
|
|
230
|
+
* Whether the view's guard allows it for this viewer: `true`, or the guard's reason. Without a
|
|
231
|
+
* viewer, guards get `NO_VIEWER` (no one), so one that needs the viewer refuses.
|
|
232
|
+
*/
|
|
233
|
+
can(action: "create", viewer?: Viewer): Allowed;
|
|
234
|
+
can(action: "update" | "delete", row: Row, viewer?: Viewer): Allowed;
|
|
217
235
|
/** The view's actions, in order, frozen. */
|
|
218
236
|
actions(): readonly ResolvedViewAction[];
|
|
219
237
|
/**
|
|
220
238
|
* Whether this row allows the named action: `true`, or the guard's reason. Only narrows what the
|
|
221
239
|
* server allows. Throws {@link ViewDefinitionError} for a name the view doesn't define.
|
|
222
240
|
*/
|
|
223
|
-
canAction(name: string, row: Row): Allowed;
|
|
241
|
+
canAction(name: string, row: Row, viewer?: Viewer): Allowed;
|
|
224
242
|
summary(row: Row): RowSummary;
|
|
225
243
|
}
|
|
226
244
|
|
|
@@ -238,7 +256,10 @@ declare function deriveDisplay(field: FieldSchema): DisplayHint;
|
|
|
238
256
|
* export const brandsView = defineView(contracts["public.brands"], {
|
|
239
257
|
* fields: { status: { display: "badge", tones: { active: "success" } } },
|
|
240
258
|
* columns: { default: ["name", "status", "created_at"], compact: ["name"] },
|
|
241
|
-
* can: {
|
|
259
|
+
* can: {
|
|
260
|
+
* delete: (row) => (row.is_default ? "The default brand stays." : true),
|
|
261
|
+
* update: (row, viewer) => (row.owner_id === viewer.id ? true : "Only its owner edits a brand."),
|
|
262
|
+
* },
|
|
242
263
|
* actions: [{ name: "archive", label: "Archive", can: (row) => (row.is_default ? "The default brand stays." : true) }],
|
|
243
264
|
* });
|
|
244
265
|
* ```
|
|
@@ -282,4 +303,166 @@ type ScopeValue = string | number | boolean | null;
|
|
|
282
303
|
*/
|
|
283
304
|
declare function scopeValues(view: Pick<ResourceView<any, any>, "resource" | "definition">, scope: ResourceScope | null | undefined): Readonly<Record<string, ScopeValue>>;
|
|
284
305
|
|
|
285
|
-
|
|
306
|
+
type AnyView = ResourceView<ResourceContract<any>, any>;
|
|
307
|
+
/** Equality filters for a child list, sent to the server: `{ brand_id: brand.id }`. `null` is "is empty". */
|
|
308
|
+
type WorkspaceFilter = Readonly<Record<string, string | number | boolean | null>>;
|
|
309
|
+
interface SectionBase {
|
|
310
|
+
/** Unique in the workspace; the screen's route segment or slot key. */
|
|
311
|
+
id: string;
|
|
312
|
+
label: string;
|
|
313
|
+
}
|
|
314
|
+
/** A child list: another view, filtered by this record. */
|
|
315
|
+
interface WorkspaceListSection<Row extends DataRow = DataRow> extends SectionBase {
|
|
316
|
+
list: {
|
|
317
|
+
view: AnyView;
|
|
318
|
+
filter: (row: Row) => WorkspaceFilter;
|
|
319
|
+
};
|
|
320
|
+
settings?: never;
|
|
321
|
+
}
|
|
322
|
+
/** A few of this record's own fields, edited in place. Each must be in `view.writable("edit")`. */
|
|
323
|
+
interface WorkspaceSettingsSection<Name extends string = string> extends SectionBase {
|
|
324
|
+
settings: readonly Name[];
|
|
325
|
+
list?: never;
|
|
326
|
+
}
|
|
327
|
+
/** A section the screen fills itself, through a slot keyed by `id`. */
|
|
328
|
+
interface WorkspaceSlotSection extends SectionBase {
|
|
329
|
+
list?: never;
|
|
330
|
+
settings?: never;
|
|
331
|
+
}
|
|
332
|
+
type WorkspaceSection<Row extends DataRow = DataRow, Name extends string = string> = WorkspaceListSection<Row> | WorkspaceSettingsSection<Name> | WorkspaceSlotSection;
|
|
333
|
+
/** One readiness check: "Has a logo", ok or not, and why not. */
|
|
334
|
+
interface WorkspaceReadinessItem {
|
|
335
|
+
/** A stable key for the check; default its position and title. */
|
|
336
|
+
id?: string;
|
|
337
|
+
title: string;
|
|
338
|
+
ok: boolean;
|
|
339
|
+
reason?: string;
|
|
340
|
+
/** Genesis chip tones only. Default `success` when ok, `warning` when not. */
|
|
341
|
+
tone?: ViewTone;
|
|
342
|
+
}
|
|
343
|
+
/** What `defineWorkspace` accepts. */
|
|
344
|
+
interface WorkspaceDefinition<C extends ResourceContract, Row extends DataRow = RowOf<C>> {
|
|
345
|
+
/** The view actions the header offers, by name, in order. Default: every view action. */
|
|
346
|
+
header?: {
|
|
347
|
+
actions?: readonly string[];
|
|
348
|
+
};
|
|
349
|
+
sections: readonly WorkspaceSection<Row, FieldNameOf<C>>[];
|
|
350
|
+
readiness?: (row: Row) => readonly WorkspaceReadinessItem[];
|
|
351
|
+
}
|
|
352
|
+
/** A section as a renderer draws it. */
|
|
353
|
+
type ResolvedWorkspaceSection = {
|
|
354
|
+
readonly id: string;
|
|
355
|
+
readonly label: string;
|
|
356
|
+
readonly kind: "list";
|
|
357
|
+
readonly view: AnyView;
|
|
358
|
+
} | {
|
|
359
|
+
readonly id: string;
|
|
360
|
+
readonly label: string;
|
|
361
|
+
readonly kind: "settings";
|
|
362
|
+
readonly settings: readonly string[];
|
|
363
|
+
} | {
|
|
364
|
+
readonly id: string;
|
|
365
|
+
readonly label: string;
|
|
366
|
+
readonly kind: "slot";
|
|
367
|
+
};
|
|
368
|
+
/** A header action with whether this row allows it. */
|
|
369
|
+
interface WorkspaceHeaderAction {
|
|
370
|
+
readonly name: string;
|
|
371
|
+
readonly label: string;
|
|
372
|
+
readonly confirm?: string;
|
|
373
|
+
readonly description?: string;
|
|
374
|
+
readonly destructive?: boolean;
|
|
375
|
+
/** `true`, or the view guard's reason. Only narrows what the server allows. */
|
|
376
|
+
readonly allowed: Allowed;
|
|
377
|
+
}
|
|
378
|
+
/** The header for one row: the view's summary plus the header's actions. */
|
|
379
|
+
interface WorkspaceHeader {
|
|
380
|
+
readonly title: string;
|
|
381
|
+
readonly subtitle?: string;
|
|
382
|
+
readonly chips: readonly {
|
|
383
|
+
label: string;
|
|
384
|
+
tone?: ViewTone;
|
|
385
|
+
}[];
|
|
386
|
+
readonly actions: readonly WorkspaceHeaderAction[];
|
|
387
|
+
}
|
|
388
|
+
/** What a screen and its provider add to the header: they only narrow the view's guards. */
|
|
389
|
+
interface WorkspaceHeaderOptions<Row extends DataRow = DataRow> {
|
|
390
|
+
/** The screen's own rule: `false` hides the action, a sentence refuses it with that reason. */
|
|
391
|
+
can?: (action: string, row: Row) => Allowed | boolean;
|
|
392
|
+
/** Whether anything can run actions here (the screen's `onAction`, or the provider's `runAction`). `false` hides them all. */
|
|
393
|
+
runnable?: boolean;
|
|
394
|
+
/** The action names the provider reports serving (`ResourceSchema.actions`), when it reports them. Others are hidden. */
|
|
395
|
+
served?: readonly string[];
|
|
396
|
+
/** Who is looking (`useViewer()`), for the view's action guards. Without it they get no one. */
|
|
397
|
+
viewer?: Viewer;
|
|
398
|
+
}
|
|
399
|
+
/** A readiness check with its tone resolved. */
|
|
400
|
+
interface ResolvedReadinessItem {
|
|
401
|
+
/** Stable, for list keys: the check's `id`, or its position and title. */
|
|
402
|
+
readonly key: string;
|
|
403
|
+
readonly title: string;
|
|
404
|
+
readonly ok: boolean;
|
|
405
|
+
readonly reason?: string;
|
|
406
|
+
readonly tone: ViewTone;
|
|
407
|
+
}
|
|
408
|
+
/** What `defineWorkspace` returns: frozen, with pure helpers for the renderers. */
|
|
409
|
+
interface ResourceWorkspace<C extends ResourceContract = ResourceContract, Row extends DataRow = RowOf<C>> {
|
|
410
|
+
readonly view: ResourceView<C, Row>;
|
|
411
|
+
readonly sections: readonly ResolvedWorkspaceSection[];
|
|
412
|
+
/** The header's action names, in order. */
|
|
413
|
+
readonly headerActions: readonly string[];
|
|
414
|
+
readonly hasReadiness: boolean;
|
|
415
|
+
section(id: string): ResolvedWorkspaceSection | undefined;
|
|
416
|
+
/** The record's address for the provider: its key fields (`keyFields`), composite keys encoded. */
|
|
417
|
+
recordId(row: Row): Identifier;
|
|
418
|
+
/**
|
|
419
|
+
* Why settings can't be saved for this row and viewer: the view's `can("update", row, viewer)`
|
|
420
|
+
* reason, or `null` when they can. Renderers then show the settings read-only with the reason.
|
|
421
|
+
* Without a viewer the guard gets no one, so one that needs the viewer locks them.
|
|
422
|
+
*/
|
|
423
|
+
settingsLocked(row: Row, viewer?: Viewer): string | null;
|
|
424
|
+
headerFor(row: Row, options?: WorkspaceHeaderOptions<Row>): WorkspaceHeader;
|
|
425
|
+
/**
|
|
426
|
+
* Settings fields as the view's form resolves them (`view.form("edit")`), in the order listed:
|
|
427
|
+
* one section's, or every settings section's without an id.
|
|
428
|
+
*/
|
|
429
|
+
settingsFields(sectionId?: string): readonly ResolvedFormField[];
|
|
430
|
+
/** Only the section's listed keys that `values` has: what a save sends. */
|
|
431
|
+
settingsPatch(sectionId: string, values: Readonly<Record<string, unknown>>): Record<string, unknown>;
|
|
432
|
+
/**
|
|
433
|
+
* A list section's filter for this row. In development, a filter with no key, or a value that
|
|
434
|
+
* isn't a string, number, boolean or null, throws: it would show every child row.
|
|
435
|
+
*/
|
|
436
|
+
listFilter(sectionId: string, row: Row): WorkspaceFilter;
|
|
437
|
+
/** The readiness checks for this row, tones resolved; empty without `readiness`. */
|
|
438
|
+
readinessFor(row: Row): readonly ResolvedReadinessItem[];
|
|
439
|
+
/**
|
|
440
|
+
* The checks in one chip's terms (genesis-core/resource's `readinessOf`): an unmet `danger`
|
|
441
|
+
* check is a problem, any other unmet check one to look at. `null` without `readiness`. Draw
|
|
442
|
+
* it with `readinessChip`.
|
|
443
|
+
*/
|
|
444
|
+
readinessSummary(row: Row): Readiness | null;
|
|
445
|
+
}
|
|
446
|
+
/** Whether a value is a workspace (`defineWorkspace`'s result). */
|
|
447
|
+
declare function isWorkspace(value: unknown): value is ResourceWorkspace;
|
|
448
|
+
/**
|
|
449
|
+
* One record's screen from its view:
|
|
450
|
+
*
|
|
451
|
+
* ```ts
|
|
452
|
+
* export const brandWorkspace = defineWorkspace(brandsView, {
|
|
453
|
+
* header: { actions: ["archive"] },
|
|
454
|
+
* sections: [
|
|
455
|
+
* { id: "products", label: "Products", list: { view: productsView, filter: (b) => ({ brand_id: b.id }) } },
|
|
456
|
+
* { id: "settings", label: "Settings", settings: ["name", "website"] },
|
|
457
|
+
* { id: "activity", label: "Activity" }, // the screen fills it
|
|
458
|
+
* ],
|
|
459
|
+
* readiness: (b) => [{ title: "Has a website", ok: !!b.website, reason: "Add the website." }],
|
|
460
|
+
* });
|
|
461
|
+
* ```
|
|
462
|
+
*
|
|
463
|
+
* Throws {@link ViewDefinitionError} for a settings field the view can't write, a header action
|
|
464
|
+
* the view doesn't define, a repeated section id, or a section that holds another workspace.
|
|
465
|
+
*/
|
|
466
|
+
declare function defineWorkspace<C extends ResourceContract, Row extends DataRow = RowOf<C>>(view: ResourceView<C, Row>, definition: WorkspaceDefinition<C, Row>): ResourceWorkspace<C, Row>;
|
|
467
|
+
|
|
468
|
+
export { type Allowed, type ComputedColumn, type DisplayHint, type FieldNameOf, type ResolvedColumn, type ResolvedFormField, type ResolvedReadinessItem, type ResolvedViewAction, type ResolvedWorkspaceSection, type ResourceView, type ResourceWorkspace, 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, Viewer, type WorkspaceDefinition, type WorkspaceFilter, type WorkspaceHeader, type WorkspaceHeaderAction, type WorkspaceHeaderOptions, type WorkspaceListSection, type WorkspaceReadinessItem, type WorkspaceSection, type WorkspaceSettingsSection, type WorkspaceSlotSection, defineView, defineWorkspace, deriveDisplay, initialsOf, isWorkspace, rowPicture, scopeValues };
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { readinessOf } from '../chunk-PP3XFFQM.js';
|
|
2
|
+
import { keyFields, encodeRecordKey } from '../chunk-KV2HW4G4.js';
|
|
3
|
+
import { NO_VIEWER } from '../chunk-OZYF55E3.js';
|
|
4
|
+
export { NO_VIEWER } from '../chunk-OZYF55E3.js';
|
|
3
5
|
import { titleCase } from '../chunk-RYEXWQFD.js';
|
|
6
|
+
import { isSafeUrl } from '../chunk-YDDPFSQ6.js';
|
|
4
7
|
|
|
5
8
|
// src/resource-view/define-view.ts
|
|
6
9
|
var ViewDefinitionError = class extends Error {
|
|
@@ -88,7 +91,10 @@ function defineView(contract, definition = {}) {
|
|
|
88
91
|
)
|
|
89
92
|
);
|
|
90
93
|
const actionList = Object.freeze(
|
|
91
|
-
|
|
94
|
+
// Names are trimmed, so " archive" and "archive" are one name.
|
|
95
|
+
(Array.isArray(definition.actions) ? definition.actions : []).map(
|
|
96
|
+
(a) => a && typeof a === "object" ? Object.freeze({ ...a, ...typeof a.name === "string" ? { name: a.name.trim() } : {} }) : a
|
|
97
|
+
)
|
|
92
98
|
);
|
|
93
99
|
const problems = [];
|
|
94
100
|
const known = (name, where) => {
|
|
@@ -141,6 +147,7 @@ function defineView(contract, definition = {}) {
|
|
|
141
147
|
problems.push(`${where}.description isn't text`);
|
|
142
148
|
}
|
|
143
149
|
if (action.can !== void 0 && typeof action.can !== "function") problems.push(`${where}.can isn't a function`);
|
|
150
|
+
if (action.destructive !== void 0 && typeof action.destructive !== "boolean") problems.push(`${where}.destructive isn't true or false`);
|
|
144
151
|
});
|
|
145
152
|
if (problems.length) throw new ViewDefinitionError(contract.name, problems);
|
|
146
153
|
const copy = definition.copy ?? {};
|
|
@@ -180,7 +187,7 @@ function defineView(contract, definition = {}) {
|
|
|
180
187
|
field: byName.get(column),
|
|
181
188
|
value: (row) => row[column],
|
|
182
189
|
...v?.tones ? { tones: v.tones } : {},
|
|
183
|
-
...v?.when ? { when: v.when } : {},
|
|
190
|
+
...v?.when ? { when: (row, viewer = NO_VIEWER) => v.when(row, viewer) } : {},
|
|
184
191
|
...v?.timeZone ? { timeZone: v.timeZone } : {}
|
|
185
192
|
});
|
|
186
193
|
})
|
|
@@ -217,10 +224,12 @@ function defineView(contract, definition = {}) {
|
|
|
217
224
|
};
|
|
218
225
|
const writable = (mode) => Object.freeze(form(mode).filter((f) => !f.readOnly).map((f) => f.field.name));
|
|
219
226
|
const guards = definition.can ?? {};
|
|
220
|
-
function can(action,
|
|
221
|
-
if (action === "create")
|
|
227
|
+
function can(action, rowOrViewer, maybeViewer) {
|
|
228
|
+
if (action === "create" && maybeViewer !== void 0) throw new TypeError(`[genesis-core] ${contract.name}: can("create", viewer) takes no row`);
|
|
229
|
+
if (action === "create") return guards.create?.(rowOrViewer ?? NO_VIEWER) ?? true;
|
|
222
230
|
const guard = guards[action];
|
|
223
|
-
|
|
231
|
+
const row = rowOrViewer;
|
|
232
|
+
return guard && row ? guard(row, maybeViewer ?? NO_VIEWER) : true;
|
|
224
233
|
}
|
|
225
234
|
const actionGuards = new Map(actionList.map((a) => [a.name, a.can]));
|
|
226
235
|
const resolvedActions = Object.freeze(
|
|
@@ -229,13 +238,18 @@ function defineView(contract, definition = {}) {
|
|
|
229
238
|
name: a.name,
|
|
230
239
|
label: a.label,
|
|
231
240
|
...a.confirm !== void 0 ? { confirm: a.confirm } : {},
|
|
232
|
-
...a.description !== void 0 ? { description: a.description } : {}
|
|
241
|
+
...a.description !== void 0 ? { description: a.description } : {},
|
|
242
|
+
...a.destructive ? { destructive: true } : {}
|
|
233
243
|
})
|
|
234
244
|
)
|
|
235
245
|
);
|
|
236
|
-
function canAction(name, row) {
|
|
246
|
+
function canAction(name, row, viewer = NO_VIEWER) {
|
|
237
247
|
if (!actionGuards.has(name)) throw new ViewDefinitionError(contract.name, [`no action "${name}"`]);
|
|
238
|
-
|
|
248
|
+
const guard = actionGuards.get(name);
|
|
249
|
+
if (!guard) return true;
|
|
250
|
+
const result = guard(row, viewer);
|
|
251
|
+
if (result === true) return true;
|
|
252
|
+
return typeof result === "string" && result.trim() ? result : "This isn't allowed.";
|
|
239
253
|
}
|
|
240
254
|
const summary = (row) => {
|
|
241
255
|
if (definition.summary) return definition.summary(row);
|
|
@@ -288,11 +302,16 @@ function initialsOf(name) {
|
|
|
288
302
|
const pair = letters.length === 1 ? [letters[0]] : [letters[0], letters[letters.length - 1]];
|
|
289
303
|
return pair.map((l) => l.toUpperCase().length === l.length ? l.toUpperCase() : l).join("");
|
|
290
304
|
}
|
|
305
|
+
function isImageSrc(src) {
|
|
306
|
+
if (!isSafeUrl(src)) return false;
|
|
307
|
+
const value = src.trim();
|
|
308
|
+
return value.startsWith("/") || /^https?:/i.test(value);
|
|
309
|
+
}
|
|
291
310
|
function rowPicture(summary) {
|
|
292
311
|
const image = summary.image;
|
|
293
312
|
if (!image) return null;
|
|
294
313
|
const initials = initialsOf(image.name);
|
|
295
|
-
if (
|
|
314
|
+
if (isImageSrc(image.src)) return { kind: "image", src: image.src.trim(), alt: image.alt ?? "", fallback: initials };
|
|
296
315
|
return initials ? { kind: "initials", text: initials } : null;
|
|
297
316
|
}
|
|
298
317
|
|
|
@@ -315,6 +334,209 @@ function scopeValues(view, scope) {
|
|
|
315
334
|
return Object.freeze(out);
|
|
316
335
|
}
|
|
317
336
|
|
|
318
|
-
|
|
337
|
+
// src/resource-view/workspace.ts
|
|
338
|
+
var WORKSPACE = /* @__PURE__ */ Symbol.for("genesis.resource-workspace");
|
|
339
|
+
var REFUSED = "This isn't allowed.";
|
|
340
|
+
var TONES = /* @__PURE__ */ new Set(["success", "warning", "danger", "info", "muted"]);
|
|
341
|
+
var SECTION_KEYS = /* @__PURE__ */ new Set(["id", "label", "list", "settings"]);
|
|
342
|
+
var dev = () => process.env.NODE_ENV !== "production";
|
|
343
|
+
function unsettable(field) {
|
|
344
|
+
if (!field) return null;
|
|
345
|
+
const facts = field;
|
|
346
|
+
if (field.type === "json") return "is JSON";
|
|
347
|
+
if (field.array) return "is a list";
|
|
348
|
+
if (field.references || field.type === "relation") return "is a link";
|
|
349
|
+
if (facts.sensitive) return "is sensitive";
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
function isWorkspace(value) {
|
|
353
|
+
return typeof value === "object" && value !== null && value[WORKSPACE] === true;
|
|
354
|
+
}
|
|
355
|
+
function isView(value) {
|
|
356
|
+
return typeof value === "object" && value !== null && typeof value.columns === "function" && typeof value.resource === "string";
|
|
357
|
+
}
|
|
358
|
+
function defineWorkspace(view, definition) {
|
|
359
|
+
const where = `${view.resource} workspace`;
|
|
360
|
+
const problems = [];
|
|
361
|
+
const writable = new Set(view.writable("edit"));
|
|
362
|
+
const actionNames = new Set(view.actions().map((a) => a.name));
|
|
363
|
+
const input = Array.isArray(definition?.sections) ? definition.sections : [];
|
|
364
|
+
if (!Array.isArray(definition?.sections)) problems.push("sections isn't a list");
|
|
365
|
+
else if (input.length === 0) problems.push("sections is empty");
|
|
366
|
+
const ids = /* @__PURE__ */ new Set();
|
|
367
|
+
const settingsSeen = /* @__PURE__ */ new Set();
|
|
368
|
+
const sections = [];
|
|
369
|
+
const filters = /* @__PURE__ */ new Map();
|
|
370
|
+
input.forEach((raw, i) => {
|
|
371
|
+
const at = `sections[${i}]`;
|
|
372
|
+
if (!raw || typeof raw !== "object") {
|
|
373
|
+
problems.push(`${at} isn't a section`);
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
const s = raw;
|
|
377
|
+
const name = typeof s.id === "string" && s.id.trim() ? s.id : void 0;
|
|
378
|
+
if (!name) problems.push(`${at} has no id`);
|
|
379
|
+
else if (ids.has(name)) problems.push(`sections lists "${name}" twice`);
|
|
380
|
+
else ids.add(name);
|
|
381
|
+
const label = `sections.${name ?? i}`;
|
|
382
|
+
if (typeof s.label !== "string" || !s.label.trim()) problems.push(`${label} has no label`);
|
|
383
|
+
if (isWorkspace(s) || "sections" in s || "workspace" in s) {
|
|
384
|
+
problems.push(`${label} holds a workspace; sections don't nest`);
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
const unknown = Object.keys(s).filter((k) => !SECTION_KEYS.has(k));
|
|
388
|
+
if (unknown.length) problems.push(`${label} has ${unknown.map((k) => `"${k}"`).join(", ")}, which isn't a section key (id, label, list, settings)`);
|
|
389
|
+
const hasList = s.list !== void 0;
|
|
390
|
+
const hasSettings = s.settings !== void 0;
|
|
391
|
+
if (hasList && hasSettings) {
|
|
392
|
+
problems.push(`${label} has both list and settings; a section has one`);
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
if (hasList) {
|
|
396
|
+
const list = s.list;
|
|
397
|
+
if (isWorkspace(list?.view)) problems.push(`${label}.list.view is a workspace; sections don't nest`);
|
|
398
|
+
else if (!isView(list?.view)) problems.push(`${label}.list.view isn't a view`);
|
|
399
|
+
if (typeof list?.filter !== "function") problems.push(`${label}.list.filter isn't a function`);
|
|
400
|
+
if (name && isView(list?.view) && !isWorkspace(list.view) && typeof list.filter === "function") {
|
|
401
|
+
filters.set(name, list.filter);
|
|
402
|
+
sections.push(Object.freeze({ id: name, label: s.label, kind: "list", view: list.view }));
|
|
403
|
+
}
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
if (hasSettings) {
|
|
407
|
+
if (!Array.isArray(s.settings)) {
|
|
408
|
+
problems.push(`${label}.settings isn't a list`);
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
if (s.settings.length === 0) problems.push(`${label}.settings lists no fields`);
|
|
412
|
+
for (const field of s.settings) {
|
|
413
|
+
const why = unsettable(view.field(field));
|
|
414
|
+
if (why) problems.push(`${label}.settings names "${field}", which ${why}; it needs an editor of its own`);
|
|
415
|
+
else if (!writable.has(field)) problems.push(`${label}.settings names "${field}", which the view can't write on edit`);
|
|
416
|
+
if (settingsSeen.has(field)) problems.push(`settings lists "${field}" twice`);
|
|
417
|
+
settingsSeen.add(field);
|
|
418
|
+
}
|
|
419
|
+
if (name) sections.push(Object.freeze({ id: name, label: s.label, kind: "settings", settings: Object.freeze(s.settings.map(String)) }));
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
422
|
+
if (name) sections.push(Object.freeze({ id: name, label: s.label, kind: "slot" }));
|
|
423
|
+
});
|
|
424
|
+
const requested = definition?.header?.actions;
|
|
425
|
+
if (requested !== void 0 && !Array.isArray(requested)) problems.push("header.actions isn't a list");
|
|
426
|
+
const headerActions = Object.freeze(
|
|
427
|
+
Array.isArray(requested) ? [...requested] : view.actions().map((a) => a.name)
|
|
428
|
+
);
|
|
429
|
+
const headerSeen = /* @__PURE__ */ new Set();
|
|
430
|
+
for (const name of headerActions) {
|
|
431
|
+
if (!actionNames.has(name)) problems.push(`header.actions names "${name}", which isn't a view action`);
|
|
432
|
+
if (headerSeen.has(name)) problems.push(`header.actions lists "${name}" twice`);
|
|
433
|
+
headerSeen.add(name);
|
|
434
|
+
}
|
|
435
|
+
if (definition?.readiness !== void 0 && typeof definition.readiness !== "function") {
|
|
436
|
+
problems.push("readiness isn't a function");
|
|
437
|
+
}
|
|
438
|
+
if (problems.length) throw new ViewDefinitionError(where, problems);
|
|
439
|
+
const frozenSections = Object.freeze(sections);
|
|
440
|
+
const byId = new Map(frozenSections.map((s) => [s.id, s]));
|
|
441
|
+
const actionByName = new Map(view.actions().map((a) => [a.name, a]));
|
|
442
|
+
const readiness = definition.readiness;
|
|
443
|
+
const settingsOf = (sectionId) => {
|
|
444
|
+
const s = byId.get(sectionId);
|
|
445
|
+
if (!s || s.kind !== "settings") throw new ViewDefinitionError(where, [`no settings section "${sectionId}"`]);
|
|
446
|
+
return s.settings;
|
|
447
|
+
};
|
|
448
|
+
const resolvedSettings = /* @__PURE__ */ new Map();
|
|
449
|
+
const settingsFields = (sectionId) => {
|
|
450
|
+
const key = sectionId ?? "";
|
|
451
|
+
const cached = resolvedSettings.get(key);
|
|
452
|
+
if (cached) return cached;
|
|
453
|
+
const names = sectionId === void 0 ? frozenSections.flatMap((s) => s.kind === "settings" ? s.settings : []) : settingsOf(sectionId);
|
|
454
|
+
const form = new Map(view.form("edit").map((f) => [f.field.name, f]));
|
|
455
|
+
const out = Object.freeze(names.map((n) => form.get(n)).filter((f) => f !== void 0));
|
|
456
|
+
resolvedSettings.set(key, out);
|
|
457
|
+
return out;
|
|
458
|
+
};
|
|
459
|
+
const headerFor = (row, options = {}) => {
|
|
460
|
+
const summary = view.summary(row);
|
|
461
|
+
const served = options.served ? new Set(options.served) : null;
|
|
462
|
+
const actions = [];
|
|
463
|
+
if (options.runnable !== false) {
|
|
464
|
+
for (const name of headerActions) {
|
|
465
|
+
if (served && !served.has(name)) continue;
|
|
466
|
+
const own2 = options.can?.(name, row);
|
|
467
|
+
if (own2 === false) continue;
|
|
468
|
+
const allowed = typeof own2 === "string" ? own2 || REFUSED : view.canAction(name, row, options.viewer);
|
|
469
|
+
actions.push(Object.freeze({ ...actionByName.get(name), allowed }));
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return Object.freeze({
|
|
473
|
+
title: summary.title,
|
|
474
|
+
...summary.subtitle !== void 0 ? { subtitle: summary.subtitle } : {},
|
|
475
|
+
chips: Object.freeze([...summary.chips ?? []]),
|
|
476
|
+
actions: Object.freeze(actions)
|
|
477
|
+
});
|
|
478
|
+
};
|
|
479
|
+
const readinessFor = (row) => {
|
|
480
|
+
if (!readiness) return Object.freeze([]);
|
|
481
|
+
return Object.freeze(
|
|
482
|
+
readiness(row).map((item, i) => {
|
|
483
|
+
let tone = item.tone ?? (item.ok ? "success" : "warning");
|
|
484
|
+
if (!TONES.has(tone)) {
|
|
485
|
+
if (dev()) console.warn(`[genesis-core] ${where}: readiness "${item.title}" has tone "${String(tone)}", which isn't a chip tone; showing it muted.`);
|
|
486
|
+
tone = "muted";
|
|
487
|
+
}
|
|
488
|
+
return Object.freeze({
|
|
489
|
+
key: item.id ?? `${i}:${item.title}`,
|
|
490
|
+
title: item.title,
|
|
491
|
+
ok: Boolean(item.ok),
|
|
492
|
+
...item.reason !== void 0 ? { reason: item.reason } : {},
|
|
493
|
+
tone
|
|
494
|
+
});
|
|
495
|
+
})
|
|
496
|
+
);
|
|
497
|
+
};
|
|
498
|
+
const readinessSummary = (row) => readiness ? readinessOf(
|
|
499
|
+
readinessFor(row).filter((r) => !r.ok).map((r) => ({ level: r.tone === "danger" ? "error" : "warning" }))
|
|
500
|
+
) : null;
|
|
501
|
+
return Object.freeze({
|
|
502
|
+
[WORKSPACE]: true,
|
|
503
|
+
view,
|
|
504
|
+
sections: frozenSections,
|
|
505
|
+
headerActions,
|
|
506
|
+
hasReadiness: Boolean(readiness),
|
|
507
|
+
section: (id) => byId.get(id),
|
|
508
|
+
recordId: (row) => encodeRecordKey(view.contract, row),
|
|
509
|
+
settingsLocked: (row, viewer) => {
|
|
510
|
+
const allowed = view.can("update", row, viewer);
|
|
511
|
+
return allowed === true ? null : typeof allowed === "string" && allowed.trim() ? allowed : REFUSED;
|
|
512
|
+
},
|
|
513
|
+
headerFor,
|
|
514
|
+
settingsFields,
|
|
515
|
+
settingsPatch: (sectionId, values) => {
|
|
516
|
+
const patch = {};
|
|
517
|
+
for (const name of settingsOf(sectionId)) if (Object.hasOwn(values, name)) patch[name] = values[name];
|
|
518
|
+
return patch;
|
|
519
|
+
},
|
|
520
|
+
listFilter: (sectionId, row) => {
|
|
521
|
+
const filter = filters.get(sectionId);
|
|
522
|
+
if (!filter) throw new ViewDefinitionError(where, [`no list section "${sectionId}"`]);
|
|
523
|
+
const out = filter(row);
|
|
524
|
+
if (dev()) {
|
|
525
|
+
const entries = out && typeof out === "object" ? Object.entries(out) : [];
|
|
526
|
+
const bad = entries.filter(([, v]) => !(v === null || typeof v === "string" || typeof v === "number" || typeof v === "boolean"));
|
|
527
|
+
if (entries.length === 0 || bad.length) {
|
|
528
|
+
throw new ViewDefinitionError(where, [
|
|
529
|
+
entries.length === 0 ? `sections.${sectionId}.list.filter gave no keys, which would list every row` : `sections.${sectionId}.list.filter gives ${bad.map(([k]) => `"${k}"`).join(", ")} no single value`
|
|
530
|
+
]);
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
return Object.freeze({ ...out });
|
|
534
|
+
},
|
|
535
|
+
readinessFor,
|
|
536
|
+
readinessSummary
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
export { ViewDefinitionError, defineView, defineWorkspace, deriveDisplay, initialsOf, isWorkspace, rowPicture, scopeValues };
|
|
319
541
|
//# sourceMappingURL=index.js.map
|
|
320
542
|
//# sourceMappingURL=index.js.map
|