@sincpro/web-framework 1.1.0 → 2.0.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 (121) hide show
  1. package/README.md +179 -78
  2. package/dist/core/cache.d.ts +1 -2
  3. package/dist/core/cache.js +8 -5
  4. package/dist/core/explain.d.ts +15 -0
  5. package/dist/core/{transport.js → explain.js} +4 -26
  6. package/dist/core/form.d.ts +1 -1
  7. package/dist/core/form.js +1 -0
  8. package/dist/core/index.d.ts +9 -15
  9. package/dist/core/index.js +8 -14
  10. package/dist/core/list.d.ts +5 -4
  11. package/dist/core/list.js +1 -0
  12. package/dist/core/lookup.d.ts +1 -1
  13. package/dist/core/lookup.js +1 -0
  14. package/dist/core/present.js +1 -1
  15. package/dist/core/relation.d.ts +1 -1
  16. package/dist/core/relation.js +1 -1
  17. package/dist/core/summary.d.ts +1 -6
  18. package/dist/core/trail.d.ts +1 -1
  19. package/dist/core/trail.js +1 -0
  20. package/dist/errors/index.d.ts +48 -0
  21. package/dist/errors/index.js +56 -0
  22. package/dist/react/context.d.ts +1 -3
  23. package/dist/react/context.js +1 -2
  24. package/dist/react/index.d.ts +2 -0
  25. package/dist/react/index.js +2 -0
  26. package/dist/react/scope.d.ts +1 -1
  27. package/dist/react/scope.js +1 -1
  28. package/dist/react/useCommand.d.ts +1 -1
  29. package/dist/react/useCommand.js +1 -2
  30. package/dist/react/useContextValues.d.ts +15 -0
  31. package/dist/react/useContextValues.js +19 -0
  32. package/dist/react/useHistory.d.ts +1 -1
  33. package/dist/react/useHistory.js +1 -0
  34. package/dist/react/useListing.d.ts +4 -4
  35. package/dist/react/useListing.js +6 -6
  36. package/dist/react/useLookup.d.ts +1 -1
  37. package/dist/react/useLookup.js +1 -0
  38. package/dist/react/useNameOf.d.ts +1 -1
  39. package/dist/react/useNameOf.js +1 -1
  40. package/dist/react/useRecord.d.ts +1 -2
  41. package/dist/react/useRecord.js +1 -4
  42. package/dist/react/useRelated.d.ts +1 -1
  43. package/dist/react/useRelated.js +1 -0
  44. package/dist/react/useService.d.ts +38 -0
  45. package/dist/react/useService.js +66 -0
  46. package/dist/react/useSummary.d.ts +1 -1
  47. package/dist/react/useSummary.js +1 -0
  48. package/dist/telemetry/index.d.ts +45 -0
  49. package/dist/telemetry/index.js +55 -0
  50. package/dist/web/Refused.js +1 -1
  51. package/dist/web/list/EntityList.d.ts +1 -1
  52. package/dist/web/list/EntityList.js +3 -3
  53. package/dist/web/list/EntitySearchBar.d.ts +2 -1
  54. package/dist/web/list/EntitySearchBar.js +17 -11
  55. package/dist/web/list/presentation.d.ts +1 -1
  56. package/dist/web/list/presentation.js +1 -0
  57. package/dist/web/list/rowCommands.d.ts +1 -1
  58. package/dist/web/list/rowCommands.js +1 -2
  59. package/dist/web/record/CommandButton.d.ts +1 -1
  60. package/dist/web/record/CommandButton.js +1 -0
  61. package/dist/web/record/EntityPicker.d.ts +1 -1
  62. package/dist/web/record/EntityPicker.js +1 -0
  63. package/dist/web/record/EntityTags.d.ts +1 -1
  64. package/dist/web/record/EntityTags.js +1 -0
  65. package/dist/web/record/RelatedList.d.ts +1 -1
  66. package/dist/web/record/RelatedList.js +1 -0
  67. package/dist/web/record/control.d.ts +1 -1
  68. package/dist/web/record/control.js +1 -1
  69. package/dist/web/record/edit.js +1 -0
  70. package/dist/web/record/opening.d.ts +1 -1
  71. package/dist/web/record/opening.js +1 -0
  72. package/dist/web/workspace/SincproApp.d.ts +1 -2
  73. package/dist/web/workspace/SincproApp.js +1 -0
  74. package/dist/web/workspace/composer.d.ts +1 -2
  75. package/dist/web/workspace/composer.js +2 -2
  76. package/dist/web/workspace/open.d.ts +1 -1
  77. package/dist/web/workspace/open.js +1 -0
  78. package/dist/web/workspace/record.d.ts +1 -1
  79. package/dist/web/workspace/record.js +1 -1
  80. package/dist/web/workspace/workspace.d.ts +1 -1
  81. package/dist/web/workspace/workspace.js +1 -0
  82. package/package.json +43 -13
  83. package/dist/codegen/cli.d.ts +0 -2
  84. package/dist/codegen/cli.js +0 -82
  85. package/dist/codegen/client.d.ts +0 -14
  86. package/dist/codegen/client.js +0 -178
  87. package/dist/codegen/domain.d.ts +0 -14
  88. package/dist/codegen/domain.js +0 -195
  89. package/dist/codegen/entities.d.ts +0 -14
  90. package/dist/codegen/entities.js +0 -158
  91. package/dist/codegen/index.d.ts +0 -11
  92. package/dist/codegen/index.js +0 -9
  93. package/dist/codegen/metamodel.d.ts +0 -89
  94. package/dist/codegen/metamodel.js +0 -1
  95. package/dist/codegen/names.d.ts +0 -40
  96. package/dist/codegen/names.js +0 -114
  97. package/dist/codegen/rest.d.ts +0 -31
  98. package/dist/codegen/rest.js +0 -27
  99. package/dist/core/contract.d.ts +0 -34
  100. package/dist/core/contract.js +0 -41
  101. package/dist/core/discovery.d.ts +0 -47
  102. package/dist/core/discovery.js +0 -68
  103. package/dist/core/entity.d.ts +0 -227
  104. package/dist/core/entity.js +0 -187
  105. package/dist/core/ids.d.ts +0 -17
  106. package/dist/core/ids.js +0 -52
  107. package/dist/core/memory.d.ts +0 -46
  108. package/dist/core/memory.js +0 -108
  109. package/dist/core/meta.d.ts +0 -121
  110. package/dist/core/meta.js +0 -130
  111. package/dist/core/metas.d.ts +0 -17
  112. package/dist/core/metas.js +0 -7
  113. package/dist/core/rest.d.ts +0 -49
  114. package/dist/core/rest.js +0 -143
  115. package/dist/core/retry.d.ts +0 -24
  116. package/dist/core/retry.js +0 -25
  117. package/dist/core/schema.d.ts +0 -39
  118. package/dist/core/schema.js +0 -128
  119. package/dist/core/transport.d.ts +0 -90
  120. package/dist/core/zone.d.ts +0 -27
  121. package/dist/core/zone.js +0 -75
