@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/filters.tsx
ADDED
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import { Button } from '@shadalaya/react/button';
|
|
2
|
+
import { Field, FieldError, FieldLabel } from '@shadalaya/react/field';
|
|
3
|
+
import { Input } from '@shadalaya/react/input';
|
|
4
|
+
import {
|
|
5
|
+
NativeSelect,
|
|
6
|
+
NativeSelectOption,
|
|
7
|
+
} from '@shadalaya/react/native-select';
|
|
8
|
+
import type { JsonSchema } from '@ts-pf/docs';
|
|
9
|
+
import { useId, useState } from 'react';
|
|
10
|
+
import { isRecord } from './catalog.js';
|
|
11
|
+
import type { Collection } from './collections.js';
|
|
12
|
+
import { fieldLabel } from './labels.js';
|
|
13
|
+
import type { Filter } from './list-state.js';
|
|
14
|
+
import type { Issue } from './refusal.js';
|
|
15
|
+
import {
|
|
16
|
+
type Field as DocumentField,
|
|
17
|
+
operatorsOf,
|
|
18
|
+
plainOf,
|
|
19
|
+
typesOf,
|
|
20
|
+
} from './schema.js';
|
|
21
|
+
|
|
22
|
+
/** A filter resolved against the collection: its field and operators. */
|
|
23
|
+
type Condition = {
|
|
24
|
+
readonly field: DocumentField;
|
|
25
|
+
readonly operators: readonly [string, JsonSchema][];
|
|
26
|
+
readonly operator: string;
|
|
27
|
+
readonly value: string;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const operatorLabels: Readonly<Record<string, string>> = {
|
|
31
|
+
eq: 'is',
|
|
32
|
+
ne: 'is not',
|
|
33
|
+
in: 'is one of',
|
|
34
|
+
nin: 'is none of',
|
|
35
|
+
gt: '>',
|
|
36
|
+
gte: '≥',
|
|
37
|
+
lt: '<',
|
|
38
|
+
lte: '≤',
|
|
39
|
+
contains: 'contains',
|
|
40
|
+
exists: 'exists',
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
function isList(operator: string): boolean {
|
|
44
|
+
return operator === 'in' || operator === 'nin';
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Whether the value is a yes or no choice, rather than typed. */
|
|
48
|
+
function isYesNo(field: DocumentField, operator: string): boolean {
|
|
49
|
+
return (
|
|
50
|
+
operator === 'exists' || (field.kind === 'boolean' && !isList(operator))
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A new condition's value: yes for a yes-or-no choice, else empty. */
|
|
55
|
+
function initialValue(field: DocumentField, operator: string): string {
|
|
56
|
+
return isYesNo(field, operator) ? 'true' : '';
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The filters whose field and operator the collection's where schema has,
|
|
61
|
+
* resolved. One per field: a later one for the same field is dropped.
|
|
62
|
+
*/
|
|
63
|
+
function conditionsOf(
|
|
64
|
+
collection: Collection,
|
|
65
|
+
filters: readonly Filter[],
|
|
66
|
+
): Condition[] {
|
|
67
|
+
const operators = operatorsOf(collection.operations.find?.input);
|
|
68
|
+
const conditions: Condition[] = [];
|
|
69
|
+
for (const [name, operator, value] of filters) {
|
|
70
|
+
const field = collection.fields.find(
|
|
71
|
+
(candidate) => candidate.name === name,
|
|
72
|
+
);
|
|
73
|
+
const available = operators.get(name) ?? [];
|
|
74
|
+
if (
|
|
75
|
+
field === undefined ||
|
|
76
|
+
!available.some(([candidate]) => candidate === operator) ||
|
|
77
|
+
conditions.some((condition) => condition.field.name === name)
|
|
78
|
+
) {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
conditions.push({ field, operators: available, operator, value });
|
|
82
|
+
}
|
|
83
|
+
return conditions;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function scalar(
|
|
87
|
+
schema: JsonSchema,
|
|
88
|
+
field: DocumentField,
|
|
89
|
+
raw: string,
|
|
90
|
+
): unknown {
|
|
91
|
+
const [type] = typesOf(schema);
|
|
92
|
+
if (type === 'boolean') return raw === 'true';
|
|
93
|
+
if (type === 'number' || type === 'integer') {
|
|
94
|
+
const number = Number(raw);
|
|
95
|
+
return Number.isNaN(number) ? raw : number;
|
|
96
|
+
}
|
|
97
|
+
if (field.kind === 'datetime') {
|
|
98
|
+
const date = new Date(raw);
|
|
99
|
+
return Number.isNaN(date.getTime()) ? raw : date.toISOString();
|
|
100
|
+
}
|
|
101
|
+
return raw;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The value as the operator's schema types it. The server checks the rest. */
|
|
105
|
+
function conditionValue(condition: Condition): unknown {
|
|
106
|
+
const entry = condition.operators.find(
|
|
107
|
+
([name]) => name === condition.operator,
|
|
108
|
+
);
|
|
109
|
+
const schema = plainOf(entry?.[1] ?? {});
|
|
110
|
+
if (typesOf(schema)[0] !== 'array') {
|
|
111
|
+
return scalar(schema, condition.field, condition.value);
|
|
112
|
+
}
|
|
113
|
+
const item = isRecord(schema.items) ? plainOf(schema.items) : {};
|
|
114
|
+
return condition.value
|
|
115
|
+
.split(',')
|
|
116
|
+
.map((part) => part.trim())
|
|
117
|
+
.filter((part) => part !== '')
|
|
118
|
+
.map((part) => scalar(item, condition.field, part));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** The where the filters make, ANDed: one key per field. */
|
|
122
|
+
export function whereOf(
|
|
123
|
+
collection: Collection,
|
|
124
|
+
filters: readonly Filter[],
|
|
125
|
+
): Record<string, unknown> {
|
|
126
|
+
return Object.fromEntries(
|
|
127
|
+
conditionsOf(collection, filters)
|
|
128
|
+
.filter((condition) => condition.value !== '')
|
|
129
|
+
.map((condition) => [
|
|
130
|
+
condition.field.name,
|
|
131
|
+
{ [condition.operator]: conditionValue(condition) },
|
|
132
|
+
]),
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** The control for one condition's value, by the operator and field. */
|
|
137
|
+
function ValueControl({
|
|
138
|
+
id,
|
|
139
|
+
condition,
|
|
140
|
+
label,
|
|
141
|
+
onChange,
|
|
142
|
+
}: {
|
|
143
|
+
readonly id: string;
|
|
144
|
+
readonly condition: Condition;
|
|
145
|
+
readonly label: string;
|
|
146
|
+
readonly onChange: (value: string) => void;
|
|
147
|
+
}) {
|
|
148
|
+
const { field, operator, value } = condition;
|
|
149
|
+
const list = isList(operator);
|
|
150
|
+
if (isYesNo(field, operator)) {
|
|
151
|
+
return (
|
|
152
|
+
<NativeSelect
|
|
153
|
+
id={id}
|
|
154
|
+
aria-label={label}
|
|
155
|
+
value={value}
|
|
156
|
+
onChange={(event) => onChange(event.target.value)}
|
|
157
|
+
>
|
|
158
|
+
<NativeSelectOption value="true">Yes</NativeSelectOption>
|
|
159
|
+
<NativeSelectOption value="false">No</NativeSelectOption>
|
|
160
|
+
</NativeSelect>
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
if (field.kind === 'enum' && !list) {
|
|
164
|
+
return (
|
|
165
|
+
<NativeSelect
|
|
166
|
+
id={id}
|
|
167
|
+
aria-label={label}
|
|
168
|
+
value={value}
|
|
169
|
+
onChange={(event) => onChange(event.target.value)}
|
|
170
|
+
>
|
|
171
|
+
<NativeSelectOption value="">Any</NativeSelectOption>
|
|
172
|
+
{field.values.map((option) => (
|
|
173
|
+
<NativeSelectOption key={option} value={option}>
|
|
174
|
+
{option}
|
|
175
|
+
</NativeSelectOption>
|
|
176
|
+
))}
|
|
177
|
+
</NativeSelect>
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
const type = list
|
|
181
|
+
? 'text'
|
|
182
|
+
: field.kind === 'number'
|
|
183
|
+
? 'number'
|
|
184
|
+
: field.kind === 'datetime'
|
|
185
|
+
? 'datetime-local'
|
|
186
|
+
: 'text';
|
|
187
|
+
return (
|
|
188
|
+
<Input
|
|
189
|
+
id={id}
|
|
190
|
+
aria-label={label}
|
|
191
|
+
type={type}
|
|
192
|
+
value={value}
|
|
193
|
+
placeholder={list ? 'Comma-separated' : ''}
|
|
194
|
+
onChange={(event) => onChange(event.target.value)}
|
|
195
|
+
/>
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** One condition's row: its field, operator, value, and any issue. */
|
|
200
|
+
function ConditionRow({
|
|
201
|
+
condition,
|
|
202
|
+
issues,
|
|
203
|
+
onChange,
|
|
204
|
+
onRemove,
|
|
205
|
+
}: {
|
|
206
|
+
readonly condition: Condition;
|
|
207
|
+
readonly issues: readonly Issue[];
|
|
208
|
+
readonly onChange: (operator: string, value: string) => void;
|
|
209
|
+
readonly onRemove: () => void;
|
|
210
|
+
}) {
|
|
211
|
+
const id = useId();
|
|
212
|
+
const label = fieldLabel(condition.field);
|
|
213
|
+
return (
|
|
214
|
+
<Field orientation="horizontal">
|
|
215
|
+
<FieldLabel htmlFor={id} className="w-32 shrink-0">
|
|
216
|
+
{label}
|
|
217
|
+
</FieldLabel>
|
|
218
|
+
<NativeSelect
|
|
219
|
+
aria-label={`${label} operator`}
|
|
220
|
+
value={condition.operator}
|
|
221
|
+
onChange={(event) =>
|
|
222
|
+
onChange(
|
|
223
|
+
event.target.value,
|
|
224
|
+
initialValue(condition.field, event.target.value),
|
|
225
|
+
)
|
|
226
|
+
}
|
|
227
|
+
>
|
|
228
|
+
{condition.operators.map(([operator]) => (
|
|
229
|
+
<NativeSelectOption key={operator} value={operator}>
|
|
230
|
+
{operatorLabels[operator] ?? operator}
|
|
231
|
+
</NativeSelectOption>
|
|
232
|
+
))}
|
|
233
|
+
</NativeSelect>
|
|
234
|
+
<ValueControl
|
|
235
|
+
id={id}
|
|
236
|
+
condition={condition}
|
|
237
|
+
label={`${label} value`}
|
|
238
|
+
onChange={(value) => onChange(condition.operator, value)}
|
|
239
|
+
/>
|
|
240
|
+
<Button type="button" variant="ghost" size="sm" onClick={onRemove}>
|
|
241
|
+
Remove
|
|
242
|
+
</Button>
|
|
243
|
+
<FieldError errors={[...issues]} />
|
|
244
|
+
</Field>
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* The list's filters: at most one condition per field, ANDed, with the
|
|
250
|
+
* operators the where schema gives each field. They apply on submit. A
|
|
251
|
+
* `VALIDATION` issue on a field shows at its condition, with the server's
|
|
252
|
+
* message.
|
|
253
|
+
*/
|
|
254
|
+
export function Filters({
|
|
255
|
+
collection,
|
|
256
|
+
applied,
|
|
257
|
+
issues,
|
|
258
|
+
onApply,
|
|
259
|
+
}: {
|
|
260
|
+
readonly collection: Collection;
|
|
261
|
+
readonly applied: readonly Filter[];
|
|
262
|
+
readonly issues: (field: string) => readonly Issue[];
|
|
263
|
+
readonly onApply: (filters: readonly Filter[]) => void;
|
|
264
|
+
}) {
|
|
265
|
+
const [drafts, setDrafts] = useState<readonly Filter[]>(applied);
|
|
266
|
+
const [picked, setPicked] = useState('');
|
|
267
|
+
const conditions = conditionsOf(collection, drafts);
|
|
268
|
+
const operators = operatorsOf(collection.operations.find?.input);
|
|
269
|
+
const unused = collection.fields.filter(
|
|
270
|
+
(field) =>
|
|
271
|
+
operators.has(field.name) &&
|
|
272
|
+
!conditions.some((condition) => condition.field.name === field.name),
|
|
273
|
+
);
|
|
274
|
+
|
|
275
|
+
const add = (name: string) => {
|
|
276
|
+
const field = unused.find((candidate) => candidate.name === name);
|
|
277
|
+
const first = operators.get(name)?.[0];
|
|
278
|
+
if (field === undefined || first === undefined) return;
|
|
279
|
+
const [operator] = first;
|
|
280
|
+
setDrafts([...drafts, [name, operator, initialValue(field, operator)]]);
|
|
281
|
+
setPicked('');
|
|
282
|
+
};
|
|
283
|
+
const change = (name: string, operator: string, value: string) =>
|
|
284
|
+
setDrafts(
|
|
285
|
+
drafts.map((filter) =>
|
|
286
|
+
filter[0] === name ? [name, operator, value] : filter,
|
|
287
|
+
),
|
|
288
|
+
);
|
|
289
|
+
const remove = (name: string) =>
|
|
290
|
+
setDrafts(drafts.filter(([field]) => field !== name));
|
|
291
|
+
|
|
292
|
+
return (
|
|
293
|
+
<form
|
|
294
|
+
aria-label="Filters"
|
|
295
|
+
className="flex flex-col gap-3"
|
|
296
|
+
onSubmit={(event) => {
|
|
297
|
+
event.preventDefault();
|
|
298
|
+
onApply(
|
|
299
|
+
conditionsOf(collection, drafts).map(({ field, operator, value }) => [
|
|
300
|
+
field.name,
|
|
301
|
+
operator,
|
|
302
|
+
value,
|
|
303
|
+
]),
|
|
304
|
+
);
|
|
305
|
+
}}
|
|
306
|
+
>
|
|
307
|
+
{conditions.map((condition) => (
|
|
308
|
+
<ConditionRow
|
|
309
|
+
key={condition.field.name}
|
|
310
|
+
condition={condition}
|
|
311
|
+
issues={issues(condition.field.name)}
|
|
312
|
+
onChange={(operator, value) =>
|
|
313
|
+
change(condition.field.name, operator, value)
|
|
314
|
+
}
|
|
315
|
+
onRemove={() => remove(condition.field.name)}
|
|
316
|
+
/>
|
|
317
|
+
))}
|
|
318
|
+
<div className="flex items-center gap-2">
|
|
319
|
+
<NativeSelect
|
|
320
|
+
aria-label="Filter by"
|
|
321
|
+
value={picked}
|
|
322
|
+
onChange={(event) => setPicked(event.target.value)}
|
|
323
|
+
>
|
|
324
|
+
<NativeSelectOption value="">Filter by…</NativeSelectOption>
|
|
325
|
+
{unused.map((field) => (
|
|
326
|
+
<NativeSelectOption key={field.name} value={field.name}>
|
|
327
|
+
{fieldLabel(field)}
|
|
328
|
+
</NativeSelectOption>
|
|
329
|
+
))}
|
|
330
|
+
</NativeSelect>
|
|
331
|
+
<Button
|
|
332
|
+
type="button"
|
|
333
|
+
variant="outline"
|
|
334
|
+
size="sm"
|
|
335
|
+
disabled={picked === ''}
|
|
336
|
+
onClick={() => add(picked)}
|
|
337
|
+
>
|
|
338
|
+
Add filter
|
|
339
|
+
</Button>
|
|
340
|
+
<Button type="submit" size="sm">
|
|
341
|
+
Apply filters
|
|
342
|
+
</Button>
|
|
343
|
+
</div>
|
|
344
|
+
</form>
|
|
345
|
+
);
|
|
346
|
+
}
|