@sincpro/web-framework 3.6.0 → 3.8.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 (37) hide show
  1. package/README.md +4 -0
  2. package/dist/core/crumbs.d.ts +4 -0
  3. package/dist/core/crumbs.js +24 -0
  4. package/dist/core/files.d.ts +47 -0
  5. package/dist/core/files.js +35 -0
  6. package/dist/core/index.d.ts +1 -0
  7. package/dist/core/index.js +1 -0
  8. package/dist/core/present.d.ts +4 -3
  9. package/dist/core/present.js +9 -4
  10. package/dist/web/files.d.ts +9 -0
  11. package/dist/web/files.js +22 -0
  12. package/dist/web/index.d.ts +6 -3
  13. package/dist/web/index.js +6 -3
  14. package/dist/web/list/EntityList.d.ts +23 -6
  15. package/dist/web/list/EntityList.js +89 -21
  16. package/dist/web/list/columns.d.ts +4 -1
  17. package/dist/web/list/columns.js +31 -17
  18. package/dist/web/list/dock.d.ts +19 -0
  19. package/dist/web/list/dock.js +20 -0
  20. package/dist/web/list/groups.d.ts +7 -1
  21. package/dist/web/list/groups.js +25 -5
  22. package/dist/web/record/ActionButton.d.ts +25 -0
  23. package/dist/web/record/ActionButton.js +39 -18
  24. package/dist/web/record/CommandButton.d.ts +22 -2
  25. package/dist/web/record/CommandButton.js +43 -17
  26. package/dist/web/record/RecordLifecycle.d.ts +28 -7
  27. package/dist/web/record/RecordLifecycle.js +63 -10
  28. package/dist/web/record/ServiceButton.d.ts +20 -2
  29. package/dist/web/record/ServiceButton.js +35 -27
  30. package/dist/web/record/dock.d.ts +31 -0
  31. package/dist/web/record/dock.js +14 -0
  32. package/dist/web/record/edit.js +10 -1
  33. package/dist/web/record/record.d.ts +14 -3
  34. package/dist/web/record/record.js +39 -6
  35. package/dist/web/workspace/record.d.ts +8 -3
  36. package/dist/web/workspace/record.js +20 -16
  37. package/package.json +3 -3
@@ -1,6 +1,6 @@
1
1
  import type { AnyRecord, Meta } from "@sincpro/criteria";
2
2
  import type { ChoiceTones } from "./presentation.js";
3
- import { type Aggregate, type DataColumn } from "@sincpro/web-ui";
3
+ import { type Aggregate, type DataColumn, type YesNoColumn } from "@sincpro/web-ui";
4
4
  import type { ReactNode } from "react";
