@sincpro/web-framework 3.18.0 → 3.20.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 +83 -54
  2. package/dist/core/copy.d.ts +14 -0
  3. package/dist/core/copy.js +57 -2
  4. package/dist/core/edit.d.ts +13 -0
  5. package/dist/core/edit.js +14 -0
  6. package/dist/core/explain.d.ts +4 -0
  7. package/dist/core/explain.js +4 -0
  8. package/dist/core/export.d.ts +29 -0
  9. package/dist/core/export.js +47 -0
  10. package/dist/core/groups.d.ts +3 -1
  11. package/dist/core/groups.js +7 -0
  12. package/dist/core/index.d.ts +4 -3
  13. package/dist/core/index.js +4 -3
  14. package/dist/core/lines.d.ts +10 -0
  15. package/dist/core/lines.js +24 -0
  16. package/dist/core/list.d.ts +12 -0
  17. package/dist/react/context.d.ts +44 -1
  18. package/dist/react/context.js +50 -1
  19. package/dist/react/index.d.ts +4 -2
  20. package/dist/react/index.js +4 -2
  21. package/dist/react/useCounts.d.ts +23 -0
  22. package/dist/react/useCounts.js +43 -0
  23. package/dist/react/useGroups.js +38 -5
  24. package/dist/react/useNameOf.d.ts +13 -0
  25. package/dist/react/useNameOf.js +32 -0
  26. package/dist/react/usePeopleOf.d.ts +14 -0
  27. package/dist/react/usePeopleOf.js +35 -0
  28. package/dist/react/useRecord.js +2 -2
  29. package/dist/react/useServiceReading.d.ts +8 -0
  30. package/dist/react/useServiceReading.js +12 -2
  31. package/dist/web/index.d.ts +4 -2
  32. package/dist/web/index.js +4 -2
  33. package/dist/web/list/EntityList.d.ts +36 -177
  34. package/dist/web/list/EntityList.js +175 -536
  35. package/dist/web/list/entityList/choosing.d.ts +50 -0
  36. package/dist/web/list/entityList/choosing.js +77 -0
  37. package/dist/web/list/entityList/faces.d.ts +53 -0
  38. package/dist/web/list/entityList/faces.js +155 -0
  39. package/dist/web/list/entityList/grouping.d.ts +50 -0
  40. package/dist/web/list/entityList/grouping.js +97 -0
  41. package/dist/web/list/entityList/opening.d.ts +68 -0
  42. package/dist/web/list/entityList/opening.js +149 -0
  43. package/dist/web/list/entityList/paging.d.ts +30 -0
  44. package/dist/web/list/entityList/paging.js +39 -0
  45. package/dist/web/list/entityList/props.d.ts +174 -0
  46. package/dist/web/list/entityList/props.js +16 -0
  47. package/dist/web/list/entityList/rowActions.d.ts +31 -0
  48. package/dist/web/list/entityList/rowActions.js +40 -0
  49. package/dist/web/list/export.d.ts +13 -0
  50. package/dist/web/list/export.js +61 -0
  51. package/dist/web/list/groups.d.ts +16 -0
  52. package/dist/web/list/groups.js +39 -0
  53. package/dist/web/list/useEntityList.d.ts +183 -0
  54. package/dist/web/list/useEntityList.js +225 -0
  55. package/dist/web/parts.d.ts +42 -0
  56. package/dist/web/parts.js +88 -0
  57. package/dist/web/record/lines.d.ts +3 -0
  58. package/dist/web/record/lines.js +75 -9
  59. package/dist/web/workspace/RecordScreen.d.ts +206 -0
  60. package/dist/web/workspace/RecordScreen.js +251 -0
  61. package/dist/web/workspace/SincproApp.d.ts +14 -2
  62. package/dist/web/workspace/SincproApp.js +27 -9
  63. package/dist/web/workspace/composer.js +1 -1
  64. package/dist/web/workspace/record.d.ts +21 -27
  65. package/dist/web/workspace/record.js +29 -77
  66. package/dist/web/workspace/workspace.d.ts +2 -0
  67. package/dist/web/workspace/workspace.js +1 -1
  68. package/package.json +7 -7
@@ -1,15 +1,20 @@
1
1
  "use client";
2
- import { jsx as _jsx } from "react/jsx-runtime";
2
+ import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
3
+ import { keysOf, listRoute } from "@sincpro/client-framework";
3
4
  import { choicesOf } from "@sincpro/criteria";
5
+ import { frameworkCopy } from "../../core/copy.js";
4
6
  import { adviceAt, defaultsOf, errorAt, linePlacesOf, pathOf, } from "../../core/edit.js";
5
- import { addingOf, blankLine, lineFields, lineKey, moved, sameLine, writtenInCell, } from "../../core/lines.js";
7
+ import { addingOf, addManyOf, blankLine, lineFields, lineKey, moved, sameLine, writtenInCell, } from "../../core/lines.js";
6
8
  import { textOf, viewOf } from "../../core/present.js";
7
9
  import { entityFor, relationFieldOf, } from "../../core/relation.js";
8
10
  import { useEntityFor, useFramework } from "../../react/context.js";
11
+ import { BATCH_LIMIT } from "../../react/useBatchCommand.js";
12
+ import { useNameOf } from "../../react/useNameOf.js";
13
+ import { refusalNotice } from "../list/records.js";
9
14
  import { controlFor, RelationPicker, } from "./control.js";
10
15
  import { RecordOpeningProvider, useRecordOpening, } from "./opening.js";
11
16
  import { usePartLook } from "./record.js";
