@sincpro/web-framework 2.4.0 → 2.5.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.
@@ -1,5 +1,6 @@
1
- import { type Entity, type MetaStore, type Transport } from "@sincpro/client-framework";
1
+ import { type ChangeFeed, type Entity, type MetaStore, type Transport } from "@sincpro/client-framework";
2
2
  import type { Meta } from "@sincpro/criteria";
3
+ import { type NameBook } from "../core/names.js";
3
4
  import type { Trail } from "../core/trail.js";
4
5
  import { type ReactNode } from "react";
5
6
  /**
@@ -19,6 +20,12 @@ import { type ReactNode } from "react";
19
20
  * process's context (`processContext.set({ company_id })`, @sincpro/client-framework): every
20
21
  * request under this provider carries it in `sincpro-context` (`screensTransport`), and a change
21
22
  * reads every screen again.
23
+ *
24
+ * What was written is the process's too (`processChanges`, @sincpro/client-framework): every write
25
+ * a transport of the framework sends is announced there, whoever sent it — a screen, or a use
26
+ * case's generated client. A reading on screen follows it (`useWrites`) and reads again when a
27
+ * write touches what it shows; the names in cells (`useNameOf`) are asked through one book per
28
+ * transport and company (`nameBook`), and forget what a write touches.
22
29
  */
