@sincpro/web-framework 2.4.0 → 2.6.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.
Files changed (68) hide show
  1. package/README.md +117 -86
  2. package/dist/core/cache.d.ts +19 -7
  3. package/dist/core/cache.js +81 -44
  4. package/dist/core/changes.d.ts +25 -0
  5. package/dist/core/changes.js +39 -0
  6. package/dist/core/edit.d.ts +19 -0
  7. package/dist/core/edit.js +46 -0
  8. package/dist/core/index.d.ts +3 -3
  9. package/dist/core/index.js +3 -3
  10. package/dist/core/list.d.ts +20 -0
  11. package/dist/core/list.js +32 -1
  12. package/dist/core/names.d.ts +25 -0
  13. package/dist/core/names.js +79 -0
  14. package/dist/core/present.d.ts +22 -0
  15. package/dist/core/present.js +51 -19
  16. package/dist/react/context.d.ts +15 -2
  17. package/dist/react/context.js +11 -4
  18. package/dist/react/index.d.ts +4 -0
  19. package/dist/react/index.js +4 -0
  20. package/dist/react/useCount.d.ts +18 -0
  21. package/dist/react/useCount.js +23 -0
  22. package/dist/react/useGroups.js +4 -1
  23. package/dist/react/useHistory.js +4 -1
  24. package/dist/react/useListing.js +9 -6
  25. package/dist/react/useNameOf.d.ts +5 -4
  26. package/dist/react/useNameOf.js +25 -26
  27. package/dist/react/usePivot.js +4 -1
  28. package/dist/react/useRecord.d.ts +4 -4
  29. package/dist/react/useRecord.js +20 -13
  30. package/dist/react/useService.d.ts +5 -1
  31. package/dist/react/useServiceReading.d.ts +35 -0
  32. package/dist/react/useServiceReading.js +75 -0
  33. package/dist/react/useTextOf.d.ts +16 -0
  34. package/dist/react/useTextOf.js +21 -0
  35. package/dist/react/useWrites.d.ts +19 -0
  36. package/dist/react/useWrites.js +35 -0
  37. package/dist/web/index.d.ts +1 -0
  38. package/dist/web/index.js +1 -0
  39. package/dist/web/list/EntityList.d.ts +4 -1
  40. package/dist/web/list/EntityList.js +6 -3
  41. package/dist/web/list/EntitySearchBar.d.ts +4 -1
  42. package/dist/web/list/EntitySearchBar.js +2 -2
  43. package/dist/web/list/groups.d.ts +13 -3
  44. package/dist/web/list/groups.js +21 -8
  45. package/dist/web/list/openIn.d.ts +4 -2
  46. package/dist/web/list/openIn.js +9 -2
  47. package/dist/web/record/ActionButton.d.ts +34 -0
  48. package/dist/web/record/ActionButton.js +41 -0
  49. package/dist/web/record/CommandButton.d.ts +5 -6
  50. package/dist/web/record/CommandButton.js +16 -33
  51. package/dist/web/record/ServiceButton.d.ts +31 -0
  52. package/dist/web/record/ServiceButton.js +40 -0
  53. package/dist/web/record/control.d.ts +9 -3
  54. package/dist/web/record/control.js +13 -4
  55. package/dist/web/record/edit.js +1 -1
  56. package/dist/web/record/lines.js +1 -1
  57. package/dist/web/record/record.js +36 -3
  58. package/dist/web/workspace/SincproApp.d.ts +8 -2
  59. package/dist/web/workspace/SincproApp.js +13 -7
  60. package/dist/web/workspace/composer.d.ts +6 -2
  61. package/dist/web/workspace/composer.js +31 -12
  62. package/dist/web/workspace/open.d.ts +1 -1
  63. package/dist/web/workspace/open.js +7 -1
  64. package/dist/web/workspace/record.d.ts +6 -2
  65. package/dist/web/workspace/record.js +6 -5
  66. package/dist/web/workspace/workspace.d.ts +7 -1
  67. package/dist/web/workspace/workspace.js +13 -4
  68. package/package.json +6 -6
package/dist/core/edit.js CHANGED
@@ -180,3 +180,49 @@ export function creationOf(meta, values) {
180
180
  }
181
181
  return sent;
182
182
  }
