simetra 0.2.0 → 0.3.0-next.1
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/dist/channel-grant-BATD6oQu.js +15 -0
- package/dist/compiler/index.d.ts +684 -77
- package/dist/compiler/index.js +24598 -13501
- package/dist/data/centrifugo/index.d.ts +24 -0
- package/dist/data/centrifugo/index.js +173 -0
- package/dist/data/index.d.ts +27 -0
- package/dist/data/index.js +9779 -0
- package/dist/data/react/index.d.ts +103 -0
- package/dist/data/react/index.js +214 -0
- package/dist/grant-Zg0hmqK7.d.ts +11 -0
- package/dist/handle-C6PFsnUX.js +44 -0
- package/dist/holder-CkR-qRYU.d.ts +585 -0
- package/dist/model/index.d.ts +4118 -476
- package/dist/model/index.js +4267 -380
- package/dist/schema/index.d.ts +1523 -572
- package/dist/schema/index.js +11698 -7402
- package/dist/server/index.d.ts +665 -21
- package/dist/server/index.js +4279 -426
- package/dist/server/signals/index.d.ts +92 -0
- package/dist/server/signals/index.js +140 -0
- package/dist/shell/index.d.ts +100 -0
- package/dist/shell/index.js +148 -0
- package/dist/ui/index.d.ts +213 -0
- package/dist/ui/index.js +1075 -0
- package/package.json +74 -3
- package/schemas/accumulation-registers.schema.json +46 -5
- package/schemas/catalogs.schema.json +110 -7
- package/schemas/constants.schema.json +31 -5
- package/schemas/custom-tables.schema.json +24 -0
- package/schemas/documents.schema.json +83 -5
- package/schemas/functions.schema.json +129 -0
- package/schemas/information-registers.schema.json +47 -5
- package/schemas/project.schema.json +11 -1
- package/schemas/rights-sets.schema.json +3 -1
- package/schemas/roles.schema.json +6 -2
package/dist/ui/index.js
ADDED
|
@@ -0,0 +1,1075 @@
|
|
|
1
|
+
import { catalogIndex, encodeItemValue } from "simetra/model";
|
|
2
|
+
import { createContext, createElement, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useReducer, useRef, useState, useSyncExternalStore } from "react";
|
|
3
|
+
import { useVirtualizer } from "@tanstack/react-virtual";
|
|
4
|
+
import { useRow, useRows, useWriter } from "simetra/data/react";
|
|
5
|
+
import { useForm, useSelector } from "@tanstack/react-form";
|
|
6
|
+
// src/ui/list/virtual-list.ts
|
|
7
|
+
const OVERSCAN = 6;
|
|
8
|
+
const noopSubscribe = () => () => {};
|
|
9
|
+
function readAnchor(node) {
|
|
10
|
+
if (node === null) return null;
|
|
11
|
+
const port = node.getBoundingClientRect().top;
|
|
12
|
+
for (const row of node.querySelectorAll("[data-row-key]")) {
|
|
13
|
+
const rect = row.getBoundingClientRect();
|
|
14
|
+
if (rect.bottom > port + 1) return {
|
|
15
|
+
key: row.getAttribute("data-row-key"),
|
|
16
|
+
top: rect.top - port
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
function readRowTop(node, key) {
|
|
22
|
+
for (const row of node.querySelectorAll("[data-row-key]")) if (row.getAttribute("data-row-key") === key) return row.getBoundingClientRect().top - node.getBoundingClientRect().top;
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A virtualized list over the rows of `useRows` (P4-B spec §6.4, spike 4 §6).
|
|
27
|
+
* Slots are keyed by the row key and stand under one `translateY`; the
|
|
28
|
+
* virtualizer's own scroll compensation of a grown row is off, the first
|
|
29
|
+
* visible row is anchored in a layout effect instead (one frame, no shift).
|
|
30
|
+
* A new `chain` remounts the list at its `target`.
|
|
31
|
+
*/
|
|
32
|
+
function VirtualList(props) {
|
|
33
|
+
const reset = useRef(null);
|
|
34
|
+
if (reset.current === null || props.chain !== void 0 && props.chain !== reset.current.chain && props.target !== "keep") reset.current = {
|
|
35
|
+
chain: props.chain ?? 0,
|
|
36
|
+
target: props.target ?? "start"
|
|
37
|
+
};
|
|
38
|
+
return createElement(Scroller, {
|
|
39
|
+
...props,
|
|
40
|
+
key: reset.current.chain,
|
|
41
|
+
startAt: reset.current.target
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function Scroller(props) {
|
|
45
|
+
const { rows, rowHeight, height, measure = false, setVisibleRange } = props;
|
|
46
|
+
const before = props.atStart === false ? 1 : 0;
|
|
47
|
+
const after = props.atEnd === false ? 1 : 0;
|
|
48
|
+
const count = before + rows.length + after;
|
|
49
|
+
const keyAt = useCallback((i) => i < before ? "@before" : i < before + rows.length ? String(rows[i - before].key) : "@after", [rows, before]);
|
|
50
|
+
const el = useRef(null);
|
|
51
|
+
const initial = props.startAt === "end" ? Math.max(0, count * rowHeight - height) : 0;
|
|
52
|
+
const virtualizer = useVirtualizer({
|
|
53
|
+
count,
|
|
54
|
+
getScrollElement: () => el.current,
|
|
55
|
+
estimateSize: () => rowHeight,
|
|
56
|
+
measureElement: (node, entry) => Math.round(entry?.borderBoxSize[0]?.blockSize ?? node.getBoundingClientRect().height),
|
|
57
|
+
getItemKey: keyAt,
|
|
58
|
+
overscan: OVERSCAN,
|
|
59
|
+
useFlushSync: true,
|
|
60
|
+
initialRect: {
|
|
61
|
+
width: props.initialWidth ?? 1e3,
|
|
62
|
+
height
|
|
63
|
+
},
|
|
64
|
+
initialOffset: initial,
|
|
65
|
+
anchorTo: "end"
|
|
66
|
+
});
|
|
67
|
+
virtualizer.shouldAdjustScrollPositionOnItemSizeChange = () => false;
|
|
68
|
+
const setEl = useCallback((node) => {
|
|
69
|
+
if (node !== null && el.current === null && initial > 0) node.scrollTop = node.scrollHeight - node.clientHeight;
|
|
70
|
+
el.current = node;
|
|
71
|
+
}, [initial]);
|
|
72
|
+
const items = virtualizer.getVirtualItems();
|
|
73
|
+
const anchor = useRef(null);
|
|
74
|
+
anchor.current = measure ? readAnchor(el.current) : null;
|
|
75
|
+
useLayoutEffect(() => {
|
|
76
|
+
const a = anchor.current;
|
|
77
|
+
const node = el.current;
|
|
78
|
+
if (a === null || node === null) return;
|
|
79
|
+
for (const slot of node.querySelectorAll("[data-index]")) virtualizer.measureElement(slot);
|
|
80
|
+
const top = readRowTop(node, a.key);
|
|
81
|
+
if (top === null) return;
|
|
82
|
+
const delta = top - a.top;
|
|
83
|
+
if (Math.abs(delta) >= .5) node.scrollTop += delta;
|
|
84
|
+
});
|
|
85
|
+
const offset = virtualizer.scrollOffset ?? initial;
|
|
86
|
+
let first;
|
|
87
|
+
let last;
|
|
88
|
+
for (const it of items) {
|
|
89
|
+
if (it.end <= offset || it.start >= offset + height) continue;
|
|
90
|
+
const i = it.index - before;
|
|
91
|
+
if (i < 0 || i >= rows.length) continue;
|
|
92
|
+
first ??= i;
|
|
93
|
+
last = i;
|
|
94
|
+
}
|
|
95
|
+
const hydrating = useSyncExternalStore(noopSubscribe, () => false, () => true);
|
|
96
|
+
const [active, setActive] = useState(() => !hydrating);
|
|
97
|
+
useEffect(() => {
|
|
98
|
+
if (active && first !== void 0 && last !== void 0) setVisibleRange({
|
|
99
|
+
first,
|
|
100
|
+
last
|
|
101
|
+
});
|
|
102
|
+
}, [
|
|
103
|
+
setVisibleRange,
|
|
104
|
+
first,
|
|
105
|
+
last,
|
|
106
|
+
active
|
|
107
|
+
]);
|
|
108
|
+
const lift = {
|
|
109
|
+
position: "absolute",
|
|
110
|
+
top: 0,
|
|
111
|
+
left: 0,
|
|
112
|
+
right: 0,
|
|
113
|
+
transform: `translateY(${items[0]?.start ?? 0}px)`
|
|
114
|
+
};
|
|
115
|
+
const slotStyle = measure ? void 0 : {
|
|
116
|
+
height: rowHeight,
|
|
117
|
+
overflow: "hidden"
|
|
118
|
+
};
|
|
119
|
+
return createElement("div", {
|
|
120
|
+
className: props.className,
|
|
121
|
+
ref: setEl,
|
|
122
|
+
"data-scroll": "list",
|
|
123
|
+
style: {
|
|
124
|
+
height,
|
|
125
|
+
overflowY: "auto",
|
|
126
|
+
overflowAnchor: "none"
|
|
127
|
+
},
|
|
128
|
+
onScroll: active ? void 0 : () => setActive(true)
|
|
129
|
+
}, createElement("div", { style: {
|
|
130
|
+
height: virtualizer.getTotalSize(),
|
|
131
|
+
position: "relative"
|
|
132
|
+
} }, createElement("div", { style: lift }, items.map((it) => {
|
|
133
|
+
const i = it.index - before;
|
|
134
|
+
const ref = measure ? virtualizer.measureElement : void 0;
|
|
135
|
+
if (i < 0 || i >= rows.length) return createElement("div", {
|
|
136
|
+
key: it.key,
|
|
137
|
+
ref,
|
|
138
|
+
"data-index": it.index,
|
|
139
|
+
"data-loading": i < 0 ? "before" : "after",
|
|
140
|
+
style: slotStyle
|
|
141
|
+
}, props.renderPlaceholder?.(i < 0 ? "before" : "after"));
|
|
142
|
+
const row = rows[i];
|
|
143
|
+
return createElement("div", {
|
|
144
|
+
key: it.key,
|
|
145
|
+
ref,
|
|
146
|
+
"data-index": it.index,
|
|
147
|
+
"data-row-key": String(row.key),
|
|
148
|
+
"data-state": props.rowState?.(row),
|
|
149
|
+
style: slotStyle
|
|
150
|
+
}, props.renderRow(row, i));
|
|
151
|
+
}))));
|
|
152
|
+
}
|
|
153
|
+
// src/ui/catalog.ts
|
|
154
|
+
const CatalogContext = createContext(null);
|
|
155
|
+
/** Provides the client catalog (`client-catalog.json`) to T5 hosts. */
|
|
156
|
+
function CatalogProvider(props) {
|
|
157
|
+
const index = useMemo(() => catalogIndex(props.catalog.objects), [props.catalog]);
|
|
158
|
+
return createElement(CatalogContext.Provider, { value: index }, props.children);
|
|
159
|
+
}
|
|
160
|
+
/** Внутрішній доступ хостів до цілей `Ref` за id; не публічний API. */
|
|
161
|
+
function useCatalogIndex() {
|
|
162
|
+
const index = useContext(CatalogContext);
|
|
163
|
+
if (index === null) throw new Error("useCatalogEntry requires a CatalogProvider");
|
|
164
|
+
return index;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* The catalog entry of `object` (`"<kind>.<name>"`). Throws `Error` without a
|
|
168
|
+
* `CatalogProvider` and `TypeError` for an object outside the catalog.
|
|
169
|
+
*/
|
|
170
|
+
function useCatalogEntry(object) {
|
|
171
|
+
const entry = useCatalogIndex().byRef.get(object);
|
|
172
|
+
if (entry === void 0) throw new TypeError(`useCatalogEntry: unknown object ${object}`);
|
|
173
|
+
return entry;
|
|
174
|
+
}
|
|
175
|
+
// src/ui/fields/columns.ts
|
|
176
|
+
/**
|
|
177
|
+
* Службова колонка — не реквізит для людини: ключ, версія, колонка скоупу й
|
|
178
|
+
* колонка-мітка пари (`typeColumn`). Ключ і версія в `attributes` каталогу вже
|
|
179
|
+
* не трапляються, але фільтр їх називає, щоб форма каталогу не вирішувала, що
|
|
180
|
+
* показувати; колонка скоупу в скоупленого довідника є й реквізитом.
|
|
181
|
+
*/
|
|
182
|
+
function serviceColumns(entry) {
|
|
183
|
+
return /* @__PURE__ */ new Set([
|
|
184
|
+
entry.key,
|
|
185
|
+
entry.versionColumn,
|
|
186
|
+
...entry.scopeColumn === void 0 ? [] : [entry.scopeColumn],
|
|
187
|
+
...entry.attributes.flatMap((a) => a.pair === void 0 ? [] : [a.pair.typeColumn])
|
|
188
|
+
]);
|
|
189
|
+
}
|
|
190
|
+
function columnsOf(entry, include) {
|
|
191
|
+
const service = serviceColumns(entry);
|
|
192
|
+
return entry.attributes.filter((a) => include(a) && !service.has(a.column)).map((attribute) => ({
|
|
193
|
+
name: attribute.name,
|
|
194
|
+
title: attribute.name,
|
|
195
|
+
attribute
|
|
196
|
+
}));
|
|
197
|
+
}
|
|
198
|
+
/** List columns: eager header attributes in catalog order, no service ones. */
|
|
199
|
+
function headerColumns(entry) {
|
|
200
|
+
return columnsOf(entry, (a) => a.load !== "lazy");
|
|
201
|
+
}
|
|
202
|
+
/** Card fields: like {@link headerColumns}, lazy attributes included. */
|
|
203
|
+
function cardFields(entry) {
|
|
204
|
+
return columnsOf(entry, () => true);
|
|
205
|
+
}
|
|
206
|
+
/** UI texts per locale; `en` is the default. */
|
|
207
|
+
const MESSAGES = {
|
|
208
|
+
en: {
|
|
209
|
+
"state.pending": "Saving…",
|
|
210
|
+
"state.unknown": "Checking the result…",
|
|
211
|
+
"state.failed": "Not saved",
|
|
212
|
+
"state.conflict": "Changed by someone else",
|
|
213
|
+
"state.stale": "May be out of date",
|
|
214
|
+
"state.hidden": "Access changed — values hidden, checking",
|
|
215
|
+
"state.removed": "Row unavailable",
|
|
216
|
+
"state.loading": "Loading…",
|
|
217
|
+
"state.refreshing": "Updating…",
|
|
218
|
+
"state.purged": "Access changed — data hidden",
|
|
219
|
+
"action.open": "Open",
|
|
220
|
+
"action.save": "Save",
|
|
221
|
+
"action.dismiss": "Dismiss",
|
|
222
|
+
"action.retry": "Retry",
|
|
223
|
+
"action.cancel": "Cancel",
|
|
224
|
+
"action.takeServer": "Take server version",
|
|
225
|
+
"action.overwrite": "Overwrite",
|
|
226
|
+
"action.remove": "Remove",
|
|
227
|
+
"action.showNew": "New rows — show",
|
|
228
|
+
"action.refreshList": "The list changed — refresh",
|
|
229
|
+
"action.refreshCard": "The card changed — refresh",
|
|
230
|
+
"action.reloadPage": "Reload the page",
|
|
231
|
+
"value.yes": "Yes",
|
|
232
|
+
"value.no": "No",
|
|
233
|
+
"value.notInSlice": "Not shown in this slice",
|
|
234
|
+
"app.updateRequired": "An app update is required"
|
|
235
|
+
},
|
|
236
|
+
uk: {
|
|
237
|
+
"state.pending": "Зберігається…",
|
|
238
|
+
"state.unknown": "Перевіряємо результат…",
|
|
239
|
+
"state.failed": "Не збережено",
|
|
240
|
+
"state.conflict": "Змінено іншим",
|
|
241
|
+
"state.stale": "Може бути застарілим",
|
|
242
|
+
"state.hidden": "Доступ змінився — значення приховано, перевіряємо",
|
|
243
|
+
"state.removed": "Рядок недоступний",
|
|
244
|
+
"state.loading": "Завантаження…",
|
|
245
|
+
"state.refreshing": "Оновлення…",
|
|
246
|
+
"state.purged": "Доступ змінився — дані приховано",
|
|
247
|
+
"action.open": "Відкрити",
|
|
248
|
+
"action.save": "Зберегти",
|
|
249
|
+
"action.dismiss": "Скасувати показ",
|
|
250
|
+
"action.retry": "Повторити",
|
|
251
|
+
"action.cancel": "Скасувати",
|
|
252
|
+
"action.takeServer": "Взяти серверне",
|
|
253
|
+
"action.overwrite": "Перезаписати",
|
|
254
|
+
"action.remove": "Прибрати",
|
|
255
|
+
"action.showNew": "Є нові рядки — показати",
|
|
256
|
+
"action.refreshList": "Список змінився — оновити",
|
|
257
|
+
"action.refreshCard": "Картка змінилась — оновити",
|
|
258
|
+
"action.reloadPage": "Оновити сторінку",
|
|
259
|
+
"value.yes": "Так",
|
|
260
|
+
"value.no": "Ні",
|
|
261
|
+
"value.notInSlice": "Не показується в зрізі",
|
|
262
|
+
"app.updateRequired": "Потрібне оновлення застосунку"
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
const LocaleContext = createContext("en");
|
|
266
|
+
/** Selects the locale for `useMessage` in the subtree. */
|
|
267
|
+
function MessagesProvider(props) {
|
|
268
|
+
return createElement(LocaleContext.Provider, { value: props.locale }, props.children);
|
|
269
|
+
}
|
|
270
|
+
/** Text for `key` in the current locale (`en` without a provider). */
|
|
271
|
+
function useMessage(key) {
|
|
272
|
+
return MESSAGES[useContext(LocaleContext)][key];
|
|
273
|
+
}
|
|
274
|
+
// src/ui/fields/ref-presentation.ts
|
|
275
|
+
/**
|
|
276
|
+
* The text of a `Ref` to an object: its main presentation attribute, else the
|
|
277
|
+
* key (P4-D spec §5.3). The attribute is the entry's `presentation`, derived
|
|
278
|
+
* from the kind registry's standard columns and `mainPresentation`; an
|
|
279
|
+
* attribute name is never guessed, since a custom `description` is not it.
|
|
280
|
+
* A removed or missing target shows an empty text.
|
|
281
|
+
*/
|
|
282
|
+
function refPresentation(row, target) {
|
|
283
|
+
if (row === null || row.state === "removed") return "";
|
|
284
|
+
const attribute = target.presentation?.attribute;
|
|
285
|
+
const value = attribute === void 0 ? void 0 : row.values[attribute];
|
|
286
|
+
return typeof value === "string" && value !== "" ? value : row.key;
|
|
287
|
+
}
|
|
288
|
+
// src/ui/fields/field.ts
|
|
289
|
+
function fieldKind(a) {
|
|
290
|
+
if (a.array === true || a.pair !== void 0) return "unsupported";
|
|
291
|
+
switch (a.type) {
|
|
292
|
+
case "String": return "text";
|
|
293
|
+
case "Text": return "longText";
|
|
294
|
+
case "Integer":
|
|
295
|
+
case "SmallInt": return "integer";
|
|
296
|
+
case "BigInt":
|
|
297
|
+
case "Numeric": return "decimal";
|
|
298
|
+
case "Boolean": return "boolean";
|
|
299
|
+
case "Date": return "date";
|
|
300
|
+
case "DateTime": return "dateTime";
|
|
301
|
+
case "Ref": return a.enumValues === void 0 ? "object" : "enum";
|
|
302
|
+
case "UUID": return "uuid";
|
|
303
|
+
default: return "unsupported";
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
/** Whether the slice can edit the attribute at all; the rest is read-only. */
|
|
307
|
+
function isEditableType(attribute) {
|
|
308
|
+
const kind = fieldKind(attribute);
|
|
309
|
+
return kind !== "object" && kind !== "uuid" && kind !== "unsupported";
|
|
310
|
+
}
|
|
311
|
+
/** Shows a value by the logical type of its attribute, without styling. */
|
|
312
|
+
function FieldView({ attribute, value, target }) {
|
|
313
|
+
const yes = useMessage("value.yes");
|
|
314
|
+
const no = useMessage("value.no");
|
|
315
|
+
const notInSlice = useMessage("value.notInSlice");
|
|
316
|
+
const kind = fieldKind(attribute);
|
|
317
|
+
let text;
|
|
318
|
+
if (kind === "unsupported") text = notInSlice;
|
|
319
|
+
else if (value === null || value === void 0) text = "";
|
|
320
|
+
else if (kind === "boolean") text = value === true ? yes : no;
|
|
321
|
+
else if (kind === "enum") text = attribute.enumValues?.find((v) => v.name === value)?.label ?? String(value);
|
|
322
|
+
else if (kind === "object") text = target === void 0 ? String(value) : refPresentation(target.row, target.entry);
|
|
323
|
+
else text = String(value);
|
|
324
|
+
return createElement("span", {
|
|
325
|
+
"data-field": attribute.name,
|
|
326
|
+
"data-type": attribute.type
|
|
327
|
+
}, text);
|
|
328
|
+
}
|
|
329
|
+
const INTEGER_INPUT = /^-?\d+$/;
|
|
330
|
+
const pad = (n, width = 2) => String(n).padStart(width, "0");
|
|
331
|
+
function toLocalInput(value) {
|
|
332
|
+
if (value === null || value === void 0) return "";
|
|
333
|
+
if (typeof value !== "string") return String(value);
|
|
334
|
+
const date = new Date(value);
|
|
335
|
+
if (Number.isNaN(date.getTime())) return value;
|
|
336
|
+
const ms = date.getMilliseconds();
|
|
337
|
+
return `${pad(date.getFullYear(), 4)}-${pad(date.getMonth() + 1)}-${pad(date.getDate())}T${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}` + (ms === 0 ? "" : `.${pad(ms, 3)}`);
|
|
338
|
+
}
|
|
339
|
+
function fromLocalInput(text) {
|
|
340
|
+
const date = new Date(text);
|
|
341
|
+
return Number.isNaN(date.getTime()) ? text : date.toISOString();
|
|
342
|
+
}
|
|
343
|
+
function read(kind, event) {
|
|
344
|
+
const target = event.target;
|
|
345
|
+
if (kind === "boolean") return target.checked;
|
|
346
|
+
if (target.value === "") return null;
|
|
347
|
+
if (kind === "integer" && INTEGER_INPUT.test(target.value)) return Number(target.value);
|
|
348
|
+
if (kind === "dateTime") return fromLocalInput(target.value);
|
|
349
|
+
return target.value;
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Edits a value by the logical type; types the slice does not edit render
|
|
353
|
+
* {@link FieldView}. Validation is the T0 codec `encodeItemValue`, the same as
|
|
354
|
+
* the core uses.
|
|
355
|
+
*/
|
|
356
|
+
function FieldEditor({ attribute, value, onChange, disabled }) {
|
|
357
|
+
const kind = fieldKind(attribute);
|
|
358
|
+
if (!isEditableType(attribute)) return createElement(FieldView, {
|
|
359
|
+
attribute,
|
|
360
|
+
value
|
|
361
|
+
});
|
|
362
|
+
const emit = (next) => {
|
|
363
|
+
const encoded = encodeItemValue(attribute, next, attribute.name);
|
|
364
|
+
if ("error" in encoded) onChange(next, encoded.error);
|
|
365
|
+
else onChange(next);
|
|
366
|
+
};
|
|
367
|
+
const common = {
|
|
368
|
+
name: attribute.name,
|
|
369
|
+
"data-field": attribute.name,
|
|
370
|
+
disabled
|
|
371
|
+
};
|
|
372
|
+
const text = value === null || value === void 0 ? "" : String(value);
|
|
373
|
+
switch (kind) {
|
|
374
|
+
case "boolean": return createElement("input", {
|
|
375
|
+
...common,
|
|
376
|
+
type: "checkbox",
|
|
377
|
+
checked: value === true,
|
|
378
|
+
onChange: (e) => emit(read(kind, e))
|
|
379
|
+
});
|
|
380
|
+
case "longText": return createElement("textarea", {
|
|
381
|
+
...common,
|
|
382
|
+
value: text,
|
|
383
|
+
onChange: (e) => emit(e.target.value)
|
|
384
|
+
});
|
|
385
|
+
case "enum": return createElement("select", {
|
|
386
|
+
...common,
|
|
387
|
+
value: text,
|
|
388
|
+
onChange: (e) => emit(e.target.value === "" ? null : e.target.value)
|
|
389
|
+
}, createElement("option", {
|
|
390
|
+
key: "",
|
|
391
|
+
value: ""
|
|
392
|
+
}, ""), ...(attribute.enumValues ?? []).map((v) => createElement("option", {
|
|
393
|
+
key: v.name,
|
|
394
|
+
value: v.name
|
|
395
|
+
}, v.label)));
|
|
396
|
+
default: return createElement("input", {
|
|
397
|
+
...common,
|
|
398
|
+
type: kind === "date" ? "date" : kind === "dateTime" ? "datetime-local" : "text",
|
|
399
|
+
...kind === "dateTime" ? { step: "any" } : {},
|
|
400
|
+
value: kind === "dateTime" ? toLocalInput(value) : text,
|
|
401
|
+
onChange: (e) => emit(read(kind, e))
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
// src/ui/navigation.ts
|
|
406
|
+
const NavigationContext = createContext(null);
|
|
407
|
+
/** Provides the router adapter to T5 hosts. */
|
|
408
|
+
function NavigationProvider(props) {
|
|
409
|
+
return createElement(NavigationContext.Provider, { value: props.adapter }, props.children);
|
|
410
|
+
}
|
|
411
|
+
/** The current adapter; throws `Error` without a `NavigationProvider`. */
|
|
412
|
+
function useNavigation() {
|
|
413
|
+
const adapter = useContext(NavigationContext);
|
|
414
|
+
if (adapter === null) throw new Error("useNavigation requires a NavigationProvider");
|
|
415
|
+
return adapter;
|
|
416
|
+
}
|
|
417
|
+
// src/ui/list/geometry.ts
|
|
418
|
+
/** Width of the state cell; override with `--simetra-state-cell-width`. */
|
|
419
|
+
const STATE_CELL_WIDTH = "var(--simetra-state-cell-width, 160px)";
|
|
420
|
+
/** Height of the selection panel; override with `--simetra-selection-panel-height`. */
|
|
421
|
+
const SELECTION_PANEL_HEIGHT = "var(--simetra-selection-panel-height, 32px)";
|
|
422
|
+
/** Width of the reserved selection-count slot; override with `--simetra-selection-count-width`. */
|
|
423
|
+
const SELECTION_COUNT_WIDTH = "var(--simetra-selection-count-width, 96px)";
|
|
424
|
+
// src/ui/list/selection-panel.ts
|
|
425
|
+
function Button$1(props) {
|
|
426
|
+
return createElement("button", {
|
|
427
|
+
type: "button",
|
|
428
|
+
onClick: props.onClick
|
|
429
|
+
}, props.label);
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* The selection panel over a list (P4-D spec §5.1): a slot of fixed height
|
|
433
|
+
* with the view status and its actions. No row counter in M3 (ADR-0091); its
|
|
434
|
+
* slot is kept empty so the counter adds no shift later.
|
|
435
|
+
*/
|
|
436
|
+
function SelectionPanel(props) {
|
|
437
|
+
const { view } = props;
|
|
438
|
+
const refreshing = useMessage("state.refreshing");
|
|
439
|
+
const purged = useMessage("state.purged");
|
|
440
|
+
const showNew = useMessage("action.showNew");
|
|
441
|
+
const refreshList = useMessage("action.refreshList");
|
|
442
|
+
const retry = useMessage("action.retry");
|
|
443
|
+
const reloadPage = useMessage("action.reloadPage");
|
|
444
|
+
const updateRequired = useMessage("app.updateRequired");
|
|
445
|
+
const isPurged = view.status === "purged";
|
|
446
|
+
return createElement("div", {
|
|
447
|
+
"data-region": "panel",
|
|
448
|
+
style: {
|
|
449
|
+
display: "flex",
|
|
450
|
+
alignItems: "center",
|
|
451
|
+
height: SELECTION_PANEL_HEIGHT,
|
|
452
|
+
overflow: "hidden"
|
|
453
|
+
}
|
|
454
|
+
}, createElement("span", {
|
|
455
|
+
"aria-live": "polite",
|
|
456
|
+
"data-panel": "status"
|
|
457
|
+
}, view.status === "refreshing" ? refreshing : null, view.status === "error" && view.error !== void 0 ? createElement("span", { "data-error": view.error.code }, view.error.code) : null), view.status === "error" ? createElement(Button$1, {
|
|
458
|
+
label: retry,
|
|
459
|
+
onClick: view.refresh
|
|
460
|
+
}) : null, !isPurged && view.hasNewRows ? createElement(Button$1, {
|
|
461
|
+
label: showNew,
|
|
462
|
+
onClick: view.showNew
|
|
463
|
+
}) : null, !isPurged && view.needsRefresh ? createElement(Button$1, {
|
|
464
|
+
label: refreshList,
|
|
465
|
+
onClick: view.refresh
|
|
466
|
+
}) : null, view.isStale ? createElement("span", null, updateRequired) : null, createElement("span", {
|
|
467
|
+
"aria-live": "assertive",
|
|
468
|
+
"data-panel": "security",
|
|
469
|
+
"data-reason": isPurged ? view.reason : void 0
|
|
470
|
+
}, isPurged ? purged : null), isPurged ? createElement(Button$1, {
|
|
471
|
+
label: reloadPage,
|
|
472
|
+
onClick: () => window.location.reload()
|
|
473
|
+
}) : null, createElement("span", {
|
|
474
|
+
"data-slot": "count",
|
|
475
|
+
style: {
|
|
476
|
+
marginInlineStart: "auto",
|
|
477
|
+
flex: "none",
|
|
478
|
+
width: SELECTION_COUNT_WIDTH
|
|
479
|
+
}
|
|
480
|
+
}));
|
|
481
|
+
}
|
|
482
|
+
// src/ui/list/state-cell.ts
|
|
483
|
+
/**
|
|
484
|
+
* Which state a row shows (P4-D spec §5.4): `hidden` wins over every other
|
|
485
|
+
* state, a mutation state and `stale` included.
|
|
486
|
+
*/
|
|
487
|
+
function displayState(row) {
|
|
488
|
+
if (row.state === "removed") return "removed";
|
|
489
|
+
if (row.hidden === true) return "hidden";
|
|
490
|
+
return row.state;
|
|
491
|
+
}
|
|
492
|
+
const STATE_KEYS = {
|
|
493
|
+
synced: void 0,
|
|
494
|
+
pending: "state.pending",
|
|
495
|
+
unknown: "state.unknown",
|
|
496
|
+
failed: "state.failed",
|
|
497
|
+
conflict: "state.conflict",
|
|
498
|
+
stale: "state.stale",
|
|
499
|
+
hidden: "state.hidden",
|
|
500
|
+
removed: "state.removed"
|
|
501
|
+
};
|
|
502
|
+
/** The message key of a display state; `synced` has none. */
|
|
503
|
+
function stateMessageKey(state) {
|
|
504
|
+
return STATE_KEYS[state];
|
|
505
|
+
}
|
|
506
|
+
function StateText(props) {
|
|
507
|
+
return useMessage(props.messageKey);
|
|
508
|
+
}
|
|
509
|
+
/**
|
|
510
|
+
* The first cell of a list row: the text name of the row state at a fixed
|
|
511
|
+
* width. A state change is announced politely (`aria-live`); `title` carries
|
|
512
|
+
* a hint, e.g. the error code of a failed save. `stale` (the `RowView.stale`
|
|
513
|
+
* fact) adds the stale hint when a mutation state takes the label.
|
|
514
|
+
*/
|
|
515
|
+
function StateCell(props) {
|
|
516
|
+
const key = stateMessageKey(props.state);
|
|
517
|
+
const staleHint = props.stale === true && props.state !== "stale" && props.state !== "hidden" && props.state !== "removed";
|
|
518
|
+
return createElement("span", {
|
|
519
|
+
role: "gridcell",
|
|
520
|
+
"aria-live": "polite",
|
|
521
|
+
title: props.title,
|
|
522
|
+
style: {
|
|
523
|
+
flex: "none",
|
|
524
|
+
width: STATE_CELL_WIDTH,
|
|
525
|
+
overflow: "hidden"
|
|
526
|
+
}
|
|
527
|
+
}, key === void 0 ? null : createElement(StateText, { messageKey: key }), staleHint ? createElement("span", { "data-hint": "stale" }, createElement(StateText, { messageKey: "state.stale" })) : null);
|
|
528
|
+
}
|
|
529
|
+
// src/ui/list/list-host.ts
|
|
530
|
+
const isObjectRef = (column) => column.attribute.type === "Ref" && column.attribute.ref !== void 0 && column.attribute.enumValues === void 0 && column.attribute.pair === void 0;
|
|
531
|
+
/**
|
|
532
|
+
* The read of a list: sort order and `include`. One function for the host and
|
|
533
|
+
* the route loader (P4-D spec §6), so the view a loader preloads is the view
|
|
534
|
+
* the host subscribes to.
|
|
535
|
+
*/
|
|
536
|
+
function listQuery(entry, options = {}) {
|
|
537
|
+
const include = options.include ?? headerColumns(entry).filter(isObjectRef).map((column) => column.name);
|
|
538
|
+
return {
|
|
539
|
+
orderBy: options.orderBy ?? [{
|
|
540
|
+
attribute: entry.keyName,
|
|
541
|
+
direction: "asc"
|
|
542
|
+
}],
|
|
543
|
+
...include.length === 0 ? {} : { include }
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
/**
|
|
547
|
+
* The `setVisibleRange` of a list host: `VirtualList` only shows the edge
|
|
548
|
+
* slots, so the host reads the next page when a not-reached edge is reached by
|
|
549
|
+
* the visible range. Exported for the host tests only.
|
|
550
|
+
*/
|
|
551
|
+
function edgeLoader(view) {
|
|
552
|
+
return (range) => {
|
|
553
|
+
const v = view();
|
|
554
|
+
v.setVisibleRange(range);
|
|
555
|
+
if (v.atEnd === false && range.last >= v.rows.length - 1) v.loadAfter();
|
|
556
|
+
if (v.atStart === false && range.first <= 0) v.loadBefore();
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
/** One row of {@link ListHost}; not part of the public API. */
|
|
560
|
+
function ListRow(props) {
|
|
561
|
+
const { row } = props;
|
|
562
|
+
const remove = useMessage("action.remove");
|
|
563
|
+
const cancel = useMessage("action.cancel");
|
|
564
|
+
const state = displayState(row);
|
|
565
|
+
const mutation = row.mutation;
|
|
566
|
+
const rowStyle = {
|
|
567
|
+
display: "flex",
|
|
568
|
+
alignItems: "center",
|
|
569
|
+
height: "100%"
|
|
570
|
+
};
|
|
571
|
+
if (row.state === "removed" || state === "hidden") return createElement("div", {
|
|
572
|
+
role: "row",
|
|
573
|
+
style: rowStyle
|
|
574
|
+
}, createElement(StateCell, { state }), row.state === "removed" ? createElement("button", {
|
|
575
|
+
type: "button",
|
|
576
|
+
onClick: props.onRemove
|
|
577
|
+
}, remove) : null, row.state === "removed" && mutation?.cancel !== void 0 ? createElement("button", {
|
|
578
|
+
type: "button",
|
|
579
|
+
onClick: mutation.cancel
|
|
580
|
+
}, cancel) : null);
|
|
581
|
+
const open = () => props.onOpen(row.key);
|
|
582
|
+
return createElement("div", {
|
|
583
|
+
role: "row",
|
|
584
|
+
tabIndex: 0,
|
|
585
|
+
style: {
|
|
586
|
+
...rowStyle,
|
|
587
|
+
cursor: "pointer"
|
|
588
|
+
},
|
|
589
|
+
onClick: open,
|
|
590
|
+
onKeyDown: (event) => {
|
|
591
|
+
if (event.key === "Enter") open();
|
|
592
|
+
}
|
|
593
|
+
}, createElement(StateCell, {
|
|
594
|
+
state,
|
|
595
|
+
title: state === "failed" ? mutation?.error?.code : void 0,
|
|
596
|
+
stale: row.stale === true
|
|
597
|
+
}), ...props.columns.map((column) => {
|
|
598
|
+
const ref = column.attribute.ref;
|
|
599
|
+
const entry = ref !== void 0 && isObjectRef(column) ? props.catalog.byId.get(ref.object) : void 0;
|
|
600
|
+
return createElement("span", {
|
|
601
|
+
key: column.name,
|
|
602
|
+
role: "gridcell",
|
|
603
|
+
style: {
|
|
604
|
+
flex: 1,
|
|
605
|
+
minWidth: 0
|
|
606
|
+
}
|
|
607
|
+
}, createElement(FieldView, {
|
|
608
|
+
attribute: column.attribute,
|
|
609
|
+
value: row.values[column.name],
|
|
610
|
+
target: entry === void 0 ? void 0 : {
|
|
611
|
+
row: row.refs?.[column.name] ?? null,
|
|
612
|
+
entry
|
|
613
|
+
}
|
|
614
|
+
}));
|
|
615
|
+
}));
|
|
616
|
+
}
|
|
617
|
+
/**
|
|
618
|
+
* The standard list of an object (P4-D spec §5.1): the eager header columns
|
|
619
|
+
* of the catalog, a state cell per row and the selection panel. A click or
|
|
620
|
+
* `Enter` on a row opens its card; `removed` and `hidden` rows open nothing.
|
|
621
|
+
* Needs `CatalogProvider`, `NavigationProvider` and `DataRuntimeProvider`.
|
|
622
|
+
*/
|
|
623
|
+
function ListHost(props) {
|
|
624
|
+
const { object } = props;
|
|
625
|
+
const entry = useCatalogEntry(object);
|
|
626
|
+
const catalog = useCatalogIndex();
|
|
627
|
+
const navigation = useNavigation();
|
|
628
|
+
const loading = useMessage("state.loading");
|
|
629
|
+
const columns = useMemo(() => headerColumns(entry), [entry]);
|
|
630
|
+
const query = useMemo(() => listQuery(entry, {
|
|
631
|
+
orderBy: props.orderBy,
|
|
632
|
+
include: props.include
|
|
633
|
+
}), [
|
|
634
|
+
entry,
|
|
635
|
+
props.orderBy,
|
|
636
|
+
props.include
|
|
637
|
+
]);
|
|
638
|
+
const view = useRows({
|
|
639
|
+
object,
|
|
640
|
+
...query
|
|
641
|
+
});
|
|
642
|
+
const latest = useRef(view);
|
|
643
|
+
latest.current = view;
|
|
644
|
+
const onRange = useMemo(() => edgeLoader(() => latest.current), []);
|
|
645
|
+
const openCard = useCallback((key) => navigation.openCard(object, key), [navigation, object]);
|
|
646
|
+
const rows = view.rows;
|
|
647
|
+
const body = view.status === "loading" && rows.length === 0 ? createElement("div", {
|
|
648
|
+
"data-loading": "list",
|
|
649
|
+
style: { height: 600 }
|
|
650
|
+
}, loading) : createElement(VirtualList, {
|
|
651
|
+
rows,
|
|
652
|
+
atStart: view.atStart,
|
|
653
|
+
atEnd: view.atEnd,
|
|
654
|
+
chain: view.chain,
|
|
655
|
+
target: view.target,
|
|
656
|
+
setVisibleRange: onRange,
|
|
657
|
+
rowState: displayState,
|
|
658
|
+
rowHeight: 40,
|
|
659
|
+
height: 600,
|
|
660
|
+
renderRow: (row) => createElement(ListRow, {
|
|
661
|
+
row,
|
|
662
|
+
columns,
|
|
663
|
+
catalog,
|
|
664
|
+
onOpen: openCard,
|
|
665
|
+
onRemove: view.refresh
|
|
666
|
+
})
|
|
667
|
+
});
|
|
668
|
+
return createElement("div", {
|
|
669
|
+
"data-region": "list",
|
|
670
|
+
"data-object": object,
|
|
671
|
+
role: "grid"
|
|
672
|
+
}, createElement(SelectionPanel, { view }), createElement("div", {
|
|
673
|
+
role: "row",
|
|
674
|
+
style: {
|
|
675
|
+
display: "flex",
|
|
676
|
+
height: 40
|
|
677
|
+
}
|
|
678
|
+
}, createElement("span", {
|
|
679
|
+
role: "columnheader",
|
|
680
|
+
style: {
|
|
681
|
+
flex: "none",
|
|
682
|
+
width: STATE_CELL_WIDTH
|
|
683
|
+
}
|
|
684
|
+
}), ...columns.map((column) => createElement("span", {
|
|
685
|
+
key: column.name,
|
|
686
|
+
role: "columnheader",
|
|
687
|
+
style: {
|
|
688
|
+
flex: 1,
|
|
689
|
+
minWidth: 0
|
|
690
|
+
}
|
|
691
|
+
}, column.title))), body);
|
|
692
|
+
}
|
|
693
|
+
// src/ui/card/use-card-draft.ts
|
|
694
|
+
const EMPTY = Object.freeze({});
|
|
695
|
+
const NO_ERRORS = Object.freeze({});
|
|
696
|
+
function writable(card, codecErrors) {
|
|
697
|
+
return card.editable && Object.keys(codecErrors).length === 0;
|
|
698
|
+
}
|
|
699
|
+
const freshMeta = (generation) => ({
|
|
700
|
+
base: void 0,
|
|
701
|
+
revision: 0,
|
|
702
|
+
revisions: /* @__PURE__ */ new Map(),
|
|
703
|
+
sent: 0,
|
|
704
|
+
generation
|
|
705
|
+
});
|
|
706
|
+
/**
|
|
707
|
+
* The draft of a card header over TanStack Form (P4-D spec §5.2): the form
|
|
708
|
+
* holds the edited values, the hook keeps the draft base and a revision per
|
|
709
|
+
* field. A success of revision A drops only the fields not edited after A
|
|
710
|
+
* and moves the base to the version of the result; `conflict` and `failed`
|
|
711
|
+
* keep every edit.
|
|
712
|
+
*/
|
|
713
|
+
function useCardDraft(input) {
|
|
714
|
+
const { object, id, card } = input;
|
|
715
|
+
const writer = useWriter();
|
|
716
|
+
const form = useForm({ defaultValues: EMPTY });
|
|
717
|
+
const draft = useSelector(form.store, (s) => s.values);
|
|
718
|
+
const meta = useRef(freshMeta(0));
|
|
719
|
+
const [, rendered] = useReducer((n) => n + 1, 0);
|
|
720
|
+
const [codecErrors, setCodecErrors] = useState(NO_ERRORS);
|
|
721
|
+
const [attempt, setAttempt] = useState({});
|
|
722
|
+
const latest = useRef(card);
|
|
723
|
+
latest.current = card;
|
|
724
|
+
const allowed = useRef({
|
|
725
|
+
save: false,
|
|
726
|
+
overwrite: false,
|
|
727
|
+
retry: false
|
|
728
|
+
});
|
|
729
|
+
const row = card.row;
|
|
730
|
+
const live = row !== void 0 && row.state !== "removed" ? row : void 0;
|
|
731
|
+
const mutation = row?.mutation;
|
|
732
|
+
const purged = card.status === "purged" || row?.state === "removed";
|
|
733
|
+
const hidden = live?.hidden === true;
|
|
734
|
+
const reset = useCallback(() => {
|
|
735
|
+
meta.current = freshMeta(meta.current.generation + 1);
|
|
736
|
+
form.reset(EMPTY);
|
|
737
|
+
setCodecErrors(NO_ERRORS);
|
|
738
|
+
setAttempt({});
|
|
739
|
+
rendered();
|
|
740
|
+
}, [form]);
|
|
741
|
+
useEffect(() => {
|
|
742
|
+
if (purged) reset();
|
|
743
|
+
}, [purged, reset]);
|
|
744
|
+
const track = useCallback((m, sent) => {
|
|
745
|
+
const generation = meta.current.generation;
|
|
746
|
+
setAttempt({ id: m.id });
|
|
747
|
+
m.settled.then((result) => {
|
|
748
|
+
const current = meta.current;
|
|
749
|
+
if (current.generation !== generation) return;
|
|
750
|
+
if (result.ok) {
|
|
751
|
+
for (const [field, revision] of [...current.revisions]) {
|
|
752
|
+
if (revision > sent) continue;
|
|
753
|
+
current.revisions.delete(field);
|
|
754
|
+
form.deleteField(field);
|
|
755
|
+
}
|
|
756
|
+
current.base = current.revisions.size === 0 ? void 0 : result.rows[0]?.version ?? current.base;
|
|
757
|
+
setAttempt({});
|
|
758
|
+
rendered();
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
const shown = latest.current.row?.mutation;
|
|
762
|
+
setAttempt(shown?.id === m.id ? {} : { error: result.error });
|
|
763
|
+
});
|
|
764
|
+
}, [form]);
|
|
765
|
+
const send = useCallback((expectedVersion) => {
|
|
766
|
+
const current = meta.current;
|
|
767
|
+
current.sent = current.revision;
|
|
768
|
+
const values = { ...form.state.values };
|
|
769
|
+
track(writer.update(object, id, values, { expectedVersion }), current.sent);
|
|
770
|
+
}, [
|
|
771
|
+
form,
|
|
772
|
+
writer,
|
|
773
|
+
object,
|
|
774
|
+
id,
|
|
775
|
+
track
|
|
776
|
+
]);
|
|
777
|
+
const setValue = useCallback((field, value, error) => {
|
|
778
|
+
const current = meta.current;
|
|
779
|
+
if (current.base === void 0) current.base = latest.current.version;
|
|
780
|
+
current.revisions.set(field, ++current.revision);
|
|
781
|
+
form.setFieldValue(field, () => value);
|
|
782
|
+
setCodecErrors((prev) => {
|
|
783
|
+
if (error === void 0 && !(field in prev)) return prev;
|
|
784
|
+
const next = { ...prev };
|
|
785
|
+
if (error === void 0) delete next[field];
|
|
786
|
+
else next[field] = error;
|
|
787
|
+
return next;
|
|
788
|
+
});
|
|
789
|
+
}, [form]);
|
|
790
|
+
const save = useCallback(() => {
|
|
791
|
+
const base = meta.current.base;
|
|
792
|
+
if (allowed.current.save && base !== void 0) send(base);
|
|
793
|
+
}, [send]);
|
|
794
|
+
const overwrite = useCallback(() => {
|
|
795
|
+
const shownRow = latest.current.row;
|
|
796
|
+
if (!allowed.current.overwrite) return;
|
|
797
|
+
if (shownRow === void 0 || shownRow.state === "removed") return;
|
|
798
|
+
meta.current.base = shownRow.version;
|
|
799
|
+
rendered();
|
|
800
|
+
send(shownRow.version);
|
|
801
|
+
}, [send]);
|
|
802
|
+
const retry = useCallback(() => {
|
|
803
|
+
if (!allowed.current.retry) return;
|
|
804
|
+
const own = latest.current.row?.mutation;
|
|
805
|
+
if (own !== void 0) {
|
|
806
|
+
const next = own.retry?.();
|
|
807
|
+
if (next !== void 0) track(next, meta.current.sent);
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
const base = meta.current.base;
|
|
811
|
+
if (base !== void 0) send(base);
|
|
812
|
+
}, [track, send]);
|
|
813
|
+
const cancel = useCallback(() => {
|
|
814
|
+
const own = latest.current.row?.mutation;
|
|
815
|
+
reset();
|
|
816
|
+
own?.cancel?.();
|
|
817
|
+
}, [reset]);
|
|
818
|
+
const dismiss = useCallback(() => {
|
|
819
|
+
latest.current.row?.mutation?.dismiss?.();
|
|
820
|
+
}, []);
|
|
821
|
+
const edited = useMemo(() => new Set(Object.keys(draft)), [draft]);
|
|
822
|
+
const failure = mutation?.error ?? attempt.error;
|
|
823
|
+
const fieldErrors = useMemo(() => {
|
|
824
|
+
const server = failure?.code === "command.invalid" && failure.path !== void 0 ? { [failure.path]: failure } : void 0;
|
|
825
|
+
return server === void 0 ? codecErrors : {
|
|
826
|
+
...server,
|
|
827
|
+
...codecErrors
|
|
828
|
+
};
|
|
829
|
+
}, [failure, codecErrors]);
|
|
830
|
+
let state;
|
|
831
|
+
if (purged) state = "purged";
|
|
832
|
+
else if (hidden) state = "hidden";
|
|
833
|
+
else if (mutation?.state === "pending" || attempt.id !== void 0) state = "saving";
|
|
834
|
+
else if (mutation !== void 0) state = mutation.state;
|
|
835
|
+
else if (attempt.error !== void 0) state = "failed";
|
|
836
|
+
else state = "idle";
|
|
837
|
+
const values = useMemo(() => purged || hidden || live === void 0 ? EMPTY : {
|
|
838
|
+
...live.values,
|
|
839
|
+
...draft
|
|
840
|
+
}, [
|
|
841
|
+
purged,
|
|
842
|
+
hidden,
|
|
843
|
+
live,
|
|
844
|
+
draft
|
|
845
|
+
]);
|
|
846
|
+
const dirty = !purged && edited.size > 0;
|
|
847
|
+
const base = meta.current.base;
|
|
848
|
+
const canWrite = writable(card, codecErrors);
|
|
849
|
+
const canSave = dirty && (state === "idle" || state === "failed") && canWrite && base !== void 0;
|
|
850
|
+
const serverRead = state === "conflict" && live !== void 0 && base !== void 0 && live.version > base;
|
|
851
|
+
const canOverwrite = serverRead && canWrite;
|
|
852
|
+
const canRetry = state === "failed" && canWrite;
|
|
853
|
+
allowed.current = {
|
|
854
|
+
save: canSave,
|
|
855
|
+
overwrite: canOverwrite,
|
|
856
|
+
retry: canRetry
|
|
857
|
+
};
|
|
858
|
+
return {
|
|
859
|
+
state,
|
|
860
|
+
dirty,
|
|
861
|
+
base: purged ? void 0 : meta.current.base,
|
|
862
|
+
values,
|
|
863
|
+
edited: purged ? /* @__PURE__ */ new Set() : edited,
|
|
864
|
+
fieldErrors: purged || hidden ? NO_ERRORS : fieldErrors,
|
|
865
|
+
...failure === void 0 || purged || hidden ? {} : { error: failure },
|
|
866
|
+
canSave,
|
|
867
|
+
serverRead,
|
|
868
|
+
canOverwrite,
|
|
869
|
+
canRetry,
|
|
870
|
+
setValue,
|
|
871
|
+
save,
|
|
872
|
+
overwrite,
|
|
873
|
+
retry,
|
|
874
|
+
cancel,
|
|
875
|
+
dismiss
|
|
876
|
+
};
|
|
877
|
+
}
|
|
878
|
+
// src/ui/card/card-host.ts
|
|
879
|
+
/**
|
|
880
|
+
* The read of a card: every tabular section of the object. One function for
|
|
881
|
+
* the host and the route loader (P4-D spec §6), so the views a loader
|
|
882
|
+
* preloads are the views the host subscribes to.
|
|
883
|
+
*/
|
|
884
|
+
function cardQuery(entry) {
|
|
885
|
+
return { tabularSections: entry.tabularSections.map((s) => s.name) };
|
|
886
|
+
}
|
|
887
|
+
const DRAFT_KEYS = {
|
|
888
|
+
saving: "state.pending",
|
|
889
|
+
failed: "state.failed",
|
|
890
|
+
conflict: "state.conflict",
|
|
891
|
+
unknown: "state.unknown"
|
|
892
|
+
};
|
|
893
|
+
function Text(props) {
|
|
894
|
+
return useMessage(props.messageKey);
|
|
895
|
+
}
|
|
896
|
+
function Button(props) {
|
|
897
|
+
return createElement("button", {
|
|
898
|
+
type: "button",
|
|
899
|
+
onClick: props.onClick,
|
|
900
|
+
disabled: props.disabled
|
|
901
|
+
}, createElement(Text, { messageKey: props.messageKey }));
|
|
902
|
+
}
|
|
903
|
+
/** The status line of the card: which message the header state shows. */
|
|
904
|
+
function statusKey(card, draft) {
|
|
905
|
+
if (card.status === "purged") return "state.purged";
|
|
906
|
+
const row = card.row;
|
|
907
|
+
if (row === void 0) return card.status === "loading" ? "state.loading" : void 0;
|
|
908
|
+
const shown = displayState(row);
|
|
909
|
+
if (shown === "hidden" || shown === "removed") return stateMessageKey(shown);
|
|
910
|
+
if (draft.state === "conflict" && !draft.serverRead) return "state.refreshing";
|
|
911
|
+
return DRAFT_KEYS[draft.state] ?? stateMessageKey(shown);
|
|
912
|
+
}
|
|
913
|
+
function Field(props) {
|
|
914
|
+
const { column, card, draft } = props;
|
|
915
|
+
const { attribute, name } = column;
|
|
916
|
+
const row = card.row;
|
|
917
|
+
const live = row !== void 0 && row.state !== "removed" ? row : void 0;
|
|
918
|
+
const showValues = draft.state !== "hidden" && draft.state !== "purged";
|
|
919
|
+
const editable = showValues && attribute.update && card.editable && !card.isStale && isEditableType(attribute);
|
|
920
|
+
const value = draft.values[name];
|
|
921
|
+
const error = draft.fieldErrors[name];
|
|
922
|
+
const staleField = live?.staleFields.includes(name) === true;
|
|
923
|
+
let body = null;
|
|
924
|
+
if (showValues && live !== void 0) body = editable ? createElement(FieldEditor, {
|
|
925
|
+
attribute,
|
|
926
|
+
value,
|
|
927
|
+
onChange: (next, codec) => draft.setValue(name, next, codec)
|
|
928
|
+
}) : createElement(FieldView, {
|
|
929
|
+
attribute,
|
|
930
|
+
value
|
|
931
|
+
});
|
|
932
|
+
return createElement("div", { "data-field-slot": name }, createElement("label", null, column.title), body, error !== void 0 && showValues ? createElement("span", {
|
|
933
|
+
role: "alert",
|
|
934
|
+
"data-field-error": error.code
|
|
935
|
+
}, error.code) : null, staleField && showValues ? createElement("span", { "data-hint": "stale" }, createElement(Text, { messageKey: "state.stale" })) : null, draft.serverRead && draft.edited.has(name) && live !== void 0 ? createElement("span", { "data-server": name }, createElement(FieldView, {
|
|
936
|
+
attribute,
|
|
937
|
+
value: live.values[name]
|
|
938
|
+
})) : null);
|
|
939
|
+
}
|
|
940
|
+
function Section(props) {
|
|
941
|
+
const { section, data, card } = props;
|
|
942
|
+
const error = card.error;
|
|
943
|
+
if (error?.code === "command.invalid" && "reason" in error && error.reason === "tabular-too-large" && (error.path === void 0 || error.path === section.name)) return createElement("div", {
|
|
944
|
+
"data-section": section.name,
|
|
945
|
+
"data-section-state": error.reason
|
|
946
|
+
}, error.reason);
|
|
947
|
+
const lines = data === void 0 || data.ownerState === "removed" ? [] : [...data.lines].sort((a, b) => Number(a.values[section.lineNumberName]) - Number(b.values[section.lineNumberName]));
|
|
948
|
+
return createElement("table", { "data-section": section.name }, createElement("thead", null, createElement("tr", null, ...section.attributes.map((a) => createElement("th", { key: a.name }, a.name)))), createElement("tbody", null, ...lines.map((line) => createElement("tr", {
|
|
949
|
+
key: line.key,
|
|
950
|
+
"data-line-key": line.key
|
|
951
|
+
}, ...section.attributes.map((a) => createElement("td", { key: a.name }, createElement(FieldView, {
|
|
952
|
+
attribute: a,
|
|
953
|
+
value: line.values[a.name]
|
|
954
|
+
})))))));
|
|
955
|
+
}
|
|
956
|
+
function Actions(props) {
|
|
957
|
+
const { card, draft } = props;
|
|
958
|
+
const updateRequired = useMessage("app.updateRequired");
|
|
959
|
+
const saveError = draft.error !== void 0 && !Object.values(draft.fieldErrors).some((e) => e === draft.error) ? draft.error : void 0;
|
|
960
|
+
if (draft.state === "purged") return createElement(Button, {
|
|
961
|
+
messageKey: "action.reloadPage",
|
|
962
|
+
onClick: () => window.location.reload()
|
|
963
|
+
});
|
|
964
|
+
if (card.row === void 0 || card.row.state === "removed") return null;
|
|
965
|
+
return createElement("div", { "data-region": "card-actions" }, card.isStale ? createElement("span", { "data-app": "update-required" }, updateRequired) : createElement(Button, {
|
|
966
|
+
messageKey: "action.save",
|
|
967
|
+
onClick: draft.save,
|
|
968
|
+
disabled: !draft.canSave
|
|
969
|
+
}), saveError === void 0 ? null : createElement("span", {
|
|
970
|
+
role: "alert",
|
|
971
|
+
"data-save-error": saveError.code
|
|
972
|
+
}, saveError.code), card.needsRefresh || draft.state === "conflict" && !draft.serverRead ? createElement(Button, {
|
|
973
|
+
messageKey: "action.refreshCard",
|
|
974
|
+
onClick: card.refresh
|
|
975
|
+
}) : null, draft.state === "failed" ? createElement(Button, {
|
|
976
|
+
messageKey: "action.retry",
|
|
977
|
+
onClick: draft.retry,
|
|
978
|
+
disabled: !draft.canRetry
|
|
979
|
+
}) : null, draft.state === "failed" ? createElement(Button, {
|
|
980
|
+
messageKey: "action.cancel",
|
|
981
|
+
onClick: draft.cancel
|
|
982
|
+
}) : null, draft.state === "conflict" ? createElement(Button, {
|
|
983
|
+
messageKey: "action.takeServer",
|
|
984
|
+
onClick: draft.cancel,
|
|
985
|
+
disabled: !draft.serverRead
|
|
986
|
+
}) : null, draft.state === "conflict" ? createElement(Button, {
|
|
987
|
+
messageKey: "action.overwrite",
|
|
988
|
+
onClick: draft.overwrite,
|
|
989
|
+
disabled: !draft.canOverwrite
|
|
990
|
+
}) : null, draft.state === "unknown" ? createElement(Button, {
|
|
991
|
+
messageKey: "action.dismiss",
|
|
992
|
+
onClick: draft.dismiss
|
|
993
|
+
}) : null);
|
|
994
|
+
}
|
|
995
|
+
const writerNumbers = /* @__PURE__ */ new WeakMap();
|
|
996
|
+
let lastWriterNumber = 0;
|
|
997
|
+
function writerNumber(writer) {
|
|
998
|
+
let n = writerNumbers.get(writer);
|
|
999
|
+
if (n === void 0) {
|
|
1000
|
+
n = ++lastWriterNumber;
|
|
1001
|
+
writerNumbers.set(writer, n);
|
|
1002
|
+
}
|
|
1003
|
+
return n;
|
|
1004
|
+
}
|
|
1005
|
+
/**
|
|
1006
|
+
* The standard card of an object (P4-D spec §5.2): the header fields in
|
|
1007
|
+
* catalog order with the card draft, and every tabular section
|
|
1008
|
+
* read-only. A dirty draft blocks navigation. A `hidden` card shows no value
|
|
1009
|
+
* of the row or the draft; a purged or `removed` one drops the draft. Another
|
|
1010
|
+
* runtime, object or key is another card with a clean draft. Needs
|
|
1011
|
+
* `CatalogProvider`, `NavigationProvider` and `DataRuntimeProvider`.
|
|
1012
|
+
*/
|
|
1013
|
+
function CardHost(props) {
|
|
1014
|
+
const { object, id } = props;
|
|
1015
|
+
const owner = JSON.stringify([
|
|
1016
|
+
writerNumber(useWriter()),
|
|
1017
|
+
object,
|
|
1018
|
+
id
|
|
1019
|
+
]);
|
|
1020
|
+
return createElement(CardBody, {
|
|
1021
|
+
key: owner,
|
|
1022
|
+
object,
|
|
1023
|
+
id
|
|
1024
|
+
});
|
|
1025
|
+
}
|
|
1026
|
+
function CardBody(props) {
|
|
1027
|
+
const { object, id } = props;
|
|
1028
|
+
const entry = useCatalogEntry(object);
|
|
1029
|
+
const query = useMemo(() => cardQuery(entry), [entry]);
|
|
1030
|
+
const fields = useMemo(() => cardFields(entry), [entry]);
|
|
1031
|
+
const card = useRow({
|
|
1032
|
+
object,
|
|
1033
|
+
id,
|
|
1034
|
+
...query
|
|
1035
|
+
});
|
|
1036
|
+
const draft = useCardDraft({
|
|
1037
|
+
object,
|
|
1038
|
+
id,
|
|
1039
|
+
card
|
|
1040
|
+
});
|
|
1041
|
+
useNavigation().useBlockWhile(draft.dirty);
|
|
1042
|
+
const key = statusKey(card, draft);
|
|
1043
|
+
const row = card.row;
|
|
1044
|
+
const live = row !== void 0 && row.state !== "removed" ? row : void 0;
|
|
1045
|
+
const showValues = live !== void 0 && draft.state !== "hidden" && draft.state !== "purged";
|
|
1046
|
+
const staleHint = showValues && live.stale === true && key !== void 0 && key !== "state.stale";
|
|
1047
|
+
return createElement("div", {
|
|
1048
|
+
"data-region": "card",
|
|
1049
|
+
"data-object": object,
|
|
1050
|
+
"data-key": id,
|
|
1051
|
+
"data-state": row === void 0 ? void 0 : displayState(row),
|
|
1052
|
+
"data-draft": draft.state
|
|
1053
|
+
}, createElement("div", { "data-region": "card-status" }, createElement("span", {
|
|
1054
|
+
"aria-live": "polite",
|
|
1055
|
+
"data-card": "status"
|
|
1056
|
+
}, key === void 0 || key === "state.purged" ? null : createElement(Text, { messageKey: key }), staleHint ? createElement("span", { "data-hint": "stale" }, createElement(Text, { messageKey: "state.stale" })) : null, card.status === "error" && card.error !== void 0 && !("reason" in card.error && card.error.reason === "tabular-too-large") ? createElement("span", { "data-error": card.error.code }, card.error.code) : null), createElement("span", {
|
|
1057
|
+
"aria-live": "assertive",
|
|
1058
|
+
"data-card": "security",
|
|
1059
|
+
"data-reason": card.status === "purged" ? card.reason : void 0
|
|
1060
|
+
}, key === "state.purged" ? createElement(Text, { messageKey: key }) : null)), row === void 0 && card.status === "loading" ? createElement("div", { "data-loading": "card" }) : null, live === void 0 || draft.state === "purged" ? null : createElement("div", { "data-region": "card-header" }, ...fields.map((column) => createElement(Field, {
|
|
1061
|
+
key: column.name,
|
|
1062
|
+
column,
|
|
1063
|
+
card,
|
|
1064
|
+
draft
|
|
1065
|
+
}))), createElement(Actions, {
|
|
1066
|
+
card,
|
|
1067
|
+
draft
|
|
1068
|
+
}), showValues ? entry.tabularSections.map((section) => createElement(Section, {
|
|
1069
|
+
key: section.name,
|
|
1070
|
+
section,
|
|
1071
|
+
data: card.tabularSections[section.name],
|
|
1072
|
+
card
|
|
1073
|
+
})) : null);
|
|
1074
|
+
}
|
|
1075
|
+
export { CardHost, CatalogProvider, FieldEditor, FieldView, ListHost, MESSAGES, MessagesProvider, NavigationProvider, VirtualList, cardFields, cardQuery, headerColumns, isEditableType, listQuery, refPresentation, useCatalogEntry, useMessage, useNavigation };
|