23
30
  type FrameworkContextValue = {
24
31
  transport: Transport;
@@ -30,8 +37,12 @@ type FrameworkContextValue = {
30
37
  trails: Map<string, Trail>;
31
38
  /** The process's context now: a reading reads again when it changes (another company). */
32
39
  context: Readonly<Record<string, unknown>>;
40
+ /** Where writes are announced: a reading reads again when one touches it (`useWrites`). */
41
+ changes: ChangeFeed;
42
+ /** The names of records by key, asked one request per entity (`useNameOf`). */
43
+ names: NameBook;
33
44
  };
34
- export declare function FrameworkProvider({ transport, locale, metas: seed, entities, timeZone, children, }: {
45
+ export declare function FrameworkProvider({ transport, locale, metas: seed, entities, timeZone, changes, children, }: {
35
46
  transport: Transport;
36
47
  /** The language labels and values are read in (`"es"`); the model's `default` without it. */
37
48
  locale?: string;
@@ -44,6 +55,8 @@ export declare function FrameworkProvider({ transport, locale, metas: seed, enti
44
55
  * the user works in: every date and time reads and is typed on its wall clock. Without it,
45
56
  * the browser's. */
46
57
  timeZone?: string;
58
+ /** Where writes are announced — the process's (`processChanges`) unless a test gives another. */
59
+ changes?: ChangeFeed;
47
60
  children: ReactNode;
48
61
  }): import("react").JSX.Element;
49
62
  export declare function useFramework(): FrameworkContextValue;
@@ -1,11 +1,12 @@
1
1
  "use client";
2
2
  import { jsx as _jsx } from "react/jsx-runtime";
3
- import { installTimeZone, metaStore, processContext, screensTransport, } from "@sincpro/client-framework";
3
+ import { installTimeZone, metaStore, processChanges, processContext, screensTransport, } from "@sincpro/client-framework";
4
+ import { nameBook } from "../core/names.js";
4
5
  import { entityFor } from "../core/relation.js";
5
6
  import { useContextValues } from "./useContextValues.js";
6
- import { createContext, useContext, useMemo, useRef, useState } from "react";
7
+ import { createContext, useContext, useEffect, useMemo, useRef, useState, } from "react";
7
8
  const FrameworkContext = createContext(null);
8
- export function FrameworkProvider({ transport, locale, metas: seed, entities = NO_ENTITIES, timeZone, children, }) {
9
+ export function FrameworkProvider({ transport, locale, metas: seed, entities = NO_ENTITIES, timeZone, changes = processChanges, children, }) {
9
10
  // Installed before the children draw, and again when it changes (another company): the
10
11
  // formatters read it, so it cannot wait for an effect.
11
12
  const zoned = useRef(null);
@@ -25,7 +26,13 @@ export function FrameworkProvider({ transport, locale, metas: seed, entities = N
25
26
  // keeps its filter, order and page, a record what was typed (`useListing`, `useRecord`). The
26
27
  // transport stays the same one: a new one would start every reading over.
27
28
  const context = useContextValues(processContext);
28
- const value = useMemo(() => ({ transport: screens, locale, metas, entities, trails, context }), [screens, locale, metas, entities, trails, context]);
29
+ // One book per transport and company: another company's names are not this one's.
30
+ const names = useMemo(() => nameBook(screens, metas, entities),
31
+ // `context` renews it: what was named in one company is asked again in the next.
32
+ // eslint-disable-next-line react-hooks/exhaustive-deps
33
+ [screens, metas, entities, context]);
34
+ useEffect(() => changes.follow((change) => names.changed(change)), [changes, names]);
35
+ const value = useMemo(() => ({ transport: screens, locale, metas, entities, trails, context, changes, names }), [screens, locale, metas, entities, trails, context, changes, names]);
29
36
  return _jsx(FrameworkContext.Provider, { value: value, children: children });
30
37
  }
31
38
  const NO_ENTITIES = [];
@@ -17,3 +17,4 @@ export { type Editing, type RecordReading, type RecordState, useRecord, } from "
17
17
  export { useRelated } from "./useRelated.js";
18
18
  export { type ServiceState, useService } from "./useService.js";
19
19
  export { useSummaryOf } from "./useSummary.js";
20
+ export { useWrites } from "./useWrites.js";
@@ -17,3 +17,4 @@ export { useRecord, } from "./useRecord.js";
17
17
  export { useRelated } from "./useRelated.js";
18
18
  export { useService } from "./useService.js";
19
19
  export { useSummaryOf } from "./useSummary.js";
20
+ export { useWrites } from "./useWrites.js";
@@ -2,6 +2,7 @@
2
2
  import { resumingFrom } from "@sincpro/criteria";
3
3
  import { measuresOf, sumsOf } from "../core/measure.js";
4
4
  import { useFramework } from "./context.js";
5
+ import { useWrites } from "./useWrites.js";
5
6
  import { useCallback, useEffect, useRef, useState } from "react";
6
7
  /** The most groups a level answers: a split into ten thousand groups is a list, not a split. */
7
8
  const MAX_GROUPS = 100;
@@ -21,6 +22,8 @@ export function useGroups(listing, options = {}) {
21
22
  const { pagination: _page, ...asked } = listing.question;
22
23
  const question = JSON.stringify(asked);
23
24
  const route = listing.route;
25
+ // Written by anyone — a save, a use case, a panel beside: the groups are counted again.
26
+ const writes = useWrites(route);
24
27
  const sums = (options.sums ?? sumsOf(listing.meta)).join(",");
25
28
  // What adds up by default is read from the model: without it yet, asking now and again when
26
29
  // it arrives would be two questions — and the groups on screen replaced under the pointer.
@@ -54,7 +57,7 @@ export function useGroups(listing, options = {}) {
54
57
  setState({ status: "failed", buckets: [], error });
55
58
  });
56
59
  return () => aborter.abort();
57
- }, [transport, route, question, levels, sums, waiting, context]);
60
+ }, [transport, route, question, levels, sums, waiting, context, writes]);
58
61
  /** One page of a group: the first (`after` null) or the one the cursor points at. */
59
62
  const read = useCallback((bucket, after) => {
60
63
  const key = keyOf(bucket);
@@ -2,6 +2,7 @@
2
2
  import { listRoute } from "@sincpro/client-framework";
3
3
  import { historyOf } from "../core/history.js";
4
4
  import { useFramework } from "./context.js";
5
+ import { useWrites } from "./useWrites.js";
5
6
  import { useEffect, useState } from "react";
6
7
  /**
7
8
  * A record's business events, newest first — what `Record.Activity` draws.
@@ -21,6 +22,8 @@ export function useHistory(entity, id, deps = []) {
21
22
  const { transport, locale, context } = useFramework();
22
23
  const [entries, setEntries] = useState(undefined);
23
24
  const history = entity.history;
25
+ // A write under the record — anyone's — adds a fact to its history: read it again.
26
+ const writes = useWrites(history === undefined || id === "" ? undefined : listRoute(history, { id }));
24
27
  useEffect(() => {
25
28
  if (history === undefined || id === "") {
26
29
  setEntries(undefined);
@@ -34,6 +37,6 @@ export function useHistory(entity, id, deps = []) {
34
37
  transport.list(listRoute(history, { id }), criteria, aborter.signal).then((page) => aborter.signal.aborted || setEntries(historyOf(page.rows, page.meta, locale)), () => aborter.signal.aborted || setEntries(undefined));
35
38
  return () => aborter.abort();
36
39
  // eslint-disable-next-line react-hooks/exhaustive-deps -- the caller names what reloads it
37
- }, [transport, history, id, locale, context, ...deps]);
40
+ }, [transport, history, id, locale, context, writes, ...deps]);
38
41
  return entries;
39
42
  }
@@ -5,6 +5,7 @@ import { useReading } from "@sincpro/criteria-react";
5
5
  import { isList, scopeOf } from "../core/list.js";
6
6
  import { useFramework } from "./context.js";
7
7
  import { useListScope } from "./scope.js";
8
+ import { useWrites } from "./useWrites.js";
8
9
  import { useCallback, useEffect, useMemo, useRef, useState } from "react";
9
10
  /** How long typing has to pause before the search goes out. */
10
11
  const TYPING_PAUSE = 200;
@@ -56,15 +57,17 @@ export function useListing(given, own = {}) {
56
57
  });
57
58
  const typing = useRef(null);
58
59
  useEffect(() => () => void (typing.current && clearTimeout(typing.current)), []);
59
- // Another company (the process's context changed): the same question read again — its filter,
60
- // order and page kept, never a reading started over.
61
- const askedIn = useRef(context);
60
+ // Another company (the process's context changed), or a write to what it lists — anyone's: a
61
+ // save, a use case, a panel beside —: the same question read again, its filter, order and page
62
+ // kept, never a reading started over.
63
+ const writes = useWrites(route);
64
+ const askedFor = useRef({ context, writes });
62
65
  useEffect(() => {
63
- if (askedIn.current === context)
66
+ if (askedFor.current.context === context && askedFor.current.writes === writes)
64
67
  return;
65
- askedIn.current = context;
68
+ askedFor.current = { context, writes };
66
69
  void reading.reload();
67
- }, [context, reading]);
70
+ }, [context, writes, reading]);
68
71
  const narrowed = (next) => {
69
72
  let criteria = base;
70
73
  const found = searchOver(meta, next.text);
@@ -3,10 +3,11 @@ import { type Entity } from "@sincpro/client-framework";
3
3
  * The name of one record of an entity, by its key — what a field shows when it holds only the
4
4
  * key (`EntityID[Tenant]`) and not the record it points at.
5
5
  *
6
- * Context: asked ONCE by key, the way `useLookup` names a chosen record it never saw: a page
7
- * narrowed to that key, one row, read with the definition the application knows. Unnamed is no
8
- * error — the field shows the key — and an entity listed under another record
9
- * (`/tenants/{tenant_id}/members`) has no list to ask by key, so it stays unnamed.
6
+ * Context: every name on screen is asked through the provider's book (`nameBook`): the keys asked
7
+ * in the same moment — a page of 50 rows naming 50 tenants — go in ONE request per entity, and a
8
+ * name already known is shared by every place that shows it. A write to the entity (a rename)
9
+ * asks again. Unnamed is no error — the field shows the key — and an entity listed under another
10
+ * record (`/tenants/{tenant_id}/members`) has no list to ask by key, so it stays unnamed.
10
11
  *
11
12
  * const name = useNameOf(tenants, record.tenant_id) ?? record.tenant_id;
12
13
  */
@@ -1,44 +1,43 @@
1
1
  "use client";
2
2
  import { listRoute } from "@sincpro/client-framework";
3
- import { lookupOf } from "../core/lookup.js";
4
3
  import { useFramework } from "./context.js";
4
+ import { useWrites } from "./useWrites.js";
5
5
  import { useEffect, useState } from "react";
6
6
  /**
7
7
  * The name of one record of an entity, by its key — what a field shows when it holds only the
8
8
  * key (`EntityID[Tenant]`) and not the record it points at.
9
9
  *
10
- * Context: asked ONCE by key, the way `useLookup` names a chosen record it never saw: a page
11
- * narrowed to that key, one row, read with the definition the application knows. Unnamed is no
12
- * error — the field shows the key — and an entity listed under another record
13
- * (`/tenants/{tenant_id}/members`) has no list to ask by key, so it stays unnamed.
10
+ * Context: every name on screen is asked through the provider's book (`nameBook`): the keys asked
11
+ * in the same moment — a page of 50 rows naming 50 tenants — go in ONE request per entity, and a
12
+ * name already known is shared by every place that shows it. A write to the entity (a rename)
13
+ * asks again. Unnamed is no error — the field shows the key — and an entity listed under another
14
+ * record (`/tenants/{tenant_id}/members`) has no list to ask by key, so it stays unnamed.
14
15
  *
15
16
  * const name = useNameOf(tenants, record.tenant_id) ?? record.tenant_id;
16
17
  */
17
18
  export function useNameOf(entity, id) {
18
- const { transport, metas, context } = useFramework();
19
+ const { names, context } = useFramework();
19
20
  const [named, setNamed] = useState(undefined);
21
+ const writes = useWrites(entity === undefined ? undefined : routeOf(entity));
20
22
  useEffect(() => {
21
23
  if (entity === undefined || id === "")
22
24
  return;
23
- let route;
24
- try {
25
- route = listRoute(entity.list);
26
- }
27
- catch {
28
- return;
29
- }
30
- const known = metas.get(entity.key);
31
- const key = known?.get_id || known?.identity || "id";
32
- const aborter = new AbortController();
33
- transport
34
- .list(route, { selection: { field: key, operator: "=", value: id }, pagination: { limit: 1 } }, aborter.signal)
35
- .then((page) => {
36
- const [one] = lookupOf(page.meta ?? known ?? null, page.rows, entity.summary);
37
- if (one !== undefined)
38
- setNamed({ id, name: one.label });
39
- })
40
- .catch(() => undefined);
41
- return () => aborter.abort();
42
- }, [transport, metas, entity, id, context]);
25
+ let current = true;
26
+ void names.nameOf(entity, id).then((name) => {
27
+ if (current && name !== undefined)
28
+ setNamed({ id, name });
29
+ });
30
+ return () => void (current = false);
31
+ // `context` and `writes` ask again: another company, or a write to the entity (a rename).
32
+ }, [names, entity, id, context, writes]);
43
33
  return named?.id === id ? named.name : undefined;
44
34
  }
35
+ /** Where `entity` is listed — or nothing to follow when it is listed only under a record. */
36
+ function routeOf(entity) {
37
+ try {
38
+ return listRoute(entity.list);
39
+ }
40
+ catch {
41
+ return undefined;
42
+ }
43
+ }
@@ -1,6 +1,7 @@
1
1
  "use client";
2
2
  import { measuresOf, sumsOf } from "../core/measure.js";
3
3
  import { useFramework } from "./context.js";
4
+ import { useWrites } from "./useWrites.js";
4
5
  import { useEffect, useState } from "react";
5
6
  /** The most groups an axis answers: a pivot of ten thousand rows is a list. */
6
7
  const MAX_KEYS = 100;
@@ -18,6 +19,8 @@ export function usePivot(listing, axes) {
18
19
  const sums = measures.join(",");
19
20
  const shape = axes === null ? "" : JSON.stringify([axes.rows, axes.columns]);
20
21
  const waiting = axes !== null && axes.measures === undefined && listing.meta === null;
22
+ // Written by anyone — a save, a use case, a panel beside: the crossing is counted again.
23
+ const writes = useWrites(listing.route);
21
24
  useEffect(() => {
22
25
  if (shape === "") {
23
26
  setState({ status: "idle", crossed: [], columns: [], error: null });
@@ -53,6 +56,6 @@ export function usePivot(listing, axes) {
53
56
  setState({ status: "failed", crossed: [], columns: [], error });
54
57
  });
55
58
  return () => aborter.abort();
56
- }, [transport, listing.route, question, shape, sums, waiting, context]);
59
+ }, [transport, listing.route, question, shape, sums, waiting, context, writes]);
57
60
  return { ...state, measures };
58
61
  }
