@sincpro/web-framework 2.6.0 → 3.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 +62 -31
- package/dist/core/copy.d.ts +19 -1
- package/dist/core/copy.js +36 -0
- package/dist/core/explain.d.ts +12 -4
- package/dist/core/explain.js +18 -6
- package/dist/core/form.d.ts +7 -3
- package/dist/core/form.js +11 -5
- package/dist/core/groups.d.ts +17 -0
- package/dist/core/groups.js +31 -0
- package/dist/core/history.d.ts +45 -1
- package/dist/core/history.js +84 -12
- package/dist/core/index.d.ts +5 -4
- package/dist/core/index.js +5 -4
- package/dist/core/list.d.ts +14 -16
- package/dist/core/list.js +18 -25
- package/dist/core/measure.d.ts +14 -5
- package/dist/core/measure.js +32 -7
- package/dist/core/present.d.ts +51 -3
- package/dist/core/present.js +162 -15
- package/dist/core/summary.d.ts +6 -2
- package/dist/react/context.d.ts +23 -2
- package/dist/react/context.js +51 -2
- package/dist/react/index.d.ts +2 -1
- package/dist/react/index.js +2 -1
- package/dist/react/useCommand.d.ts +7 -1
- package/dist/react/useCommand.js +12 -2
- package/dist/react/useGroups.js +15 -4
- package/dist/react/useHistory.d.ts +4 -2
- package/dist/react/useHistory.js +20 -5
- package/dist/react/usePivot.js +18 -3
- package/dist/react/useRecord.d.ts +5 -2
- package/dist/react/useRecord.js +14 -2
- package/dist/react/useRecordLifecycle.d.ts +43 -0
- package/dist/react/useRecordLifecycle.js +49 -0
- package/dist/web/Refused.d.ts +3 -3
- package/dist/web/Refused.js +3 -3
- package/dist/web/index.d.ts +3 -2
- package/dist/web/index.js +2 -1
- package/dist/web/list/EntityList.js +70 -17
- package/dist/web/list/columns.d.ts +6 -1
- package/dist/web/list/columns.js +53 -8
- package/dist/web/list/presentation.d.ts +20 -1
- package/dist/web/list/rowCommands.d.ts +2 -0
- package/dist/web/list/rowCommands.js +6 -1
- package/dist/web/record/RecordLifecycle.d.ts +23 -0
- package/dist/web/record/RecordLifecycle.js +29 -0
- package/dist/web/record/RelatedList.d.ts +0 -15
- package/dist/web/record/RelatedList.js +35 -6
- package/dist/web/record/edit.d.ts +5 -0
- package/dist/web/record/edit.js +48 -8
- package/dist/web/record/opening.d.ts +2 -2
- package/dist/web/record/record.js +5 -2
- package/dist/web/workspace/SincproApp.d.ts +10 -1
- package/dist/web/workspace/SincproApp.js +18 -4
- package/dist/web/workspace/composer.d.ts +1 -1
- package/dist/web/workspace/composer.js +3 -4
- package/dist/web/workspace/record.d.ts +7 -2
- package/dist/web/workspace/record.js +41 -8
- package/package.json +6 -6
package/dist/core/present.js
CHANGED
|
@@ -10,8 +10,45 @@ const UNKNOWN = {
|
|
|
10
10
|
sortable: false,
|
|
11
11
|
ops: [],
|
|
12
12
|
};
|
|
13
|
+
/**
|
|
14
|
+
* Where a name lives: a field of the model, or — written `recibo.numero` — a field of the
|
|
15
|
+
* definition of a value the record embeds. The model the field is described by, and its own
|
|
16
|
+
* name there.
|
|
17
|
+
*
|
|
18
|
+
* Context: the Meta publishes an embedded value with its fields, their labels and types (a
|
|
19
|
+
* payment's receipt: number, issued, concept, amount); an application wrote each of them by
|
|
20
|
+
* hand because nothing here could name one. A path is the same name a screen writes in a form.
|
|
21
|
+
*/
|
|
22
|
+
export function fieldAt(meta, name) {
|
|
23
|
+
const dot = name.indexOf(".");
|
|
24
|
+
if (dot < 0 || meta?.fields[name] !== undefined)
|
|
25
|
+
return { meta, name };
|
|
26
|
+
const head = meta?.fields[name.slice(0, dot)];
|
|
27
|
+
if (head?.kind !== "embedded" || head.many === true || !head.definition)
|
|
28
|
+
return { meta, name };
|
|
29
|
+
return fieldAt(head.definition, name.slice(dot + 1));
|
|
30
|
+
}
|
|
31
|
+
/** The value a name holds in a record: a field, or a field of an embedded value
|
|
32
|
+
* (`recibo.numero`). */
|
|
33
|
+
export function valueAt(meta, record, name) {
|
|
34
|
+
if (record === null || record === undefined)
|
|
35
|
+
return undefined;
|
|
36
|
+
const dot = name.indexOf(".");
|
|
37
|
+
if (dot < 0 || meta?.fields[name] !== undefined || Object.hasOwn(record, name))
|
|
38
|
+
return record[name];
|
|
39
|
+
const head = name.slice(0, dot);
|
|
40
|
+
const inner = record[head];
|
|
41
|
+
return typeof inner === "object" && inner !== null
|
|
42
|
+
? valueAt(meta?.fields[head]?.definition, inner, name.slice(dot + 1))
|
|
43
|
+
: undefined;
|
|
44
|
+
}
|
|
45
|
+
/** What the model says of a name — a field, or a field of an embedded value — if it says it. */
|
|
46
|
+
export function fieldMetaOf(meta, name) {
|
|
47
|
+
const place = fieldAt(meta, name);
|
|
48
|
+
return place.meta?.fields[place.name];
|
|
49
|
+
}
|
|
13
50
|
function fieldOf(meta, name) {
|
|
14
|
-
return meta
|
|
51
|
+
return fieldMetaOf(meta, name) ?? UNKNOWN;
|
|
15
52
|
}
|
|
16
53
|
function helpOf(field, locale) {
|
|
17
54
|
const help = field.help;
|
|
@@ -21,9 +58,10 @@ function helpOf(field, locale) {
|
|
|
21
58
|
}
|
|
22
59
|
export function viewOf(meta, name, locale) {
|
|
23
60
|
const field = fieldOf(meta, name);
|
|
61
|
+
const place = fieldAt(meta, name);
|
|
24
62
|
return {
|
|
25
63
|
name,
|
|
26
|
-
label: labelOf(meta, name, locale),
|
|
64
|
+
label: labelOf(place.meta, place.name, locale),
|
|
27
65
|
help: helpOf(field, locale),
|
|
28
66
|
type: field.type,
|
|
29
67
|
kind: field.kind,
|
|
@@ -47,6 +85,19 @@ export function defaultFields(meta) {
|
|
|
47
85
|
}
|
|
48
86
|
/** The bookkeeping every framework entity carries; shown on demand, never by default. */
|
|
49
87
|
export const BOOKKEEPING = new Set(["version", "updated_at"]);
|
|
88
|
+
/**
|
|
89
|
+
* What the framework writes on every record of its own: when and by whom it was made and last
|
|
90
|
+
* changed, its version, when it went to the trash. A record's audit and its activity say these;
|
|
91
|
+
* a form, a list inside a record and a record's facts never repeat them.
|
|
92
|
+
*/
|
|
93
|
+
export const STAMPS = new Set([
|
|
94
|
+
"version",
|
|
95
|
+
"created_at",
|
|
96
|
+
"created_by",
|
|
97
|
+
"updated_at",
|
|
98
|
+
"updated_by",
|
|
99
|
+
"archived_at",
|
|
100
|
+
]);
|
|
50
101
|
/** `"2026-03-12"` as a LOCAL date: `new Date("2026-03-12")` is UTC midnight, the day before
|
|
51
102
|
* anywhere west of Greenwich. */
|
|
52
103
|
function localDate(text) {
|
|
@@ -63,19 +114,52 @@ function decimals(text) {
|
|
|
63
114
|
* every page. The set of shapes is tiny (three date/number styles × decimals × locales).
|
|
64
115
|
*/
|
|
65
116
|
const formatters = new Map();
|
|
117
|
+
let dateStyle = "numeric";
|
|
118
|
+
/** Install how dates are written; `undefined` puts the default back (numeric). */
|
|
119
|
+
export function installDateStyle(style) {
|
|
120
|
+
dateStyle = style ?? "numeric";
|
|
121
|
+
}
|
|
122
|
+
/** The style dates are written in now. */
|
|
123
|
+
export const dateStyleNow = () => dateStyle;
|
|
66
124
|
function dateFormat(locale, withTime) {
|
|
67
125
|
// An instant reads on the wall clock of the installed zone; a calendar day has no zone.
|
|
68
126
|
const zone = withTime ? timeZoneNow() : "";
|
|
69
|
-
const
|
|
127
|
+
const numeric = dateStyle === "numeric";
|
|
128
|
+
// Numeric is the same everywhere — day, month, year — so its locale does not matter: only
|
|
129
|
+
// its parts are read (`numericDate`), in Latin digits.
|
|
130
|
+
const key = `d|${numeric ? "" : (locale ?? "")}|${withTime}|${zone}|${dateStyle}`;
|
|
70
131
|
let format = formatters.get(key);
|
|
71
132
|
if (format === undefined) {
|
|
72
|
-
format =
|
|
73
|
-
?
|
|
74
|
-
|
|
133
|
+
format = numeric
|
|
134
|
+
? new Intl.DateTimeFormat("en-GB", {
|
|
135
|
+
day: "2-digit",
|
|
136
|
+
month: "2-digit",
|
|
137
|
+
year: "numeric",
|
|
138
|
+
numberingSystem: "latn",
|
|
139
|
+
...(withTime
|
|
140
|
+
? { hour: "2-digit", minute: "2-digit", hourCycle: "h23", timeZone: zone }
|
|
141
|
+
: {}),
|
|
142
|
+
})
|
|
143
|
+
: new Intl.DateTimeFormat(locale, withTime
|
|
144
|
+
? { dateStyle: "medium", timeStyle: "short", timeZone: zone }
|
|
145
|
+
: { dateStyle: "medium" });
|
|
75
146
|
formatters.set(key, format);
|
|
76
147
|
}
|
|
77
148
|
return format;
|
|
78
149
|
}
|
|
150
|
+
/** «DD/MM/YYYY», and « HH:mm» after it for an instant — assembled from the parts, so no
|
|
151
|
+
* locale reorders them (`en-US` would write the month first). */
|
|
152
|
+
function numericDate(date, withTime) {
|
|
153
|
+
const parts = dateFormat(undefined, withTime).formatToParts(date);
|
|
154
|
+
const part = (type) => parts.find((one) => one.type === type)?.value ?? "";
|
|
155
|
+
const day = `${part("day")}/${part("month")}/${part("year")}`;
|
|
156
|
+
return withTime ? `${day} ${part("hour")}:${part("minute")}` : day;
|
|
157
|
+
}
|
|
158
|
+
function dateText(date, locale, withTime) {
|
|
159
|
+
return dateStyle === "numeric"
|
|
160
|
+
? numericDate(date, withTime)
|
|
161
|
+
: dateFormat(locale, withTime).format(date);
|
|
162
|
+
}
|
|
79
163
|
function numberFormat(locale, kept) {
|
|
80
164
|
const key = `n|${locale ?? ""}|${kept ?? ""}`;
|
|
81
165
|
let format = formatters.get(key);
|
|
@@ -144,8 +228,8 @@ function displayOf(field, value) {
|
|
|
144
228
|
* to the provider's locale.
|
|
145
229
|
*
|
|
146
230
|
* numberTextOf("1150.5", "es-BO") -> "1.150,5"
|
|
147
|
-
* dayTextOf("2026-03-12", "es") -> "12
|
|
148
|
-
* instantTextOf("2026-03-12T14:00:00Z", "es") -> "12
|
|
231
|
+
* dayTextOf("2026-03-12", "es") -> "12/03/2026"
|
|
232
|
+
* instantTextOf("2026-03-12T14:00:00Z", "es") -> "12/03/2026 10:00" (La Paz)
|
|
149
233
|
* moneyTextOf({ amount: "1150.5", currency: "BOB" }, "es-BO") -> "Bs 1.150,50"
|
|
150
234
|
*
|
|
151
235
|
* Context: every application wrote its own money and date formatting for its own screens, on
|
|
@@ -166,40 +250,103 @@ export function numberTextOf(value, locale) {
|
|
|
166
250
|
export function dayTextOf(value, locale) {
|
|
167
251
|
if (value === null || value === undefined || value === "")
|
|
168
252
|
return null;
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
253
|
+
if (typeof value !== "string")
|
|
254
|
+
return String(value);
|
|
255
|
+
const day = localDate(value.slice(0, 10));
|
|
256
|
+
return Number.isNaN(day.getTime()) ? value : dateText(day, locale, false);
|
|
172
257
|
}
|
|
173
258
|
/** An instant as a person reads it, on the wall clock of the zone the application installed. */
|
|
174
259
|
export function instantTextOf(value, locale) {
|
|
175
260
|
if (value === null || value === undefined || value === "")
|
|
176
261
|
return null;
|
|
177
262
|
const date = new Date(String(value));
|
|
178
|
-
return Number.isNaN(date.getTime()) ? String(value) :
|
|
263
|
+
return Number.isNaN(date.getTime()) ? String(value) : dateText(date, locale, true);
|
|
179
264
|
}
|
|
180
265
|
/** A money (`{ amount, currency }`) as a person reads it, rounded to its currency's decimals;
|
|
181
266
|
* `null` when the value is not one. */
|
|
182
267
|
export function moneyTextOf(value, locale) {
|
|
183
268
|
return Money.isMoney(value) ? moneyText(value, locale) : null;
|
|
184
269
|
}
|
|
270
|
+
/**
|
|
271
|
+
* The currency an amount is in when it carries none — a decimal the model draws as money
|
|
272
|
+
* (`widget: "money"`, «Monto (Bs.)»): the business's, installed once with the zone
|
|
273
|
+
* (`FrameworkProvider currency`). Without one, such an amount is rounded to two decimals and
|
|
274
|
+
* written with no symbol.
|
|
275
|
+
*/
|
|
276
|
+
let currency;
|
|
277
|
+
/** Install the business's currency (`"BOB"`); `undefined` forgets it. */
|
|
278
|
+
export function installCurrency(code) {
|
|
279
|
+
currency = code;
|
|
280
|
+
}
|
|
281
|
+
/** The business's currency now, if one was installed. */
|
|
282
|
+
export const currencyNow = () => currency;
|
|
283
|
+
/** A bare amount drawn as money: in the business's currency, or rounded to cents. */
|
|
284
|
+
function amountText(value, locale) {
|
|
285
|
+
const amount = typeof value === "number" ? String(value) : value;
|
|
286
|
+
if (typeof amount !== "string" || Number.isNaN(Number(amount)))
|
|
287
|
+
return null;
|
|
288
|
+
if (currency !== undefined)
|
|
289
|
+
return moneyText({ amount, currency }, locale);
|
|
290
|
+
return numberFormat(locale, 2).format(Number(amount));
|
|
291
|
+
}
|
|
292
|
+
/** A definition of exactly an amount and a currency: a money, whatever the entity calls it. */
|
|
293
|
+
export function isMoneyDefinition(definition) {
|
|
294
|
+
const parts = Object.keys(definition?.fields ?? {});
|
|
295
|
+
return parts.length === 2 && parts.includes("amount") && parts.includes("currency");
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* A value object read through its definition: «N.º de recibo: 0042 · Concepto: Cuota» — each
|
|
299
|
+
* field by its label, in the order the definition declares them, the empty ones left out. A
|
|
300
|
+
* money stays a money; a list of values reads as how many there are.
|
|
301
|
+
*/
|
|
302
|
+
function embeddedText(field, value, locale) {
|
|
303
|
+
const definition = field.definition;
|
|
304
|
+
// A money — by its value, or by a definition of an amount and a currency — is one, or nothing:
|
|
305
|
+
// an amount that is not one never reads as «amount: 1,50 · currency: BOB».
|
|
306
|
+
if (Money.isMoney(value) || isMoneyDefinition(definition))
|
|
307
|
+
return moneyTextOf(value, locale);
|
|
308
|
+
if (Array.isArray(value))
|
|
309
|
+
return value.length === 0 ? null : frameworkCopy().value.items(value.length, true);
|
|
310
|
+
if (!definition || typeof value !== "object" || value === null)
|
|
311
|
+
return null;
|
|
312
|
+
const parts = Object.keys(definition.fields)
|
|
313
|
+
.map((name) => {
|
|
314
|
+
const text = textOf(definition, name, value[name], locale);
|
|
315
|
+
return text === null ? null : `${labelOf(definition, name, locale)}: ${text}`;
|
|
316
|
+
})
|
|
317
|
+
.filter((part) => part !== null);
|
|
318
|
+
return parts.length === 0 ? null : parts.join(" · ");
|
|
319
|
+
}
|
|
185
320
|
/**
|
|
186
321
|
* The text a value reads as. `null` when there is nothing to read — the caller draws its own
|
|
187
322
|
* «empty» (a dash in a fact, nothing in a cell).
|
|
188
323
|
*
|
|
189
324
|
* textOf(meta, "state", "paid", "es") -> "Pagada" (choice_labels)
|
|
190
325
|
* textOf(meta, "active", true, "es") -> "sí"
|
|
191
|
-
* textOf(meta, "due", "2026-03-12", "es") -> "12 mar 2026
|
|
326
|
+
* textOf(meta, "due", "2026-03-12", "es") -> "12/03/2026" («12 mar 2026» literal)
|
|
192
327
|
* textOf(meta, "total", "1150.5", "es") -> "1150,5" (exact: digits kept)
|
|
193
328
|
* textOf(meta, "price", { amount: "19.9", currency: "BOB" }, "es") -> "19,90 Bs"
|
|
329
|
+
* textOf(meta, "monto", "25", "es") -> "Bs 25,00" (widget money, BOB installed)
|
|
330
|
+
* textOf(meta, "recibo", {numero: "0042", …}) -> "N.º de recibo: 0042 · …"
|
|
331
|
+
* textOf(meta, "recibo.numero", "0042") -> "0042"
|
|
194
332
|
* textOf(meta, "lines", {items, count: {value: 30}}) -> "30 elementos"
|
|
195
333
|
*/
|
|
196
334
|
export function textOf(meta, name, value, locale) {
|
|
197
335
|
if (value === null || value === undefined || value === "")
|
|
198
336
|
return null;
|
|
337
|
+
const place = fieldAt(meta, name);
|
|
338
|
+
if (place.meta !== meta)
|
|
339
|
+
return textOf(place.meta, place.name, value, locale);
|
|
199
340
|
const field = fieldOf(meta, name);
|
|
200
341
|
const copy = frameworkCopy();
|
|
201
342
|
if (field.choices && field.choices.length > 0)
|
|
202
343
|
return choiceLabelOf(meta, name, value, locale);
|
|
344
|
+
// An amount the model draws as money is money, though it travels bare.
|
|
345
|
+
if (field.widget === "money" && field.kind === "scalar") {
|
|
346
|
+
const amount = amountText(value, locale);
|
|
347
|
+
if (amount !== null)
|
|
348
|
+
return amount;
|
|
349
|
+
}
|
|
203
350
|
switch (field.type) {
|
|
204
351
|
case "boolean":
|
|
205
352
|
return value ? copy.value.yes : copy.value.no;
|
|
@@ -224,8 +371,8 @@ export function textOf(meta, name, value, locale) {
|
|
|
224
371
|
case "many2one":
|
|
225
372
|
return typeof value === "object" ? displayOf(field, value) : String(value);
|
|
226
373
|
case "embedded":
|
|
227
|
-
// A value object
|
|
228
|
-
return
|
|
374
|
+
// A value object reads through its definition; a money, the one every ERP holds, as one.
|
|
375
|
+
return embeddedText(field, value, locale);
|
|
229
376
|
default:
|
|
230
377
|
return typeof value === "object" ? JSON.stringify(value) : String(value);
|
|
231
378
|
}
|
package/dist/core/summary.d.ts
CHANGED
|
@@ -11,8 +11,12 @@ import type { AnyRecord, Meta } from "@sincpro/criteria";
|
|
|
11
11
|
*
|
|
12
12
|
* Which field plays each part: the one the entity declares (`entity(…, { summary })`), else
|
|
13
13
|
* the first conventional name the model holds (`description`, `state`/`status`, `created_at`,
|
|
14
|
-
* …). The name is the model's own `display`.
|
|
15
|
-
*
|
|
14
|
+
* …). The name is the model's own `display`. The convention only knows English names: a model
|
|
15
|
+
* whose state is `estado` and its description `descripcion` declares them —
|
|
16
|
+
* `entity(…, { summary: { state: "estado", description: "descripcion" } })` — or plays no such
|
|
17
|
+
* part (no tag on top, no colour on its rows). Nothing is guessed: an entity may hold two
|
|
18
|
+
* choices. It is the client's to say, never the Meta's: which field a screen shows as the state
|
|
19
|
+
* is presentation, and sincpro_framework does not publish it (#266, decided).
|
|
16
20
|
*/
|
|
17
21
|
export type RecordSummary = {
|
|
18
22
|
id: string;
|
package/dist/react/context.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type ChangeFeed, type Entity, type MetaStore, type Transport } from "@sincpro/client-framework";
|
|
2
2
|
import type { Meta } from "@sincpro/criteria";
|
|
3
3
|
import { type NameBook } from "../core/names.js";
|
|
4
|
+
import { type DateStyle } from "../core/present.js";
|
|
4
5
|
import type { Trail } from "../core/trail.js";
|
|
5
6
|
import { type ReactNode } from "react";
|
|
6
7
|
/**
|
|
@@ -19,7 +20,10 @@ import { type ReactNode } from "react";
|
|
|
19
20
|
* here. A value the backend must see on every request (the active company) goes in the
|
|
20
21
|
* process's context (`processContext.set({ company_id })`, @sincpro/client-framework): every
|
|
21
22
|
* request under this provider carries it in `sincpro-context` (`screensTransport`), and a change
|
|
22
|
-
* reads every screen again.
|
|
23
|
+
* reads every screen again. The language is one of those values: the provider puts its `locale`
|
|
24
|
+
* there as `lang`, so what the server writes in the call's language — a dry run's «Se eliminará
|
|
25
|
+
* «F-0042»», a use case's message — comes in the screen's. An application that sets `lang`
|
|
26
|
+
* itself keeps its own.
|
|
23
27
|
*
|
|
24
28
|
* What was written is the process's too (`processChanges`, @sincpro/client-framework): every write
|
|
25
29
|
* a transport of the framework sends is announced there, whoever sent it — a screen, or a use
|
|
@@ -41,8 +45,15 @@ type FrameworkContextValue = {
|
|
|
41
45
|
changes: ChangeFeed;
|
|
42
46
|
/** The names of records by key, asked one request per entity (`useNameOf`). */
|
|
43
47
|
names: NameBook;
|
|
48
|
+
/** How dates are written now — a piece that keeps its texts compares it to know when to
|
|
49
|
+
* write them again. */
|
|
50
|
+
dateStyle: DateStyle;
|
|
51
|
+
/** The business's currency, when one was installed. */
|
|
52
|
+
currency: string | undefined;
|
|
53
|
+
/** Each domain event's definition, by its wire name: what a history calls a fact's fields. */
|
|
54
|
+
events: Readonly<Record<string, Meta>>;
|
|
44
55
|
};
|
|
45
|
-
export declare function FrameworkProvider({ transport, locale, metas: seed, entities, timeZone, changes, children, }: {
|
|
56
|
+
export declare function FrameworkProvider({ transport, locale, metas: seed, entities, timeZone, dateStyle, currency, events, changes, children, }: {
|
|
46
57
|
transport: Transport;
|
|
47
58
|
/** The language labels and values are read in (`"es"`); the model's `default` without it. */
|
|
48
59
|
locale?: string;
|
|
@@ -55,6 +66,16 @@ export declare function FrameworkProvider({ transport, locale, metas: seed, enti
|
|
|
55
66
|
* the user works in: every date and time reads and is typed on its wall clock. Without it,
|
|
56
67
|
* the browser's. */
|
|
57
68
|
timeZone?: string;
|
|
69
|
+
/** How every date is written: `numeric` (the default) — «16/11/2026», «16/11/2026 15:36»,
|
|
70
|
+
* day first whatever the language — or `literal`, the month in words, where space asks
|
|
71
|
+
* for a word (a phone: `SincproApp` switches by itself). */
|
|
72
|
+
dateStyle?: DateStyle;
|
|
73
|
+
/** The business's currency (`"BOB"`): an amount the model draws as money (`widget: money`)
|
|
74
|
+
* travels bare, and is written in it. Without it, rounded to cents with no symbol. */
|
|
75
|
+
currency?: string;
|
|
76
|
+
/** Each domain event's definition, by its wire name — `eventMetasOf(contract)`
|
|
77
|
+
* (client-framework): a record's history names each fact's own fields in its words. */
|
|
78
|
+
events?: Readonly<Record<string, Meta>>;
|
|
58
79
|
/** Where writes are announced — the process's (`processChanges`) unless a test gives another. */
|
|
59
80
|
changes?: ChangeFeed;
|
|
60
81
|
children: ReactNode;
|
package/dist/react/context.js
CHANGED
|
@@ -2,11 +2,12 @@
|
|
|
2
2
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
3
|
import { installTimeZone, metaStore, processChanges, processContext, screensTransport, } from "@sincpro/client-framework";
|
|
4
4
|
import { nameBook } from "../core/names.js";
|
|
5
|
+
import { installCurrency, installDateStyle, } from "../core/present.js";
|
|
5
6
|
import { entityFor } from "../core/relation.js";
|
|
6
7
|
import { useContextValues } from "./useContextValues.js";
|
|
7
8
|
import { createContext, useContext, useEffect, useMemo, useRef, useState, } from "react";
|
|
8
9
|
const FrameworkContext = createContext(null);
|
|
9
|
-
export function FrameworkProvider({ transport, locale, metas: seed, entities = NO_ENTITIES, timeZone, changes = processChanges, children, }) {
|
|
10
|
+
export function FrameworkProvider({ transport, locale, metas: seed, entities = NO_ENTITIES, timeZone, dateStyle = "numeric", currency, events = NO_EVENTS, changes = processChanges, children, }) {
|
|
10
11
|
// Installed before the children draw, and again when it changes (another company): the
|
|
11
12
|
// formatters read it, so it cannot wait for an effect.
|
|
12
13
|
const zoned = useRef(null);
|
|
@@ -14,6 +15,29 @@ export function FrameworkProvider({ transport, locale, metas: seed, entities = N
|
|
|
14
15
|
installTimeZone(timeZone);
|
|
15
16
|
zoned.current = timeZone;
|
|
16
17
|
}
|
|
18
|
+
// The same for how dates and money are written: read by every text, so before any draws.
|
|
19
|
+
// The screens' language is the call's (`lang`, sincpro_framework's `ContextKey.LANG`): before
|
|
20
|
+
// anything reads, so the first requests already carry it — never over one the app set.
|
|
21
|
+
const spoke = useRef(null);
|
|
22
|
+
const speak = () => {
|
|
23
|
+
const now = processContext.get("lang");
|
|
24
|
+
if (locale === undefined || now === locale)
|
|
25
|
+
return;
|
|
26
|
+
if (now !== undefined && now !== spoke.current)
|
|
27
|
+
return;
|
|
28
|
+
spoke.current = locale;
|
|
29
|
+
processContext.set({ lang: locale });
|
|
30
|
+
};
|
|
31
|
+
if (spoke.current === null)
|
|
32
|
+
speak();
|
|
33
|
+
// Only a new language says it again.
|
|
34
|
+
useEffect(speak, [locale]);
|
|
35
|
+
const styled = useRef(null);
|
|
36
|
+
if (styled.current !== `${dateStyle}|${currency ?? ""}`) {
|
|
37
|
+
installDateStyle(dateStyle);
|
|
38
|
+
installCurrency(currency);
|
|
39
|
+
styled.current = `${dateStyle}|${currency ?? ""}`;
|
|
40
|
+
}
|
|
17
41
|
// One store for the provider's life: a new seed object on a re-render must not forget what
|
|
18
42
|
// the screens already learned.
|
|
19
43
|
const [metas] = useState(() => metaStore(seed));
|
|
@@ -32,10 +56,35 @@ export function FrameworkProvider({ transport, locale, metas: seed, entities = N
|
|
|
32
56
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
33
57
|
[screens, metas, entities, context]);
|
|
34
58
|
useEffect(() => changes.follow((change) => names.changed(change)), [changes, names]);
|
|
35
|
-
const value = useMemo(() => ({
|
|
59
|
+
const value = useMemo(() => ({
|
|
60
|
+
transport: screens,
|
|
61
|
+
locale,
|
|
62
|
+
metas,
|
|
63
|
+
entities,
|
|
64
|
+
trails,
|
|
65
|
+
context,
|
|
66
|
+
changes,
|
|
67
|
+
names,
|
|
68
|
+
dateStyle,
|
|
69
|
+
currency,
|
|
70
|
+
events,
|
|
71
|
+
}), [
|
|
72
|
+
screens,
|
|
73
|
+
locale,
|
|
74
|
+
metas,
|
|
75
|
+
entities,
|
|
76
|
+
trails,
|
|
77
|
+
context,
|
|
78
|
+
changes,
|
|
79
|
+
names,
|
|
80
|
+
dateStyle,
|
|
81
|
+
currency,
|
|
82
|
+
events,
|
|
83
|
+
]);
|
|
36
84
|
return _jsx(FrameworkContext.Provider, { value: value, children: children });
|
|
37
85
|
}
|
|
38
86
|
const NO_ENTITIES = [];
|
|
87
|
+
const NO_EVENTS = {};
|
|
39
88
|
export function useFramework() {
|
|
40
89
|
const value = useContext(FrameworkContext);
|
|
41
90
|
if (value === null)
|
package/dist/react/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
export { FrameworkProvider, useEntityFor, useFramework, } from "./context.js";
|
|
7
7
|
export { type ListScope, ListScopeProvider, useListScope, } from "./scope.js";
|
|
8
|
-
export { type AnswerOf, type CommandState, type Rehearsal, useCommand, } from "./useCommand.js";
|
|
8
|
+
export { type AnswerOf, type CommandState, type Rehearsal, useCommand, useRouteCommand, } from "./useCommand.js";
|
|
9
9
|
export { useContextValues } from "./useContextValues.js";
|
|
10
10
|
export { useCount } from "./useCount.js";
|
|
11
11
|
export { type Groups, useGroups } from "./useGroups.js";
|
|
@@ -15,6 +15,7 @@ export { excluding, type Lookup, useLookup } from "./useLookup.js";
|
|
|
15
15
|
export { useNameOf } from "./useNameOf.js";
|
|
16
16
|
export { type PivotAxes, type PivotReading, usePivot, } from "./usePivot.js";
|
|
17
17
|
export { type Editing, type RecordReading, type RecordState, useRecord, } from "./useRecord.js";
|
|
18
|
+
export { type LifecycleAction, type RecordLifecycleActions, useRecordLifecycle, } from "./useRecordLifecycle.js";
|
|
18
19
|
export { useRelated } from "./useRelated.js";
|
|
19
20
|
export { type ServiceState, useService } from "./useService.js";
|
|
20
21
|
export { type ServiceReading, useServiceReading, } from "./useServiceReading.js";
|
package/dist/react/index.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
export { FrameworkProvider, useEntityFor, useFramework, } from "./context.js";
|
|
7
7
|
export { ListScopeProvider, useListScope, } from "./scope.js";
|
|
8
|
-
export { useCommand, } from "./useCommand.js";
|
|
8
|
+
export { useCommand, useRouteCommand, } from "./useCommand.js";
|
|
9
9
|
export { useContextValues } from "./useContextValues.js";
|
|
10
10
|
export { useCount } from "./useCount.js";
|
|
11
11
|
export { useGroups } from "./useGroups.js";
|
|
@@ -15,6 +15,7 @@ export { excluding, useLookup } from "./useLookup.js";
|
|
|
15
15
|
export { useNameOf } from "./useNameOf.js";
|
|
16
16
|
export { usePivot, } from "./usePivot.js";
|
|
17
17
|
export { useRecord, } from "./useRecord.js";
|
|
18
|
+
export { useRecordLifecycle, } from "./useRecordLifecycle.js";
|
|
18
19
|
export { useRelated } from "./useRelated.js";
|
|
19
20
|
export { useService } from "./useService.js";
|
|
20
21
|
export { useServiceReading, } from "./useServiceReading.js";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type CommandAnswers, type CommandBodies, type Entity } from "@sincpro/client-framework";
|
|
1
|
+
import { type CommandAnswers, type CommandBodies, type Entity, type WriteRoute } from "@sincpro/client-framework";
|
|
2
2
|
import type { AnyRecord } from "@sincpro/criteria";
|
|
3
3
|
/**
|
|
4
4
|
* One Command of an entity: run it, or ask first what it would do.
|
|
@@ -38,3 +38,9 @@ export type CommandState<R, B extends AnyRecord = AnyRecord> = {
|
|
|
38
38
|
*/
|
|
39
39
|
export type AnswerOf<R, A, N> = unknown extends R ? (N extends keyof A ? A[N] : unknown) : R;
|
|
40
40
|
export declare function useCommand<T extends AnyRecord, R = unknown, C extends CommandBodies = CommandBodies, N extends keyof C & string = keyof C & string, A extends CommandAnswers = CommandAnswers>(entity: Entity<T, C, A>, name: N): CommandState<AnswerOf<R, A, N>, C[N]>;
|
|
41
|
+
/**
|
|
42
|
+
* One write route of an entity run as a Command — what `useCommand` runs for a declared Command,
|
|
43
|
+
* and `useRecordLifecycle` for the record's own `delete`. `route` may be absent (an entity that
|
|
44
|
+
* declares none): the hook is still called, and running it is refused naming the entity.
|
|
45
|
+
*/
|
|
46
|
+
export declare function useRouteCommand<R, B extends AnyRecord = AnyRecord>(route: WriteRoute | undefined, owner: string): CommandState<R, B>;
|
package/dist/react/useCommand.js
CHANGED
|
@@ -3,11 +3,19 @@ import { bodyFor, pathFor, retryKeys, } from "@sincpro/client-framework";
|
|
|
3
3
|
import { useFramework } from "./context.js";
|
|
4
4
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
5
5
|
export function useCommand(entity, name) {
|
|
6
|
-
const { transport } = useFramework();
|
|
7
6
|
// A name outside the declared ones still reaches here from untyped code: say which.
|
|
8
7
|
const route = entity.commands[name];
|
|
9
8
|
if (route === undefined)
|
|
10
9
|
throw new Error(`${entity.key} declares no command "${name}"`);
|
|
10
|
+
return useRouteCommand(route, entity.key);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* One write route of an entity run as a Command — what `useCommand` runs for a declared Command,
|
|
14
|
+
* and `useRecordLifecycle` for the record's own `delete`. `route` may be absent (an entity that
|
|
15
|
+
* declares none): the hook is still called, and running it is refused naming the entity.
|
|
16
|
+
*/
|
|
17
|
+
export function useRouteCommand(route, owner) {
|
|
18
|
+
const { transport } = useFramework();
|
|
11
19
|
const [busy, setBusy] = useState(false);
|
|
12
20
|
const [error, setError] = useState(null);
|
|
13
21
|
const [rehearsal, setRehearsal] = useState(null);
|
|
@@ -22,6 +30,8 @@ export function useCommand(entity, name) {
|
|
|
22
30
|
// not run it twice. A dry run touches nothing, so each one gets a key of its own.
|
|
23
31
|
const keys = useRef(retryKeys());
|
|
24
32
|
const send = useCallback(async (id, body, gesture) => {
|
|
33
|
+
if (route === undefined)
|
|
34
|
+
throw new Error(`${owner} declares no route for this`);
|
|
25
35
|
setBusy(true);
|
|
26
36
|
setError(null);
|
|
27
37
|
const idempotencyKey = gesture.keyFor({ id, body });
|
|
@@ -42,7 +52,7 @@ export function useCommand(entity, name) {
|
|
|
42
52
|
if (mounted.current)
|
|
43
53
|
setBusy(false);
|
|
44
54
|
}
|
|
45
|
-
}, [transport, route]);
|
|
55
|
+
}, [transport, route, owner]);
|
|
46
56
|
return {
|
|
47
57
|
run: (id, body) => send(id, body ?? {}, keys.current),
|
|
48
58
|
rehearse: async (id, body) => {
|
package/dist/react/useGroups.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { resumingFrom } from "@sincpro/criteria";
|
|
2
|
+
import { resumingFrom, } from "@sincpro/criteria";
|
|
3
|
+
import { areGroups, NotGrouped } from "../core/groups.js";
|
|
3
4
|
import { measuresOf, sumsOf } from "../core/measure.js";
|
|
4
5
|
import { useFramework } from "./context.js";
|
|
5
6
|
import { useWrites } from "./useWrites.js";
|
|
@@ -25,6 +26,8 @@ export function useGroups(listing, options = {}) {
|
|
|
25
26
|
// Written by anyone — a save, a use case, a panel beside: the groups are counted again.
|
|
26
27
|
const writes = useWrites(route);
|
|
27
28
|
const sums = (options.sums ?? sumsOf(listing.meta)).join(",");
|
|
29
|
+
// Each by the function the model adds it up with (`total`), as text: compared by content.
|
|
30
|
+
const measured = sums === "" ? "" : JSON.stringify(measuresOf(sums.split(","), listing.meta));
|
|
28
31
|
// What adds up by default is read from the model: without it yet, asking now and again when
|
|
29
32
|
// it arrives would be two questions — and the groups on screen replaced under the pointer.
|
|
30
33
|
const waiting = options.sums === undefined && listing.meta === null;
|
|
@@ -43,21 +46,29 @@ export function useGroups(listing, options = {}) {
|
|
|
43
46
|
...JSON.parse(question),
|
|
44
47
|
grouping: {
|
|
45
48
|
group_by: levels.map((level) => ({ ...level })),
|
|
46
|
-
...(
|
|
49
|
+
...(measured === ""
|
|
50
|
+
? {}
|
|
51
|
+
: { measures: JSON.parse(measured) }),
|
|
47
52
|
pagination: { limit: MAX_GROUPS },
|
|
48
53
|
},
|
|
49
54
|
// One id per group is the least a grouped answer carries; the rows come on opening.
|
|
50
55
|
pagination: { limit: 1 },
|
|
51
56
|
};
|
|
52
57
|
transport.list(route, criteria, aborter.signal).then((page) => {
|
|
53
|
-
if (
|
|
58
|
+
if (aborter.signal.aborted)
|
|
59
|
+
return;
|
|
60
|
+
// Records answered to a grouped question are not groups: the list's failure, never a
|
|
61
|
+
// table drawing piles without a count.
|
|
62
|
+
if (!areGroups(page.rows))
|
|
63
|
+
setState({ status: "failed", buckets: [], error: new NotGrouped(route) });
|
|
64
|
+
else
|
|
54
65
|
setState({ status: "ready", buckets: page.rows, error: null });
|
|
55
66
|
}, (error) => {
|
|
56
67
|
if (!aborter.signal.aborted)
|
|
57
68
|
setState({ status: "failed", buckets: [], error });
|
|
58
69
|
});
|
|
59
70
|
return () => aborter.abort();
|
|
60
|
-
}, [transport, route, question, levels,
|
|
71
|
+
}, [transport, route, question, levels, measured, waiting, context, writes]);
|
|
61
72
|
/** One page of a group: the first (`after` null) or the one the cursor points at. */
|
|
62
73
|
const read = useCallback((bucket, after) => {
|
|
63
74
|
const key = keyOf(bucket);
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { type Entity } from "@sincpro/client-framework";
|
|
2
2
|
import type { AnyRecord } from "@sincpro/criteria";
|
|
3
|
-
import { type HistoryEntry } from "../core/history.js";
|
|
3
|
+
import { type FactTone, type HistoryEntry } from "../core/history.js";
|
|
4
4
|
import { type DependencyList } from "react";
|
|
5
|
-
export declare function useHistory<T extends AnyRecord>(entity: Entity<T>, id: string, deps?: DependencyList
|
|
5
|
+
export declare function useHistory<T extends AnyRecord>(entity: Entity<T>, id: string, deps?: DependencyList, options?: {
|
|
6
|
+
tones?: Readonly<Record<string, FactTone>> | undefined;
|
|
7
|
+
}): HistoryEntry[] | undefined;
|
package/dist/react/useHistory.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { listRoute } from "@sincpro/client-framework";
|
|
3
|
-
import { historyOf } from "../core/history.js";
|
|
3
|
+
import { historyOf, } from "../core/history.js";
|
|
4
4
|
import { useFramework } from "./context.js";
|
|
5
5
|
import { useWrites } from "./useWrites.js";
|
|
6
6
|
import { useEffect, useState } from "react";
|
|
@@ -14,12 +14,22 @@ import { useEffect, useState } from "react";
|
|
|
14
14
|
* says «nothing happened», which is a different claim from «you may not see it». Pass the
|
|
15
15
|
* record among `deps` so a write that reloads it reads its new facts too.
|
|
16
16
|
*
|
|
17
|
+
* Each side of an edit is written by the record's own model when it is known (`metas`), and
|
|
18
|
+
* each kind of fact takes the tone `tones` gives its `name` (`presented(entity, { facts })`).
|
|
19
|
+
*
|
|
17
20
|
* Named after sincpro_framework's `history_of`; web-ui's `useActivity` is something else (the
|
|
18
21
|
* panel's open state).
|
|
19
22
|
*/
|
|
20
23
|
const SHOWN = 100;
|
|
21
|
-
export function useHistory(entity, id, deps = []) {
|
|
22
|
-
const { transport, locale, context } = useFramework();
|
|
24
|
+
export function useHistory(entity, id, deps = [], options = {}) {
|
|
25
|
+
const { transport, locale, context, metas, events, dateStyle, currency } = useFramework();
|
|
26
|
+
const tones = options.tones;
|
|
27
|
+
// By content: a screen passes its tones as a literal, a new object on every render.
|
|
28
|
+
const toned = tones === undefined ? "" : JSON.stringify(tones);
|
|
29
|
+
// By the events it names: an application that builds them inline must not read it again.
|
|
30
|
+
const named = Object.keys(events).join("|");
|
|
31
|
+
// How its texts are written: another tone, event, date style or currency writes them again.
|
|
32
|
+
const written = `${toned}\u0000${named}\u0000${dateStyle}\u0000${currency ?? ""}`;
|
|
23
33
|
const [entries, setEntries] = useState(undefined);
|
|
24
34
|
const history = entity.history;
|
|
25
35
|
// A write under the record — anyone's — adds a fact to its history: read it again.
|
|
@@ -34,9 +44,14 @@ export function useHistory(entity, id, deps = []) {
|
|
|
34
44
|
order: [{ field: "created_at", descending: true }],
|
|
35
45
|
pagination: { limit: SHOWN },
|
|
36
46
|
};
|
|
37
|
-
transport.list(listRoute(history, { id }), criteria, aborter.signal).then((page) => aborter.signal.aborted ||
|
|
47
|
+
transport.list(listRoute(history, { id }), criteria, aborter.signal).then((page) => aborter.signal.aborted ||
|
|
48
|
+
setEntries(historyOf(page.rows, page.meta, locale, {
|
|
49
|
+
record: metas.get(entity.key),
|
|
50
|
+
events,
|
|
51
|
+
tones,
|
|
52
|
+
})), () => aborter.signal.aborted || setEntries(undefined));
|
|
38
53
|
return () => aborter.abort();
|
|
39
54
|
// eslint-disable-next-line react-hooks/exhaustive-deps -- the caller names what reloads it
|
|
40
|
-
}, [transport, history, id, locale, context, writes, ...deps]);
|
|
55
|
+
}, [transport, history, id, locale, context, writes, written, ...deps]);
|
|
41
56
|
return entries;
|
|
42
57
|
}
|