12
- import { Field, LineItems, ReadValue, Tag, } from "@sincpro/web-ui";
17
+ import { Field, LineItems, ReadValue, Tag, useToastIfAny, } from "@sincpro/web-ui";
13
18
  import { useRef } from "react";
14
19
  /** How a field of a line reads: a many2one's key by the name of what it points at. */
15
20
  function textIn(definition, field, relation, line, locale) {
@@ -18,6 +23,26 @@ function textIn(definition, field, relation, line, locale) {
18
23
  : textOf(definition, relation.relation, line[relation.relation], locale);
19
24
  return named ?? textOf(definition, field, line[field], locale) ?? "";
20
25
  }
26
+ /**
27
+ * A line's many2one whose record is not in hand — a key alone (`EntityID`), or a line added with
28
+ * only its key («Agregar varios…») — read by the record's name, as a list's column reads it: the
29
+ * keys drawn in one request per entity (`useNameOf`), the key until it answers.
30
+ */
31
+ function LineKeyName({ target, id }) {
32
+ return _jsx(_Fragment, { children: useNameOf(useEntityFor(target), id) ?? id });
33
+ }
34
+ /** The key of `line`'s many2one when its record is not beside it — what `LineKeyName` names. */
35
+ function unnamedKey(relation, line) {
36
+ if (relation === undefined || relation.many)
37
+ return undefined;
38
+ const held = relation.relation === relation.key ? null : line[relation.relation];
39
+ const key = line[relation.key];
40
+ return held === null || held === undefined
41
+ ? key === null || key === undefined || key === ""
42
+ ? undefined
43
+ : String(key)
44
+ : undefined;
45
+ }
21
46
  /**
22
47
  * One field of a line, written — in its cell or in its dialog: a many2one of a declared entity
23
48
  * is its picker, anything else the control its type asks for.
@@ -97,8 +122,9 @@ function LineValue({ definition, field, line, set, label, invalid, locale, inCel
97
122
  */
98
123
  export function RecordLinesEditor({ record, name, lines, look = {}, }) {
99
124
  const { meta, locale, edit } = record;
100
- const { entities, metas } = useFramework();
125
+ const { entities, metas, transport } = useFramework();
101
126
  const opening = useRecordOpening();
127
+ const toast = useToastIfAny();
102
128
  const definition = meta?.fields[name]?.definition ?? null;
103
129
  // A value inside the record (an embedded list) has no identity of its own: every line is
104
130
  // keyed locally — falling back to `id` would key it by whatever it holds under that name.
@@ -136,6 +162,30 @@ export function RecordLinesEditor({ record, name, lines, look = {}, }) {
136
162
  const now = useRef(lines);
137
163
  now.current = lines;
138
164
  const write = (change) => edit.set(name, change(now.current));
165
+ const newLine = () => blankLine(defaultsOf(definition), identity);
166
+ // «Agregar varios…»: the workspace's chooser of several on the line's many2one (a product),
167
+ // one new line per record chosen — its key set, the rest as «Agregar una línea» leaves it.
168
+ // The chosen are read once with a ceiling, as a many2many's are (`keysOf`, `BATCH_LIMIT`).
169
+ const many = writing ? addManyOf(definition, declared, look.addMany) : undefined;
170
+ const manyKey = many === undefined ? undefined : relations.get(many)?.key;
171
+ const manyEntity = many === undefined
172
+ ? undefined
173
+ : entityFor(entities, relations.get(many).target, metas.get);
174
+ const chooseMany = opening?.chooseMany;
175
+ const addMany = manyKey === undefined || manyEntity === undefined || chooseMany === undefined
176
+ ? undefined
177
+ : () => chooseMany(manyEntity, (selection) => {
178
+ const meta = metas.get(manyEntity.key);
179
+ keysOf(selection, {
180
+ transport,
181
+ route: listRoute(manyEntity.list),
182
+ identity: meta?.get_id || meta?.identity || "id",
183
+ limit: BATCH_LIMIT,
184
+ }).then((keys) => write((all) => [
185
+ ...all,
186
+ ...keys.map((key) => ({ ...newLine(), [manyKey]: key })),
187
+ ]), (failure) => toast?.show(refusalNotice(failure)));
188
+ }, "");
139
189
  const drawn = new Set(shown);
140
190
  const fieldColumns = shown.map((field) => {
141
191
  const view = viewOf(definition, field, locale);
@@ -159,7 +209,14 @@ export function RecordLinesEditor({ record, name, lines, look = {}, }) {
159
209
  value: (line) => line[field],
160
210
  // Drawn as read: «Cemento 50 kg», not «CEM-50»; «1.552,00», not «1552.00».
161
211
  // A choice the record's entity coloured reads as its tag, as in a list.
162
- cell: ownCell ?? toned ?? ((line) => textIn(definition, field, relation, line, locale)),
212
+ cell: ownCell ??
213
+ toned ??
214
+ ((line) => {
215
+ const key = relation !== undefined && declared(relation.target)
216
+ ? unnamedKey(relation, line)
217
+ : undefined;
218
+ return key === undefined ? (textIn(definition, field, relation, line, locale)) : (_jsx(LineKeyName, { target: relation.target, id: key }));
219
+ }),
163
220
  text: (line) => textIn(definition, field, relation, line, locale),
164
221
  numeric: view.type === "number" || view.type === "integer",
165
222
  ...(inCell
@@ -180,7 +237,9 @@ export function RecordLinesEditor({ record, name, lines, look = {}, }) {
180
237
  ...(one.numeric === undefined ? {} : { numeric: one.numeric }),
181
238
  })),
182
239
  ];
183
- const table = (_jsx(LineItems, { lines: lines, columns: columns, getLineId: keyOf, mode: writing ? "edit" : "read", adding: adding, onChange: (key, next) => write((all) => all.map((one) => (keyOf(one) === key ? sameLine(one, next) : one))), newLine: () => blankLine(defaultsOf(definition), identity), onAdd: (line) => write((all) => [...all, line]), onRemove: (key) => write((all) => all.filter((one) => keyOf(one) !== key)), onRestore: (line, index) => write((all) => [...all.slice(0, index), line, ...all.slice(index)]), onReorder: (key, before) => write((all) => moved(all, identity, key, before)),
240
+ const table = (_jsx(LineItems, { lines: lines, columns: columns, getLineId: keyOf, mode: writing ? "edit" : "read", adding: adding, onChange: (key, next) => write((all) => all.map((one) => (keyOf(one) === key ? sameLine(one, next) : one))), newLine: newLine, ...(addMany === undefined
241
+ ? {}
242
+ : { moreAdds: [{ label: frameworkCopy().record.addMany, onAdd: addMany }] }), onAdd: (line) => write((all) => [...all, line]), onRemove: (key) => write((all) => all.filter((one) => keyOf(one) !== key)), onRestore: (line, index) => write((all) => [...all.slice(0, index), line, ...all.slice(index)]), onReorder: (key, before) => write((all) => moved(all, identity, key, before)),
184
243
  // A cell says its own (web-ui 1.6 `cellState`): the line model's hints refined by the
185
244
  // server's `items[i]`, the error pointing at it (under its value) and its warning.
186
245
  cellState: (line, column) => {
@@ -232,8 +291,15 @@ export function RecordLinesEditor({ record, name, lines, look = {}, }) {
232
291
  // screen's own controls included: the workspace opens it outside the line, a row commits when
233
292
  // the focus leaves it, and the answer would land on a line no longer being written (over the
234
293
  // line's dialog, a modal over a modal). Opening a chosen record is still the workspace's.
235
- if (opening === null || opening.choose === undefined)
236
- return table;
294
+ const inLine = opening === null ? null : withoutChoose(opening);
295
+ const lined = inLine === null ? (table) : (_jsx(RecordOpeningProvider, { value: inLine, children: table }));
296
+ return lined;
297
+ }
298
+ /** The opening inside a line: without the workspace's chooser (see the end of the editor), or
299
+ * `null` when there is none to take away. */
300
+ function withoutChoose(opening) {
301
+ if (opening.choose === undefined)
302
+ return null;
237
303
  const { choose: _, ...inLine } = opening;
238
- return _jsx(RecordOpeningProvider, { value: inLine, children: table });
304
+ return inLine;
239
305
  }
@@ -0,0 +1,206 @@
1
+ import type { Entity } from "@sincpro/client-framework";
2
+ import type { AnyRecord, Expression } from "@sincpro/criteria";
3
+ import { type ViewProps } from "@sincpro/web-composer";
4
+ import type { RecordRead } from "../../core/server.js";
5
+ import type { RecordReading } from "../../react/useRecord.js";
6
+ import { actionsOf, type ScreenActions } from "../list/dock.js";
7
+ import { type NoProps } from "../parts.js";
8
+ import type { FieldControl } from "../record/control.js";
9
+ import type { DockedAction } from "../record/dock.js";
10
+ import { type RecordTag, useRecordScreen } from "./record.js";
11
+ import { type DockAction, type DockMoment, type SmartLink } from "@sincpro/web-ui";
12
+ import { type ReactNode } from "react";
13
+ /**
14
+ * A record screen written as a tree of the framework's parts.
15
+ *
16
+ * <RecordScreen entity={invoices} {...props} actions={(invoice) => [...]}>
17
+ * <RecordScreen.Rail /> ← crumbs, pager and actions
18
+ * <RecordScreen.Paper>
19
+ * <RecordScreen.Notices />
20
+ * <InvoiceTotals /> ← the application's own block
21
+ * <RecordScreen.Fields names={["customer_id", "due_date"]} />
22
+ * <RecordScreen.Audit />
23
+ * </RecordScreen.Paper>
24
+ * </RecordScreen>
25
+ *
26
+ * With no children it draws the whole default screen (`EntityRecord` is exactly that). With
27
+ * children it draws only what is written: a part that is not written is not there, as in web-ui.
28
+ * A part written with no children of its own (`Rail`, `Paper`) draws its default content.
29
+ *
30
+ * Context: before it, a screen of its own had two choices — the whole `EntityRecord`, or
31
+ * `useRecordScreen` and web-ui's slot tree written by hand: the loading and failure guards, the
32
+ * crumbs, the pager, the audit, the notices `EntityRecord` drew and nobody else could. Each
33
+ * application wrote that again, or built a factory of screens to avoid it (quipu's
34
+ * `registroSimple`). The parts close that gap: take the default, replace one piece, keep the rest.
35
+ *
36
+ * How the parts reach web-ui: web-ui's `Record` reads its slots as DATA, by a mark on the
37
+ * element, and a slot wrapped in one of our components is never found (AGENTS.md). So a part
38
+ * here draws nothing itself: it is a mark (`part`, `web/parts.tsx`), and the root replaces it by
39
+ * web-ui's own slot, filled from the screen, before handing the tree to `Record`. A part that is
40
+ * drawn inside the paper (`Fields`, `Sections`) is an ordinary component that reads the screen
41
+ * from context. Anything else — web-ui's own slots, the application's components — passes as it
42
+ * was written.
43
+ */
44
+ /** A record that is on screen: read, never loading or failed. */
45
+ export type ReadyRecord<T> = RecordReading<T> & {
46
+ status: "ready";
47
+ record: T;
48
+ error: null;
49
+ };
50
+ type Screen = ReturnType<typeof useRecordScreen<AnyRecord>>;
51
+ type RecordScreenState = {
52
+ screen: Screen;
53
+ reading: ReadyRecord<AnyRecord>;
54
+ /** The fields the screen shows by default: the root's `fields`, or the model's form. */
55
+ fields: readonly string[];
56
+ /** The embedded values drawn as sections of their own (a payment's receipt). */
57
+ sections: readonly string[];
58
+ /** The one2many drawn as pages. */
59
+ pages: readonly string[];
60
+ page: string | undefined;
61
+ setPage: (page: string) => void;
62
+ /** How many columns the form takes where it is mounted. */
63
+ columns: 1 | 2;
64
+ phone: boolean;
65
+ /** web-ui's «settings» form style: each section a card, so the main one needs its name. */
66
+ settings: boolean;
67
+ /** The actions the view style floats in the dock (the screen's and the lifecycle's). */
68
+ docked: ReturnType<typeof actionsOf>["dock"];
69
+ /** The actions the screen draws by hand, with the lifecycle's buttons beside them. */
70
+ drawn: ReactNode | null;
71
+ /** What hangs off the record, opened in the workspace (`links`). */
72
+ links: readonly SmartLink[];
73
+ /** The restored draft's notice was put away: the draft stays, its notice is read. */
74
+ restoredAway: boolean;
75
+ putRestoredAway: () => void;
76
+ };
77
+ /**
78
+ * The record on the screen around it, for a block the application writes inside a
79
+ * `RecordScreen`. It is always read: the screen draws its children only then.
80
+ *
81
+ * function InvoiceTotals() {
82
+ * const invoice = useScreenRecord(invoices);
83
+ * return <Money value={invoice.record.total} />;
84
+ * }
85
+ *
86
+ * The entity is only its type: the record is the screen's.
87
+ */
88
+ export declare function useScreenRecord<T extends AnyRecord>(_entity?: Entity<T>): ReadyRecord<T>;
89
+ /**
90
+ * How many columns the record's form takes where the screen is mounted — two on a page or in a
91
+ * modal, one in a panel, a peek or a phone —, for a block of the application that lays itself out
92
+ * the same way («three figures side by side when wide»). The screen decided it; a block never
93
+ * asks the workspace where it opened.
94
+ */
95
+ export declare function useScreenColumns(): 1 | 2;
96
+ /** A field drawn the screen's way inside `RecordScreen.Fields`: its control, and the record. */
97
+ export type FieldDrawn = (control: FieldControl, record: ReadyRecord<AnyRecord>) => ReactNode;
98
+ /**
99
+ * The record's fields in web-ui's form grid: two columns on a page and in a modal, one in a
100
+ * panel, a peek or a phone. `names` chooses and orders them; `title` heads the section and
101
+ * `hint` says what it is for. A field the model cannot draw as the screen wants — a body with
102
+ * pictures, a year — is drawn by `controls`, with the field's control and the record. A section
103
+ * of one field that bears its name («Sesiones», the sessions) does not name it twice: the field
104
+ * is drawn bare under the section's title.
105
+ */
106
+ declare function Fields({ names, title, hint, controls, }: {
107
+ names?: readonly string[] | undefined;
108
+ title?: string | undefined;
109
+ hint?: string | undefined;
110
+ controls?: Readonly<Partial<Record<string, FieldDrawn>>> | undefined;
111
+ }): import("react").JSX.Element;
112
+ /** Each value the record embeds (a payment's receipt) as a section of its own fields. */
113
+ declare function Sections(): import("react").JSX.Element;
114
+ export type RecordScreenProps<T extends AnyRecord> = ViewProps & {
115
+ entity: Entity<T>;
116
+ /** The record and its model already read on the server that renders the screen
117
+ * (`readRecordOnServer`): drawn filled there, and not asked again in the browser. */
118
+ initial?: RecordRead<T> | undefined;
119
+ /** Its name, the screen's way — for a record whose model has no display field. */
120
+ title?: ((record: T) => string | null) | undefined;
121
+ /** The line under its name, the screen's way. Without it, the record's type, if any. */
122
+ subtitle?: ((record: T) => string | null) | undefined;
123
+ /** Its tag, the screen's way («Publicada»). Without it, its state in the entity's tone. */
124
+ tag?: ((record: T) => RecordTag | null | undefined) | undefined;
125
+ /** How a new one is named: «Nueva noticia». Without it, the model's name and «nuevo». */
126
+ creating?: string | undefined;
127
+ /** The dock's word on its moment, the screen's way — one that depends on who looks. Without
128
+ * it, the entity's (`presented(entity, { moment })`). */
129
+ moment?: ((record: T) => DockMoment | undefined) | undefined;
130
+ /** What hangs off it, opened in the workspace — `RecordScreen.Smart`, in the rail: a list of
131
+ * related records declared (`RelatedLink`: counted and opened by the screen), or a link of the
132
+ * screen's own. Not while it is new: nothing hangs off a record that does not exist yet. */
133
+ links?: ((record: T) => readonly (SmartLink | RelatedLink)[]) | undefined;
134
+ /** The fields `RecordScreen.Fields` shows, in this order. Without it, the model's (`formOf`). */
135
+ fields?: readonly string[] | undefined;
136
+ /** The one2many `RecordScreen.Pages` shows, in this order. Without it, every one the model has. */
137
+ pages?: readonly string[] | undefined;
138
+ /**
139
+ * The record's own actions, beside «save / discard» and its lifecycle («Eliminar»). As data
140
+ * (`DockAction`s) the view style draws them with the lifecycle's: the rail's buttons in
141
+ * `traditional`, one floating pill in `modern`. A node drawn by hand stays in the rail.
142
+ */
143
+ actions?: ((record: RecordReading<T>) => ScreenActions | readonly (DockAction | DockedAction)[]) | undefined;
144
+ /** `false`: the screen draws its own history — it is not read. */
145
+ history?: boolean | undefined;
146
+ /** The screen's tree. Without it, the default screen. */
147
+ children?: ReactNode;
148
+ };
149
+ declare function RecordScreenRoot<T extends AnyRecord>({ api, resource, entity, initial, title, subtitle, tag, creating, moment, links, fields, pages, actions, history, children, }: RecordScreenProps<T>): import("react").JSX.Element;
150
+ /**
151
+ * A record screen by parts: the framework's default, or any part of it replaced. See the top of
152
+ * this file.
153
+ */
154
+ export declare const RecordScreen: typeof RecordScreenRoot & {
155
+ Rail: import("../parts.js").Part<{
156
+ children?: ReactNode;
157
+ }, RecordScreenState>;
158
+ Crumbs: import("../parts.js").Part<NoProps, RecordScreenState>;
159
+ Pager: import("../parts.js").Part<NoProps, RecordScreenState>;
160
+ Smart: import("../parts.js").Part<NoProps, RecordScreenState>;
161
+ Actions: import("../parts.js").Part<NoProps, RecordScreenState>;
162
+ Paper: import("../parts.js").Part<{
163
+ children?: ReactNode;
164
+ layout?: "horizontal" | "stacked";
165
+ width?: "reading" | "full";
166
+ }, RecordScreenState>;
167
+ Notices: import("../parts.js").Part<NoProps, RecordScreenState>;
168
+ Fields: typeof Fields;
169
+ Sections: typeof Sections;
170
+ Advice: import("../parts.js").Part<NoProps, RecordScreenState>;
171
+ Pages: import("../parts.js").Part<{
172
+ names?: readonly string[];
173
+ }, RecordScreenState>;
174
+ Audit: import("../parts.js").Part<NoProps, RecordScreenState>;
175
+ Activity: import("../parts.js").Part<NoProps, RecordScreenState>;
176
+ };
177
+ /**
178
+ * The record of an entity, drawn from its model — the screen `entityView` opens when the
179
+ * application writes none: in the page, a modal, the preview or a panel, the same. It is
180
+ * `RecordScreen` with no tree of its own.
181
+ *
182
+ * entityView(invoices, { transport, name: "Facturas" }) // this screen
183
+ * entityView(invoices, { …, record: (props) =>
184
+ * <EntityRecord {...props} entity={invoices} fields={["customer_id", "due_date"]}
185
+ * actions={(invoice) => <CommandButton entity={invoices} command="post" … />} /> })
186
+ */
187
+ export declare function EntityRecord<T extends AnyRecord>(props: Omit<RecordScreenProps<T>, "children" | "history">): import("react").JSX.Element;
188
+ /**
189
+ * A list of records related to this one, declared — Odoo's smart button: its icon and words, the
190
+ * entity it lists and what keeps them (`where`, from the record), and the title its list opens
191
+ * with. The screen counts it and opens it; the application writes no hook for it.
192
+ *
193
+ * links={(pago) => [
194
+ * { icon: Receipt, label: "Sus pagos", list: pagos, title: pago.colegiado_nombre,
195
+ * where: { field: "colegiado_id", operator: "=", value: pago.colegiado_id } },
196
+ * ]}
197
+ */
198
+ export type RelatedLink = {
199
+ icon: SmartLink["icon"];
200
+ label: string;
201
+ list: Entity;
202
+ where: Expression;
203
+ /** Whose the list is: «María» of «Pagos · María». */
204
+ title?: string;
205
+ };
206
+ export {};
@@ -0,0 +1,251 @@
1
+ "use client";
2
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
3
+ import { labelOf } from "@sincpro/criteria";
4
+ import { usePhone } from "@sincpro/web-composer";
5
+ import { frameworkCopy } from "../../core/copy.js";
6
+ import { formOf } from "../../core/form.js";
7
+ import { summaryFieldsOf } from "../../core/summary.js";
8
+ import { useCounts } from "../../react/useCounts.js";
9
+ import { actionsOf } from "../list/dock.js";
10
+ import { part, resolveParts, written } from "../parts.js";
11
+ import { RecordField } from "../record/edit.js";
12
+ import { LifecycleButtons, useRecordLifecycleActions, } from "../record/RecordLifecycle.js";
13
+ import { Refused } from "../Refused.js";
14
+ import { useRecordScreen } from "./record.js";
15
+ import { Button, FormSection, Record, Skeleton, useFormStyle, } from "@sincpro/web-ui";
16
+ import { createContext, Fragment, useContext, useState } from "react";
17
+ const RecordScreenContext = createContext(null);
18
+ function useScreenState(part) {
19
+ const state = useContext(RecordScreenContext);
20
+ if (state === null)
21
+ throw new Error(`RecordScreen.${part} is drawn inside a RecordScreen.`);
22
+ return state;
23
+ }
24
+ /**
25
+ * The record on the screen around it, for a block the application writes inside a
26
+ * `RecordScreen`. It is always read: the screen draws its children only then.
27
+ *
28
+ * function InvoiceTotals() {
29
+ * const invoice = useScreenRecord(invoices);
30
+ * return <Money value={invoice.record.total} />;
31
+ * }
32
+ *
33
+ * The entity is only its type: the record is the screen's.
34
+ */
35
+ export function useScreenRecord(_entity) {
36
+ return useScreenState("useScreenRecord").reading;
37
+ }
38
+ /**
39
+ * How many columns the record's form takes where the screen is mounted — two on a page or in a
40
+ * modal, one in a panel, a peek or a phone —, for a block of the application that lays itself out
41
+ * the same way («three figures side by side when wide»). The screen decided it; a block never
42
+ * asks the workspace where it opened.
43
+ */
44
+ export function useScreenColumns() {
45
+ return useScreenState("useScreenColumns").columns;
46
+ }
47
+ /* ----------------------------------------------------------------------------------------- *
48
+ * Parts that become web-ui slots
49
+ * ----------------------------------------------------------------------------------------- */
50
+ /** A part of the record screen, resolved against its state. */
51
+ function screenPart(name, resolve) {
52
+ return part(`RecordScreen.${name}`, resolve);
53
+ }
54
+ /** The crumbs that lead back, when the workspace opened it from somewhere. */
55
+ const Crumbs = screenPart("Crumbs", (_props, { screen }) => screen.crumbs.length === 0 ? null : _jsx(Record.Crumbs, { crumbs: screen.crumbs }));
56
+ /** «3 / 64» and the neighbours, when it was opened from a list and nothing is typed. */
57
+ const Pager = screenPart("Pager", (_props, { screen }) => screen.pager === undefined ? null : _jsx(Record.Pager, { ...screen.pager }));
58
+ /** The screen's actions and the record's lifecycle — floating in the dock, or in the rail when
59
+ * the screen draws them by hand. The application's word on the record's moment goes with them. */
60
+ const Actions = screenPart("Actions", (_props, state) => {
61
+ const { docked, drawn, screen } = state;
62
+ const moment = screen.moment;
63
+ return [
64
+ drawn === null ? null : _jsx(Record.Actions, { children: drawn }, "actions"),
65
+ docked.length === 0 && moment === undefined ? null : (_jsx(Record.Dock, { actions: docked, ...(moment === undefined ? {} : { moment }) }, "dock")),
66
+ ];
67
+ });
68
+ /** What hangs off the record — its payments, its members —, as web-ui's smart buttons. */
69
+ const Smart = screenPart("Smart", (_props, { links }) => links.length === 0 ? null : _jsx(Record.Smart, { links: links }));
70
+ /** The band in front of the record. Empty, it draws crumbs, pager, links and actions — and no band at
71
+ * all when none of them has anything to say (an empty one is a blank strip in a modal). */
72
+ const Rail = screenPart("Rail", ({ children }, state) => {
73
+ const inside = written(resolveParts(children ?? [
74
+ _jsx(Crumbs, {}, "crumbs"),
75
+ _jsx(Pager, {}, "pager"),
76
+ _jsx(Smart, {}, "smart"),
77
+ _jsx(Actions, {}, "actions"),
78
+ ], state));
79
+ return inside.length === 0 ? null : _jsx(Record.Rail, { children: inside });
80
+ });
81
+ /** Who made it and who changed it last, when the record says so. */
82
+ const Audit = screenPart("Audit", (_props, { screen }) => screen.audit === undefined ? null : _jsx(Record.Audit, { ...screen.audit }));
83
+ /** The record's history beside the paper, unless the screen read none. */
84
+ const Activity = screenPart("Activity", (_props, { screen }) => screen.history === undefined ? null : _jsx(Record.Activity, { events: screen.history }));
85
+ /** The one2many as pages of a notebook — `names` chooses and orders them. Not on a new record:
86
+ * it has no lines on the server yet. */
87
+ const Pages = screenPart("Pages", ({ names }, state) => {
88
+ const { reading, page, setPage } = state;
89
+ const tabs = names ?? state.pages;
90
+ if (tabs.length === 0 || reading.isNew)
91
+ return null;
92
+ return (_jsx(Record.Pages, { current: page ?? tabs[0], onChange: setPage, children: tabs.map((name) => (_jsx(Record.Page, { id: name, label: labelOf(reading.meta, name, reading.locale), children: _jsx(RecordField, { record: reading, name: name, bare: true }) }, name))) }));
93
+ });
94
+ /**
95
+ * What happened to the edit itself, as web-ui's notices of the record — never among the fields:
96
+ * someone else saved first (`bad`: it blocks, so it stays in view between the rail and the
97
+ * paper, with «Read it again»), or a kept draft came back (`info`: into the rail's indicator,
98
+ * and it can be put away). Written at the top of the screen or inside the paper, the same.
99
+ */
100
+ const Notices = screenPart("Notices", (_props, state) => {
101
+ const { reading, restoredAway, putRestoredAway } = state;
102
+ const copy = frameworkCopy().record;
103
+ const { conflict, restored } = reading.edit;
104
+ return [
105
+ conflict ? (_jsx(Record.Notice, { tone: "bad", action: _jsx(Button, { size: "sm", role: "secondary", onClick: reading.reload, children: copy.reload }), children: copy.conflict }, "conflict")) : null,
106
+ restored && !restoredAway ? (_jsx(Record.Notice, { tone: "info", onDismiss: putRestoredAway, children: copy.restored }, "restored")) : null,
107
+ ];
108
+ });
109
+ /**
110
+ * What the server advised about the record or one of its fields, as notices of the record: one
111
+ * the save will refuse while it holds is a warning. A line's warning goes on its line.
112
+ */
113
+ const Advice = screenPart("Advice", (_props, { reading }) => reading.edit.advice
114
+ .filter((one) => (one.path?.length ?? 0) <= 1)
115
+ .map((one) => (_jsx(Record.Notice, { tone: one.blocks_save === true ? "warn" : "info", children: one.message }, one.message))));
116
+ /**
117
+ * The record's fields in web-ui's form grid: two columns on a page and in a modal, one in a
118
+ * panel, a peek or a phone. `names` chooses and orders them; `title` heads the section and
119
+ * `hint` says what it is for. A field the model cannot draw as the screen wants — a body with
120
+ * pictures, a year — is drawn by `controls`, with the field's control and the record. A section
121
+ * of one field that bears its name («Sesiones», the sessions) does not name it twice: the field
122
+ * is drawn bare under the section's title.
123
+ */
124
+ function Fields({ names, title, hint, controls, }) {
125
+ const state = useScreenState("Fields");
126
+ const shown = names ?? state.fields;
127
+ const { meta, locale } = state.reading;
128
+ const named = title !== undefined && shown.length === 1 && labelOf(meta, shown[0], locale) === title;
129
+ return (_jsx(FormSection, { columns: state.columns, ...(title === undefined ? {} : { title }), ...(hint === undefined ? {} : { hint }), children: shown.map((name) => {
130
+ const own = controls?.[name];
131
+ return (_jsx(RecordField, { record: state.reading, name: name, ...(named ? { bare: true } : {}), ...(own === undefined ? {} : { as: (control) => own(control, state.reading) }) }, name));
132
+ }) }));
133
+ }
134
+ /** Each value the record embeds (a payment's receipt) as a section of its own fields. */
135
+ function Sections() {
136
+ const { reading, sections, columns } = useScreenState("Sections");
137
+ const { meta, locale } = reading;
138
+ return (_jsx(_Fragment, { children: sections.map((name) => (_jsx(FormSection, { title: labelOf(meta, name, locale), columns: columns, children: Object.keys(meta?.fields[name]?.definition?.fields ?? {}).map((one) => (_jsx(RecordField, { record: reading, name: `${name}.${one}` }, one))) }, name))) }));
139
+ }
140
+ /** The paper's default content, in the order every record screen draws it — as elements, never
141
+ * a component: web-ui finds `Pages` and `Audit` among the paper's own children. */
142
+ function paperContent(titled) {
143
+ return (_jsxs(_Fragment, { children: [_jsx(Notices, {}), _jsx(Fields, { title: titled ? frameworkCopy().record.general : undefined }), _jsx(Sections, {}), _jsx(Advice, {}), _jsx(Pages, {}), _jsx(Audit, {})] }));
144
+ }
145
+ /** The paper: the record itself. Empty, it draws the default content. */
146
+ const Paper = screenPart("Paper", ({ children, layout, width }, state) => (_jsx(Record.Paper, { layout: layout ?? (state.phone ? "stacked" : "horizontal"), ...(width === undefined ? {} : { width }), children: resolveParts(children ?? paperContent(state.settings && state.sections.length > 0), state) })));
147
+ function RecordScreenRoot({ api, resource, entity, initial, title, subtitle, tag, creating, moment, links, fields, pages, actions, history, children, }) {
148
+ const screen = useRecordScreen(entity, { api, resource }, { title, subtitle, tag, creating, moment, initial, history });
149
+ const { reading } = screen;
150
+ const [page, setPage] = useState(undefined);
151
+ const [restoredAway, setRestoredAway] = useState(false);
152
+ // The related lists it links to, counted together — before anything returns early: the hook
153
+ // asks nothing while the record is not read.
154
+ const linked = reading.status === "ready" && !reading.isNew ? (links?.(reading.record) ?? []) : [];
155
+ const related = linked.filter(isRelated);
156
+ const counts = useCounts(related.map((one) => ({ entity: one.list, where: one.where })));
157
+ const phone = usePhone(api);
158
+ const settings = useFormStyle() === "settings";
159
+ // Its own lifecycle (`Delete`, the trash): once it is out of every reading the surface closes,
160
+ // and every list that showed it reads again by itself.
161
+ const lifecycle = useRecordLifecycleActions(reading, (done) => done === "restore" ? reading.reload() : api.close());
162
+ if (reading.status === "loading")
163
+ return _jsx(Skeleton, { rows: 6, label: frameworkCopy().record.loading });
164
+ if (reading.status === "failed")
165
+ return _jsx(Refused, { error: reading.error, onRetry: reading.reload });
166
+ const { meta, edit } = reading;
167
+ const layout = formOf(meta, entity.summary);
168
+ // What the header already says — the name, the state, the type — is not said again as a
169
+ // field, unless it can be typed in now: then the field is its control, not a repetition.
170
+ const parts = summaryFieldsOf(meta, reading.record, entity.summary);
171
+ const said = new Set([meta?.display, parts.state, parts.type]);
172
+ // An action may bring its own dialog (`DockedAction`): its action goes to the dock, its dialog
173
+ // is drawn by the screen — the screen calls its hook, never passes it.
174
+ const asked = actions?.(reading) ?? null;
175
+ const docked = Array.isArray(asked) ? asked.filter(isDocked) : [];
176
+ const own = actionsOf(Array.isArray(asked)
177
+ ? asked.map((one) => (isDocked(one) ? one.action : one))
178
+ : asked);
179
+ const state = {
180
+ screen: screen,
181
+ reading: reading,
182
+ fields: fields ??
183
+ layout.fields.filter((name) => reading.isNew || !said.has(name) || !edit.state(name).readonly),
184
+ // Chosen fields are the whole form: the embedded sections come only with the model's.
185
+ sections: fields === undefined ? layout.sections : [],
186
+ pages: pages ?? layout.pages,
187
+ page,
188
+ setPage,
189
+ // On a phone a form is one column: two side by side at 400 px squeezed a text area beside
190
+ // its word (web-ui's grid does not narrow by itself).
191
+ columns: phone || (api.surface !== "page" && api.surface !== "modal") ? 1 : 2,
192
+ phone,
193
+ settings,
194
+ docked: own.drawn === null ? [...own.dock, ...lifecycle.actions] : [],
195
+ drawn: own.drawn === null ? null : (_jsxs(_Fragment, { children: [own.drawn, _jsx(LifecycleButtons, { actions: lifecycle.actions })] })),
196
+ restoredAway,
197
+ putRestoredAway: () => setRestoredAway(true),
198
+ links: linked.map((one) => {
199
+ if (!isRelated(one))
200
+ return one;
201
+ const count = counts[related.indexOf(one)];
202
+ return {
203
+ icon: one.icon,
204
+ label: one.label,
205
+ ...(count === null || count === undefined ? {} : { count }),
206
+ onClick: () => api.open(one.list.listRef(one.where, one.title)),
207
+ };
208
+ }),
209
+ };
210
+ const tree = children ?? (_jsxs(_Fragment, { children: [_jsx(Rail, {}), _jsx(Paper, {}), _jsx(Activity, {})] }));
211
+ return (_jsxs(RecordScreenContext.Provider, { value: state, children: [_jsx(Record, { ...screen.record, children: resolveParts(tree, state) }), lifecycle.dialog, docked.map((one) => (_jsx(Fragment, { children: one.dialog }, one.action.id)))] }));
212
+ }
213
+ /**
214
+ * A record screen by parts: the framework's default, or any part of it replaced. See the top of
215
+ * this file.
216
+ */
217
+ export const RecordScreen = Object.assign(RecordScreenRoot, {
218
+ Rail,
219
+ Crumbs,
220
+ Pager,
221
+ Smart,
222
+ Actions,
223
+ Paper,
224
+ Notices,
225
+ Fields,
226
+ Sections,
227
+ Advice,
228
+ Pages,
229
+ Audit,
230
+ Activity,
231
+ });
232
+ /**
233
+ * The record of an entity, drawn from its model — the screen `entityView` opens when the
234
+ * application writes none: in the page, a modal, the preview or a panel, the same. It is
235
+ * `RecordScreen` with no tree of its own.
236
+ *
237
+ * entityView(invoices, { transport, name: "Facturas" }) // this screen
238
+ * entityView(invoices, { …, record: (props) =>
239
+ * <EntityRecord {...props} entity={invoices} fields={["customer_id", "due_date"]}
240
+ * actions={(invoice) => <CommandButton entity={invoices} command="post" … />} /> })
241
+ */
242
+ export function EntityRecord(props) {
243
+ return _jsx(RecordScreen, { ...props });
244
+ }
245
+ /** An action that brings its own dialog, told from one that is only data. */
246
+ function isDocked(one) {
247
+ return typeof one === "object" && one !== null && "action" in one && "dialog" in one;
248
+ }
249
+ function isRelated(one) {
250
+ return "list" in one;
251
+ }
@@ -5,9 +5,12 @@ import { type CacheOptions } from "../../core/cache.js";
5
5
  import type { Uploads } from "../../core/files.js";
6
6
  import type { DateStyle } from "../../core/present.js";
7
7
  import type { RichTextSettings } from "../../core/richText.js";
8
+ import { type People } from "../../react/context.js";
8
9
  import { type ReactNode } from "react";
9
- export declare function SincproApp({ transport, views, start, language, metas, entities, empty, skin, cache, timeZone, dateStyle, currency, region, richText, uploads, events, workspace: given, bar, onViewError, changes, }: {
10
- transport: Transport;
10
+ export declare function SincproApp({ transport, views, start, language, voice, metas, entities, empty, skin, cache, timeZone, dateStyle, currency, region, richText, uploads, people, events, workspace: given, bar, onViewError, changes, }: {
11
+ /** Who answers. Without it, the `FrameworkProvider` around the app — one provider for every
12
+ * face of a product. */
13
+ transport?: Transport;
11
14
  /** The entities as composer views — `entityView(…)` — and any other view of the app. */
12
15
  views: readonly ViewSpec[];
13
16
  /** The application's entities: a form draws a many2one of any of them without being told. */
@@ -16,6 +19,13 @@ export declare function SincproApp({ transport, views, start, language, metas, e
16
19
  start?: ResourceRef;
17
20
  /** The interface's language: web-ui's, the composer's and the framework's words. */
18
21
  language?: "es" | "en";
22
+ /**
23
+ * How the Spanish addresses the person: `vos` («Elegí», «podés») — the default —, or `tú`
24
+ * («Elige», «puedes»), what most of the region writes. Web-ui's and the framework's tables in
25
+ * the same voice: two voices on one screen read as two authors. The composer's Spanish never
26
+ * addresses the person, so it has one table.
27
+ */
28
+ voice?: "vos" | "tú";
19
29
  /** Definitions known before any request, by entity key. */
20
30
  metas?: Readonly<Record<string, Meta>>;
21
31
  /** Drawn when nothing is open. */
@@ -43,6 +53,8 @@ export declare function SincproApp({ transport, views, start, language, metas, e
43
53
  richText?: RichTextSettings;
44
54
  /** Where a file a person gives goes — the application's (`FrameworkProvider uploads`). */
45
55
  uploads?: Uploads;
56
+ /** Who is who: a record's history and authorship by name (`FrameworkProvider people`). */
57
+ people?: People;
46
58
  /** Each domain event's definition by its wire name (`eventMetasOf(contract)`): a history
47
59
  * names each fact's own fields in its words. */
48
60
  events?: Readonly<Record<string, Meta>>;