@@ -3,6 +3,7 @@ import { newEntityId, pathFor, retryKeys, startingValuesOf, TransportError, WITH
3
3
  import { changesOf, computedLocally, creationOf, defaultsOf, errorsByField, fieldStateOf, lineStatesOf, missingOf, needsPreview, } from "../core/edit.js";
4
4
  import { linesAnswered } from "../core/lines.js";
5
5
  import { useFramework } from "./context.js";
6
+ import { useWrites } from "./useWrites.js";
6
7
  import { useCallback, useEffect, useMemo, useRef, useState } from "react";
7
8
  /** How long typing has to pause before the server is asked what the record becomes. */
8
9
  const PREVIEW_PAUSE = 300;
@@ -138,16 +139,19 @@ export function useRecord(entity, opened) {
138
139
  };
139
140
  }, []);
140
141
  const reload = useCallback(() => setRound((n) => n + 1), []);
141
- // Another company (the process's context changed): a record with nothing typed reads again;
142
- // one being edited keeps what was typed — losing it is never the framework's decision.
143
- const askedIn = useRef(context);
142
+ // Another company (the process's context changed), or a write to it — anyone's: a use case, a
143
+ // panel beside —: a record with nothing typed reads again; one being edited keeps what was
144
+ // typed — losing it is never the framework's decision, and its save still meets the version
145
+ // it read (a 409 when someone saved). Its own save reads it again by itself.
146
+ const writes = useWrites(isNew ? undefined : entity.one(id));
147
+ const askedFor = useRef({ context, writes });
144
148
  useEffect(() => {
145
- if (askedIn.current === context)
149
+ if (askedFor.current.context === context && askedFor.current.writes === writes)
146
150
  return;
147
- askedIn.current = context;
151
+ askedFor.current = { context, writes };
148
152
  if (Object.keys(typedNow.current).length === 0)
149
153
  reload();
150
- }, [context, reload]);
154
+ }, [context, writes, reload]);
151
155
  const record = useMemo(() => (state.record ?? {}), [state.record]);