5
5
  export type ColumnOptions<T> = {
6
6
  locale?: string | undefined;
@@ -8,6 +8,9 @@ export type ColumnOptions<T> = {
8
8
  cells?: Readonly<Partial<Record<string, (row: T) => ReactNode>>>;
9
9
  /** The colour of each value of a choice (`presented(entity, { tones })`). */
10
10
  tones?: ChoiceTones<T> | undefined;
11
+ /** How a yes/no column reads — web-ui's `yesNo`: `check` (the default here: ✓ and nothing),
12
+ * `icon`, `word` — or, with `onToggle`, a `checkbox` / `switch` that writes it from the list. */
13
+ yesNo?: Readonly<Partial<Record<string, YesNoColumn<T>>>> | undefined;
11
14
  };
12
15
  /** How the model adds a field up at a list's foot, as web-ui's `aggregate`. */
13
16
  export declare function aggregateOf(meta: Meta | null | undefined, name: string): Aggregate | undefined;
@@ -1,13 +1,13 @@
1
1
  "use client";
2
2
  import { Fragment as _Fragment, jsx as _jsx } from "react/jsx-runtime";
3
- import { frameworkCopy } from "../../core/copy.js";
3
+ import { holdsFile } from "../../core/files.js";
4
4
  import { totalOf } from "../../core/measure.js";
5
5
  import { BOOKKEEPING, defaultFields, fieldMetaOf, sortKeyOf, textOf, valueAt, viewOf, } from "../../core/present.js";
6
6
  import { relationFieldOf } from "../../core/relation.js";
7
7
  import { useEntityFor } from "../../react/context.js";
8
8
  import { useNameOf } from "../../react/useNameOf.js";
9
- import { Tag } from "@sincpro/web-ui";
10
- import { Check } from "lucide-react";
9
+ import { attachmentOf } from "../files.js";
10
+ import { FileValue, Tag, } from "@sincpro/web-ui";
11
11
  /**
12
12
  * The columns of a table, from the model: name the fields, the rest follows.
13
13
  *
@@ -58,6 +58,9 @@ function columnOf(meta, name, options, hidden) {
58
58
  const whole = view.name === meta?.identity || view.numeric || money || DATES.has(view.type);
59
59
  const tones = options.tones?.[name];
60
60
  const choice = (field?.choices?.length ?? 0) > 0;
61
+ const boolean = view.type === "boolean";
62
+ // A file — a photo, a scan — is ONE value: its thumbnail and the name it was sent with.
63
+ const file = holdsFile(field);
61
64
  const aggregate = aggregateOf(meta, name);
62
65
  // The column IS a key of another record: `customer_id` beside its relation, or a key alone.
63
66
  const relation = relationFieldOf(meta, name);
@@ -78,23 +81,34 @@ function columnOf(meta, name, options, hidden) {
78
81
  id: name,
79
82
  header: view.label,
80
83
  field: name,
81
- value: (row) => sortKeyOf(meta, name, raw(row)),
84
+ value: file ? text : (row) => sortKeyOf(meta, name, raw(row)),
82
85
  cell: own ??
83
86
  named ??
84
- (choice
85
- ? // A state reads at a glance: its word in a tag, in the tone the application gave it.
86
- (row) => {
87
- const word = text(row);
88
- return word === "" ? null : (_jsx(Tag, { tone: tones?.[String(raw(row))] ?? "neutral", children: word }));
89
- }
90
- : view.type === "boolean"
91
- ? // A yes is a check and a no is nothing: a column of «sí/no» is noise to scan.
92
- (row) => raw(row) === true ? (_jsx(Check, { role: "img", "aria-label": frameworkCopy().value.yes, className: "size-(--size-icon-sm)" })) : null
93
- : whole
94
- ? (row) => _jsx("span", { className: "whitespace-nowrap", children: text(row) })
95
- : text),
96
- text,
87
+ (file
88
+ ? (row) => _jsx(FileValue, { file: attachmentOf(raw(row)) })
89
+ : choice
90
+ ? // A state reads at a glance: its word in a tag, in the tone the application gave it.
91
+ (row) => {
92
+ const word = text(row);
93
+ return word === "" ? null : (_jsx(Tag, { tone: tones?.[String(raw(row))] ?? "neutral", children: word }));
94
+ }
95
+ : boolean
96
+ ? // Drawn by web-ui from the value (`yesNo`, below): no cell of its own.
97
+ undefined
98
+ : whole
99
+ ? (row) => _jsx("span", { className: "whitespace-nowrap", children: text(row) })
100
+ : text),
101
+ // A yes/no reads by its look (web-ui's `YesNo`): with a `text` of its own the table would
102
+ // draw the words instead.
103
+ ...(boolean ? {} : { text }),
97
104
  numeric: view.numeric || money,
105
+ ...(boolean
106
+ ? {
107
+ // A yes is a check and a no is nothing by default: a column of «sí/no» is noise to
108
+ // scan. Every look still says «sí» / «no» to a screen reader.
109
+ yesNo: { look: "check", ...options.yesNo?.[name] },
110
+ }
111
+ : {}),
98
112
  sortable: view.sortable,
99
113
  ...(view.name === meta?.identity ? { mono: true } : {}),
100
114
  ...(hidden ? { optional: true, defaultHidden: true } : {}),
@@ -0,0 +1,19 @@
1
+ import type { DockAction } from "@sincpro/web-ui";
2
+ import { type ReactNode } from "react";
3
+ /**
4
+ * A screen's actions: as DATA — web-ui's `DockAction`s, which the application's view style
5
+ * draws (the bar's buttons in `traditional`, one pill floating at the bottom in `modern`) — or
6
+ * drawn by hand, a node that stays where it is put.
7
+ *
8
+ * actions={[{ id: "import", label: "Importar", icon: Upload, onSelect: importar }]}
9
+ * actions={<ImportarColegiados />} // its own dialog: drawn by hand, in the bar
10
+ */
11
+ export type ScreenActions = readonly DockAction[] | ReactNode;
12
+ /**
13
+ * The actions split by who draws them: `dock`, the ones given as data, for the view style;
14
+ * `drawn`, a node drawn by hand, to put where it goes — `null` when there is none.
15
+ */
16
+ export declare function actionsOf(actions: ScreenActions): {
17
+ dock: readonly DockAction[];
18
+ drawn: ReactNode;
19
+ };
@@ -0,0 +1,20 @@
1
+ import { isValidElement } from "react";
2
+ /** One action as data: an object with its id and its words, not an element. */
3
+ function isDockAction(one) {
4
+ return (typeof one === "object" &&
5
+ one !== null &&
6
+ !isValidElement(one) &&
7
+ "id" in one &&
8
+ "label" in one);
9
+ }
10
+ /**
11
+ * The actions split by who draws them: `dock`, the ones given as data, for the view style;
12
+ * `drawn`, a node drawn by hand, to put where it goes — `null` when there is none.
13
+ */
14
+ export function actionsOf(actions) {
15
+ if (actions === undefined || actions === null || actions === false)
16
+ return { dock: [], drawn: null };
17
+ if (Array.isArray(actions) && actions.every(isDockAction))
18
+ return { dock: actions, drawn: null };
19
+ return { dock: [], drawn: actions };
20
+ }
@@ -17,10 +17,16 @@ export declare function groupableOf(meta: Meta | null, locale?: string, only?: r
17
17
  /**
18
18
  * The server's groups as piles, with the words a person reads, and each pile's group so that
19
19
  * opening one reads its rows. Rows sit only on the deepest level, once read.
20
+ *
21
+ * What the server summed for a group is its `totals` — web-ui (2.9) says each figure under its
22
+ * column in a grouped table and a sheet, as Odoo's grouped list — and, in words beside the
23
+ * group's name (`detail`), what no column on screen says: a board's card has no columns.
20
24
  */
21
25
  export declare function pilesOf<T extends AnyRecord>(buckets: readonly Bucket<T>[], levels: readonly GroupLevel[], meta: Meta | null, locale: string | undefined, rowsOf: (bucket: Bucket<T>) => readonly T[] | undefined,
22
26
  /** What a group's next «more» brings (`Pile.more`); omitted, web-ui says nothing about it. */
23
- brings?: (bucket: Bucket<T>) => number | false | undefined): {
27
+ brings?: (bucket: Bucket<T>) => number | false | undefined,
28
+ /** The figures a column on screen already says under itself: not said again in words. */
29
+ underColumns?: ReadonlySet<string>): {
24
30
  piles: Pile<T>[];
25
31
  bucketAt: ReadonlyMap<string, Bucket<T>>;
26
32
  deepest: number;
@@ -42,13 +42,24 @@ export function groupableOf(meta, locale, only) {
42
42
  ...(field.type === "date" || field.type === "datetime" ? { grains: DATE_GRAINS } : {}),
43
43
  }));
44
44
  }
