@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,146 @@
|
|
|
1
|
+
import { isRecord } from './catalog.js';
|
|
2
|
+
import { fieldLabel, pluralHeading, relationLabel } from './labels.js';
|
|
3
|
+
import { listQuery } from './list-state.js';
|
|
4
|
+
import { useCollection } from './model.js';
|
|
5
|
+
import type { Issue, Refusal } from './refusal.js';
|
|
6
|
+
import { Link, useRouter } from './router.js';
|
|
7
|
+
import { ErrorAlert } from './states.js';
|
|
8
|
+
|
|
9
|
+
/** A relation REFERENCED names: its parent collection and its name. */
|
|
10
|
+
type RelationName = { readonly collection: string; readonly name: string };
|
|
11
|
+
|
|
12
|
+
function relationOf(value: unknown): RelationName | undefined {
|
|
13
|
+
if (!isRecord(value)) return undefined;
|
|
14
|
+
const { collection, name } = value;
|
|
15
|
+
return typeof collection === 'string' && typeof name === 'string'
|
|
16
|
+
? { collection, name }
|
|
17
|
+
: undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* REFERENCED's data: the document, a collection and field that refer to
|
|
22
|
+
* it, and for a join collection the relation that derived it.
|
|
23
|
+
*/
|
|
24
|
+
function referencedOf(data: unknown) {
|
|
25
|
+
if (!isRecord(data) || !isRecord(data.by)) return undefined;
|
|
26
|
+
const { collection, id, by } = data;
|
|
27
|
+
if (
|
|
28
|
+
typeof collection !== 'string' ||
|
|
29
|
+
typeof id !== 'string' ||
|
|
30
|
+
typeof by.collection !== 'string' ||
|
|
31
|
+
typeof by.field !== 'string'
|
|
32
|
+
) {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
return {
|
|
36
|
+
collection,
|
|
37
|
+
id,
|
|
38
|
+
child: by.collection,
|
|
39
|
+
field: by.field,
|
|
40
|
+
relation: relationOf(by.relation),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** A link in the alert's text. */
|
|
45
|
+
function TextLink({
|
|
46
|
+
href,
|
|
47
|
+
text,
|
|
48
|
+
}: {
|
|
49
|
+
readonly href: string;
|
|
50
|
+
readonly text: string;
|
|
51
|
+
}) {
|
|
52
|
+
return (
|
|
53
|
+
<p>
|
|
54
|
+
<Link className="underline underline-offset-4" href={href}>
|
|
55
|
+
{text}
|
|
56
|
+
</Link>
|
|
57
|
+
</p>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* What blocks a delete, such as "Deals still refer to this company
|
|
63
|
+
* (Company)": a link to the child list, filtered to the documents that do.
|
|
64
|
+
* A join collection is not in the catalog, so its relation speaks for it:
|
|
65
|
+
* "Deals still list this contact (Contacts)", linking to the parents' list,
|
|
66
|
+
* which cannot be filtered to the ones that do.
|
|
67
|
+
*/
|
|
68
|
+
function Referenced({
|
|
69
|
+
collection,
|
|
70
|
+
id,
|
|
71
|
+
child,
|
|
72
|
+
field,
|
|
73
|
+
relation,
|
|
74
|
+
}: {
|
|
75
|
+
readonly collection: string;
|
|
76
|
+
readonly id: string;
|
|
77
|
+
readonly child: string;
|
|
78
|
+
readonly field: string;
|
|
79
|
+
readonly relation: RelationName | undefined;
|
|
80
|
+
}) {
|
|
81
|
+
const { href } = useRouter();
|
|
82
|
+
const target = useCollection(collection);
|
|
83
|
+
const children = useCollection(child);
|
|
84
|
+
const parents = useCollection(relation?.collection);
|
|
85
|
+
const what = target === undefined ? 'document' : target.label.singular;
|
|
86
|
+
if (relation !== undefined && parents !== undefined) {
|
|
87
|
+
return (
|
|
88
|
+
<TextLink
|
|
89
|
+
href={href(parents.slug)}
|
|
90
|
+
text={`${pluralHeading(parents.label)} still list this ${what} (${relationLabel(relation.name)}).`}
|
|
91
|
+
/>
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
const childField = children?.fields.find(({ name }) => name === field);
|
|
95
|
+
const fieldText = fieldLabel(childField ?? { name: field, title: undefined });
|
|
96
|
+
const text = `${children === undefined ? child : pluralHeading(children.label)} still refer to this ${what} (${fieldText}).`;
|
|
97
|
+
// A collection outside the catalog has no list of its own: the text
|
|
98
|
+
// stands alone.
|
|
99
|
+
if (children === undefined) return <p>{text}</p>;
|
|
100
|
+
const query = listQuery({
|
|
101
|
+
filters: [[field, 'eq', id]],
|
|
102
|
+
sort: undefined,
|
|
103
|
+
cursors: [],
|
|
104
|
+
});
|
|
105
|
+
return <TextLink href={`${href(children.slug)}${query}`} text={text} />;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* A refused or failed call: the server's code and message, every issue not
|
|
110
|
+
* shown at a field, and for REFERENCED what still refers to the document.
|
|
111
|
+
* A VALIDATION whose issues are all shown at fields shows nothing here.
|
|
112
|
+
*/
|
|
113
|
+
export function RefusalAlert({
|
|
114
|
+
refusal,
|
|
115
|
+
placed = () => false,
|
|
116
|
+
}: {
|
|
117
|
+
readonly refusal: Refusal;
|
|
118
|
+
/** Whether a field shows this issue, so the alert need not. */
|
|
119
|
+
readonly placed?: (issue: Issue) => boolean;
|
|
120
|
+
}) {
|
|
121
|
+
const unplaced = refusal.issues.filter((issue) => !placed(issue));
|
|
122
|
+
if (
|
|
123
|
+
refusal.code === 'VALIDATION' &&
|
|
124
|
+
refusal.issues.length > 0 &&
|
|
125
|
+
unplaced.length === 0
|
|
126
|
+
) {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
const referenced =
|
|
130
|
+
refusal.code === 'REFERENCED' ? referencedOf(refusal.data) : undefined;
|
|
131
|
+
return (
|
|
132
|
+
<ErrorAlert title={refusal.code} message={refusal.message}>
|
|
133
|
+
{referenced === undefined ? null : <Referenced {...referenced} />}
|
|
134
|
+
{unplaced.length === 0 ? null : (
|
|
135
|
+
<ul>
|
|
136
|
+
{unplaced.map((issue) => (
|
|
137
|
+
<li key={`${issue.path.join('.')}:${issue.message}`}>
|
|
138
|
+
{issue.path.length === 0 ? '' : `${issue.path.join('.')}: `}
|
|
139
|
+
{issue.message}
|
|
140
|
+
</li>
|
|
141
|
+
))}
|
|
142
|
+
</ul>
|
|
143
|
+
)}
|
|
144
|
+
</ErrorAlert>
|
|
145
|
+
);
|
|
146
|
+
}
|
package/src/refusal.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { isPFError } from '@ts-pf/protocol';
|
|
2
|
+
import { isRecord } from './catalog.js';
|
|
3
|
+
|
|
4
|
+
/** One `VALIDATION` issue: the input path it names, and why. */
|
|
5
|
+
export type Issue = {
|
|
6
|
+
readonly path: readonly (string | number)[];
|
|
7
|
+
readonly message: string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/** A failed call, as the admin shows it. */
|
|
11
|
+
export type Refusal = {
|
|
12
|
+
readonly code: string;
|
|
13
|
+
readonly message: string;
|
|
14
|
+
readonly issues: readonly Issue[];
|
|
15
|
+
/** The refusal's data, as its declared error describes it. */
|
|
16
|
+
readonly data: unknown;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function issuesOf(data: unknown): Issue[] {
|
|
20
|
+
if (!isRecord(data) || !Array.isArray(data.issues)) return [];
|
|
21
|
+
return data.issues.flatMap((issue): Issue[] => {
|
|
22
|
+
if (!isRecord(issue) || typeof issue.message !== 'string') return [];
|
|
23
|
+
const path = Array.isArray(issue.path)
|
|
24
|
+
? issue.path.filter(
|
|
25
|
+
(key): key is string | number =>
|
|
26
|
+
typeof key === 'string' || typeof key === 'number',
|
|
27
|
+
)
|
|
28
|
+
: [];
|
|
29
|
+
return [{ path, message: issue.message }];
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** What went wrong: the server's code, message and issues, or the throw's. */
|
|
34
|
+
export function refusalOf(error: unknown): Refusal {
|
|
35
|
+
if (isPFError(error)) {
|
|
36
|
+
return {
|
|
37
|
+
code: error.code,
|
|
38
|
+
message: error.message,
|
|
39
|
+
issues: issuesOf(error.data),
|
|
40
|
+
data: error.data,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
code: 'INTERNAL',
|
|
45
|
+
message: error instanceof Error ? error.message : String(error),
|
|
46
|
+
issues: [],
|
|
47
|
+
data: undefined,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The issues whose path starts with `prefix`. */
|
|
52
|
+
export function issuesUnder(
|
|
53
|
+
issues: readonly Issue[],
|
|
54
|
+
prefix: readonly (string | number)[],
|
|
55
|
+
): Issue[] {
|
|
56
|
+
return issues.filter((issue) =>
|
|
57
|
+
prefix.every((key, index) => issue.path[index] === key),
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** A refusal's issues whose path starts with `prefix`. */
|
|
62
|
+
export function issuesAt(
|
|
63
|
+
refusal: Refusal | undefined,
|
|
64
|
+
...prefix: (string | number)[]
|
|
65
|
+
): Issue[] {
|
|
66
|
+
return issuesUnder(refusal?.issues ?? [], prefix);
|
|
67
|
+
}
|
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
import { Button } from '@shadalaya/react/button';
|
|
2
|
+
import { Field, FieldError, FieldLabel } from '@shadalaya/react/field';
|
|
3
|
+
import {
|
|
4
|
+
Table,
|
|
5
|
+
TableBody,
|
|
6
|
+
TableCell,
|
|
7
|
+
TableHead,
|
|
8
|
+
TableHeader,
|
|
9
|
+
TableRow,
|
|
10
|
+
} from '@shadalaya/react/table';
|
|
11
|
+
import type { JsonSchema } from '@ts-pf/docs';
|
|
12
|
+
import { useEffect, useId, useState } from 'react';
|
|
13
|
+
import { type Collection, documentOf, type Relation } from './collections.js';
|
|
14
|
+
import { ConfirmDialog } from './confirm.js';
|
|
15
|
+
import { useCall, useCaller, useRevalidate } from './data.js';
|
|
16
|
+
import {
|
|
17
|
+
bodyOf,
|
|
18
|
+
type Drafts,
|
|
19
|
+
draftsOf,
|
|
20
|
+
FormFields,
|
|
21
|
+
type InputField,
|
|
22
|
+
inputFieldsOf,
|
|
23
|
+
} from './form.js';
|
|
24
|
+
import { fieldLabel, relationLabel, singularHeading } from './labels.js';
|
|
25
|
+
import { Pager, pageOf } from './list-page.js';
|
|
26
|
+
import { listQuery, listStateOf } from './list-state.js';
|
|
27
|
+
import { useCollection } from './model.js';
|
|
28
|
+
import { RefPicker } from './ref-picker.js';
|
|
29
|
+
import { type Issue, issuesUnder, type Refusal, refusalOf } from './refusal.js';
|
|
30
|
+
import { RefusalAlert } from './refusal-alert.js';
|
|
31
|
+
import { useRouter } from './router.js';
|
|
32
|
+
import {
|
|
33
|
+
type Field as DocumentField,
|
|
34
|
+
fieldsOf,
|
|
35
|
+
propertiesOf,
|
|
36
|
+
resolve,
|
|
37
|
+
} from './schema.js';
|
|
38
|
+
import { Loading } from './states.js';
|
|
39
|
+
import { FieldValue, refIdOf, refTitleOf } from './value.js';
|
|
40
|
+
|
|
41
|
+
/** The most links one `set` takes, as core's `maxLinks` says. */
|
|
42
|
+
const maxLinks = 1000;
|
|
43
|
+
|
|
44
|
+
/** A page of links while loading them all: `list`'s largest page. */
|
|
45
|
+
const loadPage = 100;
|
|
46
|
+
|
|
47
|
+
/** "1 link", "2 links". */
|
|
48
|
+
function links(total: number): string {
|
|
49
|
+
return total === 1 ? '1 link' : `${total} links`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A link's columns: its target, then its fields. Not the parent's ref. */
|
|
53
|
+
function linkColumns(
|
|
54
|
+
collection: Collection,
|
|
55
|
+
relation: Relation,
|
|
56
|
+
): readonly DocumentField[] {
|
|
57
|
+
const fields = fieldsOf(documentOf(relation.list)).filter(
|
|
58
|
+
(field) =>
|
|
59
|
+
!field.system &&
|
|
60
|
+
!(field.ref === collection.slug && field.ref !== relation.target),
|
|
61
|
+
);
|
|
62
|
+
return [
|
|
63
|
+
...fields.filter((field) => field.ref === relation.target),
|
|
64
|
+
...fields.filter((field) => field.ref !== relation.target),
|
|
65
|
+
];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The schema of one link in `set`'s input, and its target's field. */
|
|
69
|
+
function linkSchemaOf(relation: Relation): {
|
|
70
|
+
readonly target: string | undefined;
|
|
71
|
+
readonly fields: readonly InputField[];
|
|
72
|
+
} {
|
|
73
|
+
const input = relation.set?.input;
|
|
74
|
+
const entry =
|
|
75
|
+
input === undefined
|
|
76
|
+
? undefined
|
|
77
|
+
: propertiesOf(input).find(([name]) => name === 'links');
|
|
78
|
+
const item: JsonSchema =
|
|
79
|
+
input === undefined || entry === undefined
|
|
80
|
+
? {}
|
|
81
|
+
: resolve(entry[1].items, input);
|
|
82
|
+
const target = propertiesOf(item).find(
|
|
83
|
+
([, schema]) => schema['x-lattice-ref'] === relation.target,
|
|
84
|
+
)?.[0];
|
|
85
|
+
return {
|
|
86
|
+
target,
|
|
87
|
+
fields: inputFieldsOf(item, target === undefined ? [] : [target]),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* One link being edited: its target's id, the target's title when the list
|
|
93
|
+
* populated it, and its fields' drafts.
|
|
94
|
+
*/
|
|
95
|
+
type Row = {
|
|
96
|
+
readonly key: number;
|
|
97
|
+
readonly target: string;
|
|
98
|
+
readonly title: string | undefined;
|
|
99
|
+
readonly drafts: Drafts;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
type Loaded =
|
|
103
|
+
| { readonly state: 'loading' }
|
|
104
|
+
| {
|
|
105
|
+
readonly state: 'loaded';
|
|
106
|
+
readonly links: readonly Record<string, unknown>[];
|
|
107
|
+
}
|
|
108
|
+
| { readonly state: 'too many' }
|
|
109
|
+
| { readonly state: 'refused'; readonly refusal: Refusal };
|
|
110
|
+
|
|
111
|
+
/** Every link of the document, page by page, up to what `set` takes. */
|
|
112
|
+
function useAllLinks(relation: Relation, id: string): Loaded {
|
|
113
|
+
const call = useCaller();
|
|
114
|
+
const [loaded, setLoaded] = useState<Loaded>({ state: 'loading' });
|
|
115
|
+
const list = relation.list;
|
|
116
|
+
useEffect(() => {
|
|
117
|
+
if (list === undefined) return;
|
|
118
|
+
let current = true;
|
|
119
|
+
const load = async () => {
|
|
120
|
+
const all: Record<string, unknown>[] = [];
|
|
121
|
+
let after: string | undefined;
|
|
122
|
+
do {
|
|
123
|
+
const page = pageOf(
|
|
124
|
+
// Populated, so each row shows its target's title without a get.
|
|
125
|
+
await call(list, {
|
|
126
|
+
id,
|
|
127
|
+
limit: loadPage,
|
|
128
|
+
populate: list.expand,
|
|
129
|
+
...(after === undefined ? {} : { after }),
|
|
130
|
+
}),
|
|
131
|
+
);
|
|
132
|
+
all.push(...(page?.docs ?? []));
|
|
133
|
+
after = page?.next ?? undefined;
|
|
134
|
+
} while (after !== undefined && all.length <= maxLinks);
|
|
135
|
+
return all.length > maxLinks ? undefined : all;
|
|
136
|
+
};
|
|
137
|
+
load().then(
|
|
138
|
+
(all) => {
|
|
139
|
+
if (current) {
|
|
140
|
+
setLoaded(
|
|
141
|
+
all === undefined
|
|
142
|
+
? { state: 'too many' }
|
|
143
|
+
: { state: 'loaded', links: all },
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
(error: unknown) => {
|
|
148
|
+
if (current) setLoaded({ state: 'refused', refusal: refusalOf(error) });
|
|
149
|
+
},
|
|
150
|
+
);
|
|
151
|
+
return () => {
|
|
152
|
+
current = false;
|
|
153
|
+
};
|
|
154
|
+
}, [call, list, id]);
|
|
155
|
+
return loaded;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* The editor of a relation's `set`: every link, each with its target and
|
|
160
|
+
* fields, then the whole set sent at once after asking. A refusal keeps
|
|
161
|
+
* the edits.
|
|
162
|
+
*/
|
|
163
|
+
function SetEditor({
|
|
164
|
+
collection,
|
|
165
|
+
relation,
|
|
166
|
+
id,
|
|
167
|
+
onDone,
|
|
168
|
+
}: {
|
|
169
|
+
readonly collection: Collection;
|
|
170
|
+
readonly relation: Relation;
|
|
171
|
+
readonly id: string;
|
|
172
|
+
readonly onDone: () => void;
|
|
173
|
+
}) {
|
|
174
|
+
const call = useCaller();
|
|
175
|
+
const revalidate = useRevalidate('refresh');
|
|
176
|
+
const target = useCollection(relation.target);
|
|
177
|
+
const schema = linkSchemaOf(relation);
|
|
178
|
+
const loaded = useAllLinks(relation, id);
|
|
179
|
+
const [rows, setRows] = useState<readonly Row[] | undefined>();
|
|
180
|
+
const [asking, setAsking] = useState(false);
|
|
181
|
+
const [refusal, setRefusal] = useState<Refusal | undefined>();
|
|
182
|
+
const [problems, setProblems] = useState<readonly Issue[]>([]);
|
|
183
|
+
const pickerId = useId();
|
|
184
|
+
const set = relation.set;
|
|
185
|
+
const targetField = schema.target;
|
|
186
|
+
|
|
187
|
+
if (loaded.state === 'loading') return <Loading />;
|
|
188
|
+
if (loaded.state === 'refused')
|
|
189
|
+
return <RefusalAlert refusal={loaded.refusal} />;
|
|
190
|
+
if (loaded.state === 'too many') {
|
|
191
|
+
return (
|
|
192
|
+
<p role="alert">
|
|
193
|
+
This document has more than {maxLinks}{' '}
|
|
194
|
+
{relationLabel(relation.name).toLowerCase()}, more than one set takes.
|
|
195
|
+
Change them with a method instead.
|
|
196
|
+
</p>
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
if (set === undefined || targetField === undefined) return null;
|
|
200
|
+
const current =
|
|
201
|
+
rows ??
|
|
202
|
+
loaded.links.map((link, key) => ({
|
|
203
|
+
key,
|
|
204
|
+
target: refIdOf(link[targetField]) ?? '',
|
|
205
|
+
title: refTitleOf(link[targetField], target),
|
|
206
|
+
drafts: draftsOf(schema.fields, link),
|
|
207
|
+
}));
|
|
208
|
+
// Every link is sent whole: `set` replaces them all.
|
|
209
|
+
const made = current.map((row, index) => {
|
|
210
|
+
const { body, problems: found } = bodyOf(schema.fields, row.drafts, {});
|
|
211
|
+
return {
|
|
212
|
+
link: { [targetField]: row.target, ...body },
|
|
213
|
+
problems: found.map((issue) => ({
|
|
214
|
+
...issue,
|
|
215
|
+
path: ['links', index, ...issue.path],
|
|
216
|
+
})),
|
|
217
|
+
};
|
|
218
|
+
});
|
|
219
|
+
// An issue shows in a row when it names a row and a field the row shows.
|
|
220
|
+
const placed = ({ path: [key, index, field] }: Issue) =>
|
|
221
|
+
key === 'links' &&
|
|
222
|
+
typeof index === 'number' &&
|
|
223
|
+
index < current.length &&
|
|
224
|
+
(field === targetField || schema.fields.some(({ name }) => name === field));
|
|
225
|
+
const issues = [...problems, ...(refusal?.issues ?? [])];
|
|
226
|
+
const update = (key: number, patch: Partial<Row>) =>
|
|
227
|
+
setRows(
|
|
228
|
+
current.map((row) => (row.key === key ? { ...row, ...patch } : row)),
|
|
229
|
+
);
|
|
230
|
+
const save = async () => {
|
|
231
|
+
setRefusal(undefined);
|
|
232
|
+
try {
|
|
233
|
+
await call(set, { id, links: made.map(({ link }) => link) });
|
|
234
|
+
} catch (error) {
|
|
235
|
+
setRefusal(refusalOf(error));
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
await revalidate(collection, ...(target === undefined ? [] : [target]));
|
|
239
|
+
onDone();
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
return (
|
|
243
|
+
<form
|
|
244
|
+
aria-label={`Edit ${relationLabel(relation.name).toLowerCase()}`}
|
|
245
|
+
className="flex flex-col gap-4"
|
|
246
|
+
onSubmit={(event) => {
|
|
247
|
+
event.preventDefault();
|
|
248
|
+
const found = made.flatMap((row) => row.problems);
|
|
249
|
+
setProblems(found);
|
|
250
|
+
if (found.length === 0) setAsking(true);
|
|
251
|
+
}}
|
|
252
|
+
>
|
|
253
|
+
{current.map((row, index) => (
|
|
254
|
+
<fieldset
|
|
255
|
+
key={row.key}
|
|
256
|
+
className="flex flex-col gap-2 rounded-lg border p-3"
|
|
257
|
+
>
|
|
258
|
+
<legend className="sr-only">Link {index + 1}</legend>
|
|
259
|
+
<Field>
|
|
260
|
+
<FieldLabel htmlFor={`${pickerId}-${row.key}`}>
|
|
261
|
+
{target === undefined
|
|
262
|
+
? relationLabel(relation.name)
|
|
263
|
+
: singularHeading(target.label)}
|
|
264
|
+
</FieldLabel>
|
|
265
|
+
<RefPicker
|
|
266
|
+
id={`${pickerId}-${row.key}`}
|
|
267
|
+
target={relation.target}
|
|
268
|
+
value={row.target}
|
|
269
|
+
title={row.title}
|
|
270
|
+
onChange={(picked) =>
|
|
271
|
+
update(row.key, { target: picked, title: undefined })
|
|
272
|
+
}
|
|
273
|
+
/>
|
|
274
|
+
<FieldError
|
|
275
|
+
errors={issuesUnder(issues, ['links', index, targetField])}
|
|
276
|
+
/>
|
|
277
|
+
</Field>
|
|
278
|
+
<FormFields
|
|
279
|
+
fields={schema.fields}
|
|
280
|
+
drafts={row.drafts}
|
|
281
|
+
issues={issues}
|
|
282
|
+
at={['links', index]}
|
|
283
|
+
onChange={(drafts) => update(row.key, { drafts })}
|
|
284
|
+
/>
|
|
285
|
+
<div>
|
|
286
|
+
<Button
|
|
287
|
+
type="button"
|
|
288
|
+
variant="ghost"
|
|
289
|
+
size="sm"
|
|
290
|
+
onClick={() =>
|
|
291
|
+
setRows(current.filter(({ key }) => key !== row.key))
|
|
292
|
+
}
|
|
293
|
+
>
|
|
294
|
+
Remove link
|
|
295
|
+
</Button>
|
|
296
|
+
</div>
|
|
297
|
+
</fieldset>
|
|
298
|
+
))}
|
|
299
|
+
<div className="flex gap-2">
|
|
300
|
+
<Button
|
|
301
|
+
type="button"
|
|
302
|
+
variant="outline"
|
|
303
|
+
onClick={() =>
|
|
304
|
+
setRows([
|
|
305
|
+
...current,
|
|
306
|
+
{
|
|
307
|
+
key: Math.max(-1, ...current.map(({ key }) => key)) + 1,
|
|
308
|
+
target: '',
|
|
309
|
+
title: undefined,
|
|
310
|
+
drafts: draftsOf(schema.fields, {}),
|
|
311
|
+
},
|
|
312
|
+
])
|
|
313
|
+
}
|
|
314
|
+
>
|
|
315
|
+
Add link
|
|
316
|
+
</Button>
|
|
317
|
+
<Button type="submit">Save links</Button>
|
|
318
|
+
<Button type="button" variant="ghost" onClick={onDone}>
|
|
319
|
+
Cancel
|
|
320
|
+
</Button>
|
|
321
|
+
</div>
|
|
322
|
+
{refusal === undefined ? null : (
|
|
323
|
+
<RefusalAlert refusal={refusal} placed={placed} />
|
|
324
|
+
)}
|
|
325
|
+
<ConfirmDialog
|
|
326
|
+
confirmation={{
|
|
327
|
+
title: `Replace the ${relationLabel(relation.name).toLowerCase()}?`,
|
|
328
|
+
description: `Links not listed here are removed. ${links(current.length)} will remain.`,
|
|
329
|
+
action: 'Save links',
|
|
330
|
+
destructive: true,
|
|
331
|
+
}}
|
|
332
|
+
open={asking}
|
|
333
|
+
onOpenChange={setAsking}
|
|
334
|
+
onConfirm={() => void save()}
|
|
335
|
+
/>
|
|
336
|
+
</form>
|
|
337
|
+
);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* A relation's tab on its document's page: its links, page by page, each
|
|
342
|
+
* going to its target, and an editor for `set`.
|
|
343
|
+
*/
|
|
344
|
+
export function RelationTab({
|
|
345
|
+
collection,
|
|
346
|
+
relation,
|
|
347
|
+
id,
|
|
348
|
+
query,
|
|
349
|
+
total,
|
|
350
|
+
}: {
|
|
351
|
+
readonly collection: Collection;
|
|
352
|
+
readonly relation: Relation;
|
|
353
|
+
readonly id: string;
|
|
354
|
+
readonly query: URLSearchParams;
|
|
355
|
+
/** How many links the document has, from its populated `get`. */
|
|
356
|
+
readonly total: number | undefined;
|
|
357
|
+
}) {
|
|
358
|
+
const { href, navigate } = useRouter();
|
|
359
|
+
const [editing, setEditing] = useState(false);
|
|
360
|
+
const { cursors } = listStateOf(query);
|
|
361
|
+
const after = cursors.at(-1);
|
|
362
|
+
const list = relation.list;
|
|
363
|
+
const { data, error, isLoading } = useCall(list, {
|
|
364
|
+
id,
|
|
365
|
+
populate: list?.expand ?? [],
|
|
366
|
+
...(after === undefined ? {} : { after }),
|
|
367
|
+
});
|
|
368
|
+
const page = pageOf(data);
|
|
369
|
+
const columns = linkColumns(collection, relation);
|
|
370
|
+
const go = (next: readonly string[]) =>
|
|
371
|
+
navigate(
|
|
372
|
+
`${href(collection.slug, id, 'relations', relation.name)}${listQuery({ filters: [], sort: undefined, cursors: next })}`,
|
|
373
|
+
);
|
|
374
|
+
|
|
375
|
+
if (editing) {
|
|
376
|
+
return (
|
|
377
|
+
<SetEditor
|
|
378
|
+
collection={collection}
|
|
379
|
+
relation={relation}
|
|
380
|
+
id={id}
|
|
381
|
+
onDone={() => setEditing(false)}
|
|
382
|
+
/>
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
return (
|
|
386
|
+
<section
|
|
387
|
+
aria-label={relationLabel(relation.name)}
|
|
388
|
+
className="flex flex-col gap-4"
|
|
389
|
+
>
|
|
390
|
+
<header className="flex items-center justify-between">
|
|
391
|
+
<p className="text-sm text-muted-foreground">
|
|
392
|
+
{total === undefined ? null : links(total)}
|
|
393
|
+
</p>
|
|
394
|
+
{relation.set === undefined ? null : (
|
|
395
|
+
<Button
|
|
396
|
+
type="button"
|
|
397
|
+
variant="outline"
|
|
398
|
+
onClick={() => setEditing(true)}
|
|
399
|
+
>
|
|
400
|
+
Edit links
|
|
401
|
+
</Button>
|
|
402
|
+
)}
|
|
403
|
+
</header>
|
|
404
|
+
{error === undefined ? null : <RefusalAlert refusal={refusalOf(error)} />}
|
|
405
|
+
{isLoading && page === undefined ? <Loading /> : null}
|
|
406
|
+
{page === undefined ? null : (
|
|
407
|
+
<Table>
|
|
408
|
+
<TableHeader>
|
|
409
|
+
<TableRow>
|
|
410
|
+
{columns.map((field) => (
|
|
411
|
+
<TableHead key={field.name}>{fieldLabel(field)}</TableHead>
|
|
412
|
+
))}
|
|
413
|
+
</TableRow>
|
|
414
|
+
</TableHeader>
|
|
415
|
+
<TableBody>
|
|
416
|
+
{page.docs.map((link) => (
|
|
417
|
+
<TableRow
|
|
418
|
+
key={
|
|
419
|
+
typeof link.id === 'string' ? link.id : JSON.stringify(link)
|
|
420
|
+
}
|
|
421
|
+
>
|
|
422
|
+
{columns.map((field) => (
|
|
423
|
+
<TableCell key={field.name}>
|
|
424
|
+
<FieldValue field={field} value={link[field.name]} />
|
|
425
|
+
</TableCell>
|
|
426
|
+
))}
|
|
427
|
+
</TableRow>
|
|
428
|
+
))}
|
|
429
|
+
</TableBody>
|
|
430
|
+
</Table>
|
|
431
|
+
)}
|
|
432
|
+
<Pager
|
|
433
|
+
canBack={cursors.length > 0}
|
|
434
|
+
next={page?.next ?? null}
|
|
435
|
+
onBack={() => go(cursors.slice(0, -1))}
|
|
436
|
+
onNext={(cursor) => go([...cursors, cursor])}
|
|
437
|
+
/>
|
|
438
|
+
</section>
|
|
439
|
+
);
|
|
440
|
+
}
|