152
156
  const values = useMemo(() => ({ ...record, ...computed, ...typed }), [record, computed, typed]);
153
157
  const changed = useMemo(() => changesOf(record, typed), [record, typed]);
@@ -0,0 +1,19 @@
1
+ /**
2
+ * How many times what lies at `route` was written since this screen started reading it — a
3
+ * dependency that makes a reading read again.
4
+ *
5
+ * const writes = useWrites(listRoute(entity.list));
6
+ * useEffect(() => { … transport.list(…) … }, [transport, route, writes]);
7
+ *
8
+ * Context: a screen must show what is true now, whoever wrote it — its own save, a use case run
9
+ * from a button, a panel beside it. Every write a transport of the framework sends is announced
10
+ * on the process's change feed (`processChanges`, @sincpro/client-framework); this follows it and
11
+ * counts the ones that touch `route` (`touches`: the collections a write names, and an entity's
12
+ * history). A reading adds the count to what it reads by, as it adds the company it reads in: the
13
+ * framework's own readings do (`useListing`, `useRecord`, `useHistory`, `useGroups`, `usePivot`,
14
+ * `useNameOf`), and so may a screen's own. It reads again on the next render, when the cache under
15
+ * it — following the same feed — has already forgotten the old answer.
16
+ *
17
+ * `undefined`: nothing is read yet, and nothing is counted.
18
+ */
19
+ export declare function useWrites(route: string | undefined): number;
@@ -0,0 +1,35 @@
1
+ "use client";
2
+ import { touches } from "../core/changes.js";
3
+ import { useFramework } from "./context.js";
4
+ import { useEffect, useState } from "react";
5
+ /**
6
+ * How many times what lies at `route` was written since this screen started reading it — a
7
+ * dependency that makes a reading read again.
8
+ *
9
+ * const writes = useWrites(listRoute(entity.list));
10
+ * useEffect(() => { … transport.list(…) … }, [transport, route, writes]);
11
+ *
12
+ * Context: a screen must show what is true now, whoever wrote it — its own save, a use case run
13
+ * from a button, a panel beside it. Every write a transport of the framework sends is announced
14
+ * on the process's change feed (`processChanges`, @sincpro/client-framework); this follows it and
15
+ * counts the ones that touch `route` (`touches`: the collections a write names, and an entity's
16
+ * history). A reading adds the count to what it reads by, as it adds the company it reads in: the
17
+ * framework's own readings do (`useListing`, `useRecord`, `useHistory`, `useGroups`, `usePivot`,
18
+ * `useNameOf`), and so may a screen's own. It reads again on the next render, when the cache under
19
+ * it — following the same feed — has already forgotten the old answer.
20
+ *
21
+ * `undefined`: nothing is read yet, and nothing is counted.
22
+ */
23
+ export function useWrites(route) {
24
+ const { changes, entities } = useFramework();
25
+ const [writes, setWrites] = useState(0);
26
+ useEffect(() => {
27
+ if (route === undefined)
28
+ return;
29
+ return changes.follow((change) => {
30
+ if (touches(change, route, entities))
31
+ setWrites((count) => count + 1);
32
+ });
33
+ }, [changes, entities, route]);
34
+ return writes;
35
+ }
@@ -19,6 +19,7 @@ export { type LineColumn, type LineControl, type LinesLook, RecordLinesEditor, }
19
19
  export { type RecordOpening, RecordOpeningProvider, useRecordOpening, } from "./record/opening.js";
