@sincpro/web-framework 0.3.0 → 0.4.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.
@@ -25,3 +25,4 @@ export { completeMeta, type JsonSchema, metaFromSchema, type SchemaComponents, }
25
25
  export { type RecordSummary, type SummaryFields, summaryFieldsOf, summaryOf, type SummaryPart, } from "./summary.js";
26
26
  export { placeOf, stepIn, type Trail } from "./trail.js";
27
27
  export { type Draft, explain, type Method, type One, type SendOptions, type Transport, TransportError, } from "./transport.js";
28
+ export { installTimeZone, instantOf, timeZoneNow, wallTimeOf, } from "./zone.js";
@@ -25,3 +25,4 @@ export { completeMeta, metaFromSchema, } from "./schema.js";
25
25
  export { summaryFieldsOf, summaryOf, } from "./summary.js";
26
26
  export { placeOf, stepIn } from "./trail.js";
27
27
  export { explain, TransportError, } from "./transport.js";
28
+ export { installTimeZone, instantOf, timeZoneNow, wallTimeOf, } from "./zone.js";
@@ -29,8 +29,7 @@ export declare function lineFields(definition: Meta | null): string[];
29
29
  *
30
30
  * Context: a cell holds a control as wide as its column. A many2one is a picker only when the
31
31
  * application declared the entity it points at — otherwise its key is an id nobody types.
32
- * A date and time, an embedded value or anything the table has no control for needs the
33
- * dialog's room.
32
+ * An embedded value or anything the table has no control for needs the dialog's room.
34
33
  */
35
34
  export declare function writtenInCell(definition: Meta, field: string, declared: Declared): boolean;
36
35
  /** What decides where a line is written, besides its model. */
@@ -74,6 +74,7 @@ const CELL_TYPES = new Set([
74
74
  "integer",
75
75
  "number",
76
76
  "date",
77
+ "datetime",
77
78
  "boolean",
78
79
  "text[]",
79
80
  ]);
