@latticecms/admin 0.0.0 → 0.1.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 +156 -2
- package/dist/admin.css +2 -0
- package/dist/admin.d.ts +18 -0
- package/dist/admin.d.ts.map +1 -0
- package/dist/admin.js +92 -0
- package/dist/admin.js.map +1 -0
- package/dist/catalog.d.ts +45 -0
- package/dist/catalog.d.ts.map +1 -0
- package/dist/catalog.js +147 -0
- package/dist/catalog.js.map +1 -0
- package/dist/collections.d.ts +54 -0
- package/dist/collections.d.ts.map +1 -0
- package/dist/collections.js +103 -0
- package/dist/collections.js.map +1 -0
- package/dist/confirm.d.ts +16 -0
- package/dist/confirm.d.ts.map +1 -0
- package/dist/confirm.js +10 -0
- package/dist/confirm.js.map +1 -0
- package/dist/data.d.ts +28 -0
- package/dist/data.d.ts.map +1 -0
- package/dist/data.js +55 -0
- package/dist/data.js.map +1 -0
- package/dist/delete-action.d.ts +10 -0
- package/dist/delete-action.d.ts.map +1 -0
- package/dist/delete-action.js +44 -0
- package/dist/delete-action.js.map +1 -0
- package/dist/detail-page.d.ts +12 -0
- package/dist/detail-page.d.ts.map +1 -0
- package/dist/detail-page.js +65 -0
- package/dist/detail-page.js.map +1 -0
- package/dist/document-table.d.ts +17 -0
- package/dist/document-table.d.ts.map +1 -0
- package/dist/document-table.js +41 -0
- package/dist/document-table.js.map +1 -0
- package/dist/filters.d.ts +18 -0
- package/dist/filters.d.ts.map +1 -0
- package/dist/filters.js +147 -0
- package/dist/filters.js.map +1 -0
- package/dist/form.d.ts +80 -0
- package/dist/form.d.ts.map +1 -0
- package/dist/form.js +318 -0
- package/dist/form.js.map +1 -0
- package/dist/index-page.d.ts +3 -0
- package/dist/index-page.d.ts.map +1 -0
- package/dist/index-page.js +11 -0
- package/dist/index-page.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/labels.d.ts +13 -0
- package/dist/labels.d.ts.map +1 -0
- package/dist/labels.js +33 -0
- package/dist/labels.js.map +1 -0
- package/dist/list-page.d.ts +25 -0
- package/dist/list-page.d.ts.map +1 -0
- package/dist/list-page.js +82 -0
- package/dist/list-page.js.map +1 -0
- package/dist/list-state.d.ts +17 -0
- package/dist/list-state.d.ts.map +1 -0
- package/dist/list-state.js +39 -0
- package/dist/list-state.js.map +1 -0
- package/dist/method-action.d.ts +16 -0
- package/dist/method-action.d.ts.map +1 -0
- package/dist/method-action.js +78 -0
- package/dist/method-action.js.map +1 -0
- package/dist/model.d.ts +11 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/model.js +16 -0
- package/dist/model.js.map +1 -0
- package/dist/ref-picker.d.ts +24 -0
- package/dist/ref-picker.d.ts.map +1 -0
- package/dist/ref-picker.js +77 -0
- package/dist/ref-picker.js.map +1 -0
- package/dist/refusal-alert.d.ts +12 -0
- package/dist/refusal-alert.d.ts.map +1 -0
- package/dist/refusal-alert.js +87 -0
- package/dist/refusal-alert.js.map +1 -0
- package/dist/refusal.d.ts +20 -0
- package/dist/refusal.d.ts.map +1 -0
- package/dist/refusal.js +40 -0
- package/dist/refusal.js.map +1 -0
- package/dist/relation-tab.d.ts +14 -0
- package/dist/relation-tab.d.ts.map +1 -0
- package/dist/relation-tab.js +205 -0
- package/dist/relation-tab.js.map +1 -0
- package/dist/router.d.ts +50 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +121 -0
- package/dist/router.js.map +1 -0
- package/dist/schema.d.ts +39 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +113 -0
- package/dist/schema.js.map +1 -0
- package/dist/states.d.ts +18 -0
- package/dist/states.d.ts.map +1 -0
- package/dist/states.js +21 -0
- package/dist/states.js.map +1 -0
- package/dist/value.d.ts +33 -0
- package/dist/value.d.ts.map +1 -0
- package/dist/value.js +96 -0
- package/dist/value.js.map +1 -0
- package/dist/write-pages.d.ts +15 -0
- package/dist/write-pages.d.ts.map +1 -0
- package/dist/write-pages.js +70 -0
- package/dist/write-pages.js.map +1 -0
- package/package.json +77 -2
- package/src/admin.css +14 -0
- package/src/admin.tsx +171 -0
- package/src/catalog.ts +192 -0
- package/src/collections.ts +147 -0
- package/src/confirm.tsx +57 -0
- package/src/data.tsx +94 -0
- package/src/delete-action.tsx +65 -0
- package/src/detail-page.tsx +189 -0
- package/src/document-table.tsx +129 -0
- package/src/filters.tsx +346 -0
- package/src/form.tsx +605 -0
- package/src/index-page.tsx +26 -0
- package/src/index.ts +1 -0
- package/src/labels.ts +41 -0
- package/src/list-page.tsx +191 -0
- package/src/list-state.ts +53 -0
- package/src/method-action.tsx +162 -0
- package/src/model.tsx +29 -0
- package/src/ref-picker.tsx +227 -0
- package/src/refusal-alert.tsx +146 -0
- package/src/refusal.ts +67 -0
- package/src/relation-tab.tsx +440 -0
- package/src/router.tsx +186 -0
- package/src/schema.ts +151 -0
- package/src/states.tsx +67 -0
- package/src/value.tsx +171 -0
- package/src/write-pages.tsx +146 -0
package/src/router.tsx
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ComponentProps,
|
|
3
|
+
createContext,
|
|
4
|
+
type MouseEvent,
|
|
5
|
+
type ReactNode,
|
|
6
|
+
useContext,
|
|
7
|
+
useMemo,
|
|
8
|
+
useSyncExternalStore,
|
|
9
|
+
} from 'react';
|
|
10
|
+
|
|
11
|
+
/** A page of the admin, from the URL under its base path. */
|
|
12
|
+
export type Route =
|
|
13
|
+
| { readonly page: 'index' }
|
|
14
|
+
| {
|
|
15
|
+
readonly page: 'list';
|
|
16
|
+
readonly collection: string;
|
|
17
|
+
/** The list's own state: its filters, sort and pages. */
|
|
18
|
+
readonly query: URLSearchParams;
|
|
19
|
+
}
|
|
20
|
+
| { readonly page: 'create'; readonly collection: string }
|
|
21
|
+
| {
|
|
22
|
+
readonly page: 'detail';
|
|
23
|
+
readonly collection: string;
|
|
24
|
+
readonly id: string;
|
|
25
|
+
/** A relation's tab, or the document's own fields. */
|
|
26
|
+
readonly tab: string | undefined;
|
|
27
|
+
/** The relation tab's pages. */
|
|
28
|
+
readonly query: URLSearchParams;
|
|
29
|
+
}
|
|
30
|
+
| { readonly page: 'edit'; readonly collection: string; readonly id: string }
|
|
31
|
+
| { readonly page: 'not-found' };
|
|
32
|
+
|
|
33
|
+
type Router = {
|
|
34
|
+
readonly route: Route;
|
|
35
|
+
/** The href of a page: the base path, then each segment, encoded. */
|
|
36
|
+
readonly href: (...segments: string[]) => string;
|
|
37
|
+
readonly navigate: (href: string) => void;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const RouterContext = createContext<Router | undefined>(undefined);
|
|
41
|
+
|
|
42
|
+
function trimmed(basePath: string): string {
|
|
43
|
+
return basePath.replace(/\/+$/, '');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function decoded(segment: string): string | undefined {
|
|
47
|
+
try {
|
|
48
|
+
return decodeURIComponent(segment);
|
|
49
|
+
} catch {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The route a URL's path and query show, under `basePath`. */
|
|
55
|
+
export function routeOf(basePath: string, url: string): Route {
|
|
56
|
+
const { pathname, searchParams } = new URL(url, 'http://admin.invalid');
|
|
57
|
+
const base = trimmed(basePath);
|
|
58
|
+
if (pathname !== base && !pathname.startsWith(`${base}/`)) {
|
|
59
|
+
return { page: 'not-found' };
|
|
60
|
+
}
|
|
61
|
+
const segments = pathname
|
|
62
|
+
.slice(base.length)
|
|
63
|
+
.split('/')
|
|
64
|
+
.filter((segment) => segment !== '')
|
|
65
|
+
.map(decoded);
|
|
66
|
+
// A malformed escape, such as `%E0%A4`, names no page.
|
|
67
|
+
if (segments.some((segment) => segment === undefined)) {
|
|
68
|
+
return { page: 'not-found' };
|
|
69
|
+
}
|
|
70
|
+
// `new` is the admin's word: ids are generated, so none is `new`. A
|
|
71
|
+
// relation's tab sits under `relations`, so no relation name collides
|
|
72
|
+
// with `edit`.
|
|
73
|
+
const [collection, id, page, tab, ...rest] = segments;
|
|
74
|
+
if (collection === undefined) return { page: 'index' };
|
|
75
|
+
if (id === undefined) {
|
|
76
|
+
return { page: 'list', collection, query: searchParams };
|
|
77
|
+
}
|
|
78
|
+
if (id === 'new') {
|
|
79
|
+
return page === undefined
|
|
80
|
+
? { page: 'create', collection }
|
|
81
|
+
: { page: 'not-found' };
|
|
82
|
+
}
|
|
83
|
+
if (page === undefined) {
|
|
84
|
+
return {
|
|
85
|
+
page: 'detail',
|
|
86
|
+
collection,
|
|
87
|
+
id,
|
|
88
|
+
tab: undefined,
|
|
89
|
+
query: searchParams,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
if (page === 'edit' && tab === undefined) {
|
|
93
|
+
return { page: 'edit', collection, id };
|
|
94
|
+
}
|
|
95
|
+
if (page === 'relations' && tab !== undefined && rest.length === 0) {
|
|
96
|
+
return { page: 'detail', collection, id, tab, query: searchParams };
|
|
97
|
+
}
|
|
98
|
+
return { page: 'not-found' };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// The admin's own navigations. The browser reports back and forward as
|
|
102
|
+
// popstate; a pushState reports nothing, so navigate tells these listeners.
|
|
103
|
+
const listeners = new Set<() => void>();
|
|
104
|
+
|
|
105
|
+
function subscribe(listener: () => void): () => void {
|
|
106
|
+
listeners.add(listener);
|
|
107
|
+
window.addEventListener('popstate', listener);
|
|
108
|
+
return () => {
|
|
109
|
+
listeners.delete(listener);
|
|
110
|
+
window.removeEventListener('popstate', listener);
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function currentUrl(): string {
|
|
115
|
+
return `${window.location.pathname}${window.location.search}`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function navigate(href: string): void {
|
|
119
|
+
window.history.pushState(null, '', href);
|
|
120
|
+
for (const listener of listeners) listener();
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* The admin's router: the History API, under the app's base path. On a
|
|
125
|
+
* server there is no URL, so the index renders until the browser takes over.
|
|
126
|
+
*/
|
|
127
|
+
export function RouterProvider({
|
|
128
|
+
basePath,
|
|
129
|
+
children,
|
|
130
|
+
}: {
|
|
131
|
+
readonly basePath: string;
|
|
132
|
+
readonly children: ReactNode;
|
|
133
|
+
}) {
|
|
134
|
+
const url = useSyncExternalStore(subscribe, currentUrl, () => basePath);
|
|
135
|
+
const router = useMemo<Router>(
|
|
136
|
+
() => ({
|
|
137
|
+
route: routeOf(basePath, url),
|
|
138
|
+
href: (...segments) =>
|
|
139
|
+
[trimmed(basePath), ...segments.map(encodeURIComponent)].join('/') ||
|
|
140
|
+
'/',
|
|
141
|
+
navigate,
|
|
142
|
+
}),
|
|
143
|
+
[basePath, url],
|
|
144
|
+
);
|
|
145
|
+
return <RouterContext value={router}>{children}</RouterContext>;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function useRouter(): Router {
|
|
149
|
+
const router = useContext(RouterContext);
|
|
150
|
+
if (router === undefined) {
|
|
151
|
+
throw new Error('Lattice admin: useRouter outside <Admin>');
|
|
152
|
+
}
|
|
153
|
+
return router;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** A modified or middle click opens a new tab, as a plain link does. */
|
|
157
|
+
function opensElsewhere(event: MouseEvent<HTMLAnchorElement>): boolean {
|
|
158
|
+
return (
|
|
159
|
+
event.button !== 0 ||
|
|
160
|
+
event.metaKey ||
|
|
161
|
+
event.ctrlKey ||
|
|
162
|
+
event.shiftKey ||
|
|
163
|
+
event.altKey
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** A real `<a href>` that navigates inside the admin on a plain click. */
|
|
168
|
+
export function Link({
|
|
169
|
+
href,
|
|
170
|
+
onClick,
|
|
171
|
+
...props
|
|
172
|
+
}: ComponentProps<'a'> & { readonly href: string }) {
|
|
173
|
+
const router = useRouter();
|
|
174
|
+
return (
|
|
175
|
+
<a
|
|
176
|
+
{...props}
|
|
177
|
+
href={href}
|
|
178
|
+
onClick={(event) => {
|
|
179
|
+
onClick?.(event);
|
|
180
|
+
if (event.defaultPrevented || opensElsewhere(event)) return;
|
|
181
|
+
event.preventDefault();
|
|
182
|
+
router.navigate(href);
|
|
183
|
+
}}
|
|
184
|
+
/>
|
|
185
|
+
);
|
|
186
|
+
}
|
package/src/schema.ts
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import type { JsonSchema } from '@ts-pf/docs';
|
|
2
|
+
import { isRecord } from './catalog.js';
|
|
3
|
+
|
|
4
|
+
/** How a field's value is shown and filtered, from its JSON Schema type. */
|
|
5
|
+
export type Kind =
|
|
6
|
+
| 'string'
|
|
7
|
+
| 'number'
|
|
8
|
+
| 'boolean'
|
|
9
|
+
| 'enum'
|
|
10
|
+
| 'datetime'
|
|
11
|
+
| 'ref'
|
|
12
|
+
| 'list'
|
|
13
|
+
| 'other';
|
|
14
|
+
|
|
15
|
+
/** A document field, as the admin derives it from a collection's schemas. */
|
|
16
|
+
export type Field = {
|
|
17
|
+
readonly name: string;
|
|
18
|
+
/** The schema's `title`, when the app gave one through Zod meta. */
|
|
19
|
+
readonly title: string | undefined;
|
|
20
|
+
readonly kind: Kind;
|
|
21
|
+
readonly required: boolean;
|
|
22
|
+
/** The values of an enum field. */
|
|
23
|
+
readonly values: readonly string[];
|
|
24
|
+
/** The collection a ref field refers to. */
|
|
25
|
+
readonly ref: string | undefined;
|
|
26
|
+
/** A field the server withholds from a caller who may not read it. */
|
|
27
|
+
readonly maskable: boolean;
|
|
28
|
+
/** `id`, `createdAt`, `updatedAt`, or the tenant field: never input. */
|
|
29
|
+
readonly system: boolean;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** The fields every Lattice document has, which no input sets. */
|
|
33
|
+
export const systemFields = ['id', 'createdAt', 'updatedAt'];
|
|
34
|
+
|
|
35
|
+
/** The schema `#/$defs/<name>` names inside `root`, or `schema` itself. */
|
|
36
|
+
export function resolve(schema: unknown, root: JsonSchema): JsonSchema {
|
|
37
|
+
if (!isRecord(schema)) return {};
|
|
38
|
+
const ref = schema.$ref;
|
|
39
|
+
if (typeof ref !== 'string' || !ref.startsWith('#/$defs/')) return schema;
|
|
40
|
+
const defs = root.$defs;
|
|
41
|
+
const target = isRecord(defs)
|
|
42
|
+
? defs[ref.slice('#/$defs/'.length)]
|
|
43
|
+
: undefined;
|
|
44
|
+
return isRecord(target) ? target : {};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** The `properties` of an object schema, in schema order. */
|
|
48
|
+
export function propertiesOf(schema: JsonSchema): [string, JsonSchema][] {
|
|
49
|
+
const { properties } = schema;
|
|
50
|
+
if (!isRecord(properties)) return [];
|
|
51
|
+
return Object.entries(properties).flatMap(([name, value]) =>
|
|
52
|
+
isRecord(value) ? [[name, value]] : [],
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function requiredOf(schema: JsonSchema): readonly unknown[] {
|
|
57
|
+
return Array.isArray(schema.required) ? schema.required : [];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The JSON Schema type names a schema allows, without null. */
|
|
61
|
+
export function typesOf(schema: JsonSchema): string[] {
|
|
62
|
+
const { type } = schema;
|
|
63
|
+
const types = Array.isArray(type) ? type : [type];
|
|
64
|
+
return types.filter(
|
|
65
|
+
(name): name is string => typeof name === 'string' && name !== 'null',
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The schema's `anyOf` branches but null, or the schema itself. */
|
|
70
|
+
function branchesOf(schema: JsonSchema): JsonSchema[] {
|
|
71
|
+
const { anyOf } = schema;
|
|
72
|
+
if (!Array.isArray(anyOf)) return [schema];
|
|
73
|
+
return anyOf.filter(isRecord).filter((branch) => branch.type !== 'null');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The plain form of a schema: a populated ref's id branch, or its non-null branch. */
|
|
77
|
+
export function plainOf(schema: JsonSchema): JsonSchema {
|
|
78
|
+
const ref = branchesOf(schema).find(
|
|
79
|
+
(branch) => typeof branch['x-lattice-ref'] === 'string',
|
|
80
|
+
);
|
|
81
|
+
return ref ?? branchesOf(schema)[0] ?? schema;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function enumOf(schema: JsonSchema): string[] {
|
|
85
|
+
const values = schema.enum;
|
|
86
|
+
return Array.isArray(values)
|
|
87
|
+
? values.filter((value): value is string => typeof value === 'string')
|
|
88
|
+
: [];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function kindOf(schema: JsonSchema): Kind {
|
|
92
|
+
if (typeof schema['x-lattice-ref'] === 'string') return 'ref';
|
|
93
|
+
if (enumOf(schema).length > 0) return 'enum';
|
|
94
|
+
const [type] = typesOf(schema);
|
|
95
|
+
if (type === 'string') {
|
|
96
|
+
return schema.format === 'date-time' ? 'datetime' : 'string';
|
|
97
|
+
}
|
|
98
|
+
if (type === 'number' || type === 'integer') return 'number';
|
|
99
|
+
if (type === 'boolean') return 'boolean';
|
|
100
|
+
if (type === 'array') return 'list';
|
|
101
|
+
return 'other';
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The fields of a collection's documents, in schema order, from the
|
|
106
|
+
* document schema of its `find` output.
|
|
107
|
+
*/
|
|
108
|
+
export function fieldsOf(document: JsonSchema): Field[] {
|
|
109
|
+
const required = requiredOf(document);
|
|
110
|
+
return propertiesOf(document).map(([name, property]) => {
|
|
111
|
+
const plain = plainOf(property);
|
|
112
|
+
// A list of ids names its target on each item.
|
|
113
|
+
const ref = isRecord(plain.items)
|
|
114
|
+
? plainOf(plain.items)['x-lattice-ref']
|
|
115
|
+
: plain['x-lattice-ref'];
|
|
116
|
+
const title = property.title ?? plain.title;
|
|
117
|
+
return {
|
|
118
|
+
name,
|
|
119
|
+
title: typeof title === 'string' ? title : undefined,
|
|
120
|
+
kind: kindOf(plain),
|
|
121
|
+
required: required.includes(name),
|
|
122
|
+
values: enumOf(plain),
|
|
123
|
+
ref: typeof ref === 'string' ? ref : undefined,
|
|
124
|
+
maskable: plain['x-lattice-maskable'] === true,
|
|
125
|
+
system: systemFields.includes(name) || plain['x-lattice-tenant'] === true,
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** The keys `find` can sort by, ascending and descending, from its input. */
|
|
131
|
+
export function sortKeysOf(findInput: JsonSchema | undefined): Set<string> {
|
|
132
|
+
if (findInput === undefined) return new Set();
|
|
133
|
+
const sort = propertiesOf(findInput).find(([name]) => name === 'sort');
|
|
134
|
+
const items = sort === undefined ? undefined : sort[1].items;
|
|
135
|
+
return new Set(isRecord(items) ? enumOf(items) : []);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Each field's filter operators and their schemas, from `find`'s where. */
|
|
139
|
+
export function operatorsOf(
|
|
140
|
+
findInput: JsonSchema | undefined,
|
|
141
|
+
): Map<string, [string, JsonSchema][]> {
|
|
142
|
+
if (findInput === undefined) return new Map();
|
|
143
|
+
const where = propertiesOf(findInput).find(([name]) => name === 'where');
|
|
144
|
+
if (where === undefined) return new Map();
|
|
145
|
+
const schema = resolve(where[1], findInput);
|
|
146
|
+
return new Map(
|
|
147
|
+
propertiesOf(schema)
|
|
148
|
+
.filter(([name]) => name !== 'and' && name !== 'or')
|
|
149
|
+
.map(([name, field]) => [name, propertiesOf(resolve(field, findInput))]),
|
|
150
|
+
);
|
|
151
|
+
}
|
package/src/states.tsx
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Alert, AlertDescription, AlertTitle } from '@shadalaya/react/alert';
|
|
2
|
+
import {
|
|
3
|
+
Empty,
|
|
4
|
+
EmptyDescription,
|
|
5
|
+
EmptyHeader,
|
|
6
|
+
EmptyTitle,
|
|
7
|
+
} from '@shadalaya/react/empty';
|
|
8
|
+
import { Skeleton } from '@shadalaya/react/skeleton';
|
|
9
|
+
import type { ReactNode } from 'react';
|
|
10
|
+
|
|
11
|
+
/** Rows still loading. */
|
|
12
|
+
export function Loading() {
|
|
13
|
+
return (
|
|
14
|
+
<div className="flex flex-col gap-2" role="status" aria-label="Loading">
|
|
15
|
+
<Skeleton className="h-8 w-full" />
|
|
16
|
+
<Skeleton className="h-8 w-full" />
|
|
17
|
+
<Skeleton className="h-8 w-2/3" />
|
|
18
|
+
</div>
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Something that went wrong, as a title, a message, and any detail. */
|
|
23
|
+
export function ErrorAlert({
|
|
24
|
+
title,
|
|
25
|
+
message,
|
|
26
|
+
children,
|
|
27
|
+
}: {
|
|
28
|
+
readonly title: string;
|
|
29
|
+
readonly message: string;
|
|
30
|
+
readonly children?: ReactNode;
|
|
31
|
+
}) {
|
|
32
|
+
return (
|
|
33
|
+
<Alert variant="destructive" role="alert">
|
|
34
|
+
<AlertTitle>{title}</AlertTitle>
|
|
35
|
+
<AlertDescription>
|
|
36
|
+
{message}
|
|
37
|
+
{children}
|
|
38
|
+
</AlertDescription>
|
|
39
|
+
</Alert>
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** A page or document that is not there. */
|
|
44
|
+
export function NotFound({ what }: { readonly what: string }) {
|
|
45
|
+
return (
|
|
46
|
+
<Empty>
|
|
47
|
+
<EmptyHeader>
|
|
48
|
+
<EmptyTitle>Not found</EmptyTitle>
|
|
49
|
+
<EmptyDescription>{what}</EmptyDescription>
|
|
50
|
+
</EmptyHeader>
|
|
51
|
+
</Empty>
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A list with no documents. */
|
|
56
|
+
export function NoDocuments({ plural }: { readonly plural: string }) {
|
|
57
|
+
return (
|
|
58
|
+
<Empty>
|
|
59
|
+
<EmptyHeader>
|
|
60
|
+
<EmptyTitle>No {plural}</EmptyTitle>
|
|
61
|
+
<EmptyDescription>
|
|
62
|
+
Nothing matches, or nothing is here yet.
|
|
63
|
+
</EmptyDescription>
|
|
64
|
+
</EmptyHeader>
|
|
65
|
+
</Empty>
|
|
66
|
+
);
|
|
67
|
+
}
|
package/src/value.tsx
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import { Badge } from '@shadalaya/react/badge';
|
|
2
|
+
import { isRecord } from './catalog.js';
|
|
3
|
+
import { type Collection, titleFieldOf } from './collections.js';
|
|
4
|
+
import { fieldLabel } from './labels.js';
|
|
5
|
+
import { useCollection } from './model.js';
|
|
6
|
+
import { Link, useRouter } from './router.js';
|
|
7
|
+
import type { Field } from './schema.js';
|
|
8
|
+
|
|
9
|
+
/** What an absent or null value shows. */
|
|
10
|
+
const none = '—';
|
|
11
|
+
|
|
12
|
+
/** A ref as it shows: its id, and its target's title when populated. */
|
|
13
|
+
type Shown = {
|
|
14
|
+
readonly id: string;
|
|
15
|
+
readonly title: string | undefined;
|
|
16
|
+
/** Populated as `{ id }`: the caller may not read the target. */
|
|
17
|
+
readonly withheld: boolean;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/** A ref's id: the value, or a populated document's `id`. */
|
|
21
|
+
export function refIdOf(value: unknown): string | undefined {
|
|
22
|
+
if (typeof value === 'string') return value;
|
|
23
|
+
return isRecord(value) && typeof value.id === 'string' ? value.id : undefined;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** A ref's target title, when it was populated with one. */
|
|
27
|
+
export function refTitleOf(
|
|
28
|
+
value: unknown,
|
|
29
|
+
target: Collection | undefined,
|
|
30
|
+
): string | undefined {
|
|
31
|
+
const field = target === undefined ? undefined : titleFieldOf(target);
|
|
32
|
+
const title =
|
|
33
|
+
field === undefined || !isRecord(value) ? undefined : value[field.name];
|
|
34
|
+
return typeof title === 'string' ? title : undefined;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function shownRef(
|
|
38
|
+
value: unknown,
|
|
39
|
+
target: Collection | undefined,
|
|
40
|
+
): Shown | undefined {
|
|
41
|
+
const id = refIdOf(value);
|
|
42
|
+
if (id === undefined) return undefined;
|
|
43
|
+
const doc = isRecord(value) ? value : undefined;
|
|
44
|
+
if (doc !== undefined && Object.keys(doc).length === 1) {
|
|
45
|
+
return { id, title: undefined, withheld: true };
|
|
46
|
+
}
|
|
47
|
+
return { id, title: refTitleOf(value, target), withheld: false };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function Withheld({ id }: { readonly id: string }) {
|
|
51
|
+
return (
|
|
52
|
+
<span className="inline-flex items-center gap-2">
|
|
53
|
+
<span className="font-mono text-xs">{id}</span>
|
|
54
|
+
<Badge variant="outline">no access</Badge>
|
|
55
|
+
</span>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* A ref: a link to the target document, labelled by its title field when
|
|
61
|
+
* populated. A populated ref the caller may not read arrives as `{ id }`.
|
|
62
|
+
*/
|
|
63
|
+
export function RefValue({
|
|
64
|
+
target,
|
|
65
|
+
value,
|
|
66
|
+
}: {
|
|
67
|
+
readonly target: string | undefined;
|
|
68
|
+
readonly value: unknown;
|
|
69
|
+
}) {
|
|
70
|
+
const { href } = useRouter();
|
|
71
|
+
const collection = useCollection(target);
|
|
72
|
+
const shown = shownRef(value, collection);
|
|
73
|
+
if (shown === undefined) return <>{none}</>;
|
|
74
|
+
if (shown.withheld) return <Withheld id={shown.id} />;
|
|
75
|
+
const text = shown.title ?? shown.id;
|
|
76
|
+
if (collection === undefined) return <>{text}</>;
|
|
77
|
+
return (
|
|
78
|
+
<Link
|
|
79
|
+
className="underline underline-offset-4"
|
|
80
|
+
href={href(collection.slug, shown.id)}
|
|
81
|
+
>
|
|
82
|
+
{text}
|
|
83
|
+
</Link>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function plainText(field: Field, value: unknown): string {
|
|
88
|
+
if (value === undefined || value === null) return none;
|
|
89
|
+
if (field.kind === 'boolean') return value === true ? 'Yes' : 'No';
|
|
90
|
+
if (field.kind === 'datetime' && typeof value === 'string') {
|
|
91
|
+
return new Date(value).toLocaleString();
|
|
92
|
+
}
|
|
93
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
94
|
+
return String(value);
|
|
95
|
+
}
|
|
96
|
+
return JSON.stringify(value);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* One field's value as text, with no link, such as inside a link to its
|
|
101
|
+
* document. A ref shows its target's title, or its id.
|
|
102
|
+
*/
|
|
103
|
+
export function FieldText({
|
|
104
|
+
field,
|
|
105
|
+
value,
|
|
106
|
+
}: {
|
|
107
|
+
readonly field: Field;
|
|
108
|
+
readonly value: unknown;
|
|
109
|
+
}) {
|
|
110
|
+
const target = useCollection(field.ref);
|
|
111
|
+
if (field.ref === undefined) return <>{plainText(field, value)}</>;
|
|
112
|
+
const shown = (Array.isArray(value) ? value : [value]).map((item) =>
|
|
113
|
+
shownRef(item, target),
|
|
114
|
+
);
|
|
115
|
+
const [only] = shown;
|
|
116
|
+
if (only?.withheld && shown.length === 1) return <Withheld id={only.id} />;
|
|
117
|
+
const texts = shown.flatMap((ref) =>
|
|
118
|
+
ref === undefined ? [] : [ref.title ?? ref.id],
|
|
119
|
+
);
|
|
120
|
+
return <>{texts.length === 0 ? none : texts.join(', ')}</>;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** One field's value, read-only, by its kind. A ref links to its document. */
|
|
124
|
+
export function FieldValue({
|
|
125
|
+
field,
|
|
126
|
+
value,
|
|
127
|
+
}: {
|
|
128
|
+
readonly field: Field;
|
|
129
|
+
readonly value: unknown;
|
|
130
|
+
}) {
|
|
131
|
+
if (field.ref !== undefined && field.kind === 'ref') {
|
|
132
|
+
return <RefValue target={field.ref} value={value} />;
|
|
133
|
+
}
|
|
134
|
+
if (field.ref !== undefined && Array.isArray(value)) {
|
|
135
|
+
return (
|
|
136
|
+
<span className="inline-flex flex-wrap gap-2">
|
|
137
|
+
{value.map((item, index) => (
|
|
138
|
+
<RefValue
|
|
139
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: ids may repeat in a list, and it never reorders
|
|
140
|
+
key={index}
|
|
141
|
+
target={field.ref}
|
|
142
|
+
value={item}
|
|
143
|
+
/>
|
|
144
|
+
))}
|
|
145
|
+
</span>
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
return <FieldText field={field} value={value} />;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Fields as label and value rows. */
|
|
152
|
+
export function FieldList({
|
|
153
|
+
fields,
|
|
154
|
+
doc,
|
|
155
|
+
}: {
|
|
156
|
+
readonly fields: readonly Field[];
|
|
157
|
+
readonly doc: Record<string, unknown>;
|
|
158
|
+
}) {
|
|
159
|
+
return (
|
|
160
|
+
<dl className="grid grid-cols-[12rem_1fr] gap-x-4 gap-y-2">
|
|
161
|
+
{fields.map((field) => (
|
|
162
|
+
<div key={field.name} className="contents">
|
|
163
|
+
<dt className="text-muted-foreground">{fieldLabel(field)}</dt>
|
|
164
|
+
<dd>
|
|
165
|
+
<FieldValue field={field} value={doc[field.name]} />
|
|
166
|
+
</dd>
|
|
167
|
+
</div>
|
|
168
|
+
))}
|
|
169
|
+
</dl>
|
|
170
|
+
);
|
|
171
|
+
}
|