@sincpro/web-framework 0.1.0 → 0.3.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 (97) hide show
  1. package/README.md +298 -40
  2. package/dist/core/cache.js +2 -2
  3. package/dist/core/copy.d.ts +26 -0
  4. package/dist/core/copy.js +32 -0
  5. package/dist/core/edit.d.ts +48 -3
  6. package/dist/core/edit.js +79 -5
  7. package/dist/core/entity.d.ts +46 -0
  8. package/dist/core/entity.js +57 -0
  9. package/dist/core/form.d.ts +21 -0
  10. package/dist/core/form.js +30 -0
  11. package/dist/core/index.d.ts +11 -3
  12. package/dist/core/index.js +10 -2
  13. package/dist/core/lines.d.ts +53 -0
  14. package/dist/core/lines.js +120 -0
  15. package/dist/core/list.d.ts +64 -0
  16. package/dist/core/list.js +22 -0
  17. package/dist/core/lookup.d.ts +7 -2
  18. package/dist/core/lookup.js +8 -2
  19. package/dist/core/measure.d.ts +16 -0
  20. package/dist/core/measure.js +38 -0
  21. package/dist/core/memory.js +18 -4
  22. package/dist/core/relation.d.ts +28 -0
  23. package/dist/core/relation.js +30 -0
  24. package/dist/core/rest.d.ts +5 -9
  25. package/dist/core/rest.js +25 -7
  26. package/dist/core/retry.d.ts +24 -0
  27. package/dist/core/retry.js +25 -0
  28. package/dist/core/summary.d.ts +50 -0
  29. package/dist/core/summary.js +54 -0
  30. package/dist/core/trail.d.ts +44 -0
  31. package/dist/core/trail.js +70 -0
  32. package/dist/core/transport.d.ts +22 -2
  33. package/dist/react/context.d.ts +20 -1
  34. package/dist/react/context.js +10 -2
  35. package/dist/react/index.d.ts +5 -2
  36. package/dist/react/index.js +4 -1
  37. package/dist/react/useCommand.d.ts +3 -1
  38. package/dist/react/useCommand.js +12 -4
  39. package/dist/react/useGroups.d.ts +52 -0
  40. package/dist/react/useGroups.js +110 -0
  41. package/dist/react/useListing.d.ts +29 -7
  42. package/dist/react/useListing.js +49 -8
  43. package/dist/react/useLookup.d.ts +30 -5
  44. package/dist/react/useLookup.js +56 -3
  45. package/dist/react/usePivot.d.ts +33 -0
  46. package/dist/react/usePivot.js +59 -0
  47. package/dist/react/useRecord.d.ts +28 -9
  48. package/dist/react/useRecord.js +156 -18
  49. package/dist/react/useSummary.d.ts +15 -0
  50. package/dist/react/useSummary.js +22 -0
  51. package/dist/web/index.d.ts +15 -3
  52. package/dist/web/index.js +14 -2
  53. package/dist/web/list/EntityList.d.ts +69 -7
  54. package/dist/web/list/EntityList.js +281 -23
  55. package/dist/web/list/EntitySearchBar.d.ts +16 -8
  56. package/dist/web/list/EntitySearchBar.js +52 -4
  57. package/dist/web/list/columns.d.ts +0 -13
  58. package/dist/web/list/columns.js +20 -1
  59. package/dist/web/list/groups.d.ts +17 -0
  60. package/dist/web/list/groups.js +80 -0
  61. package/dist/web/list/openIn.d.ts +41 -0
  62. package/dist/web/list/openIn.js +55 -0
  63. package/dist/web/list/pivot.d.ts +23 -0
  64. package/dist/web/list/pivot.js +64 -0
  65. package/dist/web/list/presentation.d.ts +51 -0
  66. package/dist/web/list/presentation.js +20 -0
  67. package/dist/web/list/rowCommands.d.ts +41 -0
  68. package/dist/web/list/rowCommands.js +56 -0
  69. package/dist/web/record/EntityPicker.d.ts +45 -14
  70. package/dist/web/record/EntityPicker.js +64 -20
  71. package/dist/web/record/EntityTags.d.ts +38 -0
  72. package/dist/web/record/EntityTags.js +52 -0
  73. package/dist/web/record/RelatedList.d.ts +8 -1
  74. package/dist/web/record/RelatedList.js +37 -3
  75. package/dist/web/record/control.d.ts +42 -0
  76. package/dist/web/record/control.js +60 -0
  77. package/dist/web/record/edit.d.ts +27 -12
  78. package/dist/web/record/edit.js +92 -25
  79. package/dist/web/record/lines.d.ts +84 -0
  80. package/dist/web/record/lines.js +190 -0
  81. package/dist/web/record/opening.d.ts +34 -0
  82. package/dist/web/record/opening.js +10 -0
  83. package/dist/web/record/summary.d.ts +22 -0
  84. package/dist/web/record/summary.js +63 -0
  85. package/dist/web/workspace/SincproApp.d.ts +4 -1
  86. package/dist/web/workspace/SincproApp.js +2 -2
  87. package/dist/web/workspace/composer.d.ts +27 -4
  88. package/dist/web/workspace/composer.js +75 -7
  89. package/dist/web/workspace/open.d.ts +49 -0
  90. package/dist/web/workspace/open.js +51 -0
  91. package/dist/web/workspace/record.d.ts +30 -0
  92. package/dist/web/workspace/record.js +83 -0
  93. package/dist/web/workspace/skin.d.ts +0 -9
  94. package/dist/web/workspace/skin.js +25 -13
  95. package/dist/web/workspace/workspace.d.ts +33 -3
  96. package/dist/web/workspace/workspace.js +63 -4
  97. package/package.json +9 -9