@@ -92,8 +93,7 @@ export function lineFields(definition) {
92
93
  *
93
94
  * Context: a cell holds a control as wide as its column. A many2one is a picker only when the
94
95
  * application declared the entity it points at — otherwise its key is an id nobody types.
95
- * A date and time, an embedded value or anything the table has no control for needs the
96
- * dialog's room.
96
+ * An embedded value or anything the table has no control for needs the dialog's room.
97
97
  */
98
98
  export function writtenInCell(definition, field, declared) {
99
99
  const relation = relationFieldOf(definition, field);
@@ -1,6 +1,7 @@
1
1
  import { choiceLabelOf, labelOf, } from "@sincpro/criteria";
2
2
  import { frameworkCopy } from "./copy.js";
3
3
  import { relatedOf } from "./related.js";
4
+ import { timeZoneNow } from "./zone.js";
4
5
  /** Present when a field is named that the model does not declare: drawn plain, never thrown. */
5
6
  const UNKNOWN = {
6
7
  type: "unknown",
@@ -63,10 +64,14 @@ function decimals(text) {
63
64
  */
64
65
  const formatters = new Map();
65
66
  function dateFormat(locale, withTime) {
66
- const key = `d|${locale ?? ""}|${withTime}`;
67
+ // An instant reads on the wall clock of the installed zone; a calendar day has no zone.
68
+ const zone = withTime ? timeZoneNow() : "";
69
+ const key = `d|${locale ?? ""}|${withTime}|${zone}`;
67
70
  let format = formatters.get(key);
68
71
  if (format === undefined) {
69
- format = new Intl.DateTimeFormat(locale, withTime ? { dateStyle: "medium", timeStyle: "short" } : { dateStyle: "medium" });
72
+ format = new Intl.DateTimeFormat(locale, withTime
73
+ ? { dateStyle: "medium", timeStyle: "short", timeZone: zone }
74
+ : { dateStyle: "medium" });
70
75
  formatters.set(key, format);
71
76
  }
72
77
  return format;
@@ -0,0 +1,27 @@
1
+ /**
2
+ * The time zone a date and time is read and written in — and the two conversions a form needs:
3
+ * the server's instant to the wall clock of that zone, and back.
4
+ *
5
+ * Context: sincpro_framework sends a `datetime` as an instant (`2026-03-12T14:30:00Z`); a person
6
+ * reads and types the wall clock of where the business is (10:30 in La Paz). Which zone that is
7
+ * belongs to whoever is above the screens — the application, the company the user works in —
8
+ * so it is installed once, like the framework's words (`FrameworkProvider timeZone`), and every
9
+ * date and time follows it. Without it, the runtime's. A date with no time (`date`) is a day of
10
+ * the calendar, not an instant: it is never converted.
11
+ */
12
+ /** Install the time zone dates and times are read and written in (IANA: `"America/La_Paz"`);
13
+ * `undefined` goes back to the runtime's. */
14
+ export declare function installTimeZone(timeZone: string | undefined): void;
15
+ /** The time zone in use. */
16
+ export declare function timeZoneNow(): string;
17
+ /** `"2026-03-12T14:30:00Z"` → `"2026-03-12T10:30"` in `America/La_Paz`: the wall clock there,
18
+ * as a date-and-time control reads it. `null` when it is not an instant. */
19
+ export declare function wallTimeOf(instant: string, timeZone: string): string | null;
20
+ /**
21
+ * `"2026-03-12T10:30"` on the wall clock of `timeZone` → the instant, `"2026-03-12T14:30:00.000Z"`.
22
+ * `null` when it is not a wall time.
23
+ *
24
+ * Context: the offset depends on the instant (daylight saving), so it is asked twice: at the
25
+ * first guess, and again at the answer when the guess fell on the other side of a change.
26
+ */
27
+ export declare function instantOf(wall: string, timeZone: string): string | null;
@@ -0,0 +1,75 @@
1
+ /**
2
+ * The time zone a date and time is read and written in — and the two conversions a form needs:
3
+ * the server's instant to the wall clock of that zone, and back.
4
+ *
5
+ * Context: sincpro_framework sends a `datetime` as an instant (`2026-03-12T14:30:00Z`); a person
6
+ * reads and types the wall clock of where the business is (10:30 in La Paz). Which zone that is
7
+ * belongs to whoever is above the screens — the application, the company the user works in —
8
+ * so it is installed once, like the framework's words (`FrameworkProvider timeZone`), and every
9
+ * date and time follows it. Without it, the runtime's. A date with no time (`date`) is a day of
10
+ * the calendar, not an instant: it is never converted.
11
+ */
12
+ let installed;
13
+ /** Install the time zone dates and times are read and written in (IANA: `"America/La_Paz"`);
14
+ * `undefined` goes back to the runtime's. */
15
+ export function installTimeZone(timeZone) {
16
+ installed = timeZone;
17
+ }
18
+ /** The time zone in use. */
19
+ export function timeZoneNow() {
20
+ return installed ?? Intl.DateTimeFormat().resolvedOptions().timeZone;
21
+ }
22
+ const clocks = new Map();
23
+ /** One formatter per zone, kept: building one costs far more than using it. */
24
+ function clockOf(timeZone) {
25
+ let clock = clocks.get(timeZone);
26
+ if (clock === undefined) {
27
+ clock = new Intl.DateTimeFormat("en-US", {
28
+ timeZone,
29
+ hourCycle: "h23",
30
+ year: "numeric",
31
+ month: "2-digit",
32
+ day: "2-digit",
33
+ hour: "2-digit",
34
+ minute: "2-digit",
35
+ second: "2-digit",
36
+ });
37
+ clocks.set(timeZone, clock);
38
+ }
39
+ return clock;
40
+ }
41
+ /** The wall clock of `timeZone` at `at` (ms), as the fields of a UTC date: what to compare. */
42
+ function wallAt(at, timeZone) {
43
+ const parts = Object.fromEntries(clockOf(timeZone)
44
+ .formatToParts(new Date(at))
45
+ .map((part) => [part.type, part.value]));
46
+ return Date.UTC(Number(parts["year"]), Number(parts["month"]) - 1, Number(parts["day"]), Number(parts["hour"]), Number(parts["minute"]), Number(parts["second"]));
47
+ }
48
+ const pad = (n) => String(n).padStart(2, "0");
49
+ /** `"2026-03-12T14:30:00Z"` → `"2026-03-12T10:30"` in `America/La_Paz`: the wall clock there,
50
+ * as a date-and-time control reads it. `null` when it is not an instant. */
51
+ export function wallTimeOf(instant, timeZone) {
52
+ const at = Date.parse(instant);
53
+ if (Number.isNaN(at))
54
+ return null;
55
+ const wall = new Date(wallAt(at, timeZone));
56
+ return (`${wall.getUTCFullYear()}-${pad(wall.getUTCMonth() + 1)}-${pad(wall.getUTCDate())}` +
57
+ `T${pad(wall.getUTCHours())}:${pad(wall.getUTCMinutes())}`);
58
+ }
59
+ /**
60
+ * `"2026-03-12T10:30"` on the wall clock of `timeZone` → the instant, `"2026-03-12T14:30:00.000Z"`.
61
+ * `null` when it is not a wall time.
62
+ *
63
+ * Context: the offset depends on the instant (daylight saving), so it is asked twice: at the
64
+ * first guess, and again at the answer when the guess fell on the other side of a change.
65
+ */
66
+ export function instantOf(wall, timeZone) {
67
+ const match = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})/.exec(wall);
68
+ if (match === null)
69
+ return null;
70
+ const [, year, month, day, hour, minute] = match.map(Number);
71
+ const guess = Date.UTC(year, month - 1, day, hour, minute);
72
+ const first = guess - (wallAt(guess, timeZone) - guess);
73
+ const second = guess - (wallAt(first, timeZone) - first);
74
+ return new Date(second).toISOString();
75
+ }
@@ -29,7 +29,7 @@ type FrameworkContextValue = {
29
29
  /** Where each open record came from (`Trail`), by the key its reference carries. */
30
30
  trails: Map<string, Trail>;
31
31
  };
32
- export declare function FrameworkProvider({ transport, locale, metas: seed, entities, children, }: {
32
+ export declare function FrameworkProvider({ transport, locale, metas: seed, entities, timeZone, children, }: {
33
33
  transport: Transport;
34
34
  /** The language labels and values are read in (`"es"`); the model's `default` without it. */
35
35
  locale?: string;
@@ -38,6 +38,10 @@ export declare function FrameworkProvider({ transport, locale, metas: seed, enti
38
38
  /** The entities of the application: a form draws a many2one as a picker of the one its
39
39
  * model points at (`relation: "Cliente"`) without being told. */
40
40
  entities?: readonly Entity[];
41
+ /** Where the business is (IANA: `"America/La_Paz"`) — the application's, or the company's
42
+ * the user works in: every date and time reads and is typed on its wall clock. Without it,
43
+ * the browser's. */
44
+ timeZone?: string;
41
45
  children: ReactNode;
42
46
  }): import("react").JSX.Element;
43
47
  export declare function useFramework(): FrameworkContextValue;
@@ -2,9 +2,17 @@
2
2
  import { jsx as _jsx } from "react/jsx-runtime";
3
3
  import { metaStore } from "../core/metas.js";
4
4
  import { entityFor } from "../core/relation.js";
5
- import { createContext, useContext, useMemo, useState } from "react";
5
+ import { installTimeZone } from "../core/zone.js";
6
+ import { createContext, useContext, useMemo, useRef, useState } from "react";
6
7
  const FrameworkContext = createContext(null);
7
- export function FrameworkProvider({ transport, locale, metas: seed, entities = NO_ENTITIES, children, }) {
8
+ export function FrameworkProvider({ transport, locale, metas: seed, entities = NO_ENTITIES, timeZone, children, }) {
9
+ // Installed before the children draw, and again when it changes (another company): the
10
+ // formatters read it, so it cannot wait for an effect.
11
+ const zoned = useRef(null);
12
+ if (zoned.current !== timeZone) {
13
+ installTimeZone(timeZone);
14
+ zoned.current = timeZone;
15
+ }
8
16
  // One store for the provider's life: a new seed object on a re-render must not forget what
9
17
  // the screens already learned.
10
18
  const [metas] = useState(() => metaStore(seed));
@@ -13,7 +13,8 @@ export type FieldControl = {
13
13
  standalone?: boolean;
14
14
  };
15
15
  /**
16
- * The control a field's TYPE asks for — a choice, a number, a date, a yes/no, a list of words,
16
+ * The control a field's TYPE asks for — a choice, a number, a date, a date and time, a yes/no, a
17
+ * list of words,
17
18
  * a text.
18
19
  * Shared by a record's field (`RecordField`) and a line's cell and dialog (`RecordLinesEditor`):
19
20
  * one rule for what a value is edited with.
@@ -1,10 +1,12 @@
1
1
  "use client";
2
2
  import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { instantOf, timeZoneNow, wallTimeOf } from "../../core/zone.js";
3
4
  import { EntityPicker } from "./EntityPicker.js";
4
5
  import { useRecordOpening } from "./opening.js";
5
- import { Checkbox, DatePicker, NumberInput, Select, TagsField, TextInput, } from "@sincpro/web-ui";
6
+ import { Checkbox, DatePicker, DateTimePicker, NumberInput, Select, TagsField, TextInput, } from "@sincpro/web-ui";
6
7
  /**
7
- * The control a field's TYPE asks for — a choice, a number, a date, a yes/no, a list of words,
8
+ * The control a field's TYPE asks for — a choice, a number, a date, a date and time, a yes/no, a
9
+ * list of words,
8
10
  * a text.
9
11
  * Shared by a record's field (`RecordField`) and a line's cell and dialog (`RecordLinesEditor`):
10
12
  * one rule for what a value is edited with.
@@ -22,6 +24,11 @@ export function controlFor(control, type, exact, choices) {
22
24
  return (_jsx(NumberInput, { value: value === null || value === undefined || value === "" ? "" : Number(value),
23
25
  // A decimal travels as text so no digit is lost; the server reads it that way back.
24
26
  onChange: (next) => set(next === "" ? null : exact ? String(next) : next), ...named }));
27
+ case "datetime": {
28
+ // An instant from the server, typed on the wall clock of the installed zone.
29
+ const zone = timeZoneNow();
30
+ return (_jsx(DateTimePicker, { value: typeof value === "string" && value !== "" ? wallTimeOf(value, zone) : null, onChange: (wall) => set(wall === null ? null : instantOf(wall, zone)), ...named }));
31
+ }
25
32
  case "date":
26
33
  return (_jsx(DatePicker, { value: typeof value === "string" && value !== "" ? value : null, onChange: set, ...named }));
27
34
  case "text[]": {
@@ -15,7 +15,7 @@ import { type ReactNode } from "react";
15
15
  * What it does NOT own: the session (the app builds the transport with its headers) and the
16
16
  * application bar (the app draws its own, around this or beside it).
17
17
  */
18
- export declare function SincproApp({ transport, views, start, language, metas, entities, empty, skin, cache, }: {
18
+ export declare function SincproApp({ transport, views, start, language, metas, entities, empty, skin, cache, timeZone, }: {
19
19
  transport: Transport;
20
20
  /** The entities as composer views — `entityView(…)` — and any other view of the app. */
21
21
  views: readonly ViewSpec[];
@@ -37,4 +37,7 @@ export declare function SincproApp({ transport, views, start, language, metas, e
37
37
  * `false` asks the server every time.
38
38
  */
39
39
  cache?: CacheOptions | false;
40
+ /** Where the business is (`"America/La_Paz"`): every date and time on its wall clock.
41
+ * Without it, the browser's. */
42
+ timeZone?: string;
40
43
  }): import("react").JSX.Element;
@@ -19,7 +19,7 @@ import { useEffect, useMemo, useState } from "react";
19
19
  * What it does NOT own: the session (the app builds the transport with its headers) and the
20
20
  * application bar (the app draws its own, around this or beside it).
21
21
  */
22
- export function SincproApp({ transport, views, start, language = "es", metas, entities, empty, skin = webUiSkin, cache = {}, }) {
22
+ export function SincproApp({ transport, views, start, language = "es", metas, entities, empty, skin = webUiSkin, cache = {}, timeZone, }) {
23
23
  // Installed once, before anything draws: the three tables are module state of their packages.
24
24
  useState(() => {
25
25
  if (language === "es") {
@@ -41,5 +41,5 @@ export function SincproApp({ transport, views, start, language = "es", metas, en
41
41
  // The first screen only: a re-render must not push it again.
42
42
  // eslint-disable-next-line react-hooks/exhaustive-deps
43
43
  }, [workspace]);
44
- return (_jsx(FrameworkProvider, { transport: cached, locale: language, ...(metas === undefined ? {} : { metas }), ...(entities === undefined ? {} : { entities }), children: _jsx(ToastProvider, { children: _jsx("div", { className: "flex h-dvh min-h-0 flex-col bg-ground", children: _jsx(Composer, { workspace: workspace, registry: registry, skin: skin, ...(empty === undefined ? {} : { empty }) }) }) }) }));
44
+ return (_jsx(FrameworkProvider, { transport: cached, locale: language, ...(metas === undefined ? {} : { metas }), ...(entities === undefined ? {} : { entities }), ...(timeZone === undefined ? {} : { timeZone }), children: _jsx(ToastProvider, { children: _jsx("div", { className: "flex h-dvh min-h-0 flex-col bg-ground", children: _jsx(Composer, { workspace: workspace, registry: registry, skin: skin, ...(empty === undefined ? {} : { empty }) }) }) }) }));
45
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sincpro/web-framework",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "The web client of sincpro_framework: entities, lists and records bound to the metadata the server publishes, drawn with @sincpro/web-ui and arranged with @sincpro/web-composer.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",