20
20
  export { countOf, factsOf, RecordLines } from "./record/record.js";
21
21
  export { RelatedList } from "./record/RelatedList.js";
22
+ export { ServiceButton } from "./record/ServiceButton.js";
22
23
  export { cardOf, relationOf } from "./record/summary.js";
23
24
  export { Refused } from "./Refused.js";
24
25
  export { entityView, type EntityViewOptions, listView, type ListViewOptions, } from "./workspace/composer.js";
package/dist/web/index.js CHANGED
@@ -19,6 +19,7 @@ export { RecordLinesEditor, } from "./record/lines.js";
19
19
  export { RecordOpeningProvider, useRecordOpening, } from "./record/opening.js";
20
20
  export { countOf, factsOf, RecordLines } from "./record/record.js";
21
21
  export { RelatedList } from "./record/RelatedList.js";
22
+ export { ServiceButton } from "./record/ServiceButton.js";
22
23
  export { cardOf, relationOf } from "./record/summary.js";
23
24
  export { Refused } from "./Refused.js";
24
25
  export { entityView, listView, } from "./workspace/composer.js";
@@ -0,0 +1,34 @@
1
+ import type { Rehearsal } from "../../react/useCommand.js";
2
+ /**
3
+ * What a button needs of an action to draw it — whatever runs behind: an entity's Command
4
+ * (`useCommand`) or one of the application's use cases (`useService`).
5
+ */
6
+ export type ButtonAction<R> = {
7
+ /** Does it; `undefined` when it failed — the failure is in `error`. */
8
+ run: () => Promise<R | undefined>;
9
+ /** Asks first what it would do — a dry run: the action destroys something. */
10
+ rehearse?: () => Promise<Rehearsal | undefined>;
11
+ busy: boolean;
12
+ error: unknown;
13
+ /** What the last dry run said it would do. */
14
+ rehearsal: Rehearsal | null;
15
+ reset: () => void;
16
+ };
17
+ /**
18
+ * An action as a button — the gesture `CommandButton` and `ServiceButton` share.
19
+ *
20
+ * Context: two kinds of action, two gestures. One that can be undone runs on the click — asking
21
+ * «are you sure?» before every confirm teaches people to click «yes» without reading. One that
22
+ * destroys (it can `rehearse`) asks FIRST what it would do and shows THAT in the window: the
23
+ * confirmation is the consequence, in the server's words, not a question. A refusal is shown
24
+ * where it happened, with what to do. Off while it runs: a click twice is one action.
25
+ *
26
+ * Internal: the gesture is drawn here once; what runs behind is each button's own.
27
+ */
28
+ export declare function ActionButton<R>({ action, label, role, disabled, onDone, }: {
29
+ action: ButtonAction<R>;
30
+ label: string;
31
+ role?: "primary" | "secondary" | "ghost";
32
+ disabled?: boolean;
33
+ onDone?: (answer: R) => void;
34
+ }): import("react").JSX.Element;
@@ -0,0 +1,41 @@
1
+ "use client";
2
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
+ import { frameworkCopy } from "../../core/copy.js";
4
+ import { Refused } from "../Refused.js";
5
+ import { Button, Dialog } from "@sincpro/web-ui";
6
+ import { useState } from "react";
7
+ /**
8
+ * An action as a button — the gesture `CommandButton` and `ServiceButton` share.
9
+ *
10
+ * Context: two kinds of action, two gestures. One that can be undone runs on the click — asking
11
+ * «are you sure?» before every confirm teaches people to click «yes» without reading. One that
12
+ * destroys (it can `rehearse`) asks FIRST what it would do and shows THAT in the window: the
13
+ * confirmation is the consequence, in the server's words, not a question. A refusal is shown
14
+ * where it happened, with what to do. Off while it runs: a click twice is one action.
15
+ *
16
+ * Internal: the gesture is drawn here once; what runs behind is each button's own.
17
+ */
18
+ export function ActionButton({ action, label, role = "secondary", disabled = false, onDone, }) {
19
+ const copy = frameworkCopy().command;
20
+ const [open, setOpen] = useState(false);
21
+ const destructive = action.rehearse !== undefined;
22
+ const run = async () => {
23
+ const answer = await action.run();
24
+ if (answer !== undefined) {
25
+ setOpen(false);
26
+ action.reset();
27
+ onDone?.(answer);
28
+ }
29
+ else
30
+ setOpen(true);
31
+ };
32
+ const ask = async () => {
33
+ setOpen(true);
34
+ await action.rehearse?.();
35
+ };
36
+ const close = () => {
37
+ setOpen(false);
38
+ action.reset();
39
+ };
40
+ 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, title: label, 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] }));
41
+ }
@@ -2,13 +2,12 @@ import { type CommandAnswers, type CommandBodies, type Entity } from "@sincpro/c
2
2
  import type { AnyRecord } from "@sincpro/criteria";