45
+ /** A summed figure as a number — the wire may carry an exact decimal as text («1204.50»). */
46
+ function figureOf(value) {
47
+ const figure = typeof value === "number" ? value : typeof value === "string" ? Number(value) : NaN;
48
+ return value === "" || !Number.isFinite(figure) ? undefined : figure;
49
+ }
45
50
  /**
46
51
  * The server's groups as piles, with the words a person reads, and each pile's group so that
47
52
  * opening one reads its rows. Rows sit only on the deepest level, once read.
53
+ *
54
+ * What the server summed for a group is its `totals` — web-ui (2.9) says each figure under its
55
+ * column in a grouped table and a sheet, as Odoo's grouped list — and, in words beside the
56
+ * group's name (`detail`), what no column on screen says: a board's card has no columns.
48
57
  */
49
58
  export function pilesOf(buckets, levels, meta, locale, rowsOf,
50
59
  /** What a group's next «more» brings (`Pile.more`); omitted, web-ui says nothing about it. */
51
- brings) {
60
+ brings,
61
+ /** The figures a column on screen already says under itself: not said again in words. */
62
+ underColumns = new Set()) {
52
63
  const bucketAt = new Map();
53
64
  const none = frameworkCopy().list.noValue;
54
65
  const labelFor = (bucket, depth, key) => {
@@ -63,9 +74,18 @@ brings) {
63
74
  }
64
75
  return textOf(meta, bucket.field, bucket.value, locale) || key;
65
76
  };