@@ -0,0 +1,33 @@
1
+ import type { AnyRecord, Bucket } from "@sincpro/criteria";
2
+ import type { GroupLevel } from "../core/list.js";
3
+ import type { Listing } from "./useListing.js";
4
+ /**
5
+ * A list's set crossed by two fields — state by month, customer by payment — counted and added
6
+ * up by the SERVER over every row that matches, never the page.
7
+ *
8
+ * Context: web-ui draws a pivot two ways: `spec` folds the rows IN HAND, `folded` draws one
9
+ * already folded. Folding the page would answer «12 paid» for a set of 187 — the rule of
10
+ * grouping (`AGENTS.md`). So the listing's question (scope, search, filter) goes to the server
11
+ * twice: grouped by both axes (each crossing, and each row's total in the outer group), and by
12
+ * the columns alone (each column's total). No cell is added up here.
13
+ *
14
+ * const pivot = usePivot(listing, { rows: { field: "state" }, columns: { field: "payment" } });
15
+ * pivot.crossed // [{ value: "paid", count: 187, measures, groups: [{ value: "cash", … }] }]
16
+ * pivot.columns // [{ value: "cash", count: 340, measures }, …]
17
+ */
18
+ export type PivotAxes = {
19
+ rows: GroupLevel;
20
+ columns: GroupLevel;
21
+ /** The fields added up in each crossing. Without it, the figures the model computes. */
22
+ measures?: readonly string[];
23
+ };
24
+ export type PivotReading<T> = {
25
+ status: "idle" | "loading" | "ready" | "failed";
26
+ /** Grouped by rows, then columns: a crossing is a row's group's group. */
27
+ crossed: readonly Bucket<T>[];
28
+ /** Grouped by the columns alone: each column's total. */
29
+ columns: readonly Bucket<T>[];
30
+ measures: readonly string[];
31
+ error: unknown;
32
+ };
33
+ export declare function usePivot<T extends AnyRecord>(listing: Listing<T>, axes: PivotAxes | null): PivotReading<T>;
@@ -0,0 +1,59 @@
1
+ "use client";
2
+ import { measuresOf, sumsOf } from "../core/measure.js";
3
+ import { useFramework } from "./context.js";
4
+ import { useEffect, useState } from "react";
5
+ /** The most groups an axis answers: a pivot of ten thousand rows is a list. */
6
+ const MAX_KEYS = 100;
7
+ export function usePivot(listing, axes) {
8
+ const { transport } = useFramework();
9
+ const [state, setState] = useState({
10
+ status: "idle",
11
+ crossed: [],
12
+ columns: [],
13
+ error: null,
14
+ });
15
+ const { pagination: _page, ...asked } = listing.question;
16
+ const question = JSON.stringify(asked);
17
+ const measures = axes?.measures ?? sumsOf(listing.meta);
18
+ const sums = measures.join(",");
19
+ const shape = axes === null ? "" : JSON.stringify([axes.rows, axes.columns]);
20
+ const waiting = axes !== null && axes.measures === undefined && listing.meta === null;
21
+ useEffect(() => {
22
+ if (shape === "") {
23
+ setState({ status: "idle", crossed: [], columns: [], error: null });
24
+ return;
25
+ }
26
+ const aborter = new AbortController();
27
+ setState((now) => ({ ...now, status: "loading", error: null }));
28
+ if (waiting)
29
+ return () => aborter.abort();
30
+ const [rows, columns] = JSON.parse(shape);
31
+ const grouped = (levels) => ({
32
+ ...JSON.parse(question),
33
+ meta: false,
34
+ grouping: {
35
+ group_by: levels.map((level) => ({ ...level })),
36
+ ...(sums === "" ? {} : { measures: measuresOf(sums.split(",")) }),
37
+ pagination: { limit: MAX_KEYS },
38
+ },
39
+ pagination: { limit: 1 },
40
+ });
41
+ Promise.all([
42
+ transport.list(listing.route, grouped([rows, columns]), aborter.signal),
43
+ transport.list(listing.route, grouped([columns]), aborter.signal),
44
+ ]).then(([crossed, byColumn]) => {
45
+ if (!aborter.signal.aborted)
46
+ setState({
47
+ status: "ready",
48
+ crossed: crossed.rows,
49
+ columns: byColumn.rows,
50
+ error: null,
51
+ });
52
+ }, (error) => {
53
+ if (!aborter.signal.aborted)
54
+ setState({ status: "failed", crossed: [], columns: [], error });
55
+ });
56
+ return () => aborter.abort();
57
+ }, [transport, listing.route, question, shape, sums, waiting]);
58
+ return { ...state, measures };
59
+ }
@@ -1,6 +1,6 @@
1
1
  import type { AnyRecord, Meta } from "@sincpro/criteria";