3
3
  import { type AnswerOf } from "../../react/useCommand.js";
4
4
  /**
5
- * An entity's Command as a button.
5
+ * An entity's Command as a button: sent as it is, over the screens' transport.
6
6
  *
7
- * Context: two kinds of action, two gestures. One that can be undone runs on the click — asking
8
- * «are you sure?» before every confirm teaches people to click «yes» without reading. One that
9
- * destroys (`destructive`) asks the server FIRST what it would do — the Command's dry run — and
10
- * shows THAT in the window: the confirmation is the consequence, in the server's words, not a
11
- * question. A refusal is shown where it happened, with what to do.
7
+ * Context: the gesture is `ActionButton`'s — run on the click, or (`destructive`) ask the
8
+ * server's dry run first and show what it would do; a refusal where it happened. An action that is
9
+ * one of the application's own use cases — logic of its own, composed, traced as
10
+ * `<context>/<Service>` — is a `ServiceButton`.
12
11
  */
13
12
  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, onDone, }: {
14
13
  entity: Entity<T, C, A>;
@@ -1,41 +1,24 @@
1
1
  "use client";
2
- import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
3
  import {} from "@sincpro/client-framework";
4
- import { frameworkCopy } from "../../core/copy.js";
5
4
  import { useCommand } from "../../react/useCommand.js";
6
- import { Refused } from "../Refused.js";
7
- import { Button, Dialog } from "@sincpro/web-ui";
8
- import { useState } from "react";
5
+ import { ActionButton } from "./ActionButton.js";
9
6
  /**
10
- * An entity's Command as a button.
7
+ * An entity's Command as a button: sent as it is, over the screens' transport.
11
8
  *
12
- * Context: two kinds of action, two gestures. One that can be undone runs on the click — asking
13
- * «are you sure?» before every confirm teaches people to click «yes» without reading. One that
14
- * destroys (`destructive`) asks the server FIRST what it would do — the Command's dry run — and
15
- * shows THAT in the window: the confirmation is the consequence, in the server's words, not a
16
- * question. A refusal is shown where it happened, with what to do.
9
+ * Context: the gesture is `ActionButton`'s — run on the click, or (`destructive`) ask the
10
+ * server's dry run first and show what it would do; a refusal where it happened. An action that is
11
+ * one of the application's own use cases — logic of its own, composed, traced as
12
+ * `<context>/<Service>` — is a `ServiceButton`.
17
13
  */
18
14
  export function CommandButton({ entity, command, id, body, label, destructive = false, role = "secondary", disabled = false, onDone, }) {
19
- const copy = frameworkCopy().command;
20
- const action = useCommand(entity, command);
21
- const [open, setOpen] = useState(false);
22
- const run = async () => {
23
- const answer = await action.run(id, body);
24
- if (answer !== undefined) {
25
- setOpen(false);
26
- action.reset();
27
- onDone?.(answer);
28
- }
29
- else
30
- setOpen(true);
31
- };
32
- const ask = async () => {
33
- setOpen(true);
34
- await action.rehearse(id, body);
35
- };
36
- const close = () => {
37
- setOpen(false);
38
- action.reset();
39
- };
40
- 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, title: label, 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] }));
15
+ const sending = useCommand(entity, command);
16
+ return (_jsx(ActionButton, { action: {
17
+ run: () => sending.run(id, body),
18
+ ...(destructive ? { rehearse: () => sending.rehearse(id, body) } : {}),
19
+ busy: sending.busy,
20
+ error: sending.error,
21
+ rehearsal: sending.rehearsal,
22
+ reset: sending.reset,
23
+ }, label: label, role: role, disabled: disabled, ...(onDone === undefined ? {} : { onDone }) }));
41
24
  }