66
- const detailed = (bucket) => {
67
- const detail = detailOf(meta, bucket.measures, locale);
68
- return detail === undefined ? {} : { detail };
77
+ const summed = (bucket) => {
78
+ const measures = Object.entries(bucket.measures ?? {});
79
+ const totals = Object.fromEntries(measures.flatMap(([field, value]) => {
80
+ const figure = figureOf(value);
81
+ return figure === undefined ? [] : [[field, figure]];
82
+ }));
83
+ const inWords = measures.filter(([field]) => !underColumns.has(field));
84
+ const detail = detailOf(meta, Object.fromEntries(inWords), locale);
85
+ return {
86
+ ...(Object.keys(totals).length === 0 ? {} : { totals }),
87
+ ...(detail === undefined ? {} : { detail }),
88
+ };
69
89
  };
70
90
  const more = (bucket, deepest) => {
71
91
  const next = deepest ? brings?.(bucket) : undefined;
@@ -83,7 +103,7 @@ brings) {
83
103
  key,
84
104
  label: labelFor(bucket, depth, key),
85
105
  count: bucket.count,
86
- ...detailed(bucket),
106
+ ...summed(bucket),
87
107
  items: below.length === 0 ? [...(rowsOf(bucket) ?? [])] : [],
88
108
  piles: walk(below, path, depth + 1),
89
109
  ...more(bucket, below.length === 0),
@@ -1,5 +1,6 @@
1
1
  import type { Rehearsal } from "../../react/useCommand.js";
2
2
  import type { AskForm } from "./ask.js";
3
+ import { type ReactNode } from "react";
3
4
  /** What a person filled in before the action runs, by field name. */
4
5
  export type Asked = Readonly<Record<string, unknown>>;
5
6
  /** What an action's window says, when the screen says it its way. */
@@ -54,3 +55,27 @@ export declare function ActionButton<R>({ action, label, role, disabled, ask: fo
54
55
  dialog?: ActionDialog;
55
56
  onDone?: (answer: R) => void;
56
57
  }): import("react").JSX.Element;
58
+ /** An action's gesture, for whatever starts it — a button, a dock's action, a shortcut. */
59
+ export type ActionGesture = {
60
+ /** Starts it: opens what it asks, asks first what it would do (it destroys), or runs. */
61
+ start: () => void;
62
+ /** It runs: not offered again until it ends. */
63
+ busy: boolean;
64
+ /** Its window — what it asks, what it would do, a refusal —; `null` while closed. Put it
65
+ * anywhere on the screen: it opens over everything. */
66
+ dialog: ReactNode;
67
+ };
68
+ /**
69
+ * `ActionButton`'s gesture without the button: started by anything, its window put where the
70
+ * screen puts it.
71
+ *
72
+ * Context: web-ui's dock (`Record.Dock`, the modern style's floating pill) takes actions as
73
+ * data — a label and an `onSelect` —, not buttons. The gesture is the same: `start` is the
74
+ * button's click, and `dialog` the window it opened.
75
+ */
76
+ export declare function useActionGesture<R>(action: ButtonAction<R>, { label, ask: form, dialog, onDone, }: {
77
+ label: string;
78
+ ask?: AskForm;
79
+ dialog?: ActionDialog;
80
+ onDone?: (answer: R) => void;
81
+ }): ActionGesture;
@@ -65,36 +65,57 @@ function AskDialog({ action, form, words, onClose, onDone, }) {
65
65
  * Internal: the gesture is drawn here once; what runs behind is each button's own.
66
66
  */
67
67
  export function ActionButton({ action, label, role = "secondary", disabled = false, ask: form, dialog, onDone, }) {
68
+ const gesture = useActionGesture(action, {
69
+ label,
70
+ ...(form === undefined ? {} : { ask: form }),
71
+ ...(dialog === undefined ? {} : { dialog }),
72
+ ...(onDone === undefined ? {} : { onDone }),
73
+ });
74
+ // One that destroys is never the gold button: what it would do is read first.
75
+ const destructive = form === undefined && action.rehearse !== undefined;
76
+ return (_jsxs(_Fragment, { children: [_jsx(Button, { size: "sm", role: destructive ? "secondary" : role, disabled: disabled || gesture.busy, onClick: gesture.start, children: label }), gesture.dialog] }));
77
+ }
78
+ /**
79
+ * `ActionButton`'s gesture without the button: started by anything, its window put where the
80
+ * screen puts it.
81
+ *
82
+ * Context: web-ui's dock (`Record.Dock`, the modern style's floating pill) takes actions as
83
+ * data — a label and an `onSelect` —, not buttons. The gesture is the same: `start` is the
84
+ * button's click, and `dialog` the window it opened.
85
+ */
86
+ export function useActionGesture(action, { label, ask: form, dialog, onDone, }) {
68
87
  const copy = frameworkCopy().command;
69
88
  const [open, setOpen] = useState(false);
70
89
  const words = wordsOf(label, dialog);
90
+ const close = () => {
91
+ setOpen(false);
92
+ action.reset();
93
+ };
94
+ const done = (answer) => {
95
+ close();
96
+ onDone?.(answer);
97
+ };
71
98
  if (form !== undefined)
72
- return (_jsxs(_Fragment, { children: [_jsx(Button, { size: "sm", role: role, disabled: disabled || action.busy, onClick: () => setOpen(true), children: label }), open ? (_jsx(AskDialog, { action: action, form: form, words: words, onClose: () => {
73
- setOpen(false);
74
- action.reset();
75
- }, onDone: (answer) => {
76
- setOpen(false);
77
- action.reset();
78
- onDone?.(answer);
79
- } })) : null] }));
99
+ return {
100
+ start: () => setOpen(true),
101
+ busy: action.busy,
102
+ dialog: open ? (_jsx(AskDialog, { action: action, form: form, words: words, onClose: close, onDone: done })) : null,
103
+ };
80
104
  const destructive = action.rehearse !== undefined;
81
105
  const run = async () => {
82
106
  const answer = await action.run();
83
- if (answer !== undefined) {
84
- setOpen(false);
85
- action.reset();
86
- onDone?.(answer);
87
- }
107
+ if (answer !== undefined)
108
+ done(answer);
88
109
  else
89
110
  setOpen(true);
90
111
  };
91
- const ask = async () => {
112
+ const rehearse = async () => {
92
113
  setOpen(true);
93
114
  await action.rehearse?.();
94
115
  };
95
- const close = () => {
96
- setOpen(false);
97
- action.reset();
116
+ return {
117
+ start: () => void (destructive ? rehearse() : run()),
118
+ busy: action.busy,
119
+ dialog: open ? (_jsx(Dialog, { open: true, ...words, onClose: close, footer: destructive && action.error === null ? (_jsxs("div", { className: "flex justify-end gap-2", children: [_jsx(Button, { size: "sm", role: "secondary", onClick: close, children: copy.cancel }), _jsx(Button, { size: "sm", role: "primary", disabled: action.busy || action.rehearsal === null, onClick: () => void run(), children: copy.confirm })] })) : undefined, children: action.error !== null ? (_jsx(Refused, { error: action.error })) : action.rehearsal === null ? (_jsx("p", { className: "text-meta text-muted", children: copy.rehearsing })) : (_jsx("p", { className: "text-ink", children: action.rehearsal.text })) })) : null,
98
120
  };
99
- return (_jsxs(_Fragment, { children: [_jsx(Button, { size: "sm", role: destructive ? "secondary" : role, disabled: disabled || action.busy, onClick: () => void (destructive ? ask() : run()), children: label }), open ? (_jsx(Dialog, { open: true, ...words, onClose: close, footer: destructive && action.error === null ? (_jsxs("div", { className: "flex justify-end gap-2", children: [_jsx(Button, { size: "sm", role: "secondary", onClick: close, children: copy.cancel }), _jsx(Button, { size: "sm", role: "primary", disabled: action.busy || action.rehearsal === null, onClick: () => void run(), children: copy.confirm })] })) : undefined, children: action.error !== null ? (_jsx(Refused, { error: action.error })) : action.rehearsal === null ? (_jsx("p", { className: "text-meta text-muted", children: copy.rehearsing })) : (_jsx("p", { className: "text-ink", children: action.rehearsal.text })) })) : null] }));
100
121
  }
@@ -3,6 +3,7 @@ import type { AnyRecord } from "@sincpro/criteria";
3
3
  import { type AnswerOf } from "../../react/useCommand.js";
4
4
  import { type ActionDialog } from "./ActionButton.js";
5
5
  import { type AskField } from "./ask.js";
6
+ import { type ActionLook, type DockedAction } from "./dock.js";
6
7
  /**
7
8
  * An entity's Command as a button: sent as it is, over the screens' transport.
8
9
  *
@@ -15,7 +16,8 @@ import { type AskField } from "./ask.js";
15
16
  * record's fields of the same name, filled from `record`, and what is filled goes in the body
16
17
  * beside `body`.
17
18
  */
18
- export declare function CommandButton<T extends AnyRecord, R = unknown, C extends CommandBodies = CommandBodies, N extends keyof C & string = keyof C & string, A extends CommandAnswers = CommandAnswers>({ entity, command, id, body, label, destructive, role, disabled, ask, record, dialog, onDone, }: {
19
+ /** What a Command's button and dock action are given. */
20
+ export type CommandActionProps<T extends AnyRecord, R, C extends CommandBodies, N extends keyof C & string, A extends CommandAnswers> = {
19
21
  entity: Entity<T, C, A>;
20
22
  /** One the entity declares; a typed entity takes only its own names. */
21
23
  command: N;
@@ -25,7 +27,6 @@ export declare function CommandButton<T extends AnyRecord, R = unknown, C extend
25
27
  label: string;
26
28
  /** Asks the server what it would do before doing it. */
27
29
  destructive?: boolean;
28
- role?: "primary" | "secondary" | "ghost";
29
30
  /** Not offered right now — «Mover» with no other tenant chosen. The server still decides. */
30
31
  disabled?: boolean;
31
32
  /** The fields it asks for before it runs, sent in its body: `["areas"]`. */
@@ -35,4 +36,23 @@ export declare function CommandButton<T extends AnyRecord, R = unknown, C extend
35
36
  /** What its window says — its title (the label without «…» by default), a sentence under it. */
36
37
  dialog?: ActionDialog;
37
38
  onDone?: (answer: AnswerOf<R, A, N>) => void;
39
+ };
40
+ export declare function CommandButton<T extends AnyRecord, R = unknown, C extends CommandBodies = CommandBodies, N extends keyof C & string = keyof C & string, A extends CommandAnswers = CommandAnswers>({ role, ...props }: CommandActionProps<T, R, C, N, A> & {
41
+ role?: "primary" | "secondary" | "ghost";
38
42
  }): import("react").JSX.Element;
43
+ /**
44
+ * An entity's Command as an action of a dock — what `CommandButton` is as a button, for web-ui's
45
+ * `Record.Dock`: the view style draws it, and its window is put on the screen by whoever uses
46
+ * it. One that destroys (`destructive`: asked first what it would do) is drawn as such — apart,
47
+ * in red — unless `danger` says otherwise.
48
+ *
49
+ * const anular = useCommandAction({
50
+ * entity: invoices, command: "cancel", id: invoice.id, label: "Anular", destructive: true,
51
+ * });
52
+ * <Record.Dock actions={[confirmar.action, anular.action]} />
53
+ * {anular.dialog}
54
+ */
55
+ export declare function useCommandAction<T extends AnyRecord, R = unknown, C extends CommandBodies = CommandBodies, N extends keyof C & string = keyof C & string, A extends CommandAnswers = CommandAnswers>({ icon, primary, danger, tucked, shortcut, hint, actionId, ...props }: CommandActionProps<T, R, C, N, A> & Omit<ActionLook, "id"> & {
56
+ /** The dock's id for it — the record's is `id`. Defaults to the label. */
57
+ actionId?: string;
58
+ }): DockedAction;
@@ -4,21 +4,46 @@ import {} from "@sincpro/client-framework";
4
4
  import { entityFor } from "../../core/relation.js";
5
5
  import { useFramework } from "../../react/context.js";
6
6
  import { useCommand } from "../../react/useCommand.js";
7
- import { ActionButton, } from "./ActionButton.js";
8
- import { askFormOf } from "./ask.js";
7
+ import { ActionButton, useActionGesture, } from "./ActionButton.js";
8
+ import { askFormOf, } from "./ask.js";
9
+ import { dockedOf, } from "./dock.js";
10
+ export function CommandButton({ role = "secondary", ...props }) {
11
+ const { action, form } = useCommandBehind(props);
12
+ return (_jsx(ActionButton, { ...(form === undefined ? {} : { ask: form }), ...(props.dialog === undefined ? {} : { dialog: props.dialog }), action: action, label: props.label, role: role, disabled: props.disabled ?? false, ...(props.onDone === undefined ? {} : { onDone: props.onDone }) }));
13
+ }
9
14
  /**
10
- * An entity's Command as a button: sent as it is, over the screens' transport.
11
- *
12
- * Context: the gesture is `ActionButton`'s — run on the click, or (`destructive`) ask the
13
- * server's dry run first and show what it would do; a refusal where it happened. An action that is
14
- * one of the application's own use cases — logic of its own, composed, traced as
15
- * `<context>/<Service>` — is a `ServiceButton`.
15
+ * An entity's Command as an action of a dock — what `CommandButton` is as a button, for web-ui's
16
+ * `Record.Dock`: the view style draws it, and its window is put on the screen by whoever uses
17
+ * it. One that destroys (`destructive`: asked first what it would do) is drawn as such — apart,
18
+ * in red — unless `danger` says otherwise.
16
19
  *
17
- * One that needs input names the fields it asks for (`ask`): a dialog draws them as the
18
- * record's fields of the same name, filled from `record`, and what is filled goes in the body
19
- * beside `body`.
20
+ * const anular = useCommandAction({
21
+ * entity: invoices, command: "cancel", id: invoice.id, label: "Anular", destructive: true,
22
+ * });
23
+ * <Record.Dock actions={[confirmar.action, anular.action]} />
24
+ * {anular.dialog}
20
25
  */
21
- export function CommandButton({ entity, command, id, body, label, destructive = false, role = "secondary", disabled = false, ask, record, dialog, onDone, }) {
26
+ export function useCommandAction({ icon, primary, danger, tucked, shortcut, hint, actionId, ...props }) {
27
+ const { action, form } = useCommandBehind(props);
28
+ const gesture = useActionGesture(action, {
29
+ label: props.label,
30
+ ...(form === undefined ? {} : { ask: form }),
31
+ ...(props.dialog === undefined ? {} : { dialog: props.dialog }),
32
+ ...(props.onDone === undefined ? {} : { onDone: props.onDone }),
33
+ });
34
+ const look = {
35
+ icon,
36
+ primary,
37
+ danger: danger ?? props.destructive,
38
+ tucked,
39
+ shortcut,
40
+ hint,
41
+ id: actionId,
42
+ };
43
+ return dockedOf(gesture, props.label, look, props.disabled);
44
+ }
45
+ /** What runs behind a Command's button or dock action: sending it, its dry run, what it asks. */
46
+ function useCommandBehind({ entity, command, id, body, destructive = false, ask, record, }) {
22
47
  const sending = useCommand(entity, command);
23
48
  const { metas, entities, locale } = useFramework();
24
49
  const form = ask === undefined
@@ -30,14 +55,15 @@ export function CommandButton({ entity, command, id, body, label, destructive =
30
55
  entityFor: (target) => entityFor(entities, target, metas.get),
31
56
  });
32
57
  const bodyWith = (asked) => (asked === undefined ? body : { ...body, ...asked });
33
- return (_jsx(ActionButton, { ...(form === undefined ? {} : { ask: form }), ...(dialog === undefined ? {} : { dialog }), action: {
58
+ return {
59
+ action: {
34
60
  run: (asked) => sending.run(id, bodyWith(asked)),
35
- ...(destructive
36
- ? { rehearse: (asked) => sending.rehearse(id, bodyWith(asked)) }
37
- : {}),
61
+ ...(destructive ? { rehearse: (asked) => sending.rehearse(id, bodyWith(asked)) } : {}),
38
62
  busy: sending.busy,
39
63
  error: sending.error,
40
64
  rehearsal: sending.rehearsal,
41
65
  reset: sending.reset,
42
- }, label: label, role: role, disabled: disabled, ...(onDone === undefined ? {} : { onDone }) }));
66
+ },
67
+ form,
68
+ };
43
69
  }
@@ -1,5 +1,9 @@
1
1
  import type { AnyRecord } from "@sincpro/criteria";
2
2
  import type { RecordReading } from "../../react/useRecord.js";
3
+ import { type DockAction } from "@sincpro/web-ui";
4
+ import type { ReactNode } from "react";
5
+ /** What the record's lifecycle did: after `delete` and `archive` it is out of every reading. */
6
+ export type LifecycleDone = "delete" | "archive" | "restore";
3
7
  /**
4
8
  * The record's own lifecycle as buttons — «Mover a la papelera» / «Restaurar» and «Eliminar»,
5
9
  * each when its entity declares sincpro_framework's `Archive`, `Restore`, `Delete` — a deletion
@@ -11,13 +15,30 @@ import type { RecordReading } from "../../react/useRecord.js";
11
15
  * <RecordLifecycle record={member} onDone={() => api.close()} />
12
16
  * </Record.Actions>
13
17
  *
14
- * Context: `EntityRecord` draws it; a screen of its own draws it the same, or drops to
15
- * `useRecordLifecycle` and draws its own. What happens after is the screen's (`onDone`): in a
16
- * workspace the surface closes — the record is gone —, and every list that showed it reads
17
- * again by itself (the write is announced).
18
+ * Context: `EntityRecord` draws it; a screen of its own draws it the same, or takes them as a
19
+ * dock's actions (`useRecordLifecycleActions`), or drops to `useRecordLifecycle` and draws its
20
+ * own. What happens after is the screen's (`onDone`): in a workspace the surface closes — the
21
+ * record is gone —, and every list that showed it reads again by itself (the write is announced).
18
22
  */
19
23
  export declare function RecordLifecycle<T extends AnyRecord>({ record, onDone, }: {
20
24
  record: RecordReading<T>;
21
- /** Done: which action — after `delete` and `archive` the record is out of every reading. */
22
- onDone?: (action: "delete" | "archive" | "restore") => void;
23
- }): import("react").JSX.Element | null;
25
+ onDone?: (done: LifecycleDone) => void;
26
+ }): import("react").JSX.Element;
27
+ /** The lifecycle's actions as the rail's buttons — beside actions a screen drew by hand. */
28
+ export declare function LifecycleButtons({ actions }: {
29
+ actions: readonly DockAction[];
30
+ }): import("react").JSX.Element;
31
+ /**
32
+ * The record's own lifecycle as actions of a dock (web-ui's `Record.Dock`) — what
33
+ * `RecordLifecycle` draws as buttons: the view style draws them (the rail's buttons, or the
34
+ * modern style's floating pill, «Eliminar» apart and in red), and their window — what a deletion
35
+ * would do, a refusal — is put on the screen by whoever uses them.
36
+ *
37
+ * const lifecycle = useRecordLifecycleActions(member, () => api.close());
38
+ * <Record.Dock actions={[...own, ...lifecycle.actions]} />
39
+ * {lifecycle.dialog}
40
+ */
41
+ export declare function useRecordLifecycleActions<T extends AnyRecord>(record: RecordReading<T>, onDone?: (done: LifecycleDone) => void): {
42
+ actions: readonly DockAction[];
43
+ dialog: ReactNode;
44
+ };
@@ -1,8 +1,19 @@
1
1
  "use client";
2
- import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
3
3
  import { frameworkCopy } from "../../core/copy.js";
4
4
  import { useRecordLifecycle } from "../../react/useRecordLifecycle.js";
5
- import { ActionButton } from "./ActionButton.js";
5
+ import { useActionGesture, } from "./ActionButton.js";
6
+ import { dockedOf } from "./dock.js";
7
+ import { Button } from "@sincpro/web-ui";
8
+ import { Archive, RotateCcw, Trash2 } from "lucide-react";
9
+ /** In place of an action the entity does not declare: never offered, never started. */
10
+ const NOT_DECLARED = {
11
+ run: async () => undefined,
12
+ busy: false,
13
+ error: null,
14
+ rehearsal: null,
15
+ reset: () => { },
16
+ };
6
17
  /**
7
18
  * The record's own lifecycle as buttons — «Mover a la papelera» / «Restaurar» and «Eliminar»,
8
19
  * each when its entity declares sincpro_framework's `Archive`, `Restore`, `Delete` — a deletion
@@ -14,16 +25,58 @@ import { ActionButton } from "./ActionButton.js";
14
25
  * <RecordLifecycle record={member} onDone={() => api.close()} />
15
26
  * </Record.Actions>
16
27
  *
17
- * Context: `EntityRecord` draws it; a screen of its own draws it the same, or drops to
18
- * `useRecordLifecycle` and draws its own. What happens after is the screen's (`onDone`): in a
19
- * workspace the surface closes — the record is gone —, and every list that showed it reads
20
- * again by itself (the write is announced).
28
+ * Context: `EntityRecord` draws it; a screen of its own draws it the same, or takes them as a
29
+ * dock's actions (`useRecordLifecycleActions`), or drops to `useRecordLifecycle` and draws its
30
+ * own. What happens after is the screen's (`onDone`): in a workspace the surface closes — the
31
+ * record is gone —, and every list that showed it reads again by itself (the write is announced).
21
32
  */
22
33
  export function RecordLifecycle({ record, onDone, }) {
34
+ const lifecycle = useRecordLifecycleActions(record, onDone);
35
+ return (_jsxs(_Fragment, { children: [_jsx(LifecycleButtons, { actions: lifecycle.actions }), lifecycle.dialog] }));
36
+ }
37
+ /** The lifecycle's actions as the rail's buttons — beside actions a screen drew by hand. */
38
+ export function LifecycleButtons({ actions }) {
39
+ return (_jsx(_Fragment, { children: actions.map((one) => (_jsx(Button, { size: "sm", role: "secondary", disabled: one.disabled ?? false, onClick: () => one.onSelect?.(), children: one.label }, one.id))) }));
40
+ }
41
+ /**
42
+ * The record's own lifecycle as actions of a dock (web-ui's `Record.Dock`) — what
43
+ * `RecordLifecycle` draws as buttons: the view style draws them (the rail's buttons, or the
44
+ * modern style's floating pill, «Eliminar» apart and in red), and their window — what a deletion
45
+ * would do, a refusal — is put on the screen by whoever uses them.
46
+ *
47
+ * const lifecycle = useRecordLifecycleActions(member, () => api.close());
48
+ * <Record.Dock actions={[...own, ...lifecycle.actions]} />
49
+ * {lifecycle.dialog}
50
+ */
51
+ export function useRecordLifecycleActions(record, onDone) {
23
52
  const lifecycle = useRecordLifecycle(record);
24
53
  const copy = frameworkCopy().record;
25
- const shown = ["archive", "restore", "delete"].filter((one) => lifecycle[one] !== undefined);
26
- if (shown.length === 0)
27
- return null;
28
- return (_jsx(_Fragment, { children: shown.map((one) => (_jsx(ActionButton, { action: lifecycle[one], label: copy[one], onDone: () => onDone?.(one) }, one))) }));
54
+ // Always the three, in this order: a hook is not skipped. One not declared is not offered.
55
+ const archive = useActionGesture(lifecycle.archive ?? NOT_DECLARED, {
56
+ label: copy.archive,
57
+ onDone: () => onDone?.("archive"),
58
+ });
59
+ const restore = useActionGesture(lifecycle.restore ?? NOT_DECLARED, {
60
+ label: copy.restore,
61
+ onDone: () => onDone?.("restore"),
62
+ });
63
+ const remove = useActionGesture(lifecycle.delete ?? NOT_DECLARED, {
64
+ label: copy.delete,
65
+ onDone: () => onDone?.("delete"),
66
+ });
67
+ const offered = [
68
+ lifecycle.archive === undefined
69
+ ? null
70
+ : dockedOf(archive, copy.archive, { id: "archive", icon: Archive }),
71
+ lifecycle.restore === undefined
72
+ ? null
73
+ : dockedOf(restore, copy.restore, { id: "restore", icon: RotateCcw }),
74
+ lifecycle.delete === undefined
75
+ ? null
76
+ : dockedOf(remove, copy.delete, { id: "delete", icon: Trash2, danger: true }),
77
+ ].filter((one) => one !== null);
78
+ return {
79
+ actions: offered.map((one) => one.action),
80
+ dialog: offered.map((one) => one.dialog),
81
+ };
29
82
  }