183
+ /**
184
+ * What an edit of a STORED record sends (sincpro_framework's `Edit`): the fields the person
185
+ * changed, and inside them nothing the model marks `readonly` — a line keeps only its identity,
186
+ * the way the form names the line it edits.
187
+ *
188
+ * editedOf(invoiceMeta, record, ["discount", "lines"])
189
+ * -> { discount: "5", lines: [{ id: "l1", qty: 2 }, { qty: 1 }] }
190
+ *
191
+ * Context: the form used to send the record whole, as it holds it — `id`, `version`,
192
+ * `created_at` and each line's own among them. sincpro_framework up to 3.36 refused a field the
193
+ * framework writes at every level (`apply_values`); 3.37 leaves that echo out, but still
194
+ * refuses a relation read as a page (`{items, count, cursor}`) written back, since it would
195
+ * remove every record the page did not bring. So a stored record could be created and never
196
+ * edited. The backend keeps what it stored and the caller's draft, and puts this edit on top of
197
+ * both: what changed is all it needs, on either version. The model marks the fields the
198
+ * framework writes `readonly` (and the computed ones), so nothing here names them. A new record
199
+ * goes through `creationOf`.
200
+ */
201
+ export function editedOf(meta, values, changed) {
202
+ const sent = {};
203
+ for (const name of changed) {
204
+ const field = meta?.fields[name];
205
+ if (field?.readonly === true)
206
+ continue;
207
+ sent[name] = writableOf(field?.definition, values[name]);
208
+ }
209
+ return sent;
210
+ }
211
+ /** A value of a list of records (the lines) or of one record held inside (a value object),
212
+ * without what the model of those records marks `readonly` — the identity kept. Anything else
213
+ * as it is. */
214
+ function writableOf(definition, value) {
215
+ if (definition == null || value === null || typeof value !== "object")
216
+ return value;
217
+ if (Array.isArray(value))
218
+ return value.map((one) => writableOf(definition, one));
219
+ const identity = definition.identity || "id";
220
+ const kept = {};
221
+ for (const [name, one] of Object.entries(value)) {
222
+ const field = definition.fields[name];
223
+ if (name !== identity && field?.readonly === true)
224
+ continue;
225
+ kept[name] = writableOf(field?.definition, one);
226
+ }
227
+ return kept;
228
+ }
@@ -9,15 +9,15 @@
9
9
  */
10
10
  export { type CachedTransport, cachedTransport, type CacheOptions, } from "./cache.js";
11
11
  export { ENGLISH_FRAMEWORK_COPY, type FrameworkCopy, frameworkCopy, installFrameworkCopy, SPANISH_FRAMEWORK_COPY, } from "./copy.js";
12
- export { type Advice, adviceAt, changesOf, computedLocally, creationOf, defaultsOf, errorAt, errorsByField, type FieldState, fieldStateOf, type FieldStates, linePlacesOf, lineStatesOf, missingOf, needsPreview, pathOf, type ServerFieldState, } from "./edit.js";
12
+ export { type Advice, adviceAt, changesOf, computedLocally, creationOf, defaultsOf, editedOf, errorAt, errorsByField, type FieldState, fieldStateOf, type FieldStates, linePlacesOf, lineStatesOf, missingOf, needsPreview, pathOf, type ServerFieldState, } from "./edit.js";
13
13
  export { explain } from "./explain.js";
14
14
  export { type FormLayout, formOf } from "./form.js";
15
15
  export { type HistoryEntry, historyOf } from "./history.js";
16
16
  export { addingOf, type AddingRule, blankLine, type Declared, editableLines, isReference, type LineAdding, lineFields, lineKey, linesAnswered, moved, sameLine, writtenInCell, } from "./lines.js";
17
- export { defineList, type GroupLevel, isList, type ListDefinition, type ListSpec, type NamedFilter, scopeOf, } from "./list.js";
17
+ export { defineList, type GroupLevel, isList, type ListDefinition, type ListSpec, type NamedFilter, SCOPE_NAME_PARAM, scopedRef, scopeNameOf, scopeOf, } from "./list.js";
18
18
  export { lookupOf, type LookupOption } from "./lookup.js";
19
19
  export { detailOf, measuresOf, sumsOf } from "./measure.js";
20
- export { BOOKKEEPING, defaultFields, type FieldView, sortKeyOf, textOf, viewOf, } from "./present.js";
20
+ export { BOOKKEEPING, dayTextOf, defaultFields, type FieldView, instantTextOf, moneyTextOf, numberTextOf, sortKeyOf, textOf, viewOf, } from "./present.js";
21
21
  export { type Related, relatedOf } from "./related.js";
22
22
  export { backReference, entityFor, relatedScope, type RelationField, relationFieldOf, } from "./relation.js";
23
23
  export { type RecordSummary, summaryFieldsOf, summaryOf, } from "./summary.js";
@@ -9,15 +9,15 @@
9
9
  */
10
10
  export { cachedTransport, } from "./cache.js";
11
11
  export { ENGLISH_FRAMEWORK_COPY, frameworkCopy, installFrameworkCopy, SPANISH_FRAMEWORK_COPY, } from "./copy.js";
12
- export { adviceAt, changesOf, computedLocally, creationOf, defaultsOf, errorAt, errorsByField, fieldStateOf, linePlacesOf, lineStatesOf, missingOf, needsPreview, pathOf, } from "./edit.js";
12
+ export { adviceAt, changesOf, computedLocally, creationOf, defaultsOf, editedOf, errorAt, errorsByField, fieldStateOf, linePlacesOf, lineStatesOf, missingOf, needsPreview, pathOf, } from "./edit.js";
13
13
  export { explain } from "./explain.js";
14
14
  export { formOf } from "./form.js";
15
15
  export { historyOf } from "./history.js";
16
16
  export { addingOf, blankLine, editableLines, isReference, lineFields, lineKey, linesAnswered, moved, sameLine, writtenInCell, } from "./lines.js";
17
- export { defineList, isList, scopeOf, } from "./list.js";
17
+ export { defineList, isList, SCOPE_NAME_PARAM, scopedRef, scopeNameOf, scopeOf, } from "./list.js";
18
18
  export { lookupOf } from "./lookup.js";
19
19
  export { detailOf, measuresOf, sumsOf } from "./measure.js";