2
- import { type FieldState } from "../core/edit.js";
3
- import { type Entity } from "../core/entity.js";
2
+ import { type Advice, type FieldState } from "../core/edit.js";
3
+ import { type Entity, type EntityRef } from "../core/entity.js";
4
4
  import { type Draft } from "../core/transport.js";
5
5
  /**
6
6
  * ONE record of an entity, with the definition of its model — read, and edited.
@@ -18,6 +18,17 @@ import { type Draft } from "../core/transport.js";
18
18
  * in between, the server answers 409 and the screen says so instead of overwriting.
19
19
  * - **drafts** — an edit kept between requests (sincpro_framework's `Drafts`) comes back with
20
20
  * the record and is shown as restored; `keep()` stores the current one.
21
+ * - **creating** — with no id it is a record that does not exist yet: it starts from the
22
+ * defaults the model publishes, previews like any other, and `save()` sends it to the
23
+ * entity's `create` route with an `Idempotency-Key` (a retry never creates two). When the
24
+ * answer carries the new identity, the hook becomes that record; otherwise `edit.created`
25
+ * holds the answer and the screen decides where to go. Given the REFERENCE it was opened
26
+ * with, a new record starts with what the reference carries over the defaults
27
+ * (`newRef(values)`): an invoice created from its customer starts with that customer.
28
+ *
29
+ * const invoice = useRecord(invoices, ""); // a new one
30
+ * const invoice = useRecord(invoices, "F-2026-7"); // an existing one
31
+ * const invoice = useRecord(invoices, resource); // what the workspace opened
21
32
  */
