@sincpro/web-framework 3.13.0 → 3.14.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.
@@ -32,6 +32,8 @@ export type FrameworkCopy = {
32
32
  capped: (shown: number) => string;
33
33
  /** Conditions the server could not answer: the list is WIDER than asked. */
34
34
  dropped: (fields: readonly string[]) => string;
35
+ /** A row put in its place by hand was dropped among another group's: nothing was written. */
36
+ reorderElsewhere: string;
35
37
  /** The group of the records that hold no value in the field grouped by. */
36
38
  noValue: string;
37
39
  /** More groups than shown: narrow the set instead of scrolling them. */
package/dist/core/copy.js CHANGED
@@ -32,6 +32,7 @@ export const ENGLISH_FRAMEWORK_COPY = {
32
32
  builder: "Criteria builder",
33
33
  capped: (shown) => `Showing the first ${shown}. Narrow the search to see the rest.`,
34
34
  dropped: (fields) => `The server ignored a condition on ${fields.join(", ")}: the list shows more than asked.`,
35
+ reorderElsewhere: "A row only moves among the rows of its own group.",
35
36
  noValue: "No value",
36
37
  groupsCapped: (shown) => `The first ${shown} groups. Narrow the search to see the rest.`,
37
38
  groups: (groups, records) => `${groups} ${groups === 1 ? "group" : "groups"} · ${records} ${records === 1 ? "record" : "records"}`,
@@ -137,6 +138,7 @@ export const SPANISH_FRAMEWORK_COPY = {
137
138
  builder: "Constructor de criteria",
138
139
  capped: (shown) => `Se muestran los primeros ${shown}. Refiná la búsqueda para ver el resto.`,
139
140
  dropped: (fields) => `El servidor ignoró una condición sobre ${fields.join(", ")}: la lista muestra más de lo pedido.`,
141
+ reorderElsewhere: "Una fila solo se mueve entre las de su mismo grupo.",
140
142
  noValue: "Sin valor",
141
143
  groupsCapped: (shown) => `Los primeros ${shown} grupos. Acotá la búsqueda para ver el resto.`,
142
144
  groups: (groups, records) => `${groups} ${groups === 1 ? "grupo" : "grupos"} · ${records} ${records === 1 ? "registro" : "registros"}`,
@@ -23,6 +23,7 @@ export { detailOf, measuresOf, sumsOf, type Total, totalOf, } from "./measure.js
23
23
  export { amountTextOf, BOOKKEEPING, currencyNow, type DateStyle, dateStyleNow, dayTextOf, defaultFields, fieldAt, fieldMetaOf, type FieldView, installCurrency, installDateStyle, installRegion, instantTextOf, moneyTextOf, numberTextOf, regionNow, shortTextOf, sortKeyOf, STAMPS, textOf, valueAt, viewOf, } from "./present.js";
24
24
  export { type Related, relatedOf } from "./related.js";
25
25
  export { backReference, entityFor, relatedScope, type RelationField, relationFieldOf, } from "./relation.js";
26
+ export { GAP, HANDLE_WIDGET, handleOf, type Move, movesFor, placed, type Reordering, reorderingOf, } from "./reorder.js";
26
27
  export { holdsRichText, RICH_TEXT_WIDGET, richTextPlainOf, type RichTextSettings, } from "./richText.js";
27
28
  export { readOnServer, readRecordOnServer, type RecordRead, type ServerRead, } from "./server.js";
28
29
  export { type RecordSummary, summaryFieldsOf, summaryOf, } from "./summary.js";
@@ -23,6 +23,7 @@ export { detailOf, measuresOf, sumsOf, totalOf, } from "./measure.js";
23
23
  export { amountTextOf, BOOKKEEPING, currencyNow, dateStyleNow, dayTextOf, defaultFields, fieldAt, fieldMetaOf, installCurrency, installDateStyle, installRegion, instantTextOf, moneyTextOf, numberTextOf, regionNow, shortTextOf, sortKeyOf, STAMPS, textOf, valueAt, viewOf, } from "./present.js";
24
24
  export { relatedOf } from "./related.js";
25
25
  export { backReference, entityFor, relatedScope, relationFieldOf, } from "./relation.js";
26
+ export { GAP, HANDLE_WIDGET, handleOf, movesFor, placed, reorderingOf, } from "./reorder.js";
26
27
  export { holdsRichText, RICH_TEXT_WIDGET, richTextPlainOf, } from "./richText.js";
27
28
  export { readOnServer, readRecordOnServer, } from "./server.js";
28
29
  export { summaryFieldsOf, summaryOf, } from "./summary.js";
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Records put in their order by hand: which field is the position, when the rows on screen may
3
+ * be dragged, and what a drop writes.
4
+ *
5
+ * const reordering = reorderingOf(meta, listing.order); // undefined: no grip
6
+ * const moves = movesFor(rows, keyOf, reordering, "h7", "h2");
7
+ * // [{ id: "h7", value: 1500 }] — one Edit of one record
8
+ *
9
+ * Context: sincpro_framework has no reorder Command and needs none (sincpro_framework#390). The
10
+ * position is a plain integer field the model marks with the widget `handle`
11
+ * (`presentation(widgets=lambda h: {h.orden: "handle"})`), the order comes from the criteria,
12
+ * and a move is one normal `Edit` of the record moved. So the rule lives here, once, read from
13
+ * what the backend publishes — not in each application's screen.
14
+ *
15
+ * WHEN — the active order (the Search's, else the model's `default_order`) holds the handle
16
+ * field ascending. The sorts before it are the GROUP (`album_id,orden`: a photo moves within its
17
+ * album); the ones after it only break ties. Sorted by anything else, the order shown is not the
18
+ * one being changed, and there is no grip.
19
+ *
20
+ * WHAT — the new position goes between its neighbours of the same group, with gaps of `GAP`:
21
+ * halfway when there is room, a gap above the first or below the last. Without room (ties, such
22
+ * as a set never ordered — all `0` —, or neighbours one apart) the group on screen is respaced in
23
+ * its new order: one Edit per row whose value changes. Rare, not atomic, and re-running it
24
+ * converges.
25
+ */
26
+ import { type AnyRecord, type Meta, type Sort } from "@sincpro/criteria";
27
+ /** The widget that marks a field as the record's position by hand. */
28
+ export declare const HANDLE_WIDGET = "handle";
29
+ /** The room left between two positions: a thousand moves between them before a respacing. */
30
+ export declare const GAP = 1000;
31
+ export type Reordering = {
32
+ /** The position field. */
33
+ handle: string;
34
+ /** The fields a row keeps while it moves: it only lands among rows with the same values. */
35
+ group: readonly string[];
36
+ };
37
+ /** One write a drop asks for: the record and its new position. */
38
+ export type Move = {
39
+ id: string;
40
+ value: number;
41
+ };
42
+ /** The field the model marks as the position, if any. */
43
+ export declare function handleOf(meta: Meta | null | undefined): string | undefined;
44
+ /** Whether the rows in `order` (the Search's; empty, the model's own) may be dragged, and
45
+ * within which group. */
46
+ export declare function reorderingOf(meta: Meta | null | undefined, order: readonly Sort[]): Reordering | undefined;
47
+ /**
48
+ * The writes that put `id` in front of `before` (`null`: after the last row) among `rows` — the
49
+ * rows on screen, in their order. `"elsewhere"` when the row would land outside its group;
50
+ * `[]` when it is dropped where it already is.
51
+ *
52
+ * 1. The landing place among the rest of the rows, and its neighbours there.
53
+ * 2. A neighbour of another group does not count; with none of its own, it is elsewhere.
54
+ * 3. Final: halfway between the two, a gap past the one, or — without room — the group respaced.
55
+ */
56
+ export declare function movesFor<T extends AnyRecord>(rows: readonly T[], keyOf: (row: T) => string, { handle, group }: Reordering, id: string, before: string | null): Move[] | "elsewhere";
57
+ /** `rows` with `id` in front of `before` (`null`: the end) — what the screen shows while the
58
+ * writes travel. */
59
+ export declare function placed<T>(rows: readonly T[], keyOf: (row: T) => string, id: string, before: string | null): T[];
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Records put in their order by hand: which field is the position, when the rows on screen may
3
+ * be dragged, and what a drop writes.
4
+ *
5
+ * const reordering = reorderingOf(meta, listing.order); // undefined: no grip
6
+ * const moves = movesFor(rows, keyOf, reordering, "h7", "h2");
7
+ * // [{ id: "h7", value: 1500 }] — one Edit of one record
8
+ *
9
+ * Context: sincpro_framework has no reorder Command and needs none (sincpro_framework#390). The
10
+ * position is a plain integer field the model marks with the widget `handle`
11
+ * (`presentation(widgets=lambda h: {h.orden: "handle"})`), the order comes from the criteria,
12
+ * and a move is one normal `Edit` of the record moved. So the rule lives here, once, read from
13
+ * what the backend publishes — not in each application's screen.
14
+ *
15
+ * WHEN — the active order (the Search's, else the model's `default_order`) holds the handle
16
+ * field ascending. The sorts before it are the GROUP (`album_id,orden`: a photo moves within its
17
+ * album); the ones after it only break ties. Sorted by anything else, the order shown is not the
18
+ * one being changed, and there is no grip.
19
+ *
20
+ * WHAT — the new position goes between its neighbours of the same group, with gaps of `GAP`:
21
+ * halfway when there is room, a gap above the first or below the last. Without room (ties, such
22
+ * as a set never ordered — all `0` —, or neighbours one apart) the group on screen is respaced in
23
+ * its new order: one Edit per row whose value changes. Rare, not atomic, and re-running it
24
+ * converges.
25
+ */
26
+ import { parseOrder } from "@sincpro/criteria";
27
+ /** The widget that marks a field as the record's position by hand. */
28
+ export const HANDLE_WIDGET = "handle";
29
+ /** The room left between two positions: a thousand moves between them before a respacing. */
30
+ export const GAP = 1000;
31
+ /** The field the model marks as the position, if any. */
32
+ export function handleOf(meta) {
33
+ if (meta == null)
34
+ return undefined;
35
+ return Object.entries(meta.fields).find(([, field]) => field.widget === HANDLE_WIDGET)?.[0];
36
+ }
37
+ /** Whether the rows in `order` (the Search's; empty, the model's own) may be dragged, and
38
+ * within which group. */
39
+ export function reorderingOf(meta, order) {
40
+ const handle = handleOf(meta);
41
+ if (handle === undefined || meta == null)
42
+ return undefined;
43
+ const active = order.length > 0 ? order : parseOrder(meta.default_order ?? "");
44
+ const at = active.findIndex((sort) => String(sort.field) === handle);
45
+ if (at < 0 || active[at]?.descending === true)
46
+ return undefined;
47
+ return { handle, group: active.slice(0, at).map((sort) => String(sort.field)) };
48
+ }
49
+ const positionOf = (row, handle) => {
50
+ const value = Number(row[handle]);
51
+ return Number.isFinite(value) ? value : 0;
52
+ };
53
+ /**
54
+ * The writes that put `id` in front of `before` (`null`: after the last row) among `rows` — the
55
+ * rows on screen, in their order. `"elsewhere"` when the row would land outside its group;
56
+ * `[]` when it is dropped where it already is.
57
+ *
58
+ * 1. The landing place among the rest of the rows, and its neighbours there.
59
+ * 2. A neighbour of another group does not count; with none of its own, it is elsewhere.
60
+ * 3. Final: halfway between the two, a gap past the one, or — without room — the group respaced.
61
+ */
62
+ export function movesFor(rows, keyOf, { handle, group }, id, before) {
63
+ const index = rows.findIndex((row) => keyOf(row) === id);
64
+ const moved = rows[index];
65
+ const following = rows[index + 1];
66
+ // Dropped where it already is: in front of itself or of the row that follows it.
67
+ const stays = before === id ||
68
+ (following === undefined ? before === null : before === keyOf(following));
69
+ if (moved === undefined || stays)
70
+ return [];
71
+ const groupOf = (row) => JSON.stringify(group.map((field) => row[field] ?? null));
72
+ const own = groupOf(moved);
73
+ const rest = rows.filter((row) => keyOf(row) !== id);
74
+ const at = before === null ? rest.length : rest.findIndex((row) => keyOf(row) === before);
75
+ if (at < 0)
76
+ return [];
77
+ const above = rest[at - 1];
78
+ const below = rest[at];
79
+ const prev = above !== undefined && groupOf(above) === own ? above : undefined;
80
+ const next = below !== undefined && groupOf(below) === own ? below : undefined;
81
+ if (prev === undefined && next === undefined)
82
+ return "elsewhere";
83
+ const low = prev === undefined ? undefined : positionOf(prev, handle);
84
+ const high = next === undefined ? undefined : positionOf(next, handle);
85
+ if (low === undefined && high !== undefined)
86
+ return [{ id, value: high - GAP }];
87
+ if (high === undefined && low !== undefined)
88
+ return [{ id, value: low + GAP }];
89
+ if (low !== undefined && high !== undefined && high - low > 1)
90
+ return [{ id, value: Math.floor((low + high) / 2) }];
91
+ const placed = [...rest.slice(0, at), moved, ...rest.slice(at)].filter((row) => groupOf(row) === own);
92
+ return placed.flatMap((row, index) => {
93
+ const value = (index + 1) * GAP;
94
+ return positionOf(row, handle) === value ? [] : [{ id: keyOf(row), value }];
95
+ });
96
+ }
97
+ /** `rows` with `id` in front of `before` (`null`: the end) — what the screen shows while the
98
+ * writes travel. */
99
+ export function placed(rows, keyOf, id, before) {
100
+ const moved = rows.find((row) => keyOf(row) === id);
101
+ if (moved === undefined)
102
+ return [...rows];
103
+ const rest = rows.filter((row) => keyOf(row) !== id);
104
+ const at = before === null ? rest.length : rest.findIndex((row) => keyOf(row) === before);
105
+ return at < 0 ? [...rows] : [...rest.slice(0, at), moved, ...rest.slice(at)];
106
+ }
@@ -12,6 +12,7 @@ export { OpenInMenu } from "./list/openIn.js";
12
12
  export { pivotOf } from "./list/pivot.js";
13
13
  export { type CardLook, type ChoiceTones, type OptionLook, type PartLook, partLookOf, type PartLooks, type PartOf, type Presentation, presentationOf, type Presented, presented, type RowLook, type TileLook, tonesOver, } from "./list/presentation.js";
14
14
  export { type Choice, type Chooser, findChoices, type FrameworkReads, modelLoader, recordsBrowser, RecordsBrowserProvider, recordsSearch, refusalNotice, useLoadModel, useRecordsBrowser, useSearchRecords, } from "./list/records.js";
15
+ export { type Reorder, useReorder } from "./list/reorder.js";
15
16
  export { type RowCommand, type RowCommands, useRowCommands, } from "./list/rowCommands.js";
16
17
  export { facetsOf, modelOf } from "./list/search.js";
17
18
  export { type ActionDialog, type Asked, } from "./record/ActionButton.js";
package/dist/web/index.js CHANGED
@@ -12,6 +12,7 @@ export { OpenInMenu } from "./list/openIn.js";
12
12
  export { pivotOf } from "./list/pivot.js";
13
13
  export { partLookOf, presentationOf, presented, tonesOver, } from "./list/presentation.js";
14
14
  export { findChoices, modelLoader, recordsBrowser, RecordsBrowserProvider, recordsSearch, refusalNotice, useLoadModel, useRecordsBrowser, useSearchRecords, } from "./list/records.js";
15
+ export { useReorder } from "./list/reorder.js";
15
16
  export { useRowCommands, } from "./list/rowCommands.js";
16
17
  export { facetsOf, modelOf } from "./list/search.js";
17
18
  export {} from "./record/ActionButton.js";
@@ -21,6 +21,7 @@ import { pilesOf } from "./groups.js";
21
21
  import { OpenInMenu, openOptions, targetsFor } from "./openIn.js";
22
22
  import { pivotOf } from "./pivot.js";
23
23
  import { over, presentationOf, } from "./presentation.js";
24
+ import { useReorder } from "./reorder.js";
24
25
  import { RESTORE, useRowCommands, } from "./rowCommands.js";
25
26
  import { Button, Callout, List, Pagination, pathKey, RecordCard, SearchField, Tag, } from "@sincpro/web-ui";
26
27
  import { Plus, RotateCcw } from "lucide-react";
@@ -448,7 +449,10 @@ export function EntityList({ listing, api, columns: askedColumns, cells, yesNo,
448
449
  const rowActions = offered.length === 0 && askedRowActions === undefined
449
450
  ? undefined
450
451
  : (row) => [...rowCommands.rowActions(row), ...(askedRowActions?.(row) ?? [])];
451
- const refused = rowCommands.error === null ? null : explain(rowCommands.error);
452
+ // Rows put in their order by hand: only on the whole order, never grouped or while choosing.
453
+ const reorder = useReorder(listing, idOf, !grouped && !choosing);
454
+ const refusal = rowCommands.error ?? reorder.error;
455
+ const refused = refusal === null ? null : explain(refusal);
452
456
  // The way through the set. Inside a record or a panel, only while there is more than what is
453
457
  // on screen: «1–5 / 5» repeats the rows.
454
458
  const fits = embedded && !partial;
@@ -460,10 +464,12 @@ export function EntityList({ listing, api, columns: askedColumns, cells, yesNo,
460
464
  // written, an empty one too. Its parts side by side and apart: the slot does not space them,
461
465
  // and «Totales de esta página» read glued to «1–50 / 112».
462
466
  const paging = !pageTotals && dropped.length === 0 && through === null ? null : (_jsxs("span", { className: "flex items-center gap-3", children: [pageTotals ? _jsx("span", { className: "text-meta text-muted", children: copy.pageTotals }) : null, dropped.length === 0 ? null : (_jsx(Tag, { tone: "warn", title: copy.dropped(dropped), children: copy.dropped(dropped) })), through] }));
463
- return (_jsxs(List, { items: listing.rows, keyOf: idOf, embedded: embedded, present: present, onPresentChange: setPresent, ...grouping,
467
+ return (_jsxs(List, { items: reorder.rows, keyOf: idOf, embedded: embedded, present: present, onPresentChange: setPresent, ...grouping,
464
468
  // The whole order, as web-ui 1.7 shows and sets it: ⇧-click on a header adds a field,
465
469
  // the sort menu reorders them — the same list the criteria builder writes.
466
- sort: sortStates, ...(embedded ? {} : { sortable }), onSortChange: (next) => listing.orderBy(next.map((one) => ({ field: one.key, descending: one.direction === "desc" }))), state: {
470
+ // While the rows can be dragged the order shown IS their position: the table gets no column
471
+ // sort (web-ui turns its grip off under one), the list keeps its order as it is.
472
+ sort: reorder.move === undefined ? sortStates : [], ...(embedded ? {} : { sortable }), onSortChange: (next) => listing.orderBy(next.map((one) => ({ field: one.key, descending: one.direction === "desc" }))), state: {
467
473
  // Only the FIRST reading shows the skeleton. Refining (typing, a filter, the next
468
474
  // page) keeps the rows on screen until the server answers: web-ui swaps the body for
469
475
  // a skeleton while `loading`, and a row that vanished under the pointer lost a click.
@@ -487,7 +493,7 @@ export function EntityList({ listing, api, columns: askedColumns, cells, yesNo,
487
493
  // Emptied (or cleared with ×): the whole set again, at once.
488
494
  if (next === "" && listing.text !== "")
489
495
  listing.search("", { now: true });
490
- }, onSubmit: (text) => listing.search(text, { now: true }), placeholder: copy.search, "aria-label": copy.search }) })) : null, started.drawn === null ? null : (_jsxs(List.Actions, { children: [started.drawn, creating === undefined ? null : (_jsx(Button, { role: "primary", size: "sm", onClick: creating, children: frameworkCopy().record.create }))] })), startDock.length === 0 && forChosen.dock.length === 0 ? null : (_jsx(List.Dock, { ...(startDock.length === 0 ? {} : { actions: startDock }), ...(forChosen.dock.length === 0 ? {} : { selection: forChosen.dock }) })), paging === null ? null : _jsx(List.Pagination, { children: paging })] }), views.includes("table") ? (_jsx(List.Table, { columns: shown, ...(totalled ? { totals: true } : {}), ...(rowActions === undefined ? {} : { rowActions }) })) : null, _jsx(List.Rows, { row: rowOf }), views.includes("sheet") ? (_jsx(List.Sheet, { columns: sheetColumns, cell: (row, column) => {
496
+ }, onSubmit: (text) => listing.search(text, { now: true }), placeholder: copy.search, "aria-label": copy.search }) })) : null, started.drawn === null ? null : (_jsxs(List.Actions, { children: [started.drawn, creating === undefined ? null : (_jsx(Button, { role: "primary", size: "sm", onClick: creating, children: frameworkCopy().record.create }))] })), startDock.length === 0 && forChosen.dock.length === 0 ? null : (_jsx(List.Dock, { ...(startDock.length === 0 ? {} : { actions: startDock }), ...(forChosen.dock.length === 0 ? {} : { selection: forChosen.dock }) })), paging === null ? null : _jsx(List.Pagination, { children: paging })] }), views.includes("table") ? (_jsx(List.Table, { columns: shown, ...(totalled ? { totals: true } : {}), ...(rowActions === undefined ? {} : { rowActions }), ...(reorder.move === undefined ? {} : { onReorder: reorder.move }) })) : null, _jsx(List.Rows, { row: rowOf }), views.includes("sheet") ? (_jsx(List.Sheet, { columns: sheetColumns, cell: (row, column) => {
491
497
  const value = row[column.id];
492
498
  return value === null || value === undefined
493
499
  ? null
@@ -501,5 +507,5 @@ export function EntityList({ listing, api, columns: askedColumns, cells, yesNo,
501
507
  measures: [],
502
508
  meta,
503
509
  locale,
504
- }) })) : null, views.includes("board") ? (_jsx(List.Board, { ...(cardOfRow === undefined ? {} : { card: cardOfRow }) })) : null, views.includes("tiles") ? (_jsx(List.Tiles, { ...(tileOfRow === undefined ? {} : { card: tileOfRow }) })) : null, (faces ?? []).map((face) => (_jsx(List.Present, { id: face.id, label: face.label, icon: face.icon, ...(face.wide === undefined ? {} : { wide: face.wide }), children: face.render }, face.id))), refused === null ? null : (_jsx(Callout, { tone: "bad", title: refused.heading, children: refused.sentence }))] }));
510
+ }) })) : null, views.includes("board") ? (_jsx(List.Board, { ...(cardOfRow === undefined ? {} : { card: cardOfRow }) })) : null, views.includes("tiles") ? (_jsx(List.Tiles, { ...(tileOfRow === undefined ? {} : { card: tileOfRow }) })) : null, (faces ?? []).map((face) => (_jsx(List.Present, { id: face.id, label: face.label, icon: face.icon, ...(face.wide === undefined ? {} : { wide: face.wide }), children: face.render }, face.id))), reorder.elsewhere ? (_jsx(Callout, { tone: "warn", onDismiss: reorder.dismiss, children: copy.reorderElsewhere })) : null, refused === null ? null : (_jsx(Callout, { tone: "bad", title: refused.heading, children: refused.sentence }))] }));
505
511
  }
