@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
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { Button, buttonVariants } from '@shadalaya/react/button';
|
|
2
|
+
import { isRecord } from './catalog.js';
|
|
3
|
+
import { type Collection, columnsOf } from './collections.js';
|
|
4
|
+
import { useCall } from './data.js';
|
|
5
|
+
import { DocumentTable } from './document-table.js';
|
|
6
|
+
import { Filters, whereOf } from './filters.js';
|
|
7
|
+
import { pluralHeading } from './labels.js';
|
|
8
|
+
import { type ListState, listQuery, listStateOf } from './list-state.js';
|
|
9
|
+
import { isDocumentMethod, MethodAction } from './method-action.js';
|
|
10
|
+
import { type Issue, issuesAt, refusalOf } from './refusal.js';
|
|
11
|
+
import { RefusalAlert } from './refusal-alert.js';
|
|
12
|
+
import { Link, useRouter } from './router.js';
|
|
13
|
+
import { sortKeysOf } from './schema.js';
|
|
14
|
+
import { Loading, NoDocuments } from './states.js';
|
|
15
|
+
|
|
16
|
+
type Page = {
|
|
17
|
+
readonly docs: readonly Record<string, unknown>[];
|
|
18
|
+
readonly next: string | null;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** A `find` output, or nothing when it is not one. */
|
|
22
|
+
export function pageOf(data: unknown): Page | undefined {
|
|
23
|
+
if (!isRecord(data) || !Array.isArray(data.docs)) return undefined;
|
|
24
|
+
const next = typeof data.next === 'string' ? data.next : null;
|
|
25
|
+
return { docs: data.docs.filter(isRecord), next };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function countOf(data: unknown): number | undefined {
|
|
29
|
+
return isRecord(data) && typeof data.count === 'number'
|
|
30
|
+
? data.count
|
|
31
|
+
: undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Whether a filter or a column header shows an issue. */
|
|
35
|
+
function atControl({ path: [key] }: Issue): boolean {
|
|
36
|
+
return key === 'where' || key === 'sort';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Back and Next over the cursors seen so far (0008: no page numbers). */
|
|
40
|
+
export function Pager({
|
|
41
|
+
canBack,
|
|
42
|
+
next,
|
|
43
|
+
onBack,
|
|
44
|
+
onNext,
|
|
45
|
+
}: {
|
|
46
|
+
readonly canBack: boolean;
|
|
47
|
+
readonly next: string | null;
|
|
48
|
+
readonly onBack: () => void;
|
|
49
|
+
readonly onNext: (cursor: string) => void;
|
|
50
|
+
}) {
|
|
51
|
+
return (
|
|
52
|
+
<nav aria-label="Pages" className="flex justify-end gap-2">
|
|
53
|
+
<Button variant="outline" size="sm" disabled={!canBack} onClick={onBack}>
|
|
54
|
+
Back
|
|
55
|
+
</Button>
|
|
56
|
+
<Button
|
|
57
|
+
variant="outline"
|
|
58
|
+
size="sm"
|
|
59
|
+
disabled={next === null}
|
|
60
|
+
onClick={() => {
|
|
61
|
+
if (next !== null) onNext(next);
|
|
62
|
+
}}
|
|
63
|
+
>
|
|
64
|
+
Next
|
|
65
|
+
</Button>
|
|
66
|
+
</nav>
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** "1 result", "2 results". */
|
|
71
|
+
function results(total: number): string {
|
|
72
|
+
return total === 1 ? '1 result' : `${total} results`;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** What the collection can do: create, and its collection methods. */
|
|
76
|
+
function CollectionActions({
|
|
77
|
+
collection,
|
|
78
|
+
}: {
|
|
79
|
+
readonly collection: Collection;
|
|
80
|
+
}) {
|
|
81
|
+
const { href } = useRouter();
|
|
82
|
+
return (
|
|
83
|
+
<div className="flex flex-wrap items-start gap-2">
|
|
84
|
+
{collection.operations.create === undefined ? null : (
|
|
85
|
+
<Link className={buttonVariants()} href={href(collection.slug, 'new')}>
|
|
86
|
+
New {collection.label.singular}
|
|
87
|
+
</Link>
|
|
88
|
+
)}
|
|
89
|
+
{collection.methods
|
|
90
|
+
.filter((method) => !isDocumentMethod(method))
|
|
91
|
+
.map((method) => (
|
|
92
|
+
<MethodAction
|
|
93
|
+
key={method.name}
|
|
94
|
+
collection={collection}
|
|
95
|
+
method={method}
|
|
96
|
+
id={undefined}
|
|
97
|
+
/>
|
|
98
|
+
))}
|
|
99
|
+
</div>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* A collection's list: filters, a one-column sort, cursor pages, and a
|
|
105
|
+
* count of what the filters match. Its state is the URL's query, so back,
|
|
106
|
+
* forward and reload return to it.
|
|
107
|
+
*/
|
|
108
|
+
export function ListPage({
|
|
109
|
+
collection,
|
|
110
|
+
query,
|
|
111
|
+
}: {
|
|
112
|
+
readonly collection: Collection;
|
|
113
|
+
readonly query: URLSearchParams;
|
|
114
|
+
}) {
|
|
115
|
+
const { href, navigate } = useRouter();
|
|
116
|
+
const { find, count } = collection.operations;
|
|
117
|
+
const state = listStateOf(query);
|
|
118
|
+
const go = (next: ListState) =>
|
|
119
|
+
navigate(`${href(collection.slug)}${listQuery(next)}`);
|
|
120
|
+
const where = whereOf(collection, state.filters);
|
|
121
|
+
const filtered = Object.keys(where).length > 0 ? { where } : {};
|
|
122
|
+
const after = state.cursors.at(-1);
|
|
123
|
+
const found = useCall(find, {
|
|
124
|
+
...filtered,
|
|
125
|
+
...(state.sort === undefined ? {} : { sort: [state.sort] }),
|
|
126
|
+
...(after === undefined ? {} : { after }),
|
|
127
|
+
populate: find?.expand ?? [],
|
|
128
|
+
});
|
|
129
|
+
const counted = useCall(count, filtered);
|
|
130
|
+
const refusal =
|
|
131
|
+
found.error === undefined ? undefined : refusalOf(found.error);
|
|
132
|
+
const page = pageOf(found.data);
|
|
133
|
+
const total = countOf(counted.data);
|
|
134
|
+
// Shown at the filter or column it names, so the table stays to show it.
|
|
135
|
+
const atControls =
|
|
136
|
+
refusal?.code === 'VALIDATION' &&
|
|
137
|
+
refusal.issues.length > 0 &&
|
|
138
|
+
refusal.issues.every(atControl);
|
|
139
|
+
const sortBy = (sort: string) => go({ ...state, sort, cursors: [] });
|
|
140
|
+
|
|
141
|
+
return (
|
|
142
|
+
<section className="flex flex-col gap-4">
|
|
143
|
+
<header className="flex items-baseline justify-between">
|
|
144
|
+
<h1 className="text-2xl font-semibold">
|
|
145
|
+
{pluralHeading(collection.label)}
|
|
146
|
+
</h1>
|
|
147
|
+
{total === undefined ? null : (
|
|
148
|
+
<p className="text-sm text-muted-foreground">{results(total)}</p>
|
|
149
|
+
)}
|
|
150
|
+
</header>
|
|
151
|
+
<CollectionActions collection={collection} />
|
|
152
|
+
<Filters
|
|
153
|
+
// Back and forward change the applied filters: start over from them.
|
|
154
|
+
key={JSON.stringify(state.filters)}
|
|
155
|
+
collection={collection}
|
|
156
|
+
applied={state.filters}
|
|
157
|
+
issues={(field) => issuesAt(refusal, 'where', field)}
|
|
158
|
+
onApply={(filters) => go({ ...state, filters, cursors: [] })}
|
|
159
|
+
/>
|
|
160
|
+
{refusal === undefined ? null : (
|
|
161
|
+
<RefusalAlert refusal={refusal} placed={atControl} />
|
|
162
|
+
)}
|
|
163
|
+
{counted.error !== undefined && refusal === undefined ? (
|
|
164
|
+
<RefusalAlert refusal={refusalOf(counted.error)} />
|
|
165
|
+
) : null}
|
|
166
|
+
{found.isLoading && page === undefined ? <Loading /> : null}
|
|
167
|
+
{page !== undefined && page.docs.length === 0 && !atControls ? (
|
|
168
|
+
<NoDocuments plural={collection.label.plural} />
|
|
169
|
+
) : null}
|
|
170
|
+
{(page !== undefined && page.docs.length > 0) || atControls ? (
|
|
171
|
+
<DocumentTable
|
|
172
|
+
collection={collection}
|
|
173
|
+
columns={columnsOf(collection, page?.docs ?? [])}
|
|
174
|
+
docs={page?.docs ?? []}
|
|
175
|
+
sortKeys={sortKeysOf(find?.input)}
|
|
176
|
+
sort={state.sort}
|
|
177
|
+
sortIssues={issuesAt(refusal, 'sort')}
|
|
178
|
+
onSort={sortBy}
|
|
179
|
+
/>
|
|
180
|
+
) : null}
|
|
181
|
+
<Pager
|
|
182
|
+
canBack={state.cursors.length > 0}
|
|
183
|
+
next={page?.next ?? null}
|
|
184
|
+
onBack={() => go({ ...state, cursors: state.cursors.slice(0, -1) })}
|
|
185
|
+
onNext={(cursor) =>
|
|
186
|
+
go({ ...state, cursors: [...state.cursors, cursor] })
|
|
187
|
+
}
|
|
188
|
+
/>
|
|
189
|
+
</section>
|
|
190
|
+
);
|
|
191
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/** One applied filter: a field, its operator, and the value as typed. */
|
|
2
|
+
export type Filter = readonly [field: string, operator: string, value: string];
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* What a list shows, kept in its URL's query so back, forward and reload
|
|
6
|
+
* return to it: `filters` (JSON), `sort`, and one `after` per page turned.
|
|
7
|
+
*/
|
|
8
|
+
export type ListState = {
|
|
9
|
+
readonly filters: readonly Filter[];
|
|
10
|
+
readonly sort: string | undefined;
|
|
11
|
+
/** The cursor of each page after the first, so Back can return. */
|
|
12
|
+
readonly cursors: readonly string[];
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
function isFilter(value: unknown): value is Filter {
|
|
16
|
+
return (
|
|
17
|
+
Array.isArray(value) &&
|
|
18
|
+
value.length === 3 &&
|
|
19
|
+
value.every((part) => typeof part === 'string')
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function filtersOf(text: string | null): Filter[] {
|
|
24
|
+
if (text === null) return [];
|
|
25
|
+
try {
|
|
26
|
+
const parsed: unknown = JSON.parse(text);
|
|
27
|
+
return Array.isArray(parsed) ? parsed.filter(isFilter) : [];
|
|
28
|
+
} catch {
|
|
29
|
+
// A hand-edited query that is not JSON filters nothing.
|
|
30
|
+
return [];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** The list state a query holds. Anything unreadable is left out. */
|
|
35
|
+
export function listStateOf(query: URLSearchParams): ListState {
|
|
36
|
+
return {
|
|
37
|
+
filters: filtersOf(query.get('filters')),
|
|
38
|
+
sort: query.get('sort') ?? undefined,
|
|
39
|
+
cursors: query.getAll('after'),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The query that holds `state`, with its `?`, or nothing for the default. */
|
|
44
|
+
export function listQuery(state: ListState): string {
|
|
45
|
+
const query = new URLSearchParams();
|
|
46
|
+
if (state.filters.length > 0) {
|
|
47
|
+
query.set('filters', JSON.stringify(state.filters));
|
|
48
|
+
}
|
|
49
|
+
if (state.sort !== undefined) query.set('sort', state.sort);
|
|
50
|
+
for (const cursor of state.cursors) query.append('after', cursor);
|
|
51
|
+
const text = query.toString();
|
|
52
|
+
return text === '' ? '' : `?${text}`;
|
|
53
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { Button } from '@shadalaya/react/button';
|
|
2
|
+
import {
|
|
3
|
+
Card,
|
|
4
|
+
CardContent,
|
|
5
|
+
CardHeader,
|
|
6
|
+
CardTitle,
|
|
7
|
+
} from '@shadalaya/react/card';
|
|
8
|
+
import { useState } from 'react';
|
|
9
|
+
import { isRecord } from './catalog.js';
|
|
10
|
+
import type { Collection, Method } from './collections.js';
|
|
11
|
+
import { ConfirmDialog } from './confirm.js';
|
|
12
|
+
import { useCaller, useRevalidate } from './data.js';
|
|
13
|
+
import { draftsOf, InputForm, inputFieldsOf } from './form.js';
|
|
14
|
+
import { methodLabel } from './labels.js';
|
|
15
|
+
import { type Refusal, refusalOf } from './refusal.js';
|
|
16
|
+
import { RefusalAlert } from './refusal-alert.js';
|
|
17
|
+
import { fieldsOf, propertiesOf } from './schema.js';
|
|
18
|
+
import { FieldList } from './value.js';
|
|
19
|
+
|
|
20
|
+
/** Whether a method acts on one document: its input takes an `id`. */
|
|
21
|
+
export function isDocumentMethod({ procedure }: Method): boolean {
|
|
22
|
+
return (
|
|
23
|
+
procedure.input !== undefined &&
|
|
24
|
+
propertiesOf(procedure.input).some(([name]) => name === 'id')
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* What a method returned, read-only: the fields its output schema names,
|
|
30
|
+
* or JSON when it names none. Nothing returned shows nothing.
|
|
31
|
+
*/
|
|
32
|
+
function MethodResult({
|
|
33
|
+
method,
|
|
34
|
+
output,
|
|
35
|
+
}: {
|
|
36
|
+
readonly method: Method;
|
|
37
|
+
readonly output: unknown;
|
|
38
|
+
}) {
|
|
39
|
+
if (output === undefined || output === null) return null;
|
|
40
|
+
const fields = fieldsOf(method.procedure.output ?? {});
|
|
41
|
+
return (
|
|
42
|
+
<Card
|
|
43
|
+
size="sm"
|
|
44
|
+
role="region"
|
|
45
|
+
aria-label={`${methodLabel(method.name)} result`}
|
|
46
|
+
>
|
|
47
|
+
<CardContent>
|
|
48
|
+
{isRecord(output) && fields.length > 0 ? (
|
|
49
|
+
<FieldList fields={fields} doc={output} />
|
|
50
|
+
) : (
|
|
51
|
+
<pre>{JSON.stringify(output, null, 2)}</pre>
|
|
52
|
+
)}
|
|
53
|
+
</CardContent>
|
|
54
|
+
</Card>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A method as an action: a document action when `id` is given, else a
|
|
60
|
+
* collection action. It is labelled by its name, with its summary beside
|
|
61
|
+
* it. Its form is its input but `id`. With no other field it is a button:
|
|
62
|
+
* a read runs at once, and a write asks first. A destructive method always
|
|
63
|
+
* asks first.
|
|
64
|
+
*/
|
|
65
|
+
export function MethodAction({
|
|
66
|
+
collection,
|
|
67
|
+
method,
|
|
68
|
+
id,
|
|
69
|
+
}: {
|
|
70
|
+
readonly collection: Collection;
|
|
71
|
+
readonly method: Method;
|
|
72
|
+
readonly id: string | undefined;
|
|
73
|
+
}) {
|
|
74
|
+
const call = useCaller();
|
|
75
|
+
const revalidate = useRevalidate('refresh');
|
|
76
|
+
const [open, setOpen] = useState(false);
|
|
77
|
+
const [output, setOutput] = useState<
|
|
78
|
+
{ readonly value: unknown } | undefined
|
|
79
|
+
>();
|
|
80
|
+
const [refusal, setRefusal] = useState<Refusal | undefined>();
|
|
81
|
+
const { procedure } = method;
|
|
82
|
+
const label = methodLabel(method.name);
|
|
83
|
+
const summary = procedure.docs?.summary ?? '';
|
|
84
|
+
const fields = inputFieldsOf(procedure.input, id === undefined ? [] : ['id']);
|
|
85
|
+
const confirmation = {
|
|
86
|
+
title: `${label}?`,
|
|
87
|
+
description: summary,
|
|
88
|
+
action: label,
|
|
89
|
+
destructive: procedure.effect === 'destructive',
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const run = async (body: Record<string, unknown>) => {
|
|
93
|
+
const value = await call(
|
|
94
|
+
procedure,
|
|
95
|
+
id === undefined ? body : { ...body, id },
|
|
96
|
+
);
|
|
97
|
+
await revalidate(collection);
|
|
98
|
+
setOpen(false);
|
|
99
|
+
// The page's own document, refreshed: the page shows it.
|
|
100
|
+
setOutput(
|
|
101
|
+
isRecord(value) && id !== undefined && value.id === id
|
|
102
|
+
? undefined
|
|
103
|
+
: { value },
|
|
104
|
+
);
|
|
105
|
+
};
|
|
106
|
+
const runAlone = () => {
|
|
107
|
+
run({}).catch((error: unknown) => setRefusal(refusalOf(error)));
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
return (
|
|
111
|
+
<div className="flex flex-col gap-2">
|
|
112
|
+
<Button
|
|
113
|
+
type="button"
|
|
114
|
+
variant="outline"
|
|
115
|
+
title={summary}
|
|
116
|
+
onClick={() => {
|
|
117
|
+
setRefusal(undefined);
|
|
118
|
+
setOutput(undefined);
|
|
119
|
+
if (fields.length > 0) {
|
|
120
|
+
setOpen(!open);
|
|
121
|
+
} else if (procedure.effect === 'read') {
|
|
122
|
+
runAlone();
|
|
123
|
+
} else {
|
|
124
|
+
setOpen(true);
|
|
125
|
+
}
|
|
126
|
+
}}
|
|
127
|
+
>
|
|
128
|
+
{label}
|
|
129
|
+
</Button>
|
|
130
|
+
{fields.length === 0 && procedure.effect !== 'read' ? (
|
|
131
|
+
<ConfirmDialog
|
|
132
|
+
confirmation={confirmation}
|
|
133
|
+
open={open}
|
|
134
|
+
onOpenChange={setOpen}
|
|
135
|
+
onConfirm={runAlone}
|
|
136
|
+
/>
|
|
137
|
+
) : null}
|
|
138
|
+
{fields.length > 0 && open ? (
|
|
139
|
+
<Card>
|
|
140
|
+
<CardHeader>
|
|
141
|
+
<CardTitle>{label}</CardTitle>
|
|
142
|
+
<p className="text-sm text-muted-foreground">{summary}</p>
|
|
143
|
+
</CardHeader>
|
|
144
|
+
<CardContent>
|
|
145
|
+
<InputForm
|
|
146
|
+
label={label}
|
|
147
|
+
fields={fields}
|
|
148
|
+
initial={draftsOf(fields, {})}
|
|
149
|
+
submitLabel={label}
|
|
150
|
+
{...(confirmation.destructive ? { confirmation } : {})}
|
|
151
|
+
onSubmit={run}
|
|
152
|
+
/>
|
|
153
|
+
</CardContent>
|
|
154
|
+
</Card>
|
|
155
|
+
) : null}
|
|
156
|
+
{refusal === undefined ? null : <RefusalAlert refusal={refusal} />}
|
|
157
|
+
{output === undefined ? null : (
|
|
158
|
+
<MethodResult method={method} output={output.value} />
|
|
159
|
+
)}
|
|
160
|
+
</div>
|
|
161
|
+
);
|
|
162
|
+
}
|
package/src/model.tsx
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { createContext, type ReactNode, useContext } from 'react';
|
|
2
|
+
import type { Collection } from './collections.js';
|
|
3
|
+
|
|
4
|
+
const CollectionsContext = createContext<readonly Collection[]>([]);
|
|
5
|
+
|
|
6
|
+
/** The catalog's collections, for every page below. */
|
|
7
|
+
export function CollectionsProvider({
|
|
8
|
+
collections,
|
|
9
|
+
children,
|
|
10
|
+
}: {
|
|
11
|
+
readonly collections: readonly Collection[];
|
|
12
|
+
readonly children: ReactNode;
|
|
13
|
+
}) {
|
|
14
|
+
return (
|
|
15
|
+
<CollectionsContext value={collections}>{children}</CollectionsContext>
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function useCollections(): readonly Collection[] {
|
|
20
|
+
return useContext(CollectionsContext);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** The collection with `slug`, if the catalog has it. */
|
|
24
|
+
export function useCollection(
|
|
25
|
+
slug: string | undefined,
|
|
26
|
+
): Collection | undefined {
|
|
27
|
+
const collections = useCollections();
|
|
28
|
+
return collections.find((collection) => collection.slug === slug);
|
|
29
|
+
}
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { Button } from '@shadalaya/react/button';
|
|
2
|
+
import { Input } from '@shadalaya/react/input';
|
|
3
|
+
import { useState } from 'react';
|
|
4
|
+
import { isRecord } from './catalog.js';
|
|
5
|
+
import { type Collection, titleFieldOf } from './collections.js';
|
|
6
|
+
import { useCall } from './data.js';
|
|
7
|
+
import { useCollection } from './model.js';
|
|
8
|
+
import type { Field } from './schema.js';
|
|
9
|
+
|
|
10
|
+
/** How many matches a search shows. */
|
|
11
|
+
const shownMatches = 10;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A document's title: `known` when the caller already has it, else read
|
|
15
|
+
* with the target's get, else its id.
|
|
16
|
+
*/
|
|
17
|
+
function RefTitle({
|
|
18
|
+
collection,
|
|
19
|
+
title,
|
|
20
|
+
id,
|
|
21
|
+
known,
|
|
22
|
+
}: {
|
|
23
|
+
readonly collection: Collection;
|
|
24
|
+
readonly title: Field;
|
|
25
|
+
readonly id: string;
|
|
26
|
+
readonly known: string | undefined;
|
|
27
|
+
}) {
|
|
28
|
+
const { data } = useCall(
|
|
29
|
+
known === undefined ? collection.operations.get : undefined,
|
|
30
|
+
{ id },
|
|
31
|
+
);
|
|
32
|
+
const read = isRecord(data) ? data[title.name] : undefined;
|
|
33
|
+
return <>{known ?? (typeof read === 'string' ? read : id)}</>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** A match: its id, and its title to show. */
|
|
37
|
+
type Match = { readonly id: string; readonly name: string };
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A search box over the target's title field, with `contains`, and its
|
|
41
|
+
* matches as buttons that pick one. Enter picks the first match instead of
|
|
42
|
+
* submitting the form around it.
|
|
43
|
+
*/
|
|
44
|
+
function Search({
|
|
45
|
+
id,
|
|
46
|
+
collection,
|
|
47
|
+
title,
|
|
48
|
+
onPick,
|
|
49
|
+
}: {
|
|
50
|
+
readonly id: string;
|
|
51
|
+
readonly collection: Collection;
|
|
52
|
+
readonly title: Field;
|
|
53
|
+
readonly onPick: (id: string) => void;
|
|
54
|
+
}) {
|
|
55
|
+
const [text, setText] = useState('');
|
|
56
|
+
const query = text.trim();
|
|
57
|
+
const { data } = useCall(
|
|
58
|
+
query === '' ? undefined : collection.operations.find,
|
|
59
|
+
{ where: { [title.name]: { contains: query } }, limit: shownMatches },
|
|
60
|
+
);
|
|
61
|
+
// An empty search shows no matches, not the last search's.
|
|
62
|
+
const docs =
|
|
63
|
+
query !== '' && isRecord(data) && Array.isArray(data.docs) ? data.docs : [];
|
|
64
|
+
const matches: Match[] = docs.filter(isRecord).flatMap((doc) => {
|
|
65
|
+
const name = doc[title.name];
|
|
66
|
+
return typeof doc.id === 'string'
|
|
67
|
+
? [{ id: doc.id, name: typeof name === 'string' ? name : doc.id }]
|
|
68
|
+
: [];
|
|
69
|
+
});
|
|
70
|
+
const pick = (picked: string) => {
|
|
71
|
+
onPick(picked);
|
|
72
|
+
setText('');
|
|
73
|
+
};
|
|
74
|
+
return (
|
|
75
|
+
<div className="flex flex-col gap-1">
|
|
76
|
+
<Input
|
|
77
|
+
id={id}
|
|
78
|
+
type="search"
|
|
79
|
+
placeholder={`Search ${collection.label.plural}`}
|
|
80
|
+
value={text}
|
|
81
|
+
onChange={(event) => setText(event.target.value)}
|
|
82
|
+
onKeyDown={(event) => {
|
|
83
|
+
if (event.key !== 'Enter') return;
|
|
84
|
+
event.preventDefault();
|
|
85
|
+
const [first] = matches;
|
|
86
|
+
if (first !== undefined) pick(first.id);
|
|
87
|
+
}}
|
|
88
|
+
/>
|
|
89
|
+
<ul
|
|
90
|
+
aria-label={`Matching ${collection.label.plural}`}
|
|
91
|
+
className="flex flex-col"
|
|
92
|
+
>
|
|
93
|
+
{matches.map((match) => (
|
|
94
|
+
<li key={match.id}>
|
|
95
|
+
<Button
|
|
96
|
+
type="button"
|
|
97
|
+
variant="ghost"
|
|
98
|
+
size="sm"
|
|
99
|
+
onClick={() => pick(match.id)}
|
|
100
|
+
>
|
|
101
|
+
{match.name}
|
|
102
|
+
</Button>
|
|
103
|
+
</li>
|
|
104
|
+
))}
|
|
105
|
+
</ul>
|
|
106
|
+
</div>
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Picks one document of `target` by searching its title field. A target
|
|
112
|
+
* with no title field takes a plain id. `title` is the picked document's,
|
|
113
|
+
* when the caller already has it, so no get is needed to show it.
|
|
114
|
+
*/
|
|
115
|
+
export function RefPicker({
|
|
116
|
+
id,
|
|
117
|
+
target,
|
|
118
|
+
value,
|
|
119
|
+
title: known,
|
|
120
|
+
onChange,
|
|
121
|
+
}: {
|
|
122
|
+
readonly id: string;
|
|
123
|
+
readonly target: string | undefined;
|
|
124
|
+
readonly value: string;
|
|
125
|
+
readonly title?: string | undefined;
|
|
126
|
+
readonly onChange: (id: string) => void;
|
|
127
|
+
}) {
|
|
128
|
+
const collection = useCollection(target);
|
|
129
|
+
const title = collection === undefined ? undefined : titleFieldOf(collection);
|
|
130
|
+
if (collection === undefined || title === undefined) {
|
|
131
|
+
return (
|
|
132
|
+
<Input
|
|
133
|
+
id={id}
|
|
134
|
+
value={value}
|
|
135
|
+
onChange={(event) => onChange(event.target.value)}
|
|
136
|
+
/>
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
return (
|
|
140
|
+
<div className="flex flex-col gap-2">
|
|
141
|
+
{value === '' ? null : (
|
|
142
|
+
<p className="flex items-center gap-2">
|
|
143
|
+
<span>
|
|
144
|
+
<RefTitle
|
|
145
|
+
collection={collection}
|
|
146
|
+
title={title}
|
|
147
|
+
id={value}
|
|
148
|
+
known={known}
|
|
149
|
+
/>
|
|
150
|
+
</span>
|
|
151
|
+
<Button
|
|
152
|
+
type="button"
|
|
153
|
+
variant="ghost"
|
|
154
|
+
size="xs"
|
|
155
|
+
onClick={() => onChange('')}
|
|
156
|
+
>
|
|
157
|
+
Clear
|
|
158
|
+
</Button>
|
|
159
|
+
</p>
|
|
160
|
+
)}
|
|
161
|
+
<Search id={id} collection={collection} title={title} onPick={onChange} />
|
|
162
|
+
</div>
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Picks a list of documents of `target`, one search at a time. With no
|
|
168
|
+
* title field to search, the ids are typed, comma-separated, and the text
|
|
169
|
+
* is kept as typed.
|
|
170
|
+
*/
|
|
171
|
+
export function RefsPicker({
|
|
172
|
+
id,
|
|
173
|
+
target,
|
|
174
|
+
value,
|
|
175
|
+
onChange,
|
|
176
|
+
}: {
|
|
177
|
+
readonly id: string;
|
|
178
|
+
readonly target: string | undefined;
|
|
179
|
+
readonly value: string | readonly string[];
|
|
180
|
+
readonly onChange: (ids: string | readonly string[]) => void;
|
|
181
|
+
}) {
|
|
182
|
+
const collection = useCollection(target);
|
|
183
|
+
const title = collection === undefined ? undefined : titleFieldOf(collection);
|
|
184
|
+
if (collection === undefined || title === undefined) {
|
|
185
|
+
return (
|
|
186
|
+
<Input
|
|
187
|
+
id={id}
|
|
188
|
+
placeholder="Comma-separated ids"
|
|
189
|
+
value={typeof value === 'string' ? value : value.join(', ')}
|
|
190
|
+
onChange={(event) => onChange(event.target.value)}
|
|
191
|
+
/>
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
const picked = typeof value === 'string' ? value.split(',') : value;
|
|
195
|
+
return (
|
|
196
|
+
<div className="flex flex-col gap-2">
|
|
197
|
+
<ul className="flex flex-wrap gap-2">
|
|
198
|
+
{picked.map((item) => (
|
|
199
|
+
<li key={item} className="flex items-center gap-1">
|
|
200
|
+
<RefTitle
|
|
201
|
+
collection={collection}
|
|
202
|
+
title={title}
|
|
203
|
+
id={item}
|
|
204
|
+
known={undefined}
|
|
205
|
+
/>
|
|
206
|
+
<Button
|
|
207
|
+
type="button"
|
|
208
|
+
variant="ghost"
|
|
209
|
+
size="xs"
|
|
210
|
+
onClick={() => onChange(picked.filter((other) => other !== item))}
|
|
211
|
+
>
|
|
212
|
+
Remove
|
|
213
|
+
</Button>
|
|
214
|
+
</li>
|
|
215
|
+
))}
|
|
216
|
+
</ul>
|
|
217
|
+
<Search
|
|
218
|
+
id={id}
|
|
219
|
+
collection={collection}
|
|
220
|
+
title={title}
|
|
221
|
+
onPick={(added) =>
|
|
222
|
+
onChange(picked.includes(added) ? picked : [...picked, added])
|
|
223
|
+
}
|
|
224
|
+
/>
|
|
225
|
+
</div>
|
|
226
|
+
);
|
|
227
|
+
}
|