22
33
  export type RecordState<T> = {
23
34
  status: "loading";
@@ -35,10 +46,6 @@ export type RecordState<T> = {
35
46
  meta: Meta | null;
36
47
  error: unknown;
37
48
  };
38
- export type Advice = {
39
- field?: string;
40
- text: string;
41
- };
42
49
  export type Editing = {
43
50
  /** The record as on screen: what was read, what the server computed, what was typed. */
44
51
  values: AnyRecord;
@@ -46,8 +53,13 @@ export type Editing = {
46
53
  dirty: boolean;
47
54
  set: (name: string, value: unknown) => void;
48
55
  state: (name: string) => FieldState;
49
- /** The server's word on fields that did not validate, by field. */
56
+ /** A cell of a list of records — line `index` of `name`, its `field`: the line model's hints
57
+ * over that line, refined by what the server's preview said of it (`items[index]`). */
58
+ cell: (name: string, index: number, field: string) => FieldState;
59
+ /** The server's word on what did not validate, by where it points (`pathOf`): `discount`,
60
+ * `lines/1/qty`. Read a field's — its lines' included — with `errorAt`. */
50
61
  errors: Record<string, string>;
62
+ /** What the server's preview warned about, each with where it points; `adviceAt` places it. */
51
63
  advice: Advice[];
52
64
  /** Required, visible, editable and empty: what blocks a save. */
53
65
  missing: string[];
@@ -62,13 +74,20 @@ export type Editing = {
62
74
  draft: Draft | null;
63
75
  save: () => Promise<boolean>;
64
76
  discard: () => void;
65
- /** Store the current edit as a draft — only when the entity declares where. */
77
+ /** Store the current edit as a draft — only when the entity declares where, and the record
78
+ * exists: a draft is kept against a record. */
66
79
  keep?: () => Promise<void>;
80
+ /** What the last create answered, when it did not say the new record's identity. */
81
+ created: unknown;
67
82
  };
68
83
  export type RecordReading<T> = RecordState<T> & {
69
84
  entity: Entity<T & AnyRecord>;
85
+ /** The record's key — `""` while it is new, its identity once created. */
86
+ id: string;
87
+ /** It does not exist on the server yet. */
88
+ isNew: boolean;
70
89
  locale: string | undefined;
71
90
  reload: () => void;
72
91
  edit: Editing;
73
92
  };
74
- export declare function useRecord<T extends AnyRecord>(entity: Entity<T>, id: string): RecordReading<T>;
93
+ export declare function useRecord<T extends AnyRecord>(entity: Entity<T>, opened: string | EntityRef): RecordReading<T>;
@@ -1,19 +1,35 @@
1
1
  "use client";
2
- import { changesOf, errorsByField, fieldStateOf, missingOf, } from "../core/edit.js";
3
- import { pathFor } from "../core/entity.js";
2
+ import { changesOf, creationOf, defaultsOf, errorsByField, fieldStateOf, lineStatesOf, missingOf, } from "../core/edit.js";
3
+ import { pathFor, startingValuesOf, WITH_PARAM, } from "../core/entity.js";
4
+ import { linesAnswered } from "../core/lines.js";
5
+ import { retryKeys } from "../core/retry.js";
4
6
  import { TransportError } from "../core/transport.js";
5
7
  import { useFramework } from "./context.js";
6
8
  import { useCallback, useEffect, useMemo, useRef, useState } from "react";
7
9
  /** How long typing has to pause before the server is asked what the record becomes. */
8
10
  const PREVIEW_PAUSE = 300;
9
- export function useRecord(entity, id) {
11
+ export function useRecord(entity, opened) {
10
12
  const { transport, locale, metas } = useFramework();
13
+ const given = typeof opened === "string" ? opened : (opened.id ?? "");
14
+ // As written in the reference: a text compares by value, so a new render with the same
15
+ // reference is not a new start.
16
+ const start = typeof opened === "string" ? undefined : opened.params?.[WITH_PARAM];
17
+ // A create that answered its identity turns this into that record — until the screen asks
18
+ // for another one.
19
+ const [born, setBorn] = useState(null);
20
+ const id = born !== null && born.from === given ? born.id : given;
21
+ const isNew = id === "";
22
+ const [created, setCreated] = useState(null);
23
+ const creating = useRef(retryKeys());
11
24
  const [state, setState] = useState({
12
25
  status: "loading",
13
26
  record: null,
14
27
  meta: null,
15
28
  error: null,
16
29
  });
30
+ // Which record `state` was read for: the render after the id changed — a create that answered,
31
+ // another record asked — must not show the previous one under the new id.
32
+ const [readFor, setReadFor] = useState(null);
17
33
  const [round, setRound] = useState(0);
18
34
  const [typed, setTyped] = useState({});
19
35
  const [computed, setComputed] = useState({});
@@ -45,15 +61,54 @@ export function useRecord(entity, id) {
45
61
  };
46
62
  useEffect(() => {
47
63
  const aborter = new AbortController();
48
- setState((now) => ({ status: "loading", record: null, meta: now.meta, error: null }));
64
+ // Read again — after a save, a reload — the record on screen STAYS until the answer: a form
65
+ // that blinked through a skeleton lost its focus, its scroll and the click that landed then.
66
+ // Only a record not shown yet (another id, the first read) starts as «loading».
67
+ const again = !isNew && readFor === id;
68
+ if (!again)
69
+ setState((now) => ({ status: "loading", record: null, meta: now.meta, error: null }));
70
+ if (isNew) {
71
+ // Nothing to read but the model: known already, or asked of the list with no rows.
72
+ const known = metas.get(entity.key);
73
+ const asked = known
74
+ ? Promise.resolve(known)
75
+ : transport
76
+ .list(entity.list, { pagination: { limit: 1 } }, aborter.signal)
77
+ .then((page) => page.meta);
78
+ asked.then((meta) => {
79
+ if (meta)
80
+ metas.set(entity.key, meta);
81
+ if (aborter.signal.aborted)
82
+ return;
83
+ forget();
84
+ setDraft(null);
85
+ setReadFor(id);
86
+ const starting = start === undefined ? {} : startingValuesOf({ params: { [WITH_PARAM]: start } });
87
+ setState({
88
+ status: "ready",
89
+ record: { ...defaultsOf(meta), ...starting },
90
+ meta: meta ?? null,
91
+ error: null,
92
+ });
93
+ }, (error) => {
94
+ if (aborter.signal.aborted)
95
+ return;
96
+ setReadFor(id);
97
+ setState((now) => ({ status: "failed", record: null, meta: now.meta, error }));
98
+ });
99
+ return () => aborter.abort();
100
+ }
49
101
  transport.one(entity.one(id), aborter.signal).then((answer) => {
50
102
  if (answer.meta)
51
103
  metas.set(entity.key, answer.meta);
52
104
  if (aborter.signal.aborted)
53
105
  return;
54
106
  forget();
107
+ setReadFor(id);
55
108
  setDraft(answer.draft);
56
- if (answer.draft)
109
+ // A server that answers the record FROM the draft (sincpro_framework) sends no values
110
+ // apart: the record is the draft. One that sends them apart, they are what was typed.
111
+ if (answer.draft?.values)
57
112
  setTyped(answer.draft.values);
58
113
  setState({
59
114
  status: "ready",
@@ -62,14 +117,16 @@ export function useRecord(entity, id) {
62
117
  error: null,
63
118
  });
64
119
  }, (error) => {
65
- if (!aborter.signal.aborted)
66
- setState((now) => ({ status: "failed", record: null, meta: now.meta, error }));
120
+ if (aborter.signal.aborted)
121
+ return;
122
+ setReadFor(id);
123
+ setState((now) => ({ status: "failed", record: null, meta: now.meta, error }));
67
124
  });
68
125
  return () => aborter.abort();
69
126
  // `forget` only resets this hook's own state; listing it would reload the record on every
70
127
  // render. What reloads it is the transport, the entity, the id or an explicit `reload()`.
71
128
  // eslint-disable-next-line react-hooks/exhaustive-deps
72
- }, [transport, metas, entity, id, round]);
129
+ }, [transport, metas, entity, id, isNew, start, round]);
73
130
  useEffect(() => {
74
131
  mounted.current = true;
75
132
  return () => {
@@ -91,13 +148,30 @@ export function useRecord(entity, id) {
91
148
  const aborter = new AbortController();
92
149
  asking.current = aborter;
93
150
  transport
94
- .send(preview.method, pathFor(preview, id), { id, values: changesOf(record, next), changed: [name] }, aborter.signal)
151
+ .send(preview.method, pathFor(preview, isNew ? undefined : id), {
152
+ id: isNew ? null : id,
153
+ values: isNew ? { ...record, ...next } : changesOf(record, next),
154
+ changed: [name],
155
+ }, aborter.signal)
95
156
  .then((answer) => {
96
157
  if (aborter.signal.aborted || !mounted.current)
97
158
  return;
98
- setComputed(answer.values ?? {});
99
- setFromServer(answer.fields ?? {});
100
- setAdvice(answer.advice ?? []);
159
+ // A list the form sent (the lines) comes back computed — subtotals — as a whole:
160
+ // it takes the place of what was typed, each line keeping its key by position (a
161
+ // new line has no id to be found by). Typing again cancels an answer in flight, so
162
+ // `next` is still what is on screen.
163
+ const fresh = answer.values ?? {};
164
+ const lists = Object.keys(fresh).filter((key) => Array.isArray(fresh[key]) && Array.isArray(next[key]));
165
+ if (lists.length > 0)
166
+ setTyped((now) => {
167
+ const out = { ...now };
168
+ for (const key of lists)
169
+ out[key] = linesAnswered(next[key], fresh[key]);
170
+ return out;
171
+ });
172
+ setComputed(Object.fromEntries(Object.entries(fresh).filter(([key]) => !lists.includes(key))));
173
+ setFromServer(answer.states ?? {});
174
+ setAdvice(answer.warnings ?? []);
101
175
  }, () => undefined);
102
176
  }, PREVIEW_PAUSE);
103
177
  };
@@ -117,15 +191,24 @@ export function useRecord(entity, id) {
117
191
  ask(next, name);
118
192
  },
119
193
  state: (name) => fieldStateOf(state.meta, name, values, fromServer),
194
+ cell: (name, index, field) => {
195
+ const line = (Array.isArray(values[name]) ? values[name] : [])[index];
196
+ const definition = state.meta?.fields[name]?.definition;
197
+ return fieldStateOf(definition, field, line ?? {}, lineStatesOf(fromServer, name)[index]);
198
+ },
120
199
  errors: failure instanceof TransportError ? errorsByField(failure.problem?.errors) : {},
121
200
  advice,
122
201
  missing: missingOf(state.meta, values, fromServer),
123
202
  saving,
124
203
  failure,
125
204
  conflict,
126
- restored: draft !== null && Object.keys(changed).length > 0,
205
+ // Restored: the draft's values were put back over the record (they came apart), or the
206
+ // record itself was answered from the draft (sincpro_framework: no values apart).
207
+ restored: draft !== null && (draft.values === undefined || Object.keys(changed).length > 0),
127
208
  draft,
128
209
  save: async () => {
210
+ if (isNew)
211
+ return create();
129
212
  const route = entity.save;
130
213
  if (route === undefined)
131
214
  throw new Error(`${entity.key} declares no save route`);
@@ -154,17 +237,32 @@ export function useRecord(entity, id) {
154
237
  }
155
238
  },
156
239
  discard: forget,
157
- ...(entity.draft === undefined
240
+ created,
241
+ ...(entity.draft === undefined || isNew
158
242
  ? {}
159
243
  : {
160
244
  keep: async () => {
161
245
  const route = entity.draft;
246
+ // sincpro_framework's `Edit`: the record as the form holds it, whole; the fields
247
+ // changed; the draft version read (`0`: none) — another tab that kept first wins —,
248
+ // and the stored version the edit started from.
162
249
  try {
163
250
  const kept = await transport.send(route.method, pathFor(route, id), {
164
- values: changed,
165
- expected: draft?.version ?? 0,
251
+ id,
252
+ values,
253
+ changed: Object.keys(changed),
254
+ version: draft?.version ?? 0,
255
+ ...(typeof version === "number" ? { origin_version: version } : {}),
166
256
  });
167
- setDraft(kept);
257
+ const now = kept.draft;
258
+ if (now)
259
+ setDraft({
260
+ values: changed,
261
+ version: now.version,
262
+ ...(now.origin_version === undefined
263
+ ? {}
264
+ : { origin_version: now.origin_version }),
265
+ });
168
266
  }
169
267
  catch (error) {
170
268
  refused(error);
@@ -172,5 +270,45 @@ export function useRecord(entity, id) {
172
270
  },
173
271
  }),
174
272
  };
175
- return { ...state, entity, locale, reload, edit };
273
+ async function create() {
274
+ const route = entity.create;
275
+ if (route === undefined)
276
+ throw new Error(`${entity.key} declares no create route`);
277
+ cancelPreview();
278
+ setSaving(true);
279
+ setFailure(null);
280
+ setCreated(null);
281
+ const body = creationOf(state.meta, values);
282
+ const idempotencyKey = creating.current.keyFor(body);
283
+ try {
284
+ const answer = await transport.send(route.method, pathFor(route), body, undefined, { idempotencyKey });
285
+ creating.current.settle(null);
286
+ if (!mounted.current)
287
+ return true;
288
+ const key = state.meta?.get_id || state.meta?.identity || "id";
289
+ const made = typeof answer === "object" && answer !== null
290
+ ? answer[key]
291
+ : undefined;
292
+ forget();
293
+ if (typeof made === "string" || typeof made === "number")
294
+ setBorn({ from: given, id: String(made) });
295
+ else
296
+ setCreated(answer);
297
+ return true;
298
+ }
299
+ catch (error) {
300
+ creating.current.settle(error);
301
+ if (mounted.current)
302
+ refused(error);
303
+ return false;
304
+ }
305
+ finally {
306
+ if (mounted.current)
307
+ setSaving(false);
308
+ }
309
+ }
310
+ const shown = readFor === id
311
+ ? state
312
+ : { status: "loading", record: null, meta: state.meta, error: null };
313
+ return { ...shown, entity, id, isNew, locale, reload, edit };
176
314
  }
@@ -0,0 +1,15 @@
1
+ import type { AnyRecord, Meta } from "@sincpro/criteria";
2
+ import type { Entity } from "../core/entity.js";
3
+ import { type RecordSummary } from "../core/summary.js";
4
+ /**
5
+ * The summary of any record of an entity, read with the model this application already knows
6
+ * for it (a list or a record of it was read, or `metas` preloaded it) and in its language.
7
+ *
8
+ * Context: a function, not a summary, so a list of N rows asks once and summarizes each row.
9
+ * The model is looked up on every call: it may arrive after the first render (the first page
10
+ * of a list brings it), and the next render of whoever holds the rows reads it.
11
+ *
12
+ * const summarize = useSummaryOf(contactos);
13
+ * <RecordCard {...cardOf(summarize(row))} />
14
+ */
15
+ export declare function useSummaryOf<T extends AnyRecord>(entity: Entity<T>): (record: AnyRecord, meta?: Meta | null) => RecordSummary;
@@ -0,0 +1,22 @@
1
+ "use client";
2
+ import { summaryOf } from "../core/summary.js";
3
+ import { useFramework } from "./context.js";
4
+ import { useCallback } from "react";
5
+ /**
6
+ * The summary of any record of an entity, read with the model this application already knows
7
+ * for it (a list or a record of it was read, or `metas` preloaded it) and in its language.
8
+ *
9
+ * Context: a function, not a summary, so a list of N rows asks once and summarizes each row.
10
+ * The model is looked up on every call: it may arrive after the first render (the first page
11
+ * of a list brings it), and the next render of whoever holds the rows reads it.
12
+ *
13
+ * const summarize = useSummaryOf(contactos);
14
+ * <RecordCard {...cardOf(summarize(row))} />
15
+ */
16
+ export function useSummaryOf(entity) {
17
+ const { metas, locale } = useFramework();
18
+ return useCallback((record, meta) => summaryOf(meta ?? metas.get(entity.key) ?? null, record, {
19
+ fields: entity.summary,
20
+ ...(locale === undefined ? {} : { locale }),
21
+ }), [metas, entity, locale]);
22
+ }
@@ -2,16 +2,28 @@
2
2
  * The web bindings: what draws a `Listing` and a record with `@sincpro/web-ui`.
3
3
  */
4
4
  export { type ColumnOptions, columnsOf } from "./list/columns.js";
5
- export { EntityList, type EntityListProps, type NamedFilter, } from "./list/EntityList.js";
5
+ export { type EntityFace, EntityList, type EntityListProps, type View, } from "./list/EntityList.js";
6
6
  export { EntitySearchBar } from "./list/EntitySearchBar.js";
7
+ export { groupableOf, pilesOf } from "./list/groups.js";
8
+ export { OpenInMenu } from "./list/openIn.js";
9
+ export { pivotOf } from "./list/pivot.js";
10
+ export { type CardLook, type OptionLook, type Presentation, presentationOf, type Presented, presented, type RowLook, } from "./list/presentation.js";
11
+ export { type RowCommand, type RowCommands, useRowCommands, } from "./list/rowCommands.js";
7
12
  export { facetsOf, modelOf } from "./list/search.js";
8
13
  export { CommandButton } from "./record/CommandButton.js";
14
+ export { RelationPicker } from "./record/control.js";
9
15
  export { dirtyOf, type FieldControl, RecordField, } from "./record/edit.js";
10
16
  export { EntityPicker } from "./record/EntityPicker.js";
17
+ export { EntityTags } from "./record/EntityTags.js";
18
+ export { type LineColumn, type LineControl, type LinesLook, RecordLinesEditor, } from "./record/lines.js";
19
+ export { type RecordOpening, RecordOpeningProvider, useRecordOpening, } from "./record/opening.js";
11
20
  export { countOf, factsOf, RecordLines } from "./record/record.js";
12
21
  export { RelatedList } from "./record/RelatedList.js";
22
+ export { cardOf, relationOf } from "./record/summary.js";
13
23
  export { Refused } from "./Refused.js";
14
- export { entityView, type EntityViewOptions, } from "./workspace/composer.js";
24
+ export { entityView, type EntityViewOptions, listView, type ListViewOptions, } from "./workspace/composer.js";
25
+ export { openIn, openOptions, RECORD_SIZE, RelatedRecord, targetsFor, } from "./workspace/open.js";
26
+ export { EntityRecord } from "./workspace/record.js";
15
27
  export { SincproApp } from "./workspace/SincproApp.js";
16
28
  export { webUiSkin } from "./workspace/skin.js";
17
- export { useRecordInWorkspace } from "./workspace/workspace.js";
29
+ export { chooserIn, useFollowed, useRecordInWorkspace, } from "./workspace/workspace.js";
package/dist/web/index.js CHANGED
@@ -4,14 +4,26 @@
4
4
  export { columnsOf } from "./list/columns.js";
5
5
  export { EntityList, } from "./list/EntityList.js";
6
6
  export { EntitySearchBar } from "./list/EntitySearchBar.js";
7
+ export { groupableOf, pilesOf } from "./list/groups.js";
8
+ export { OpenInMenu } from "./list/openIn.js";
9
+ export { pivotOf } from "./list/pivot.js";
10
+ export { presentationOf, presented, } from "./list/presentation.js";
11
+ export { useRowCommands, } from "./list/rowCommands.js";
7
12
  export { facetsOf, modelOf } from "./list/search.js";
8
13
  export { CommandButton } from "./record/CommandButton.js";
14
+ export { RelationPicker } from "./record/control.js";
9
15
  export { dirtyOf, RecordField, } from "./record/edit.js";
10
16
  export { EntityPicker } from "./record/EntityPicker.js";
17
+ export { EntityTags } from "./record/EntityTags.js";
18
+ export { RecordLinesEditor, } from "./record/lines.js";
19
+ export { RecordOpeningProvider, useRecordOpening, } from "./record/opening.js";
11
20
  export { countOf, factsOf, RecordLines } from "./record/record.js";
12
21
  export { RelatedList } from "./record/RelatedList.js";
22
+ export { cardOf, relationOf } from "./record/summary.js";
13
23
  export { Refused } from "./Refused.js";
14
- export { entityView, } from "./workspace/composer.js";
24
+ export { entityView, listView, } from "./workspace/composer.js";
25
+ export { openIn, openOptions, RECORD_SIZE, RelatedRecord, targetsFor, } from "./workspace/open.js";
26
+ export { EntityRecord } from "./workspace/record.js";
15
27
  export { SincproApp } from "./workspace/SincproApp.js";
16
28
  export { webUiSkin } from "./workspace/skin.js";
17
- export { useRecordInWorkspace } from "./workspace/workspace.js";
29
+ export { chooserIn, useFollowed, useRecordInWorkspace, } from "./workspace/workspace.js";
@@ -1,8 +1,9 @@
1
1
  import { type AnyRecord } from "@sincpro/criteria";
2
2
  import { type WorkspaceApi } from "@sincpro/web-composer";
3
+ import type { GroupLevel, NamedFilter } from "../../core/list.js";
3
4
  import type { Listing } from "../../react/useListing.js";
4
- import { type NamedFilter } from "./EntitySearchBar.js";
5
- import { type ListCrumb } from "@sincpro/web-ui";
5
+ import { type RowCommand } from "./rowCommands.js";
6
+ import { type ListCrumb, type ListPresentProps, type ListSet, type OpenTarget, type RowAction } from "@sincpro/web-ui";
6
7
  import { type ReactNode } from "react";
7
8
  /**
8
9
  * A whole collection from a `Listing`: the bar, the search, the set in the views it offers,
@@ -17,6 +18,11 @@ import { type ReactNode } from "react";
17
18
  * its ceiling and asks to narrow the search. Nothing is filtered, ordered or counted on the
18
19
  * page: every gesture is a question to the server.
19
20
  *
21
+ * Grouping obeys the same rule: «Group by» in the search bar asks the SERVER for the groups of
22
+ * the whole set (`useGroups`), drawn as piles with their real count; a group's rows are read
23
+ * when it is opened, and «See N more» reads its next page on the server's cursor. Since web-ui
24
+ * 1.4 every face draws piles counted elsewhere — the table too — so grouping keeps the face.
25
+ *
20
26
  * Where a record opens is the composer's question: with `api`, a row opens the page and the
21
27
  * identity column is the composer's `RecordLink` (peek, side panel, window); without it, the
22
28
  * screen passes `onOpen`.
@@ -30,16 +36,29 @@ export type EntityListProps<T extends AnyRecord> = {
30
36
  * breadcrumb is the composer's — nothing for the screen to write.
31
37
  */
32
38
  api?: WorkspaceApi;
33
- /** The fields shown, in order. Without it, every scalar field of the model. */
39
+ /** The fields shown, in order. Without it, the named list's, or every scalar field. */
34
40
  columns?: readonly string[];
35
41
  /** The cells that are not plain text: a link, a tag. Memoise it: compared by identity. */
36
42
  cells?: Readonly<Partial<Record<string, (row: T) => ReactNode>>>;
37
43
  /**
38
44
  * The ways to look at the set, the first one shown: `table` (columns), `rows` (a name and
39
- * its facts, what a narrow panel wants) and `board` (cards). The same set, the same search,
40
- * the same page in each.
45
+ * its facts, what a narrow panel wants), `board` (cards) and `sheet` (a spreadsheet, every
46
+ * cell a value — web-ui's, windowed). The same set, the same search, the same page in each.
47
+ * Without it, a named list's; else all four on a screen, and the table (rows when narrow)
48
+ * inside a record. The id of one of `faces` opens on that face.
49
+ */
50
+ views?: readonly (View | (string & {}))[];
51
+ /**
52
+ * The set crossed by two fields — `{ rows: "state", columns: "payment" }` — counted and added
53
+ * up by the server (`usePivot`), offered as the `pivot` face. Only while it is on screen does
54
+ * it ask; never folded from the page.
41
55
  */
42
- views?: readonly ("table" | "rows" | "board")[];
56
+ pivot?: {
57
+ rows: string | GroupLevel;
58
+ columns: string | GroupLevel;
59
+ /** What each crossing adds up. Without it, the figures the model computes. */
60
+ measures?: readonly string[];
61
+ };
43
62
  /** Without it, the model's own name. */
44
63
  title?: string;
45
64
  description?: string;
@@ -47,13 +66,26 @@ export type EntityListProps<T extends AnyRecord> = {
47
66
  actions?: ReactNode;
48
67
  onOpen?: (row: T) => void;
49
68
  onPeek?: (row: T) => void;
69
+ /**
70
+ * Where a row's record may open besides a press (the page): a look (👁), a modal, beside, a
71
+ * window — offered in «Abrir en…» by the row's name. With `api`, everywhere the workspace
72
+ * allows from here (`targetsFor`: no modal from inside a modal); `[]` or a few restrict it.
73
+ */
74
+ openTargets?: readonly OpenTarget[];
75
+ /** Open a row's record there — without `api`, how a screen (a one2many) places it. */
76
+ onOpenIn?: (row: T, to: OpenTarget) => void;
50
77
  /** What an empty list says. */
51
78
  empty?: string;
52
79
  /**
53
80
  * `"bar"` (the default) is the full search bar with the criteria builder; `"box"` is one
54
81
  * text box over the entity's search template — a chooser, a narrow panel; `false` is none.
82
+ * Both ask the server on Enter, never per keystroke: a table is searched on purpose.
55
83
  */
56
84
  search?: "bar" | "box" | false;
85
+ /** What each group adds up when the list is grouped — «Total 1.204.933,00» in its header,
86
+ * summed by the server over every row of it. Without it, the figures the model computes
87
+ * (read-only exact numbers, `sumsOf`); `false` adds nothing up. */
88
+ sums?: readonly string[] | false;
57
89
  /** The cuts this list offers by name: «sólo pagadas», «vencidas». */
58
90
  filters?: readonly NamedFilter<T>[];
59
91
  /**
@@ -65,5 +97,35 @@ export type EntityListProps<T extends AnyRecord> = {
65
97
  selectionActions?: ReactNode;
66
98
  /** The list is a FIELD of a record — its lines, its members — and not a screen. */
67
99
  embedded?: boolean;
100
+ /**
101
+ * With `api` and an entity that declares `create`, the bar offers «New», which opens the
102
+ * record's form with no id (`entity.newRef()`). `false` hides it — a list of what only a
103
+ * Command produces, or a caller who may not create.
104
+ */
105
+ create?: boolean;
106
+ /**
107
+ * The entity's Commands offered on each row («Confirmar»), sent with their idempotency key;
108
+ * the list is read again when one is done (`useRowCommands`).
109
+ */
110
+ commands?: readonly RowCommand<T>[];
111
+ /** Any other action of a row — or the commands built by hand with `useRowCommands`. */
112
+ rowActions?: (row: T) => readonly RowAction[];
113
+ /**
114
+ * Ways of looking at the set the SCREEN writes — a gallery, a map, a record with its lines and
115
+ * its buttons — offered beside the built-in ones (web-ui's `List.Present`). Each is handed the
116
+ * same set the others draw: the page, the search's grouping, the selection and what a press
117
+ * does (`ListSet`). Offered after `views`; name one in `views` to open on it.
118
+ */
119
+ faces?: readonly EntityFace<T>[];
120
+ };
121
+ /** A face of a list written by the screen: its name and icon in the switcher, and how it draws
122
+ * the set. `wide` when it needs width (a narrow list does not offer it). */
123
+ export type EntityFace<T> = {
124
+ id: string;
125
+ label: string;
126
+ icon: ListPresentProps<T>["icon"];
127
+ wide?: boolean;
128
+ render: (set: ListSet<T>) => ReactNode;
68
129
  };
69
- export declare function EntityList<T extends AnyRecord>({ listing, api, columns, cells, views, title, description, crumbs, actions, onOpen, onPeek, empty, search, filters, selectable, selectionActions, embedded, }: EntityListProps<T>): import("react").JSX.Element;
130
+ export type View = "table" | "rows" | "board" | "sheet" | "pivot";
131
+ export declare function EntityList<T extends AnyRecord>({ listing, api, columns: askedColumns, cells, views: askedViews, title: askedTitle, description: askedDescription, crumbs, actions, onOpen, onPeek, openTargets, onOpenIn, pivot: pivotAsked, empty, search, sums, filters: askedFilters, selectable, selectionActions, embedded, create, commands, rowActions: askedRowActions, faces, }: EntityListProps<T>): import("react").JSX.Element;