20
- export { BOOKKEEPING, defaultFields, sortKeyOf, textOf, viewOf, } from "./present.js";
20
+ export { BOOKKEEPING, dayTextOf, defaultFields, instantTextOf, moneyTextOf, numberTextOf, sortKeyOf, textOf, viewOf, } from "./present.js";
21
21
  export { relatedOf } from "./related.js";
22
22
  export { backReference, entityFor, relatedScope, relationFieldOf, } from "./relation.js";
23
23
  export { summaryFieldsOf, summaryOf, } from "./summary.js";
@@ -44,6 +44,8 @@ export type ListDefinition<T extends AnyRecord = AnyRecord> = {
44
44
  views?: readonly ("table" | "rows" | "board" | "sheet" | "pivot")[];
45
45
  /** Grouped from the start — the server counts the groups. */
46
46
  groupBy?: readonly GroupLevel[];
47
+ /** The fields «Group by» offers, in this order — among those the model lets split the set. */
48
+ groupable?: readonly string[];
47
49
  };
48
50
  /** A cut offered by name in the search bar: «sólo pagadas», «vencidas». */
49
51
  export type NamedFilter<T> = {
@@ -63,3 +65,21 @@ export declare function defineList<T extends AnyRecord>(entity: Entity<T>, spec:
63
65
  export declare function isList<T extends AnyRecord>(given: Entity<T> | ListDefinition<T>): given is ListDefinition<T>;
64
66
  /** The criteria a definition always asks: its scope, in its order. */
65
67
  export declare function scopeOf<T extends AnyRecord>(list: ListDefinition<T>): Criteria<T>;
68
+ /** What a list opened under a cut is named by — «María» of «Pagos · María». */
69
+ export declare const SCOPE_NAME_PARAM = "of";
70
+ /**
71
+ * An entity's list as a workspace reference, opened under a cut: «this member's payments»,
72
+ * what a smart button opens. With `name`, the list says whose it is: «Pagos · María» in its
73
+ * heading, the tab and the breadcrumb — a cut that travels without one reads as the whole list.
74
+ *
75
+ * api.open(scopedRef(payments, { field: "member_id", value: member.id }, member.name))
76
+ *
77
+ * Context: the cut travels in the reference — client-framework's `SCOPE_PARAM`, the param a
78
+ * chooser carries its cut in (`chooserRef(query, scope)`) — so it survives a reload and a link,
79
+ * and the list opened from it, the application's own too, reads under it ANDed with everything
80
+ * a person searches (`entityView` gives it to `useListing`). An application built this reference
81
+ * by hand. It narrows what is shown; the server still decides what may be read.
82
+ */
83
+ export declare function scopedRef<T extends AnyRecord>(entity: Entity<T>, selection: Expression<T>, name?: string): EntityRef;
84
+ /** The name a reference's cut carries (`scopedRef(…, name)`), or nothing. */
85
+ export declare function scopeNameOf(ref: Pick<EntityRef, "params">): string | undefined;
package/dist/core/list.js CHANGED
@@ -1,4 +1,4 @@
1
- import {} from "@sincpro/client-framework";
1
+ import { SCOPE_PARAM } from "@sincpro/client-framework";
2
2
  export function defineList(entity, spec) {
3
3
  if (!/^[a-z0-9][a-z0-9-]*$/.test(spec.id))
4
4
  throw new Error(`list id "${spec.id}" is not kebab-case: it becomes a workspace kind`);
@@ -21,3 +21,34 @@ export function scopeOf(list) {
21
21
  ...(list.order === undefined ? {} : { order: [list.order] }),
22
22
  };
23
23
  }
24
+ /** What a list opened under a cut is named by — «María» of «Pagos · María». */
25
+ export const SCOPE_NAME_PARAM = "of";
26
+ /**
27
+ * An entity's list as a workspace reference, opened under a cut: «this member's payments»,
28
+ * what a smart button opens. With `name`, the list says whose it is: «Pagos · María» in its
29
+ * heading, the tab and the breadcrumb — a cut that travels without one reads as the whole list.
30
+ *
31
+ * api.open(scopedRef(payments, { field: "member_id", value: member.id }, member.name))
32
+ *
33
+ * Context: the cut travels in the reference — client-framework's `SCOPE_PARAM`, the param a
34
+ * chooser carries its cut in (`chooserRef(query, scope)`) — so it survives a reload and a link,
35
+ * and the list opened from it, the application's own too, reads under it ANDed with everything
36
+ * a person searches (`entityView` gives it to `useListing`). An application built this reference
37
+ * by hand. It narrows what is shown; the server still decides what may be read.
38
+ */
39
+ export function scopedRef(entity, selection, name) {
40
+ const list = entity.ref();
41
+ return {
42
+ ...list,
43
+ params: {
44
+ ...list.params,
45
+ [SCOPE_PARAM]: JSON.stringify(selection),
46
+ ...(name === undefined || name === "" ? {} : { [SCOPE_NAME_PARAM]: name }),
47
+ },
48
+ };
49
+ }
50
+ /** The name a reference's cut carries (`scopedRef(…, name)`), or nothing. */
51
+ export function scopeNameOf(ref) {
52
+ const name = ref.params?.[SCOPE_NAME_PARAM];
53
+ return name === undefined || name === "" ? undefined : name;
54
+ }
@@ -0,0 +1,25 @@
1
+ import { type Change, type Entity, type MetaStore, type Transport } from "@sincpro/client-framework";
2
+ /**
3
+ * The names of records by their keys — what a cell, a fact or a chip that holds only a key shows
4
+ * — asked in ONE request per entity, however many of them ask at once.
5
+ *
6
+ * Context: a page of 50 rows naming 50 tenants asked 50 requests, one per cell, so screens showed
7
+ * the key instead of the name (ROADMAP: «asking for a name per row does not scale»). Here every
8
+ * key asked in the same tick waits together, and one page answers them all — the entity's own
9
+ * list, narrowed to those keys (`in`), in slices of `SLICE` so no page is asked without a
10
+ * ceiling. A name is kept for the book's life, shared by every place that shows it, and forgotten
11
+ * when a write touches its entity (`changed`: a rename). Unnamed is no error — the place shows the
12
+ * key —; a request that fails is not kept, so the next place asks again. An entity listed only
13
+ * under another record (`/tenants/{tenant_id}/members`) has no list to ask by key: it stays
14
+ * unnamed.
15
+ *
16
+ * const names = nameBook(transport, metas, entities);
17
+ * await names.nameOf(tenants, "acme") -> "ACME S.R.L."
18
+ */
19
+ export type NameBook = {
20
+ /** The name of record `id` of `entity`; `undefined` when it has none to read or it failed. */
21
+ nameOf(entity: Entity, id: string): Promise<string | undefined>;
22
+ /** A write landed: forget the names it may have changed. */
23
+ changed(change: Change): void;
24
+ };
25
+ export declare function nameBook(transport: Transport, metas: MetaStore, entities?: readonly Entity[]): NameBook;
@@ -0,0 +1,79 @@
1
+ import { listRoute, } from "@sincpro/client-framework";
2
+ import { touches } from "./changes.js";
3
+ import { lookupOf } from "./lookup.js";
4
+ /** The most keys one request asks for. */
5
+ const SLICE = 100;
6
+ /** The route an entity is listed at, or `undefined` when it is listed only under a record. */
7
+ function routeOf(entity) {
8
+ try {
9
+ return listRoute(entity.list);
10
+ }
11
+ catch {
12
+ return undefined;
13
+ }
14
+ }
15
+ export function nameBook(transport, metas, entities = []) {
16
+ /** By entity key: where it is listed, and by record key the name, asked or answered. */
17
+ const known = new Map();
18
+ /** By entity key: the keys waiting for the next request, and how to answer each. */
19
+ const asking = new Map();
20
+ async function ask(entityKey) {
21
+ const batch = asking.get(entityKey);
22
+ if (batch === undefined)
23
+ return;
24
+ asking.delete(entityKey);
25
+ const { entity, route, waiting } = batch;
26
+ const meta = metas.get(entity.key);
27
+ const field = meta?.get_id || meta?.identity || "id";
28
+ const keys = [...waiting.keys()];
29
+ for (let at = 0; at < keys.length; at += SLICE) {
30
+ const slice = keys.slice(at, at + SLICE);
31
+ const criteria = {
32
+ selection: { field, operator: "in", value: slice },
33
+ pagination: { limit: slice.length },
34
+ };
35
+ try {
36
+ const page = await transport.list(route, criteria);
37
+ const named = new Map(lookupOf(page.meta ?? meta ?? null, page.rows, entity.summary).map((one) => [
38
+ one.id,
39
+ one.label,
40
+ ]));
41
+ for (const id of slice)
42
+ waiting.get(id)?.(named.get(id));
43
+ }
44
+ catch {
45
+ for (const id of slice) {
46
+ known.get(entityKey)?.names.delete(id);
47
+ waiting.get(id)?.(undefined);
48
+ }
49
+ }
50
+ }
51
+ }
52
+ return {
53
+ nameOf(entity, id) {
54
+ const route = routeOf(entity);
55
+ if (route === undefined || id === "")
56
+ return Promise.resolve(undefined);
57
+ const shelf = known.get(entity.key) ?? { route, names: new Map() };
58
+ known.set(entity.key, shelf);
59
+ const kept = shelf.names.get(id);
60
+ if (kept !== undefined)
61
+ return kept;
62
+ const name = new Promise((answer) => {
63
+ const batch = asking.get(entity.key);
64
+ if (batch !== undefined)
65
+ return void batch.waiting.set(id, answer);
66
+ asking.set(entity.key, { entity, route, waiting: new Map([[id, answer]]) });
67
+ // Every key asked in this tick goes in the same request.
68
+ queueMicrotask(() => void ask(entity.key));
69
+ });
70
+ shelf.names.set(id, name);
71
+ return name;
72
+ },
73
+ changed(change) {
74
+ for (const [entityKey, shelf] of [...known])
75
+ if (touches(change, shelf.route, entities))
76
+ known.delete(entityKey);
77
+ },
78
+ };
79
+ }
@@ -30,6 +30,28 @@ export declare function viewOf(meta: Meta | null | undefined, name: string, loca
30
30
  export declare function defaultFields(meta: Meta | null | undefined): string[];
31
31
  /** The bookkeeping every framework entity carries; shown on demand, never by default. */
32
32
  export declare const BOOKKEEPING: ReadonlySet<string>;
33
+ /**
34
+ * What a value reads as when a screen holds it outside any model — a total it added up, a
35
+ * due date of its own: the same text `textOf` gives a field of that type, in `locale`, on the
36
+ * zone the application installed. `null` when there is nothing to read. `useTextOf()` binds them
37
+ * to the provider's locale.
38
+ *
39
+ * numberTextOf("1150.5", "es-BO") -> "1.150,5"
40
+ * dayTextOf("2026-03-12", "es") -> "12 mar 2026"
41
+ * instantTextOf("2026-03-12T14:00:00Z", "es") -> "12 mar 2026, 10:00" (La Paz)
42
+ * moneyTextOf({ amount: "1150.5", currency: "BOB" }, "es-BO") -> "Bs 1.150,50"
43
+ *
44
+ * Context: every application wrote its own money and date formatting for its own screens, on
45
+ * the browser's zone and its own locale, while the framework had the right one private.
46
+ */
47
+ export declare function numberTextOf(value: unknown, locale?: string): string | null;
48
+ /** A day (`"2026-03-12"`) as a person reads it. A day has no zone: it is never converted. */
49
+ export declare function dayTextOf(value: unknown, locale?: string): string | null;
50
+ /** An instant as a person reads it, on the wall clock of the zone the application installed. */
51
+ export declare function instantTextOf(value: unknown, locale?: string): string | null;
52
+ /** A money (`{ amount, currency }`) as a person reads it, rounded to its currency's decimals;
53
+ * `null` when the value is not one. */
54
+ export declare function moneyTextOf(value: unknown, locale?: string): string | null;
33
55
  /**
34
56
  * The text a value reads as. `null` when there is nothing to read — the caller draws its own
35
57
  * «empty» (a dash in a fact, nothing in a cell).
@@ -137,6 +137,51 @@ function displayOf(field, value) {
137
137
  const shown = value[key] ?? value["display_name"] ?? value["name"] ?? value["id"];
138
138
  return shown === undefined || shown === null ? "" : String(shown);
139
139
  }
140
+ /**
141
+ * What a value reads as when a screen holds it outside any model — a total it added up, a
142
+ * due date of its own: the same text `textOf` gives a field of that type, in `locale`, on the
143
+ * zone the application installed. `null` when there is nothing to read. `useTextOf()` binds them
144
+ * to the provider's locale.
145
+ *
146
+ * numberTextOf("1150.5", "es-BO") -> "1.150,5"
147
+ * dayTextOf("2026-03-12", "es") -> "12 mar 2026"
148
+ * instantTextOf("2026-03-12T14:00:00Z", "es") -> "12 mar 2026, 10:00" (La Paz)
149
+ * moneyTextOf({ amount: "1150.5", currency: "BOB" }, "es-BO") -> "Bs 1.150,50"
150
+ *
151
+ * Context: every application wrote its own money and date formatting for its own screens, on
152
+ * the browser's zone and its own locale, while the framework had the right one private.
153
+ */
154
+ export function numberTextOf(value, locale) {
155
+ if (value === null || value === undefined || value === "")
156
+ return null;
157
+ if (Number.isNaN(Number(value)))
158
+ return String(value);
159
+ // A decimal that travels as text goes to `Intl` as text: every digit kept, none through a
160
+ // float (`"12345678901234567.89"` reads whole).
161
+ if (typeof value === "string")
162
+ return numberFormat(locale, decimals(value)).format(value.trim());
163
+ return numberFormat(locale, undefined).format(Number(value));
164
+ }
165
+ /** A day (`"2026-03-12"`) as a person reads it. A day has no zone: it is never converted. */
166
+ export function dayTextOf(value, locale) {
167
+ if (value === null || value === undefined || value === "")
168
+ return null;
169
+ return typeof value === "string"
170
+ ? dateFormat(locale, false).format(localDate(value))
171
+ : String(value);
172
+ }
173
+ /** An instant as a person reads it, on the wall clock of the zone the application installed. */
174
+ export function instantTextOf(value, locale) {
175
+ if (value === null || value === undefined || value === "")
176
+ return null;
177
+ const date = new Date(String(value));
178
+ return Number.isNaN(date.getTime()) ? String(value) : dateFormat(locale, true).format(date);
179
+ }
180
+ /** A money (`{ amount, currency }`) as a person reads it, rounded to its currency's decimals;
181
+ * `null` when the value is not one. */
182
+ export function moneyTextOf(value, locale) {
183
+ return Money.isMoney(value) ? moneyText(value, locale) : null;
184
+ }
140
185
  /**
141
186
  * The text a value reads as. `null` when there is nothing to read — the caller draws its own
142
187
  * «empty» (a dash in a fact, nothing in a cell).
@@ -159,25 +204,12 @@ export function textOf(meta, name, value, locale) {
159
204
  case "boolean":
160
205
  return value ? copy.value.yes : copy.value.no;
161
206
  case "date":
162
- return typeof value === "string"
163
- ? dateFormat(locale, false).format(localDate(value))
164
- : String(value);
165
- case "datetime": {
166
- const date = new Date(String(value));
167
- return Number.isNaN(date.getTime())
168
- ? String(value)
169
- : dateFormat(locale, true).format(date);
170
- }
207
+ return dayTextOf(value, locale);
208
+ case "datetime":
209
+ return instantTextOf(value, locale);
171
210
  case "integer":
172
- case "number": {
173
- if (Number.isNaN(Number(value)))
174
- return String(value);
175
- // A decimal that travels as text goes to `Intl` as text: every digit kept, none through a
176
- // float (`"12345678901234567.89"` reads whole).
177
- if (typeof value === "string")
178
- return numberFormat(locale, decimals(value)).format(value.trim());
179
- return numberFormat(locale, undefined).format(Number(value));
180
- }
211
+ case "number":
212
+ return numberTextOf(value, locale);
181
213
  case "text[]":
182
214
  return Array.isArray(value)
183
215
  ? value.length === 0
@@ -193,7 +225,7 @@ export function textOf(meta, name, value, locale) {
193
225
  return typeof value === "object" ? displayOf(field, value) : String(value);
194
226
  case "embedded":
195
227
  // A value object has no text of its own — but a money, the one every ERP holds, does.
196
- return Money.isMoney(value) ? moneyText(value, locale) : null;
228
+ return moneyTextOf(value, locale);
197
229
  default:
198
230
  return typeof value === "object" ? JSON.stringify(value) : String(value);
199
231
  }
@@ -1,5 +1,6 @@
1
- import { type Entity, type MetaStore, type Transport } from "@sincpro/client-framework";
1
+ import { type ChangeFeed, type Entity, type MetaStore, type Transport } from "@sincpro/client-framework";
2
2
  import type { Meta } from "@sincpro/criteria";
3
+ import { type NameBook } from "../core/names.js";
3
4
  import type { Trail } from "../core/trail.js";
4
5
  import { type ReactNode } from "react";
5
6
  /**
@@ -19,6 +20,12 @@ import { type ReactNode } from "react";
19
20
  * process's context (`processContext.set({ company_id })`, @sincpro/client-framework): every
20
21
  * request under this provider carries it in `sincpro-context` (`screensTransport`), and a change
21
22
  * reads every screen again.
23
+ *
24
+ * What was written is the process's too (`processChanges`, @sincpro/client-framework): every write
25
+ * a transport of the framework sends is announced there, whoever sent it — a screen, or a use
26
+ * case's generated client. A reading on screen follows it (`useWrites`) and reads again when a
27
+ * write touches what it shows; the names in cells (`useNameOf`) are asked through one book per
28
+ * transport and company (`nameBook`), and forget what a write touches.
22
29
  */
23
30
  type FrameworkContextValue = {
24
31
  transport: Transport;
@@ -30,8 +37,12 @@ type FrameworkContextValue = {
30
37
  trails: Map<string, Trail>;
31
38
  /** The process's context now: a reading reads again when it changes (another company). */
32
39
  context: Readonly<Record<string, unknown>>;
40
+ /** Where writes are announced: a reading reads again when one touches it (`useWrites`). */
41
+ changes: ChangeFeed;
42
+ /** The names of records by key, asked one request per entity (`useNameOf`). */
43
+ names: NameBook;
33
44
  };
34
- export declare function FrameworkProvider({ transport, locale, metas: seed, entities, timeZone, children, }: {
45
+ export declare function FrameworkProvider({ transport, locale, metas: seed, entities, timeZone, changes, children, }: {
35
46
  transport: Transport;
36
47
  /** The language labels and values are read in (`"es"`); the model's `default` without it. */
37
48
  locale?: string;
@@ -44,6 +55,8 @@ export declare function FrameworkProvider({ transport, locale, metas: seed, enti
44
55
  * the user works in: every date and time reads and is typed on its wall clock. Without it,
45
56
  * the browser's. */
46
57
  timeZone?: string;
58
+ /** Where writes are announced — the process's (`processChanges`) unless a test gives another. */
59
+ changes?: ChangeFeed;
47
60
  children: ReactNode;
48
61
  }): import("react").JSX.Element;
49
62
  export declare function useFramework(): FrameworkContextValue;
@@ -1,11 +1,12 @@
1
1
  "use client";
2
2
  import { jsx as _jsx } from "react/jsx-runtime";
3
- import { installTimeZone, metaStore, processContext, screensTransport, } from "@sincpro/client-framework";
3
+ import { installTimeZone, metaStore, processChanges, processContext, screensTransport, } from "@sincpro/client-framework";
4
+ import { nameBook } from "../core/names.js";
4
5
  import { entityFor } from "../core/relation.js";
5
6
  import { useContextValues } from "./useContextValues.js";
6
- import { createContext, useContext, useMemo, useRef, useState } from "react";
7
+ import { createContext, useContext, useEffect, useMemo, useRef, useState, } from "react";
7
8
  const FrameworkContext = createContext(null);
8
- export function FrameworkProvider({ transport, locale, metas: seed, entities = NO_ENTITIES, timeZone, children, }) {
9
+ export function FrameworkProvider({ transport, locale, metas: seed, entities = NO_ENTITIES, timeZone, changes = processChanges, children, }) {
9
10
  // Installed before the children draw, and again when it changes (another company): the
10
11
  // formatters read it, so it cannot wait for an effect.
11
12
  const zoned = useRef(null);
@@ -25,7 +26,13 @@ export function FrameworkProvider({ transport, locale, metas: seed, entities = N
25
26
  // keeps its filter, order and page, a record what was typed (`useListing`, `useRecord`). The
26
27
  // transport stays the same one: a new one would start every reading over.
27
28
  const context = useContextValues(processContext);
28
- const value = useMemo(() => ({ transport: screens, locale, metas, entities, trails, context }), [screens, locale, metas, entities, trails, context]);
29
+ // One book per transport and company: another company's names are not this one's.
30
+ const names = useMemo(() => nameBook(screens, metas, entities),
31
+ // `context` renews it: what was named in one company is asked again in the next.
32
+ // eslint-disable-next-line react-hooks/exhaustive-deps
33
+ [screens, metas, entities, context]);
34
+ useEffect(() => changes.follow((change) => names.changed(change)), [changes, names]);
35
+ const value = useMemo(() => ({ transport: screens, locale, metas, entities, trails, context, changes, names }), [screens, locale, metas, entities, trails, context, changes, names]);
29
36
  return _jsx(FrameworkContext.Provider, { value: value, children: children });
30
37
  }
31
38
  const NO_ENTITIES = [];
@@ -7,6 +7,7 @@ export { FrameworkProvider, useEntityFor, useFramework, } from "./context.js";
7
7
  export { type ListScope, ListScopeProvider, useListScope, } from "./scope.js";
8
8
  export { type AnswerOf, type CommandState, type Rehearsal, useCommand, } from "./useCommand.js";
9
9
  export { useContextValues } from "./useContextValues.js";
10
+ export { useCount } from "./useCount.js";
10
11
  export { type Groups, useGroups } from "./useGroups.js";
11
12
  export { useHistory } from "./useHistory.js";
12
13
  export { type Listing, type ListingOptions, type Paging, useListing, } from "./useListing.js";
@@ -16,4 +17,7 @@ export { type PivotAxes, type PivotReading, usePivot, } from "./usePivot.js";
16
17
  export { type Editing, type RecordReading, type RecordState, useRecord, } from "./useRecord.js";
17
18
  export { useRelated } from "./useRelated.js";
18
19
  export { type ServiceState, useService } from "./useService.js";
20
+ export { type ServiceReading, useServiceReading, } from "./useServiceReading.js";
19
21
  export { useSummaryOf } from "./useSummary.js";
22
+ export { useTextOf, type ValueTexts } from "./useTextOf.js";
23
+ export { useWrites } from "./useWrites.js";
@@ -7,6 +7,7 @@ export { FrameworkProvider, useEntityFor, useFramework, } from "./context.js";
7
7
  export { ListScopeProvider, useListScope, } from "./scope.js";
8
8
  export { useCommand, } from "./useCommand.js";
9
9
  export { useContextValues } from "./useContextValues.js";
10
+ export { useCount } from "./useCount.js";
10
11
  export { useGroups } from "./useGroups.js";
11
12
  export { useHistory } from "./useHistory.js";
12
13
  export { useListing, } from "./useListing.js";
@@ -16,4 +17,7 @@ export { usePivot, } from "./usePivot.js";
16
17
  export { useRecord, } from "./useRecord.js";
17
18
  export { useRelated } from "./useRelated.js";
18
19
  export { useService } from "./useService.js";
20
+ export { useServiceReading, } from "./useServiceReading.js";
19
21
  export { useSummaryOf } from "./useSummary.js";
22
+ export { useTextOf } from "./useTextOf.js";
23
+ export { useWrites } from "./useWrites.js";
@@ -0,0 +1,18 @@
1
+ import type { CommandBodies, Entity } from "@sincpro/client-framework";
2
+ import type { AnyRecord, Count, Criteria } from "@sincpro/criteria";
3
+ import type { ListDefinition } from "../core/list.js";
4
+ /**
5
+ * How many records of an entity a criteria holds, counted by the SERVER: «this member's
6
+ * payments (2)», the number of a smart button. `null` until it answers — and while it fails —,
7
+ * which web-ui's `SmartButton` reads as «—», never as 0.
8
+ *
9
+ * const pagos = useCount(payments, { selection: { field: "member_id", value: member.id } });
10
+ * <SmartButton icon={Wallet} label="Pagos" count={pagos?.value} onClick={…} />
11
+ *
12
+ * Context: an application counted «this member's payments» with a hook of its own over a list
13
+ * reference it built by hand. It is a listing of one row: the count is the one the server sends
14
+ * with every page (a floor, `exact: false`, when it stopped counting at its cap), and it follows
15
+ * the context and every write to the entity as any list does — a payment saved in a modal moves
16
+ * the number behind it. Never a page fetched to be counted here.
17
+ */
18
+ export declare function useCount<T extends AnyRecord, C extends CommandBodies = CommandBodies>(given: Entity<T, C> | ListDefinition<T>, criteria?: Criteria<T>): Count | null;
@@ -0,0 +1,23 @@
1
+ "use client";
2
+ import { useListing } from "./useListing.js";
3
+ /**
4
+ * How many records of an entity a criteria holds, counted by the SERVER: «this member's
5
+ * payments (2)», the number of a smart button. `null` until it answers — and while it fails —,
6
+ * which web-ui's `SmartButton` reads as «—», never as 0.
7
+ *
8
+ * const pagos = useCount(payments, { selection: { field: "member_id", value: member.id } });
9
+ * <SmartButton icon={Wallet} label="Pagos" count={pagos?.value} onClick={…} />
10
+ *
11
+ * Context: an application counted «this member's payments» with a hook of its own over a list
12
+ * reference it built by hand. It is a listing of one row: the count is the one the server sends
13
+ * with every page (a floor, `exact: false`, when it stopped counting at its cap), and it follows
14
+ * the context and every write to the entity as any list does — a payment saved in a modal moves
15
+ * the number behind it. Never a page fetched to be counted here.
16
+ */
17
+ export function useCount(given, criteria) {
18
+ const listing = useListing(given, {
19
+ pageSize: 1,
20
+ ...(criteria === undefined ? {} : { criteria }),
21
+ });
22
+ return listing.status === "failed" ? null : listing.count;
23
+ }
@@ -2,6 +2,7 @@
2
2
  import { resumingFrom } from "@sincpro/criteria";
3
3
  import { measuresOf, sumsOf } from "../core/measure.js";
4
4
  import { useFramework } from "./context.js";
5
+ import { useWrites } from "./useWrites.js";
5
6
  import { useCallback, useEffect, useRef, useState } from "react";
6
7
  /** The most groups a level answers: a split into ten thousand groups is a list, not a split. */
7
8
  const MAX_GROUPS = 100;
@@ -21,6 +22,8 @@ export function useGroups(listing, options = {}) {
21
22
  const { pagination: _page, ...asked } = listing.question;
22
23
  const question = JSON.stringify(asked);
23
24
  const route = listing.route;
25
+ // Written by anyone — a save, a use case, a panel beside: the groups are counted again.
26
+ const writes = useWrites(route);
24
27
  const sums = (options.sums ?? sumsOf(listing.meta)).join(",");
25
28
  // What adds up by default is read from the model: without it yet, asking now and again when
26
29
  // it arrives would be two questions — and the groups on screen replaced under the pointer.
@@ -54,7 +57,7 @@ export function useGroups(listing, options = {}) {
54
57
  setState({ status: "failed", buckets: [], error });
55
58
  });
56
59
  return () => aborter.abort();
57
- }, [transport, route, question, levels, sums, waiting, context]);
60
+ }, [transport, route, question, levels, sums, waiting, context, writes]);
58
61
  /** One page of a group: the first (`after` null) or the one the cursor points at. */
59
62
  const read = useCallback((bucket, after) => {
60
63
  const key = keyOf(bucket);
@@ -2,6 +2,7 @@
2
2
  import { listRoute } from "@sincpro/client-framework";
3
3
  import { historyOf } from "../core/history.js";
4
4
  import { useFramework } from "./context.js";
5
+ import { useWrites } from "./useWrites.js";
5
6
  import { useEffect, useState } from "react";
6
7
  /**
7
8
  * A record's business events, newest first — what `Record.Activity` draws.
@@ -21,6 +22,8 @@ export function useHistory(entity, id, deps = []) {
21
22
  const { transport, locale, context } = useFramework();
22
23
  const [entries, setEntries] = useState(undefined);
23
24
  const history = entity.history;
25
+ // A write under the record — anyone's — adds a fact to its history: read it again.
26
+ const writes = useWrites(history === undefined || id === "" ? undefined : listRoute(history, { id }));
24
27
  useEffect(() => {
25
28
  if (history === undefined || id === "") {
26
29
  setEntries(undefined);
@@ -34,6 +37,6 @@ export function useHistory(entity, id, deps = []) {
34
37
  transport.list(listRoute(history, { id }), criteria, aborter.signal).then((page) => aborter.signal.aborted || setEntries(historyOf(page.rows, page.meta, locale)), () => aborter.signal.aborted || setEntries(undefined));
35
38
  return () => aborter.abort();
36
39
  // eslint-disable-next-line react-hooks/exhaustive-deps -- the caller names what reloads it
37
- }, [transport, history, id, locale, context, ...deps]);
40
+ }, [transport, history, id, locale, context, writes, ...deps]);
38
41
  return entries;
39
42
  }
@@ -5,6 +5,7 @@ import { useReading } from "@sincpro/criteria-react";
5
5
  import { isList, scopeOf } from "../core/list.js";
6
6
  import { useFramework } from "./context.js";
7
7
  import { useListScope } from "./scope.js";
8
+ import { useWrites } from "./useWrites.js";
8
9
  import { useCallback, useEffect, useMemo, useRef, useState } from "react";
9
10
  /** How long typing has to pause before the search goes out. */
10
11
  const TYPING_PAUSE = 200;
@@ -56,15 +57,17 @@ export function useListing(given, own = {}) {
56
57
  });
57
58
  const typing = useRef(null);
58
59
  useEffect(() => () => void (typing.current && clearTimeout(typing.current)), []);
59
- // Another company (the process's context changed): the same question read again — its filter,
60
- // order and page kept, never a reading started over.
61
- const askedIn = useRef(context);
60
+ // Another company (the process's context changed), or a write to what it lists — anyone's: a
61
+ // save, a use case, a panel beside —: the same question read again, its filter, order and page
62
+ // kept, never a reading started over.
63
+ const writes = useWrites(route);
64
+ const askedFor = useRef({ context, writes });
62
65
  useEffect(() => {
63
- if (askedIn.current === context)
66
+ if (askedFor.current.context === context && askedFor.current.writes === writes)
64
67
  return;
65
- askedIn.current = context;
68
+ askedFor.current = { context, writes };
66
69
  void reading.reload();
67
- }, [context, reading]);
70
+ }, [context, writes, reading]);
68
71
  const narrowed = (next) => {
69
72
  let criteria = base;
70
73
  const found = searchOver(meta, next.text);