@@ -1,5 +1,5 @@
1
+ import { type Entity, type EntityRef } from "@sincpro/client-framework";
1
2
  import type { AnyRecord, Criteria, Expression, Grain, Sort } from "@sincpro/criteria";
2
- import type { Entity, EntityRef } from "./entity.js";
3
3
  /**
4
4
  * A named way of looking at an entity: «Por cobrar», «Borradores», «Mis tickets».
5
5
  *
@@ -17,9 +17,9 @@ import type { Entity, EntityRef } from "./entity.js";
17
17
  * export const porCobrar = defineList(facturas, {
18
18
  * id: "por-cobrar",
19
19
  * title: "Por cobrar",
20
- * scope: { where: { field: "state", operator: "=", value: "posted" } },
20
+ * scope: { selection: { field: "state", operator: "=", value: "posted" } },
21
21
  * columns: ["number", "customer", "due_date", "amount_total"],
22
- * filters: [{ id: "vencidas", label: "Vencidas", where: … }],
22
+ * filters: [{ id: "vencidas", label: "Vencidas", selection: … }],
23
23
  * });
24
24
  */
25
25
  export type ListDefinition<T extends AnyRecord = AnyRecord> = {
@@ -49,7 +49,8 @@ export type ListDefinition<T extends AnyRecord = AnyRecord> = {
49
49
  export type NamedFilter<T> = {
50
50
  id: string;
51
51
  label: string;
52
- where: Expression<T>;
52
+ /** What it keeps, as criteria's `selection`. */
53
+ selection: Expression<T>;
53
54
  };
54
55
  /** One level of a grouping: by which field, and by which cut of time when it is a date. */
55
56
  export type GroupLevel = {
package/dist/core/list.js CHANGED
@@ -1,3 +1,4 @@
1
+ import {} from "@sincpro/client-framework";
1
2
  export function defineList(entity, spec) {
2
3
  if (!/^[a-z0-9][a-z0-9-]*$/.test(spec.id))
3
4
  throw new Error(`list id "${spec.id}" is not kebab-case: it becomes a workspace kind`);
@@ -1,5 +1,5 @@
1
+ import { type SummaryFields } from "@sincpro/client-framework";
1
2
  import type { AnyRecord, Meta } from "@sincpro/criteria";
2
- import { type SummaryFields } from "./summary.js";
3
3
  /**
4
4
  * A record as a choice: what a many2one select, a «share with» and ⌘K show for it.
5
5
  *
@@ -1,3 +1,4 @@
1
+ import {} from "@sincpro/client-framework";
1
2
  import { summaryFieldsOf } from "./summary.js";
2
3
  export function lookupOf(meta, rows, summary = {}) {
3
4
  const key = meta?.get_id || meta?.identity || "id";
@@ -1,7 +1,7 @@
1
+ import { timeZoneNow } from "@sincpro/client-framework";
1
2
  import { choiceLabelOf, labelOf, } from "@sincpro/criteria";
2
3
  import { frameworkCopy } from "./copy.js";
3
4
  import { relatedOf } from "./related.js";
4
- import { timeZoneNow } from "./zone.js";
5
5
  /** Present when a field is named that the model does not declare: drawn plain, never thrown. */
6
6
  const UNKNOWN = {
7
7
  type: "unknown",
@@ -1,5 +1,5 @@
1
+ import { type Entity } from "@sincpro/client-framework";
1
2
  import type { FieldMeta, Meta } from "@sincpro/criteria";
2
- import type { Entity } from "./entity.js";
3
3
  /**
4
4
  * Which record a field of a form is about, read from the model — so a form draws a many2one
5
5
  * as a picker of the right entity without being told.
@@ -1,4 +1,4 @@
1
- import { entityKeyOf, kindOf } from "./contract.js";
1
+ import { entityKeyOf, kindOf } from "@sincpro/client-framework";
2
2
  export function relationFieldOf(meta, name) {
3
3
  if (!meta)
4
4
  return undefined;
@@ -1,3 +1,4 @@
1
+ import type { SummaryFields, SummaryPart } from "@sincpro/client-framework";
1
2
  import type { AnyRecord, Meta } from "@sincpro/criteria";
2
3
  /**
3
4
  * What is known about ANY record, the same for every entity: its name and, when it has them,
@@ -13,12 +14,6 @@ import type { AnyRecord, Meta } from "@sincpro/criteria";
13
14
  * …). The name is the model's own `display`. When the backend publishes these parts in its
14
15
  * model definition, they are read from there and the convention steps aside.
15
16
  */
16
- export type SummaryPart = "description" | "state" | "type" | "image" | "createdAt" | "createdBy" | "updatedAt" | "updatedBy";
17
- /** Which field of the entity plays each part, when it is not the conventional one.
18
- * `person`: its records are people or companies — a round face, initials without a photo. */
19
- export type SummaryFields = Partial<Record<SummaryPart, string>> & {
20
- person?: boolean;
21
- };
22
17
  export type RecordSummary = {
23
18
  id: string;
24
19
  name: string;
@@ -1,5 +1,5 @@
1
+ import { type Transport } from "@sincpro/client-framework";
1
2
  import type { Criteria } from "@sincpro/criteria";
2
- import type { Transport } from "./transport.js";
3
3
  /**
4
4
  * Where a record was opened from: the page of the list it was a row of, so the record can go
5
5
  * to the previous and the next one — «3 / 64» and two arrows — without going back to the list.
@@ -1,3 +1,4 @@
1
+ import {} from "@sincpro/client-framework";
1
2
  import { resumingFrom } from "@sincpro/criteria";
2
3
  /** Where a record stands in its trail: «3 of 64», 1-based. */
3
4
  export function placeOf(trail, id) {
@@ -0,0 +1,48 @@
1
+ import type { ErrorReporter } from "@sincpro/client-framework";
2
+ /**
3
+ * An `ErrorReporter` for the browser, built from the application's own settings: a
4
+ * Sentry-compatible client — GlitchTip, where the backend reports too — with the application's
5
+ * DSN.
6
+ *
7
+ * // the application — domains/common/infrastructure/telemetry.ts
8
+ * import { glitchtipReporter } from "./index.js";
9
+ * const reporter = glitchtipReporter({ dsn: settings.glitchtip, release, environment });
10
+ * configureProcess({ tracer, reporter }); // entrypoints/main.tsx
11
+ *
12
+ * Context: optional on every count, like `./telemetry`: an entry of its own, `@sentry/browser`
13
+ * an optional peer, and only a convenience — an application with its own client wraps it with
14
+ * `sentryReporter` (`@sincpro/client-framework/sentry`), or hands any `ErrorReporter`.
15
+ *
16
+ * It never breaks what it serves: the SDK loads after the first render; what is reported before
17
+ * it is there waits (a handful), and if it is not installed or fails, reports are dropped and
18
+ * that is said once on the console. Nothing global: no `Sentry.init`, no handlers on `window` —
19
+ * this client is the reporter's own, and only what the process decides is unexpected reaches it.
20
+ *
21
+ * What it sends is narrow by default, because in an ERP a request body is an invoice and a
22
+ * customer: no breadcrumbs or default integrations, and every event leaves without cookies,
23
+ * headers, request bodies, query strings or the user's address. `beforeSend` is the
24
+ * application's, run after that.
25
+ */
26
+ export type GlitchtipOptions = {
27
+ /** The application's DSN. Public by nature: it ships to the browser. */
28
+ dsn: string;
29
+ release?: string;
30
+ environment?: string;
31
+ /** Fraction of errors sent, 0 to 1. Default 1. */
32
+ sampling?: number;
33
+ /** The application's last word on an event: change it, or answer `null` to drop it. */
34
+ beforeSend?: (event: SentEvent) => SentEvent | null;
35
+ };
36
+ /** The parts of an event this reporter narrows; the rest passes as the SDK built it. */
37
+ export type SentEvent = {
38
+ request?: {
39
+ url?: string;
40
+ cookies?: unknown;
41
+ headers?: unknown;
42
+ data?: unknown;
43
+ query_string?: unknown;
44
+ };
45
+ user?: unknown;
46
+ [key: string]: unknown;
47
+ };
48
+ export declare function glitchtipReporter(options: GlitchtipOptions): ErrorReporter;
@@ -0,0 +1,56 @@
1
+ const WAITING_AT_MOST = 20;
2
+ /** Every event leaves without what may carry a person's or a customer's data. */
3
+ function narrowed(event) {
4
+ const { user: _user, ...rest } = event;
5
+ if (rest.request === undefined)
6
+ return rest;
7
+ const { cookies: _c, headers: _h, data: _d, query_string: _q, ...request } = rest.request;
8
+ return {
9
+ ...rest,
10
+ request: { ...request, ...(request.url ? { url: request.url.split("?")[0] } : {}) },
11
+ };
12
+ }
13
+ export function glitchtipReporter(options) {
14
+ let ready = null;
15
+ let failed = false;
16
+ const waiting = [];
17
+ void (async () => {
18
+ try {
19
+ const [sentry, { sentryReporter }] = await Promise.all([
20
+ import("@sentry/browser"),
21
+ import("@sincpro/client-framework/sentry"),
22
+ ]);
23
+ const client = new sentry.BrowserClient({
24
+ dsn: options.dsn,
25
+ transport: sentry.makeFetchTransport,
26
+ stackParser: sentry.defaultStackParser,
27
+ integrations: [],
28
+ ...(options.release ? { release: options.release } : {}),
29
+ ...(options.environment ? { environment: options.environment } : {}),
30
+ ...(options.sampling === undefined ? {} : { sampleRate: options.sampling }),
31
+ beforeSend: (event) => {
32
+ const own = narrowed(event);
33
+ const kept = options.beforeSend ? options.beforeSend(own) : own;
34
+ return kept;
35
+ },
36
+ });
37
+ client.init();
38
+ ready = sentryReporter(client);
39
+ for (const [error, about] of waiting.splice(0))
40
+ ready.report(error, about);
41
+ }
42
+ catch (error) {
43
+ failed = true;
44
+ waiting.length = 0;
45
+ console.warn("@sincpro/web-framework/errors: error reporting is off — @sentry/browser did not load", error);
46
+ }
47
+ })();
48
+ return {
49
+ report(error, about) {
50
+ if (ready !== null)
51
+ return ready.report(error, about);
52
+ if (!failed && waiting.length < WAITING_AT_MOST)
53
+ waiting.push([error, about]);
54
+ },
55
+ };
56
+ }
@@ -1,8 +1,6 @@
1
+ import { type Entity, type MetaStore, type Transport } from "@sincpro/client-framework";
1
2
  import type { Meta } from "@sincpro/criteria";
2
- import type { Entity } from "../core/entity.js";
3
- import { type MetaStore } from "../core/metas.js";
4
3
  import type { Trail } from "../core/trail.js";
5
- import type { Transport } from "../core/transport.js";
6
4
  import { type ReactNode } from "react";
7
5
  /**
8
6
  * Who answers, in which language a screen reads, and which model definitions are already
@@ -1,8 +1,7 @@
1
1
  "use client";
2
2
  import { jsx as _jsx } from "react/jsx-runtime";
3
- import { metaStore } from "../core/metas.js";
3
+ import { installTimeZone, metaStore, } from "@sincpro/client-framework";
4
4
  import { entityFor } from "../core/relation.js";
5
- import { installTimeZone } from "../core/zone.js";
6
5
  import { createContext, useContext, useMemo, useRef, useState } from "react";
7
6
  const FrameworkContext = createContext(null);
8
7
  export function FrameworkProvider({ transport, locale, metas: seed, entities = NO_ENTITIES, timeZone, children, }) {
@@ -6,6 +6,7 @@
6
6
  export { FrameworkProvider, useEntityFor, useFramework, } from "./context.js";
7
7
  export { type ListScope, ListScopeProvider, useListScope, } from "./scope.js";
8
8
  export { type AnswerOf, type CommandState, type Rehearsal, useCommand, } from "./useCommand.js";
9
+ export { useContextValues } from "./useContextValues.js";
9
10
  export { type Groups, useGroups } from "./useGroups.js";
10
11
  export { useHistory } from "./useHistory.js";
11
12
  export { type Listing, type ListingOptions, type Paging, useListing, } from "./useListing.js";
@@ -14,4 +15,5 @@ export { useNameOf } from "./useNameOf.js";
14
15
  export { type PivotAxes, type PivotReading, usePivot, } from "./usePivot.js";
15
16
  export { type Editing, type RecordReading, type RecordState, useRecord, } from "./useRecord.js";
16
17
  export { useRelated } from "./useRelated.js";
18
+ export { type ServiceState, useService } from "./useService.js";
17
19
  export { useSummaryOf } from "./useSummary.js";
@@ -6,6 +6,7 @@
6
6
  export { FrameworkProvider, useEntityFor, useFramework, } from "./context.js";
7
7
  export { ListScopeProvider, useListScope, } from "./scope.js";
8
8
  export { useCommand, } from "./useCommand.js";
9
+ export { useContextValues } from "./useContextValues.js";
9
10
  export { useGroups } from "./useGroups.js";
10
11
  export { useHistory } from "./useHistory.js";
11
12
  export { useListing, } from "./useListing.js";
@@ -14,4 +15,5 @@ export { useNameOf } from "./useNameOf.js";
14
15
  export { usePivot, } from "./usePivot.js";
15
16
  export { useRecord, } from "./useRecord.js";
16
17
  export { useRelated } from "./useRelated.js";
18
+ export { useService } from "./useService.js";
17
19
  export { useSummaryOf } from "./useSummary.js";
@@ -13,7 +13,7 @@ import { type ReactNode } from "react";
13
13
  */
14
14
  export type ListScope = {
15
15
  key: string;
16
- where: Expression;
16
+ selection: Expression;
17
17
  };
18
18
  export declare function ListScopeProvider({ value, children, }: {
19
19
  value: ListScope;
@@ -8,5 +8,5 @@ export function ListScopeProvider({ value, children, }) {
8
8
  /** The cut the screen gave for the lists of `key`, or none. */
9
9
  export function useListScope(key) {
10
10
  const scope = useContext(Scope);
11
- return scope !== null && scope.key === key ? scope.where : undefined;
11
+ return scope !== null && scope.key === key ? scope.selection : undefined;
12
12
  }
@@ -1,5 +1,5 @@
1
+ import { type CommandAnswers, type CommandBodies, type Entity } from "@sincpro/client-framework";
1
2
  import type { AnyRecord } from "@sincpro/criteria";
2
- import { type CommandAnswers, type CommandBodies, type Entity } from "../core/entity.js";
3
3
  /**
4
4
  * One Command of an entity: run it, or ask first what it would do.
5
5
  *
@@ -1,6 +1,5 @@
1
1
  "use client";
2
- import { bodyFor, pathFor, } from "../core/entity.js";
3
- import { retryKeys } from "../core/retry.js";
2
+ import { bodyFor, pathFor, retryKeys, } from "@sincpro/client-framework";
4
3
  import { useFramework } from "./context.js";
5
4
  import { useCallback, useEffect, useRef, useState } from "react";
6
5
  export function useCommand(entity, name) {
@@ -0,0 +1,15 @@
1
+ import type { Context, ContextValues } from "@sincpro/client-framework";
2
+ /**
3
+ * The values of one level of the context, drawn again whenever they change.
4
+ *
5
+ * const { company_id } = useContextValues(processContext); // the company picker changed it
6
+ * const own = useContextValues(project.context); // only what «project» runs with
7
+ *
8
+ * Context: the context is @sincpro/client-framework's — what every use case runs with and sends
9
+ * to the server — and where it lives is the platform's (`ContextStore`, chosen in `main` with
10
+ * `configureProcess`). A screen only reads it: a level's `values` is the same object until it
11
+ * changes and its `watch` is bound, which is exactly what React's external stores ask for. Writing
12
+ * stays the level's (`processContext.set({ company_id })`), so a screen, a service and a test
13
+ * change it the same way and nothing here is a second store.
14
+ */
15
+ export declare function useContextValues<C extends object = ContextValues>(context: Context<C>): Readonly<Partial<C>>;
@@ -0,0 +1,19 @@
1
+ "use client";
2
+ import { useSyncExternalStore } from "react";
3
+ /**
4
+ * The values of one level of the context, drawn again whenever they change.
5
+ *
6
+ * const { company_id } = useContextValues(processContext); // the company picker changed it
7
+ * const own = useContextValues(project.context); // only what «project» runs with
8
+ *
9
+ * Context: the context is @sincpro/client-framework's — what every use case runs with and sends
10
+ * to the server — and where it lives is the platform's (`ContextStore`, chosen in `main` with
11
+ * `configureProcess`). A screen only reads it: a level's `values` is the same object until it
12
+ * changes and its `watch` is bound, which is exactly what React's external stores ask for. Writing
13
+ * stays the level's (`processContext.set({ company_id })`), so a screen, a service and a test
14
+ * change it the same way and nothing here is a second store.
15
+ */
16
+ export function useContextValues(context) {
17
+ const values = () => context.values;
18
+ return useSyncExternalStore(context.watch, values, values);
19
+ }
@@ -1,5 +1,5 @@
1
+ import { type Entity } from "@sincpro/client-framework";
1
2
  import type { AnyRecord } from "@sincpro/criteria";
2
- import type { Entity } from "../core/entity.js";
3
3
  import { type HistoryEntry } from "../core/history.js";
4
4
  import { type DependencyList } from "react";
5
5
  export declare function useHistory<T extends AnyRecord>(entity: Entity<T>, id: string, deps?: DependencyList): HistoryEntry[] | undefined;
@@ -1,4 +1,5 @@
1
1
  "use client";
2
+ import {} from "@sincpro/client-framework";
2
3
  import { historyOf } from "../core/history.js";
3
4
  import { useFramework } from "./context.js";
4
5
  import { useEffect, useState } from "react";
@@ -1,5 +1,5 @@
1
+ import { type CommandBodies, type Entity } from "@sincpro/client-framework";
1
2
  import { type AnyRecord, type Count, type Criteria, type Dropped, type Expression, type Meta, type ReadingStatus, type Sort } from "@sincpro/criteria";
2
- import { type CommandBodies, type Entity } from "../core/entity.js";
3
3
  import { type ListDefinition } from "../core/list.js";
4
4
  /**
5
5
  * N records of an entity, live: what a list draws and the gestures that drive it.
@@ -47,9 +47,9 @@ export type Listing<T, C extends CommandBodies = CommandBodies> = {
47
47
  search: (text: string, options?: {
48
48
  now?: boolean;
49
49
  }) => void;
50
- /** What the search bar put; `undefined` is nothing. */
51
- where: Expression<T> | undefined;
52
- filter: (where: Expression<T> | undefined) => void;
50
+ /** What the search bar put, as criteria's `selection`; `undefined` is nothing. */
51
+ selection: Expression<T> | undefined;
52
+ filter: (selection: Expression<T> | undefined) => void;
53
53
  /** The order the server was asked for, by one field then the next; empty is the entity's own
54
54
  * default. */
55
55
  order: readonly Sort[];
@@ -1,7 +1,7 @@
1
1
  "use client";
2
+ import { listRoute } from "@sincpro/client-framework";
2
3
  import { merge, resource, resumingFrom, searchOver, } from "@sincpro/criteria";
3
4
  import { useReading } from "@sincpro/criteria-react";
4
- import { listRoute, } from "../core/entity.js";
5
5
  import { isList, scopeOf } from "../core/list.js";
6
6
  import { useFramework } from "./context.js";
7
7
  import { useListScope } from "./scope.js";
@@ -51,7 +51,7 @@ export function useListing(given, own = {}) {
51
51
  const meta = reading.meta ?? known.current;
52
52
  const [narrowing, setNarrowing] = useState({
53
53
  text: "",
54
- where: undefined,
54
+ selection: undefined,
55
55
  order: [],
56
56
  });
57
57
  const typing = useRef(null);
@@ -61,8 +61,8 @@ export function useListing(given, own = {}) {
61
61
  const found = searchOver(meta, next.text);
62
62
  if (found !== undefined)
63
63
  criteria = merge(criteria, { selection: found });
64
- if (next.where !== undefined)
65
- criteria = merge(criteria, { selection: next.where });
64
+ if (next.selection !== undefined)
65
+ criteria = merge(criteria, { selection: next.selection });
66
66
  if (next.order.length > 0)
67
67
  criteria = merge(criteria, { order: [...next.order] });
68
68
  return criteria;
@@ -135,8 +135,8 @@ export function useListing(given, own = {}) {
135
135
  reload: () => void reading.reload(),
136
136
  text: narrowing.text,
137
137
  search: (text, options) => narrow({ text }, options?.now !== true),
138
- where: narrowing.where,
139
- filter: (where) => narrow({ where }, false),
138
+ selection: narrowing.selection,
139
+ filter: (selection) => narrow({ selection }, false),
140
140
  order: narrowing.order,
141
141
  orderBy: (order) => narrow({ order: order === null ? [] : Array.isArray(order) ? order : [order] }, false),
142
142
  paging,
@@ -1,5 +1,5 @@
1
+ import { type Entity } from "@sincpro/client-framework";
1
2
  import { type AnyRecord, type Criteria, type Meta } from "@sincpro/criteria";
2
- import type { Entity } from "../core/entity.js";
3
3
  import { type ListDefinition } from "../core/list.js";
4
4
  import { type LookupOption } from "../core/lookup.js";
5
5
  /**
@@ -1,4 +1,5 @@
1
1
  "use client";
2
+ import {} from "@sincpro/client-framework";
2
3
  import { merge } from "@sincpro/criteria";
3
4
  import { isList } from "../core/list.js";
4
5
  import { lookupOf } from "../core/lookup.js";
@@ -1,4 +1,4 @@
1
- import { type Entity } from "../core/entity.js";
1
+ import { type Entity } from "@sincpro/client-framework";
2
2
  /**
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.
@@ -1,5 +1,5 @@
1
1
  "use client";
2
- import { listRoute } from "../core/entity.js";
2
+ import { listRoute } from "@sincpro/client-framework";
3
3
  import { lookupOf } from "../core/lookup.js";
4
4
  import { useFramework } from "./context.js";
5
5
  import { useEffect, useState } from "react";
@@ -1,7 +1,6 @@
1
+ import { type Draft, type Entity, type EntityRef } from "@sincpro/client-framework";
1
2
  import type { AnyRecord, Meta } from "@sincpro/criteria";
2
3
  import { type Advice, type FieldState } from "../core/edit.js";
3
- import { type Entity, type EntityRef } from "../core/entity.js";
4
- import { type Draft } from "../core/transport.js";
5
4
  /**
6
5
  * ONE record of an entity, with the definition of its model — read, and edited.
7
6
  *
@@ -1,10 +1,7 @@
1
1
  "use client";
2
+ import { newEntityId, pathFor, retryKeys, startingValuesOf, TransportError, WITH_PARAM, } from "@sincpro/client-framework";
2
3
  import { changesOf, computedLocally, creationOf, defaultsOf, errorsByField, fieldStateOf, lineStatesOf, missingOf, needsPreview, } from "../core/edit.js";
3
- import { pathFor, startingValuesOf, WITH_PARAM, } from "../core/entity.js";
4
- import { newEntityId } from "../core/ids.js";
5
4
  import { linesAnswered } from "../core/lines.js";
6
- import { retryKeys } from "../core/retry.js";
7
- import { TransportError } from "../core/transport.js";
8
5
  import { useFramework } from "./context.js";
9
6
  import { useCallback, useEffect, useMemo, useRef, useState } from "react";
10
7
  /** How long typing has to pause before the server is asked what the record becomes. */
@@ -1,5 +1,5 @@
1
+ import { type Entity } from "@sincpro/client-framework";
1
2
  import type { AnyRecord, Criteria } from "@sincpro/criteria";
2
- import type { Entity } from "../core/entity.js";
3
3
  import type { Listing, ListingOptions } from "./useListing.js";
4
4
  import type { RecordReading } from "./useRecord.js";
5
5
  /**
@@ -1,4 +1,5 @@
1
1
  "use client";
2
+ import {} from "@sincpro/client-framework";
2
3
  import { useListing } from "./useListing.js";
3
4
  /**
4
5
  * The records a relation of ONE record points to, as a live listing of the related entity.
@@ -0,0 +1,38 @@
1
+ /**
2
+ * What a screen keeps of running one of the application's use cases.
3
+ *
4
+ * `run` answers what the use case answered, or `undefined` when it failed — the failure is in
5
+ * `error`, for the screen to say (`explain`, `Refused`); it never throws at the screen.
6
+ */
7
+ export type ServiceState<A extends unknown[], R> = {
8
+ run: (...args: A) => Promise<R | undefined>;
9
+ /** A run is on its way. */
10
+ busy: boolean;
11
+ /** What the last run answered, until another one starts or `reset`. */
12
+ answer: R | undefined;
13
+ error: unknown;
14
+ reset: () => void;
15
+ };
16
+ /** A use case as a screen runs it: a method of a service, read from its bounded context. */
17
+ type UseCase<A extends unknown[], R> = (...args: A) => R | Promise<R>;
18
+ /**
19
+ * One of the application's use cases, run from a screen.
20
+ *
21
+ * const close = useService(project.Issues.close);
22
+ * <Button disabled={close.busy} onPress={() => close.run({ issue_id })}>Cerrar</Button>
23
+ * {close.error ? <Refused error={close.error} /> : null}
24
+ *
25
+ * Context: all of an application's logic lives in its services (@sincpro/client-framework's
26
+ * `Feature` and `ApplicationService`, reached through their bounded context), traced and reported
27
+ * by its interceptors; what a screen adds is the state of running one — busy, the answer, the
28
+ * failure. Written by hand, that state is the 300-line context every mobile screen repeats; here
29
+ * it is one hook, the same for every use case. The span and the report are the service's: this
30
+ * hook neither opens one nor reports. A bounded context answers the same function every time a
31
+ * method is read, so `project.Issues.close` can be handed as it is.
32
+ *
33
+ * The last run wins: an answer that arrives after a newer run started is dropped, so a slow first
34
+ * click never overwrites the second. A run is never aborted — the server may have done it — but
35
+ * its answer does not land on a screen that left.
36
+ */
37
+ export declare function useService<A extends unknown[], R>(useCase: UseCase<A, R>): ServiceState<A, Awaited<R>>;
38
+ export {};
@@ -0,0 +1,66 @@
1
+ "use client";
2
+ import { useCallback, useEffect, useRef, useState } from "react";
3
+ /**
4
+ * One of the application's use cases, run from a screen.
5
+ *
6
+ * const close = useService(project.Issues.close);
7
+ * <Button disabled={close.busy} onPress={() => close.run({ issue_id })}>Cerrar</Button>
8
+ * {close.error ? <Refused error={close.error} /> : null}
9
+ *
10
+ * Context: all of an application's logic lives in its services (@sincpro/client-framework's
11
+ * `Feature` and `ApplicationService`, reached through their bounded context), traced and reported
12
+ * by its interceptors; what a screen adds is the state of running one — busy, the answer, the
13
+ * failure. Written by hand, that state is the 300-line context every mobile screen repeats; here
14
+ * it is one hook, the same for every use case. The span and the report are the service's: this
15
+ * hook neither opens one nor reports. A bounded context answers the same function every time a
16
+ * method is read, so `project.Issues.close` can be handed as it is.
17
+ *
18
+ * The last run wins: an answer that arrives after a newer run started is dropped, so a slow first
19
+ * click never overwrites the second. A run is never aborted — the server may have done it — but
20
+ * its answer does not land on a screen that left.
21
+ */
22
+ export function useService(useCase) {
23
+ const [busy, setBusy] = useState(false);
24
+ const [answer, setAnswer] = useState(undefined);
25
+ const [error, setError] = useState(null);
26
+ // The use case as the latest render gave it: a run started from an old callback still runs
27
+ // the one the screen holds now (`project.scoped(…)` changed, say).
28
+ const latest = useRef(useCase);
29
+ latest.current = useCase;
30
+ const mounted = useRef(true);
31
+ useEffect(() => {
32
+ mounted.current = true;
33
+ return () => void (mounted.current = false);
34
+ }, []);
35
+ const runs = useRef(0);
36
+ const run = useCallback(async (...args) => {
37
+ const mine = ++runs.current;
38
+ const current = () => mounted.current && runs.current === mine;
39
+ setBusy(true);
40
+ setError(null);
41
+ try {
42
+ const answered = (await latest.current(...args));
43
+ if (current())
44
+ setAnswer(answered);
45
+ return answered;
46
+ }
47
+ catch (failure) {
48
+ if (current()) {
49
+ setAnswer(undefined);
50
+ setError(failure);
51
+ }
52
+ return undefined;
53
+ }
54
+ finally {
55
+ if (current())
56
+ setBusy(false);
57
+ }
58
+ }, []);
59
+ const reset = useCallback(() => {
60
+ runs.current += 1;
61
+ setBusy(false);
62
+ setAnswer(undefined);
63
+ setError(null);
64
+ }, []);
65
+ return { run, busy, answer, error, reset };
66
+ }
@@ -1,5 +1,5 @@
1
+ import { type Entity } from "@sincpro/client-framework";
1
2
  import type { AnyRecord, Meta } from "@sincpro/criteria";
2
- import type { Entity } from "../core/entity.js";
3
3
  import { type RecordSummary } from "../core/summary.js";
4
4
  /**
5
5
  * The summary of any record of an entity, read with the model this application already knows
@@ -1,4 +1,5 @@
1
1
  "use client";
2
+ import {} from "@sincpro/client-framework";
2
3
  import { summaryOf } from "../core/summary.js";
3
4
  import { useFramework } from "./context.js";
4
5
  import { useCallback } from "react";