@@ -0,0 +1,31 @@
1
+ import type { Rehearsal } from "../../react/useCommand.js";
2
+ /**
3
+ * One of the application's use cases as a button — its Feature or ApplicationService, run on its
4
+ * bounded context.
5
+ *
6
+ * <ServiceButton
7
+ * label="Cancelar"
8
+ * run={() => projectBoundedContext.execute(new CommandCancelIssue(issue.id))}
9
+ * rehearse={() => projectBoundedContext.execute(new CommandCancelIssue(issue.id, true))}
10
+ * />
11
+ *
12
+ * Context: an action of an application is a use case — its logic, its composition, its span
13
+ * `<context>/<Service>`, its request carrying the run's trace to the server —, never a request a
14
+ * screen sends; what the screen adds is the state of running it (`useService`). The gesture is
15
+ * `CommandButton`'s, drawn once by `ActionButton`: run on the click, off while it runs, a refusal
16
+ * where it happened; and an action that destroys asks first (`rehearse`, the use case run dry)
17
+ * and shows what it would do. What it changed, every screen hears by itself: the use case's
18
+ * client announces its write (`processChanges`), so `onDone` is for what only this screen does
19
+ * next — a toast, closing a window —, never to read again.
20
+ */
21
+ export declare function ServiceButton<R>({ label, run, rehearse, role, disabled, onDone, }: {
22
+ label: string;
23
+ /** The use case, executed on its bounded context. */
24
+ run: () => Promise<R>;
25
+ /** The same use case run dry — what it would do, nothing touched —, asked before `run`. */
26
+ rehearse?: () => Promise<Rehearsal>;
27
+ role?: "primary" | "secondary" | "ghost";
28
+ /** Not offered right now. The use case still decides. */
29
+ disabled?: boolean;
30
+ onDone?: (answer: R) => void;
31
+ }): import("react").JSX.Element;