@@ -0,0 +1,31 @@
1
+ import type { AnyRecord } from "@sincpro/criteria";
2
+ import { type Reordering } from "../../core/reorder.js";
3
+ import type { Listing } from "../../react/useListing.js";
4
+ /**
5
+ * A listing's rows put in their order by hand: when they may be dragged (`reorderingOf`), the
6
+ * rows as the screen shows them while a move travels, and the move itself — one `Edit` of the
7
+ * record moved through its entity's own edit route, or one per row when its group is respaced.
8
+ *
9
+ * const reorder = useReorder(listing, idOf);
10
+ * <List.Table onReorder={reorder.move} /> // absent: no grip
11
+ *
12
+ * Context: the rule is `core/reorder.ts`; this only sends it. The row moves on screen at once;
13
+ * the list is read again when the writes are done — and also when one is refused (someone else
14
+ * moved it: `StaleAggregate`), so the screen goes back to what is stored and says why.
15
+ */
16
+ export type Reorder<T> = {
17
+ /** Where the rows may move — `undefined`: they may not, here and now. */
18
+ reordering: Reordering | undefined;
19
+ /** The listing's rows, with a move still travelling already in its place. */
20
+ rows: readonly T[];
21
+ /** Puts `id` in front of `before` (`null`: the end). Absent while the rows cannot move. */
22
+ move?: (id: string, before: string | null) => void;
23
+ /** The last refusal of a write, until `dismiss`. */
24
+ error: unknown;
25
+ /** The last drop landed outside the row's group, and nothing was written. */
26
+ elsewhere: boolean;
27
+ dismiss: () => void;
28
+ };
29
+ export declare function useReorder<T extends AnyRecord>(listing: Listing<T>, keyOf: (row: T) => string,
30
+ /** `false` while the screen does not allow it (grouped, choosing). */
31
+ allowed?: boolean): Reorder<T>;
@@ -0,0 +1,67 @@
1
+ "use client";
2
+ import { pathFor } from "@sincpro/client-framework";
3
+ import { movesFor, placed, reorderingOf, } from "../../core/reorder.js";
4
+ import { useFramework } from "../../react/context.js";
5
+ import { useEffect, useMemo, useState } from "react";
6
+ export function useReorder(listing, keyOf,
7
+ /** `false` while the screen does not allow it (grouped, choosing). */
8
+ allowed = true) {
9
+ const { transport } = useFramework();
10
+ const { entity, meta, order, rows: read } = listing;
11
+ const route = entity.edit;
12
+ const reordering = useMemo(() => (allowed && route !== undefined ? reorderingOf(meta, order) : undefined), [allowed, route, meta, order]);
13
+ const [shown, setShown] = useState(null);
14
+ const [error, setError] = useState(null);
15
+ const [elsewhere, setElsewhere] = useState(false);
16
+ // A new reading is what is stored: it replaces the order a move showed ahead of it.
17
+ useEffect(() => setShown(null), [read]);
18
+ const rows = shown ?? read;
19
+ const move = reordering === undefined || route === undefined
20
+ ? undefined
21
+ : (id, before) => {
22
+ const moves = movesFor(rows, keyOf, reordering, id, before);
23
+ if (moves === "elsewhere") {
24
+ setElsewhere(true);
25
+ return;
26
+ }
27
+ if (moves.length === 0)
28
+ return;
29
+ setElsewhere(false);
30
+ setError(null);
31
+ setShown(placed(rows, keyOf, id, before));
32
+ void (async () => {
33
+ try {
34
+ for (const one of moves) {
35
+ const row = rows.find((candidate) => keyOf(candidate) === one.id) ?? {};
36
+ await transport.send(route.method, pathFor(route, one.id, row), {
37
+ id: one.id,
38
+ values: { [reordering.handle]: one.value },
39
+ changed: [reordering.handle],
40
+ version: 0,
41
+ ...(typeof row["version"] === "number"
42
+ ? { origin_version: row["version"] }
43
+ : {}),
44
+ action: "save",
45
+ });
46
+ }
47
+ }
48
+ catch (failure) {
49
+ setError(failure);
50
+ }
51
+ finally {
52
+ listing.reload();
53
+ }
54
+ })();
55
+ };
56
+ return {
57
+ reordering,
58
+ rows,
59
+ ...(move === undefined ? {} : { move }),
60
+ error,
61
+ elsewhere,
62
+ dismiss: () => {
63
+ setError(null);
64
+ setElsewhere(false);
65
+ },
66
+ };
67
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sincpro/web-framework",
3
- "version": "3.13.0",
3
+ "version": "3.14.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",
@@ -55,7 +55,7 @@
55
55
  "@sincpro/criteria": "^2.4.0",
56
56
  "@sincpro/criteria-react": "^2.4.0",
57
57
  "@sincpro/web-composer": "^1.7.0",
58
- "@sincpro/web-ui": "^2.15.0",
58
+ "@sincpro/web-ui": "^2.18.0",
59
59
  "react": ">=19",
60
60
  "react-dom": ">=19"
61
61
  },
@@ -99,7 +99,7 @@
99
99
  "@sincpro/criteria": "^2.5.0",
100
100
  "@sincpro/criteria-react": "^2.5.0",
101
101
  "@sincpro/web-composer": "^1.9.0",
102
- "@sincpro/web-ui": "^2.16.0",
102
+ "@sincpro/web-ui": "^2.18.0",
103
103
  "@storybook/addon-a11y": "10.6.5",
104
104
  "@storybook/addon-docs": "10.6.5",
105
105
  "@storybook/addon-mcp": "10.6.5",