@sincpro/web-framework 3.14.0 → 3.16.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.
- package/dist/core/copy.d.ts +2 -1
- package/dist/core/groups.d.ts +55 -1
- package/dist/core/groups.js +72 -0
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.js +1 -1
- package/dist/react/useGroups.d.ts +51 -9
- package/dist/react/useGroups.js +323 -66
- package/dist/react/useListing.d.ts +15 -0
- package/dist/react/useListing.js +35 -5
- package/dist/web/list/EntityList.js +32 -22
- package/dist/web/list/groups.d.ts +10 -2
- package/dist/web/list/groups.js +21 -6
- package/package.json +7 -7
package/dist/core/copy.d.ts
CHANGED
|
@@ -36,7 +36,8 @@ export type FrameworkCopy = {
|
|
|
36
36
|
reorderElsewhere: string;
|
|
37
37
|
/** The group of the records that hold no value in the field grouped by. */
|
|
38
38
|
noValue: string;
|
|
39
|
-
/** More groups than shown: narrow the set instead of scrolling them.
|
|
39
|
+
/** More groups than shown: narrow the set instead of scrolling them.
|
|
40
|
+
* @deprecated The groups are paged («Ver N grupos más», web-ui's); nothing says this now. */
|
|
40
41
|
groupsCapped: (shown: number) => string;
|
|
41
42
|
/** «12 groups · 187 records»: what the grouping split. */
|
|
42
43
|
groups: (groups: number, records: number) => string;
|
package/dist/core/groups.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { Bucket } from "@sincpro/criteria";
|
|
1
|
+
import type { AnyRecord, Bucket, Criteria, Measure } from "@sincpro/criteria";
|
|
2
|
+
import type { GroupLevel } from "./list.js";
|
|
2
3
|
/**
|
|
3
4
|
* Whether an answer to a grouped question holds groups — each with how many records it holds
|
|
4
5
|
* (`count`) and the criteria that opens it, its levels below too.
|
|
@@ -10,8 +11,61 @@ import type { Bucket } from "@sincpro/criteria";
|
|
|
10
11
|
* a list that got no groups says so in its failed state.
|
|
11
12
|
*/
|
|
12
13
|
export declare function areGroups(rows: readonly unknown[]): rows is readonly Bucket[];
|
|
14
|
+
/**
|
|
15
|
+
* Whether the answer to a grouped question of the first level is groups: buckets, and never
|
|
16
|
+
* none over a set that holds records.
|
|
17
|
+
*
|
|
18
|
+
* Context: the groups come with no record beside them (`pagination: { limit: 0 }`), so a backend
|
|
19
|
+
* that does not group answers an empty page — which `areGroups` reads as «no groups». It counts
|
|
20
|
+
* the records it was asked to count, though, and records always make at least one group.
|
|
21
|
+
*/
|
|
22
|
+
export declare function isGroupedAnswer(page: {
|
|
23
|
+
rows: readonly unknown[];
|
|
24
|
+
count?: {
|
|
25
|
+
value: number;
|
|
26
|
+
} | null;
|
|
27
|
+
}): boolean;
|
|
13
28
|
/** What a grouped question was answered with when it was not groups: the list's failure. */
|
|
14
29
|
export declare class NotGrouped extends Error {
|
|
15
30
|
readonly route: string;
|
|
16
31
|
constructor(route: string);
|
|
17
32
|
}
|
|
33
|
+
/** Where a page of groups starts: the first, or past the groups already in hand. */
|
|
34
|
+
export type GroupPage = {
|
|
35
|
+
limit: number;
|
|
36
|
+
offset?: number;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* The question for ONE level of groups — the first page of it, or the next.
|
|
40
|
+
*
|
|
41
|
+
* `base` is what the groups split: the list's question at the first level, the opened group's
|
|
42
|
+
* own criteria (`bucket.criteria`) below it. Each level is the first level of its own question,
|
|
43
|
+
* so every level pages and counts the same way (sincpro_framework#391): the backend pages only
|
|
44
|
+
* the first level of a grouping and answers `groups_count` for it — asking all the levels at
|
|
45
|
+
* once brought every sub-group of every group, unpaged.
|
|
46
|
+
*
|
|
47
|
+
* No record comes with the groups (`pagination: { limit: 0 }`): a group's rows are read when it
|
|
48
|
+
* is opened. A page of one record, as before, made the backend read one record per group, all
|
|
49
|
+
* of them. `counted` asks the total of records beside the groups — the first level's summary
|
|
50
|
+
* («800 grupos · 2 329 registros»); below it every bucket already says its own `count`.
|
|
51
|
+
*
|
|
52
|
+
* levelQuestion(listing.question, { field: "state" }, { limit: 80 }, { counted: true })
|
|
53
|
+
* levelQuestion(bucket.criteria, { field: "month" }, { limit: 80, offset: 80 })
|
|
54
|
+
*/
|
|
55
|
+
export declare function levelQuestion<T extends AnyRecord>(base: Criteria<T>, level: GroupLevel, page: GroupPage, options?: {
|
|
56
|
+
measures?: Readonly<Record<string, Measure>>;
|
|
57
|
+
counted?: boolean;
|
|
58
|
+
}): Criteria<T>;
|
|
59
|
+
/**
|
|
60
|
+
* The question for the rows of a group of the deepest level: its own criteria, a page of rows.
|
|
61
|
+
* Whatever grouped or counted it is left out — the rows of a group are a plain list.
|
|
62
|
+
*/
|
|
63
|
+
export declare function rowsQuestion<T extends AnyRecord>(bucket: Bucket<T>, limit: number): Criteria<T>;
|
|
64
|
+
/**
|
|
65
|
+
* What the next «more» of a level brings: how many groups, or `false` when none are left —
|
|
66
|
+
* web-ui's `morePiles`. `undefined` when nobody counted the level (an older backend): then
|
|
67
|
+
* web-ui offers no foot, as before.
|
|
68
|
+
*/
|
|
69
|
+
export declare function nextGroups(shown: number, count: number | null | undefined, pageSize: number): number | false | undefined;
|
|
70
|
+
/** A group's value as the text that keys it — the same `pilesOf` writes in a pile's path. */
|
|
71
|
+
export declare function groupKeyOf(bucket: Pick<Bucket, "value">): string;
|
package/dist/core/groups.js
CHANGED
|
@@ -20,6 +20,17 @@ export function areGroups(rows) {
|
|
|
20
20
|
(bucket.groups === undefined || areGroups(bucket.groups)));
|
|
21
21
|
});
|
|
22
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Whether the answer to a grouped question of the first level is groups: buckets, and never
|
|
25
|
+
* none over a set that holds records.
|
|
26
|
+
*
|
|
27
|
+
* Context: the groups come with no record beside them (`pagination: { limit: 0 }`), so a backend
|
|
28
|
+
* that does not group answers an empty page — which `areGroups` reads as «no groups». It counts
|
|
29
|
+
* the records it was asked to count, though, and records always make at least one group.
|
|
30
|
+
*/
|
|
31
|
+
export function isGroupedAnswer(page) {
|
|
32
|
+
return areGroups(page.rows) && !(page.rows.length === 0 && (page.count?.value ?? 0) > 0);
|
|
33
|
+
}
|
|
23
34
|
/** What a grouped question was answered with when it was not groups: the list's failure. */
|
|
24
35
|
export class NotGrouped extends Error {
|
|
25
36
|
route;
|
|
@@ -29,3 +40,64 @@ export class NotGrouped extends Error {
|
|
|
29
40
|
this.name = "NotGrouped";
|
|
30
41
|
}
|
|
31
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* The question for ONE level of groups — the first page of it, or the next.
|
|
45
|
+
*
|
|
46
|
+
* `base` is what the groups split: the list's question at the first level, the opened group's
|
|
47
|
+
* own criteria (`bucket.criteria`) below it. Each level is the first level of its own question,
|
|
48
|
+
* so every level pages and counts the same way (sincpro_framework#391): the backend pages only
|
|
49
|
+
* the first level of a grouping and answers `groups_count` for it — asking all the levels at
|
|
50
|
+
* once brought every sub-group of every group, unpaged.
|
|
51
|
+
*
|
|
52
|
+
* No record comes with the groups (`pagination: { limit: 0 }`): a group's rows are read when it
|
|
53
|
+
* is opened. A page of one record, as before, made the backend read one record per group, all
|
|
54
|
+
* of them. `counted` asks the total of records beside the groups — the first level's summary
|
|
55
|
+
* («800 grupos · 2 329 registros»); below it every bucket already says its own `count`.
|
|
56
|
+
*
|
|
57
|
+
* levelQuestion(listing.question, { field: "state" }, { limit: 80 }, { counted: true })
|
|
58
|
+
* levelQuestion(bucket.criteria, { field: "month" }, { limit: 80, offset: 80 })
|
|
59
|
+
*/
|
|
60
|
+
export function levelQuestion(base, level, page, options = {}) {
|
|
61
|
+
// What paged, grouped or counted the base means nothing here: a bucket's criteria carries the
|
|
62
|
+
// question that made it (its empty grouping, its page of no rows).
|
|
63
|
+
const { pagination: _page, grouping: _grouping, count: _count, ...own } = base;
|
|
64
|
+
const offset = page.offset ?? 0;
|
|
65
|
+
return {
|
|
66
|
+
...own,
|
|
67
|
+
count: options.counted === true ? "exact" : "none",
|
|
68
|
+
grouping: {
|
|
69
|
+
group_by: [{ ...level }],
|
|
70
|
+
...(options.measures === undefined || Object.keys(options.measures).length === 0
|
|
71
|
+
? {}
|
|
72
|
+
: { measures: { ...options.measures } }),
|
|
73
|
+
pagination: {
|
|
74
|
+
limit: page.limit,
|
|
75
|
+
...(offset > 0 ? { strategy: { rows: offset } } : {}),
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
pagination: { limit: 0 },
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The question for the rows of a group of the deepest level: its own criteria, a page of rows.
|
|
83
|
+
* Whatever grouped or counted it is left out — the rows of a group are a plain list.
|
|
84
|
+
*/
|
|
85
|
+
export function rowsQuestion(bucket, limit) {
|
|
86
|
+
const { grouping: _grouping, pagination: _page, count: _count, ...own } = bucket.criteria;
|
|
87
|
+
return { ...own, count: "none", pagination: { limit } };
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* What the next «more» of a level brings: how many groups, or `false` when none are left —
|
|
91
|
+
* web-ui's `morePiles`. `undefined` when nobody counted the level (an older backend): then
|
|
92
|
+
* web-ui offers no foot, as before.
|
|
93
|
+
*/
|
|
94
|
+
export function nextGroups(shown, count, pageSize) {
|
|
95
|
+
if (count === null || count === undefined)
|
|
96
|
+
return undefined;
|
|
97
|
+
const left = count - shown;
|
|
98
|
+
return left <= 0 ? false : Math.min(pageSize, left);
|
|
99
|
+
}
|
|
100
|
+
/** A group's value as the text that keys it — the same `pilesOf` writes in a pile's path. */
|
|
101
|
+
export function groupKeyOf(bucket) {
|
|
102
|
+
return bucket.value === null || bucket.value === undefined ? "" : String(bucket.value);
|
|
103
|
+
}
|
package/dist/core/index.d.ts
CHANGED
|
@@ -14,7 +14,7 @@ export { type Advice, adviceAt, changesOf, computedLocally, creationOf, defaults
|
|
|
14
14
|
export { explain } from "./explain.js";
|
|
15
15
|
export { FILE_WIDGETS, holdsFile, type Published, renditionUrl, type RenditionValue, sendFile, storedFileOf, type StoredFileValue, type UploadAbout, uploadRefusalOf, type Uploads, } from "./files.js";
|
|
16
16
|
export { type FormLayout, formOf } from "./form.js";
|
|
17
|
-
export { areGroups, NotGrouped } from "./groups.js";
|
|
17
|
+
export { areGroups, groupKeyOf, type GroupPage, isGroupedAnswer, levelQuestion, nextGroups, NotGrouped, rowsQuestion, } from "./groups.js";
|
|
18
18
|
export { FACT_TONES, type FactTone, type HistoryChange, type HistoryEntry, historyOf, toneOfFact, } from "./history.js";
|
|
19
19
|
export { addingOf, type AddingRule, blankLine, type Declared, editableLines, isReference, type LineAdding, lineFields, lineKey, linesAnswered, moved, sameLine, writtenInCell, } from "./lines.js";
|
|
20
20
|
export { CHOOSE_MANY, choosesMany, defineList, type GroupLevel, isList, type ListDefinition, listingAsk, type ListingQuestion, type ListSpec, type NamedFilter, readListing, scopeOf, trashOf, } from "./list.js";
|
package/dist/core/index.js
CHANGED
|
@@ -14,7 +14,7 @@ export { adviceAt, changesOf, computedLocally, creationOf, defaultsOf, editedOf,
|
|
|
14
14
|
export { explain } from "./explain.js";
|
|
15
15
|
export { FILE_WIDGETS, holdsFile, renditionUrl, sendFile, storedFileOf, uploadRefusalOf, } from "./files.js";
|
|
16
16
|
export { formOf } from "./form.js";
|
|
17
|
-
export { areGroups, NotGrouped } from "./groups.js";
|
|
17
|
+
export { areGroups, groupKeyOf, isGroupedAnswer, levelQuestion, nextGroups, NotGrouped, rowsQuestion, } from "./groups.js";
|
|
18
18
|
export { FACT_TONES, historyOf, toneOfFact, } from "./history.js";
|
|
19
19
|
export { addingOf, blankLine, editableLines, isReference, lineFields, lineKey, linesAnswered, moved, sameLine, writtenInCell, } from "./lines.js";
|
|
20
20
|
export { CHOOSE_MANY, choosesMany, defineList, isList, listingAsk, readListing, scopeOf, trashOf, } from "./list.js";
|
|
@@ -2,33 +2,70 @@ import { type AnyRecord, type Bucket } from "@sincpro/criteria";
|
|
|
2
2
|
import type { GroupLevel } from "../core/list.js";
|
|
3
3
|
import type { Listing } from "./useListing.js";
|
|
4
4
|
/**
|
|
5
|
-
* The set of a list, split into groups — counted by the SERVER over every row that matches
|
|
5
|
+
* The set of a list, split into groups — counted by the SERVER over every row that matches, a
|
|
6
|
+
* page of groups at a time, each level read when its group is opened.
|
|
6
7
|
*
|
|
7
8
|
* Context: grouping is the same question as filtering («which part of this am I looking at»),
|
|
8
9
|
* and it obeys the same rule: never on the page. Twelve rows on screen cannot say «187 paid»;
|
|
9
10
|
* whoever keeps the set can. So the listing's own question (scope, search, filter) goes to the
|
|
10
|
-
* server with `group_by`, and what comes back is the groups: their value, their real count,
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
11
|
+
* server with `group_by`, and what comes back is the groups: their value, their real count, and
|
|
12
|
+
* the criteria that opens each one (sincpro_framework's aggregation; the in-memory server
|
|
13
|
+
* answers the same with criteria's engine). Nobody rebuilds a filter here.
|
|
14
|
+
*
|
|
15
|
+
* A register of 2 329 people grouped by surname makes ~800 groups: the first level comes a page
|
|
16
|
+
* at a time, with how many there are in all (`groups_count`), and «more groups» asks the next.
|
|
17
|
+
* A level below is asked when its group opens — the opened group's criteria, split by the next
|
|
18
|
+
* level, paged the same way (sincpro_framework#391). The rows of a group of the deepest level
|
|
19
|
+
* are read when it opens, a page at a time on the server's cursor.
|
|
20
|
+
*
|
|
21
|
+
* What is open stays open while the question changes — a search, a filter, a save —: each open
|
|
22
|
+
* group whose value is still there is read again, its pages kept, and what was on screen stays
|
|
23
|
+
* until the answer replaces it.
|
|
14
24
|
*
|
|
15
25
|
* const groups = useGroups(listing);
|
|
16
26
|
* groups.group([{ field: "state" }, { field: "invoice_date", grain: "month" }]);
|
|
17
|
-
* groups.buckets
|
|
18
|
-
* groups.
|
|
27
|
+
* groups.buckets // the first page of the first level
|
|
28
|
+
* groups.groupsCount // how many groups it makes in all
|
|
29
|
+
* groups.toggle(bucket) // opens it: the level below, or its first page of rows
|
|
30
|
+
* groups.groupsOf(bucket) // the level below, once read
|
|
31
|
+
* groups.rowsOf(bucket) // its rows, once read (the deepest level)
|
|
19
32
|
*/
|
|
20
33
|
export type { GroupLevel };
|
|
21
34
|
export type Groups<T> = {
|
|
22
35
|
levels: readonly GroupLevel[];
|
|
23
36
|
/** Split by these levels, outermost first; none is no grouping. */
|
|
24
37
|
group: (levels: readonly GroupLevel[]) => void;
|
|
38
|
+
/** The first level's reading. */
|
|
25
39
|
status: "idle" | "loading" | "ready" | "failed";
|
|
40
|
+
/** The groups of the first level read so far — its pages, in order. */
|
|
26
41
|
buckets: readonly Bucket<T>[];
|
|
27
42
|
error: unknown;
|
|
28
|
-
/**
|
|
43
|
+
/** How many groups the first level makes in all; `null` when the server did not say. */
|
|
44
|
+
groupsCount: number | null;
|
|
45
|
+
/** How many records the groups hold in all; `null` when the server did not say. */
|
|
46
|
+
records: number | null;
|
|
47
|
+
/**
|
|
48
|
+
* More groups exist than are read.
|
|
49
|
+
* @deprecated The groups are paged: ask `bringsGroups(null)` and `moreGroups(null)`.
|
|
50
|
+
*/
|
|
29
51
|
capped: boolean;
|
|
30
|
-
/** Read the
|
|
52
|
+
/** Read the next page of groups: of the first level (`null`), or below an open group. */
|
|
53
|
+
moreGroups: (parent: Bucket<T> | null) => void;
|
|
54
|
+
/** What the next `moreGroups` brings: a number of groups, `false` when none are left,
|
|
55
|
+
* `undefined` while the level is unread or nobody counted it — web-ui's `morePiles`. */
|
|
56
|
+
bringsGroups: (parent: Bucket<T> | null) => number | false | undefined;
|
|
57
|
+
/** Whether the group is on the deepest level: it holds rows, not groups. */
|
|
58
|
+
isDeepest: (bucket: Bucket<T>) => boolean;
|
|
59
|
+
/** Whether the person opened it. */
|
|
60
|
+
isOpen: (bucket: Bucket<T>) => boolean;
|
|
61
|
+
/** Opens it and reads what it holds, if not read yet: the level below, or its rows. */
|
|
31
62
|
open: (bucket: Bucket<T>) => void;
|
|
63
|
+
close: (bucket: Bucket<T>) => void;
|
|
64
|
+
toggle: (bucket: Bucket<T>) => void;
|
|
65
|
+
/** The level below a group, once read: its groups so far. `undefined` while unread. */
|
|
66
|
+
groupsOf: (bucket: Bucket<T>) => readonly Bucket<T>[] | undefined;
|
|
67
|
+
/** How many groups the level below makes in all, once read; `null` when nobody counted. */
|
|
68
|
+
groupsCountOf: (bucket: Bucket<T>) => number | null | undefined;
|
|
32
69
|
/** The rows of a group once read; `undefined` while it was never opened or is loading. */
|
|
33
70
|
rowsOf: (bucket: Bucket<T>) => readonly T[] | undefined;
|
|
34
71
|
/** Read the group's next page and append it — on the server's cursor, like the list's «load
|
|
@@ -40,9 +77,14 @@ export type Groups<T> = {
|
|
|
40
77
|
* has no next page, or the ceiling was reached), `undefined` while the group is unread.
|
|
41
78
|
* web-ui's `Pile.more`: «Show 50 more of 49,950», or «Showing 500 of 49,950». */
|
|
42
79
|
brings: (bucket: Bucket<T>) => number | false | undefined;
|
|
80
|
+
/** Why reading what a group holds failed — its level below or its rows —, or `null`. */
|
|
81
|
+
errorOf: (bucket: Bucket<T>) => unknown;
|
|
43
82
|
};
|
|
44
83
|
export declare function useGroups<T extends AnyRecord>(listing: Listing<T>, options?: {
|
|
84
|
+
/** Rows a group reads at a time. 50 by default. */
|
|
45
85
|
pageSize?: number;
|
|
86
|
+
/** Groups a level reads at a time — 80 by default, as Odoo's grouped list. */
|
|
87
|
+
groupPageSize?: number;
|
|
46
88
|
/** The fields each group adds up, over every row of it (`detailOf` reads them). Without
|
|
47
89
|
* it, the figures the model computes (`sumsOf`); `[]` adds nothing up. */
|
|
48
90
|
sums?: readonly string[];
|
package/dist/react/useGroups.js
CHANGED
|
@@ -1,23 +1,36 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { resumingFrom
|
|
3
|
-
import { areGroups, NotGrouped } from "../core/groups.js";
|
|
2
|
+
import { resumingFrom } from "@sincpro/criteria";
|
|
3
|
+
import { areGroups, groupKeyOf, isGroupedAnswer, levelQuestion, nextGroups, NotGrouped, rowsQuestion, } from "../core/groups.js";
|
|
4
4
|
import { measuresOf, sumsOf } from "../core/measure.js";
|
|
5
5
|
import { useFramework } from "./context.js";
|
|
6
6
|
import { useWrites } from "./useWrites.js";
|
|
7
7
|
import { useCallback, useEffect, useRef, useState } from "react";
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
const IDLE = {
|
|
9
|
+
status: "idle",
|
|
10
|
+
buckets: [],
|
|
11
|
+
error: null,
|
|
12
|
+
groupsCount: null,
|
|
13
|
+
records: null,
|
|
14
|
+
};
|
|
15
|
+
/** A group is known by the values of its path — what survives a new question, unlike its
|
|
16
|
+
* criteria, which carries the search. */
|
|
17
|
+
const keyOf = (path) => JSON.stringify(path);
|
|
18
|
+
/** Whether `path` is `above` or lies under it. */
|
|
19
|
+
const within = (path, above) => above.length <= path.length && above.every((part, index) => path[index] === part);
|
|
20
|
+
const without = (map, above) => {
|
|
21
|
+
const kept = [...map].filter(([, value]) => !within(value.path, above));
|
|
22
|
+
return kept.length === map.size ? map : new Map(kept);
|
|
23
|
+
};
|
|
12
24
|
export function useGroups(listing, options = {}) {
|
|
13
25
|
const { transport, context } = useFramework();
|
|
14
26
|
// A named list may open grouped (`groupBy`); a person regroups from there.
|
|
15
27
|
const [levels, setLevels] = useState(() => listing.definition?.groupBy ?? []);
|
|
16
|
-
const [
|
|
28
|
+
const [root, setRoot] = useState(IDLE);
|
|
29
|
+
const [below, setBelow] = useState(new Map());
|
|
17
30
|
const [rows, setRows] = useState(new Map());
|
|
18
|
-
|
|
19
|
-
const reading = useRef(new Set());
|
|
31
|
+
const [opened, setOpened] = useState(new Set());
|
|
20
32
|
const pageSize = options.pageSize ?? 50;
|
|
33
|
+
const groupPageSize = options.groupPageSize ?? 80;
|
|
21
34
|
const maxRows = options.maxRows ?? 500;
|
|
22
35
|
// The question without what pages it: a cursor or a limit of rows has no meaning for groups.
|
|
23
36
|
const { pagination: _page, ...asked } = listing.question;
|
|
@@ -31,93 +44,337 @@ export function useGroups(listing, options = {}) {
|
|
|
31
44
|
// What adds up by default is read from the model: without it yet, asking now and again when
|
|
32
45
|
// it arrives would be two questions — and the groups on screen replaced under the pointer.
|
|
33
46
|
const waiting = options.sums === undefined && listing.meta === null;
|
|
47
|
+
const grouped = levels.length > 0;
|
|
48
|
+
// Grouped, the list's own page is nobody's: it waits until the grouping ends.
|
|
49
|
+
const hold = listing.hold;
|
|
34
50
|
useEffect(() => {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
51
|
+
hold(grouped);
|
|
52
|
+
return () => hold(false);
|
|
53
|
+
}, [hold, grouped]);
|
|
54
|
+
// What a reading answering later needs NOW — a callback made in an older render would page
|
|
55
|
+
// from groups no longer on screen.
|
|
56
|
+
const now = useRef({ levels, below, rows, opened, root, measured });
|
|
57
|
+
now.current = { levels, below, rows, opened, root, measured };
|
|
58
|
+
// Where each bucket sits: its values from the first level down, written as it arrives.
|
|
59
|
+
const paths = useRef(new WeakMap());
|
|
60
|
+
// A reading of an older question answers into nothing; each question has its own signal.
|
|
61
|
+
const generation = useRef({ id: 0, signal: new AbortController().signal });
|
|
62
|
+
const inFlight = useRef(new Set());
|
|
63
|
+
const pathOf = (bucket) => paths.current.get(bucket) ?? [groupKeyOf(bucket)];
|
|
64
|
+
const measuresNow = () => now.current.measured === ""
|
|
65
|
+
? undefined
|
|
66
|
+
: JSON.parse(now.current.measured);
|
|
67
|
+
/** Reads the rows of a group of the deepest level: its first page, the next, or again. */
|
|
68
|
+
const readRows = useCallback((bucket, path, mode) => {
|
|
69
|
+
const key = keyOf(path);
|
|
70
|
+
const tag = `rows:${key}`;
|
|
71
|
+
if (inFlight.current.has(tag))
|
|
39
72
|
return;
|
|
73
|
+
const had = now.current.rows.get(key);
|
|
74
|
+
if (mode === "more" && (had === undefined || had.after === null))
|
|
75
|
+
return;
|
|
76
|
+
inFlight.current.add(tag);
|
|
77
|
+
const { id, signal } = generation.current;
|
|
78
|
+
// Read again, as many as were on screen: a search does not undo the person's «more».
|
|
79
|
+
const limit = mode === "again"
|
|
80
|
+
? Math.min(maxRows, Math.max(pageSize, had?.rows.length ?? 0))
|
|
81
|
+
: pageSize;
|
|
82
|
+
const first = rowsQuestion(bucket, limit);
|
|
83
|
+
const criteria = mode === "more" && had !== undefined ? resumingFrom(first, had.after) : first;
|
|
84
|
+
if (had !== undefined)
|
|
85
|
+
setRows((map) => new Map(map).set(key, { ...had, reading: true, error: null }));
|
|
86
|
+
transport.list(route, criteria, signal).then((page) => {
|
|
87
|
+
inFlight.current.delete(tag);
|
|
88
|
+
if (generation.current.id !== id)
|
|
89
|
+
return;
|
|
90
|
+
setRows((map) => {
|
|
91
|
+
const before = mode === "more" ? (map.get(key)?.rows ?? []) : [];
|
|
92
|
+
return new Map(map).set(key, {
|
|
93
|
+
path,
|
|
94
|
+
rows: [...before, ...page.rows],
|
|
95
|
+
after: page.cursor,
|
|
96
|
+
reading: false,
|
|
97
|
+
error: null,
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
}, (error) => {
|
|
101
|
+
inFlight.current.delete(tag);
|
|
102
|
+
if (generation.current.id !== id)
|
|
103
|
+
return;
|
|
104
|
+
setRows((map) => new Map(map).set(key, {
|
|
105
|
+
...(map.get(key) ?? { path, rows: [], after: null }),
|
|
106
|
+
reading: false,
|
|
107
|
+
error,
|
|
108
|
+
}));
|
|
109
|
+
});
|
|
110
|
+
}, [transport, route, pageSize, maxRows]);
|
|
111
|
+
/**
|
|
112
|
+
* What arrived at a level, under `above`: each bucket learns its path, an open one reads what
|
|
113
|
+
* it holds again, and a closed one forgets it — reopened later, it reads it fresh.
|
|
114
|
+
*/
|
|
115
|
+
const arrived = useRef(() => { });
|
|
116
|
+
/** Reads a level below an open group: its first page of groups, the next, or again. */
|
|
117
|
+
const readLevel = useCallback((bucket, path, mode) => {
|
|
118
|
+
const key = keyOf(path);
|
|
119
|
+
const tag = `level:${key}`;
|
|
120
|
+
const level = now.current.levels[path.length];
|
|
121
|
+
if (level === undefined || inFlight.current.has(tag))
|
|
122
|
+
return;
|
|
123
|
+
const had = now.current.below.get(key);
|
|
124
|
+
if (mode === "more" && had === undefined)
|
|
125
|
+
return;
|
|
126
|
+
inFlight.current.add(tag);
|
|
127
|
+
const { id, signal } = generation.current;
|
|
128
|
+
const limit = mode === "again" ? Math.max(groupPageSize, had?.buckets.length ?? 0) : groupPageSize;
|
|
129
|
+
const offset = mode === "more" ? (had?.buckets.length ?? 0) : 0;
|
|
130
|
+
const measures = measuresNow();
|
|
131
|
+
const criteria = levelQuestion(bucket.criteria, level, { limit, offset }, measures === undefined ? {} : { measures });
|
|
132
|
+
setBelow((map) => new Map(map).set(key, {
|
|
133
|
+
...(had ?? { path, buckets: [], count: null }),
|
|
134
|
+
reading: true,
|
|
135
|
+
error: null,
|
|
136
|
+
}));
|
|
137
|
+
transport.list(route, criteria, signal).then((page) => {
|
|
138
|
+
inFlight.current.delete(tag);
|
|
139
|
+
if (generation.current.id !== id)
|
|
140
|
+
return;
|
|
141
|
+
if (!areGroups(page.rows)) {
|
|
142
|
+
const error = new NotGrouped(route);
|
|
143
|
+
setBelow((map) => new Map(map).set(key, {
|
|
144
|
+
...(map.get(key) ?? { path, buckets: [], count: null }),
|
|
145
|
+
reading: false,
|
|
146
|
+
error,
|
|
147
|
+
}));
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
const arriving = page.rows;
|
|
151
|
+
setBelow((map) => {
|
|
152
|
+
const before = mode === "more" ? (map.get(key)?.buckets ?? []) : [];
|
|
153
|
+
return new Map(map).set(key, {
|
|
154
|
+
path,
|
|
155
|
+
buckets: [...before, ...arriving],
|
|
156
|
+
count: page.groupsCount ?? null,
|
|
157
|
+
reading: false,
|
|
158
|
+
error: null,
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
arrived.current(arriving, path);
|
|
162
|
+
}, (error) => {
|
|
163
|
+
inFlight.current.delete(tag);
|
|
164
|
+
if (generation.current.id !== id)
|
|
165
|
+
return;
|
|
166
|
+
setBelow((map) => new Map(map).set(key, {
|
|
167
|
+
...(map.get(key) ?? { path, buckets: [], count: null }),
|
|
168
|
+
reading: false,
|
|
169
|
+
error,
|
|
170
|
+
}));
|
|
171
|
+
});
|
|
172
|
+
}, [transport, route, groupPageSize]);
|
|
173
|
+
arrived.current = (buckets, above) => {
|
|
174
|
+
const deepest = above.length === now.current.levels.length - 1;
|
|
175
|
+
const closed = [];
|
|
176
|
+
for (const bucket of buckets) {
|
|
177
|
+
const path = [...above, groupKeyOf(bucket)];
|
|
178
|
+
paths.current.set(bucket, path);
|
|
179
|
+
if (!now.current.opened.has(keyOf(path)))
|
|
180
|
+
closed.push(path);
|
|
181
|
+
else if (deepest)
|
|
182
|
+
readRows(bucket, path, "again");
|
|
183
|
+
else
|
|
184
|
+
readLevel(bucket, path, "again");
|
|
40
185
|
}
|
|
186
|
+
if (closed.length === 0)
|
|
187
|
+
return;
|
|
188
|
+
setBelow((map) => closed.reduce((kept, path) => without(kept, path), map));
|
|
189
|
+
setRows((map) => closed.reduce((kept, path) => without(kept, path), map));
|
|
190
|
+
};
|
|
191
|
+
// Another split starts closed; the same split under another question keeps what is open.
|
|
192
|
+
const split = JSON.stringify(levels);
|
|
193
|
+
const shownSplit = useRef(split);
|
|
194
|
+
useEffect(() => {
|
|
195
|
+
const fresh = shownSplit.current !== split;
|
|
196
|
+
shownSplit.current = split;
|
|
41
197
|
const aborter = new AbortController();
|
|
42
|
-
|
|
198
|
+
generation.current = { id: generation.current.id + 1, signal: aborter.signal };
|
|
199
|
+
const { id } = generation.current;
|
|
200
|
+
inFlight.current.clear();
|
|
201
|
+
if (fresh) {
|
|
202
|
+
setOpened(new Set());
|
|
203
|
+
setBelow(new Map());
|
|
204
|
+
setRows(new Map());
|
|
205
|
+
}
|
|
206
|
+
const first = now.current.levels[0];
|
|
207
|
+
if (first === undefined) {
|
|
208
|
+
setRoot(IDLE);
|
|
209
|
+
return () => aborter.abort();
|
|
210
|
+
}
|
|
211
|
+
setRoot((was) => ({
|
|
212
|
+
...(fresh ? IDLE : was),
|
|
213
|
+
status: "loading",
|
|
214
|
+
error: null,
|
|
215
|
+
}));
|
|
43
216
|
if (waiting)
|
|
44
217
|
return () => aborter.abort();
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
...(measured === ""
|
|
50
|
-
? {}
|
|
51
|
-
: { measures: JSON.parse(measured) }),
|
|
52
|
-
pagination: { limit: MAX_GROUPS },
|
|
53
|
-
},
|
|
54
|
-
// One id per group is the least a grouped answer carries; the rows come on opening.
|
|
55
|
-
pagination: { limit: 1 },
|
|
56
|
-
};
|
|
218
|
+
// As many groups as were on screen: a search does not undo the person's «more groups».
|
|
219
|
+
const shown = fresh ? 0 : now.current.root.buckets.length;
|
|
220
|
+
const measures = measuresNow();
|
|
221
|
+
const criteria = levelQuestion(JSON.parse(question), first, { limit: Math.max(groupPageSize, shown) }, { counted: true, ...(measures === undefined ? {} : { measures }) });
|
|
57
222
|
transport.list(route, criteria, aborter.signal).then((page) => {
|
|
58
|
-
if (
|
|
223
|
+
if (generation.current.id !== id)
|
|
59
224
|
return;
|
|
60
225
|
// Records answered to a grouped question are not groups: the list's failure, never a
|
|
61
226
|
// table drawing piles without a count.
|
|
62
|
-
if (!
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
227
|
+
if (!isGroupedAnswer(page)) {
|
|
228
|
+
setRoot({ ...IDLE, status: "failed", error: new NotGrouped(route) });
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
const buckets = page.rows;
|
|
232
|
+
setRoot({
|
|
233
|
+
status: "ready",
|
|
234
|
+
buckets,
|
|
235
|
+
error: null,
|
|
236
|
+
groupsCount: page.groupsCount ?? null,
|
|
237
|
+
records: page.count?.value ?? null,
|
|
238
|
+
});
|
|
239
|
+
arrived.current(buckets, []);
|
|
66
240
|
}, (error) => {
|
|
67
|
-
if (
|
|
68
|
-
|
|
241
|
+
if (generation.current.id === id)
|
|
242
|
+
setRoot({ ...IDLE, status: "failed", error });
|
|
69
243
|
});
|
|
70
244
|
return () => aborter.abort();
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
245
|
+
// `listing.question` is read through `question`, its content.
|
|
246
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
247
|
+
}, [transport, route, question, split, measured, waiting, context, writes, groupPageSize]);
|
|
248
|
+
/** The first page of the root, more of it: offset past what is in hand. */
|
|
249
|
+
const moreRoot = () => {
|
|
250
|
+
const first = levels[0];
|
|
251
|
+
const tag = "level:[]";
|
|
252
|
+
if (first === undefined || root.status !== "ready" || inFlight.current.has(tag))
|
|
76
253
|
return;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
254
|
+
if (!nextGroups(root.buckets.length, root.groupsCount, groupPageSize))
|
|
255
|
+
return;
|
|
256
|
+
inFlight.current.add(tag);
|
|
257
|
+
const { id, signal } = generation.current;
|
|
258
|
+
const measures = measuresNow();
|
|
259
|
+
const criteria = levelQuestion(JSON.parse(question), first, { limit: groupPageSize, offset: root.buckets.length }, measures === undefined ? {} : { measures });
|
|
260
|
+
setRoot((was) => ({ ...was, status: "loading" }));
|
|
261
|
+
transport.list(route, criteria, signal).then((page) => {
|
|
262
|
+
inFlight.current.delete(tag);
|
|
263
|
+
if (generation.current.id !== id)
|
|
264
|
+
return;
|
|
265
|
+
if (!areGroups(page.rows)) {
|
|
266
|
+
setRoot((was) => ({ ...was, status: "failed", error: new NotGrouped(route) }));
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
const arriving = page.rows;
|
|
270
|
+
setRoot((was) => ({
|
|
271
|
+
...was,
|
|
272
|
+
status: "ready",
|
|
273
|
+
buckets: [...was.buckets, ...arriving],
|
|
274
|
+
groupsCount: page.groupsCount ?? was.groupsCount,
|
|
275
|
+
}));
|
|
276
|
+
arrived.current(arriving, []);
|
|
89
277
|
}, (error) => {
|
|
90
|
-
|
|
91
|
-
|
|
278
|
+
inFlight.current.delete(tag);
|
|
279
|
+
if (generation.current.id === id)
|
|
280
|
+
setRoot((was) => ({ ...was, status: "ready", error }));
|
|
92
281
|
});
|
|
93
|
-
}
|
|
94
|
-
const
|
|
95
|
-
const
|
|
282
|
+
};
|
|
283
|
+
const isDeepest = (bucket) => pathOf(bucket).length >= levels.length;
|
|
284
|
+
const levelOf = (bucket) => below.get(keyOf(pathOf(bucket)));
|
|
285
|
+
const readOf = (bucket) => rows.get(keyOf(pathOf(bucket)));
|
|
96
286
|
const hasMore = (bucket) => {
|
|
97
|
-
const
|
|
98
|
-
return
|
|
287
|
+
const read = readOf(bucket);
|
|
288
|
+
return read !== undefined && read.after !== null && read.rows.length < maxRows;
|
|
289
|
+
};
|
|
290
|
+
const open = (bucket) => {
|
|
291
|
+
const path = pathOf(bucket);
|
|
292
|
+
const key = keyOf(path);
|
|
293
|
+
if (!opened.has(key))
|
|
294
|
+
setOpened((was) => new Set(was).add(key));
|
|
295
|
+
// Read once; a reading that failed is tried again when the group is opened again.
|
|
296
|
+
const unread = (read) => read === undefined || (read.error !== null && read.error !== undefined);
|
|
297
|
+
if (isDeepest(bucket)) {
|
|
298
|
+
if (unread(rows.get(key)))
|
|
299
|
+
readRows(bucket, path, "first");
|
|
300
|
+
}
|
|
301
|
+
else if (unread(below.get(key)))
|
|
302
|
+
readLevel(bucket, path, "first");
|
|
303
|
+
};
|
|
304
|
+
const close = (bucket) => {
|
|
305
|
+
const key = keyOf(pathOf(bucket));
|
|
306
|
+
setOpened((was) => {
|
|
307
|
+
if (!was.has(key))
|
|
308
|
+
return was;
|
|
309
|
+
const next = new Set(was);
|
|
310
|
+
next.delete(key);
|
|
311
|
+
return next;
|
|
312
|
+
});
|
|
99
313
|
};
|
|
100
314
|
return {
|
|
101
315
|
levels,
|
|
102
316
|
group: setLevels,
|
|
103
|
-
|
|
104
|
-
|
|
317
|
+
status: root.status,
|
|
318
|
+
buckets: root.buckets,
|
|
319
|
+
error: root.error,
|
|
320
|
+
groupsCount: root.groupsCount,
|
|
321
|
+
records: root.records,
|
|
322
|
+
capped: nextGroups(root.buckets.length, root.groupsCount, groupPageSize) !== false &&
|
|
323
|
+
root.groupsCount !== null,
|
|
324
|
+
moreGroups: (parent) => {
|
|
325
|
+
if (parent === null)
|
|
326
|
+
return moreRoot();
|
|
327
|
+
const level = levelOf(parent);
|
|
328
|
+
if (level === undefined || level.reading)
|
|
329
|
+
return;
|
|
330
|
+
if (!nextGroups(level.buckets.length, level.count, groupPageSize))
|
|
331
|
+
return;
|
|
332
|
+
readLevel(parent, pathOf(parent), "more");
|
|
333
|
+
},
|
|
334
|
+
bringsGroups: (parent) => {
|
|
335
|
+
if (parent === null)
|
|
336
|
+
return root.status === "idle" || root.status === "failed"
|
|
337
|
+
? undefined
|
|
338
|
+
: nextGroups(root.buckets.length, root.groupsCount, groupPageSize);
|
|
339
|
+
const level = levelOf(parent);
|
|
340
|
+
return level === undefined
|
|
341
|
+
? undefined
|
|
342
|
+
: nextGroups(level.buckets.length, level.count, groupPageSize);
|
|
343
|
+
},
|
|
344
|
+
isDeepest,
|
|
345
|
+
isOpen: (bucket) => opened.has(keyOf(pathOf(bucket))),
|
|
105
346
|
open,
|
|
106
|
-
|
|
347
|
+
close,
|
|
348
|
+
toggle: (bucket) => (opened.has(keyOf(pathOf(bucket))) ? close(bucket) : open(bucket)),
|
|
349
|
+
groupsOf: (bucket) => {
|
|
350
|
+
const level = levelOf(bucket);
|
|
351
|
+
// Still unread: nothing to draw yet — a group never opens onto an empty level.
|
|
352
|
+
return level === undefined || (level.buckets.length === 0 && level.count === null)
|
|
353
|
+
? undefined
|
|
354
|
+
: level.buckets;
|
|
355
|
+
},
|
|
356
|
+
groupsCountOf: (bucket) => levelOf(bucket)?.count,
|
|
357
|
+
rowsOf: (bucket) => {
|
|
358
|
+
const read = readOf(bucket);
|
|
359
|
+
// Unread until the first page answered: a group that failed holds no rows.
|
|
360
|
+
return read === undefined || (read.rows.length === 0 && read.reading)
|
|
361
|
+
? undefined
|
|
362
|
+
: read.rows;
|
|
363
|
+
},
|
|
107
364
|
more: (bucket) => {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
read(bucket, now.after);
|
|
365
|
+
if (hasMore(bucket))
|
|
366
|
+
readRows(bucket, pathOf(bucket), "more");
|
|
111
367
|
},
|
|
112
368
|
hasMore,
|
|
113
369
|
brings: (bucket) => {
|
|
114
|
-
const
|
|
115
|
-
if (
|
|
370
|
+
const read = readOf(bucket);
|
|
371
|
+
if (read === undefined)
|
|
116
372
|
return undefined;
|
|
117
373
|
if (!hasMore(bucket))
|
|
118
374
|
return false;
|
|
119
|
-
const left = Math.min(maxRows, bucket.count) -
|
|
375
|
+
const left = Math.min(maxRows, bucket.count) - read.rows.length;
|
|
120
376
|
return Math.max(0, Math.min(pageSize, left));
|
|
121
377
|
},
|
|
378
|
+
errorOf: (bucket) => (isDeepest(bucket) ? readOf(bucket) : levelOf(bucket))?.error ?? null,
|
|
122
379
|
};
|
|
123
380
|
}
|
|
@@ -73,8 +73,23 @@ export type Listing<T, C extends CommandBodies = CommandBodies> = {
|
|
|
73
73
|
};
|
|
74
74
|
next: () => void;
|
|
75
75
|
prev: () => void;
|
|
76
|
+
/** Rows per page from now on — the page's range, touched and typed. Another size is another
|
|
77
|
+
* question: it starts on the first page, its search, filter and order kept. Between 1 and
|
|
78
|
+
* `maxRows`: a page is what the DOM holds, and a size past that is the frozen screen
|
|
79
|
+
* `pages` exists to avoid. */
|
|
80
|
+
resize: (size: number) => void;
|
|
76
81
|
/** `more` mode reached its ceiling: refine the search instead of loading on. */
|
|
77
82
|
capped: boolean;
|
|
83
|
+
/**
|
|
84
|
+
* Stops reading its rows while another reading answers for the same set — the list grouped
|
|
85
|
+
* (`useGroups` asks the groups, and each group its own rows): `hold(true)`. The question on
|
|
86
|
+
* screen keeps following the search, the filter and the order, so the groups ask the right
|
|
87
|
+
* one; `hold(false)` reads it once, if it changed meanwhile.
|
|
88
|
+
*
|
|
89
|
+
* Context: grouped, the list kept asking its ungrouped page under the groups on every
|
|
90
|
+
* narrowing — a request whose answer nobody drew.
|
|
91
|
+
*/
|
|
92
|
+
hold: (on: boolean) => void;
|
|
78
93
|
};
|
|
79
94
|
/**
|
|
80
95
|
* - `pages`: one page on screen at a time, walked by the server's own cursor (keyset — no
|
package/dist/react/useListing.js
CHANGED
|
@@ -50,11 +50,18 @@ export function useListing(given, own = {}) {
|
|
|
50
50
|
// kept, never a reading started over.
|
|
51
51
|
const writes = useWrites(route);
|
|
52
52
|
const askedFor = useRef({ context, writes });
|
|
53
|
+
// Held while another reading answers for the set (`hold`): what would have been asked is only
|
|
54
|
+
// marked, and asked once when the hold ends.
|
|
55
|
+
const held = useRef(false);
|
|
56
|
+
const stale = useRef(false);
|
|
53
57
|
useEffect(() => {
|
|
54
58
|
if (askedFor.current.context === context && askedFor.current.writes === writes)
|
|
55
59
|
return;
|
|
56
60
|
askedFor.current = { context, writes };
|
|
57
|
-
|
|
61
|
+
if (held.current)
|
|
62
|
+
stale.current = true;
|
|
63
|
+
else
|
|
64
|
+
void reading.reload();
|
|
58
65
|
}, [context, writes, reading]);
|
|
59
66
|
const narrowed = (next) => {
|
|
60
67
|
let criteria = base;
|
|
@@ -65,12 +72,17 @@ export function useListing(given, own = {}) {
|
|
|
65
72
|
criteria = merge(criteria, { selection: next.selection });
|
|
66
73
|
if (next.order.length > 0)
|
|
67
74
|
criteria = merge(criteria, { order: [...next.order] });
|
|
75
|
+
if (next.size !== undefined)
|
|
76
|
+
criteria = { ...criteria, pagination: { ...criteria.pagination, limit: next.size } };
|
|
68
77
|
return criteria;
|
|
69
78
|
};
|
|
70
79
|
const refine = useCallback((next) => {
|
|
71
80
|
// Another question starts on its first page: a cursor belongs to one order and one filter.
|
|
72
81
|
setCursors([null]);
|
|
73
|
-
|
|
82
|
+
if (held.current)
|
|
83
|
+
stale.current = true;
|
|
84
|
+
else
|
|
85
|
+
void reading.refine(narrowed(next));
|
|
74
86
|
},
|
|
75
87
|
// `base` is the caller's literal: its identity changes every render, its content does not.
|
|
76
88
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
@@ -92,6 +104,7 @@ export function useListing(given, own = {}) {
|
|
|
92
104
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
93
105
|
}, [meta]);
|
|
94
106
|
const index = cursors.length - 1;
|
|
107
|
+
const pageSize = narrowing.size ?? size;
|
|
95
108
|
const turn = (to) => {
|
|
96
109
|
setCursors(to);
|
|
97
110
|
void reading.refine(resumingFrom(narrowed(narrowing), to[to.length - 1] ?? null));
|
|
@@ -115,6 +128,16 @@ export function useListing(given, own = {}) {
|
|
|
115
128
|
else
|
|
116
129
|
refine(next);
|
|
117
130
|
};
|
|
131
|
+
// Stable: a screen holds it from an effect, and reads only refs.
|
|
132
|
+
const hold = useCallback((on) => {
|
|
133
|
+
if (held.current === on)
|
|
134
|
+
return;
|
|
135
|
+
held.current = on;
|
|
136
|
+
if (on || !stale.current)
|
|
137
|
+
return;
|
|
138
|
+
stale.current = false;
|
|
139
|
+
latest.current(now.current);
|
|
140
|
+
}, []);
|
|
118
141
|
return {
|
|
119
142
|
entity,
|
|
120
143
|
...(definition === undefined ? {} : { definition }),
|
|
@@ -142,9 +165,9 @@ export function useListing(given, own = {}) {
|
|
|
142
165
|
paging,
|
|
143
166
|
page: {
|
|
144
167
|
index,
|
|
145
|
-
size,
|
|
146
|
-
from: reading.rows.length === 0 ? 0 : index *
|
|
147
|
-
to: index *
|
|
168
|
+
size: pageSize,
|
|
169
|
+
from: reading.rows.length === 0 ? 0 : index * pageSize + 1,
|
|
170
|
+
to: index * pageSize + reading.rows.length,
|
|
148
171
|
hasPrev: paging === "pages" && index > 0,
|
|
149
172
|
hasNext: paging === "pages" && reading.cursor !== null,
|
|
150
173
|
cursors,
|
|
@@ -158,6 +181,13 @@ export function useListing(given, own = {}) {
|
|
|
158
181
|
if (paging === "pages" && index > 0)
|
|
159
182
|
turn(cursors.slice(0, -1));
|
|
160
183
|
},
|
|
184
|
+
resize: (next) => {
|
|
185
|
+
const rows = Math.min(maxRows, Math.max(1, Math.floor(next)));
|
|
186
|
+
if (!Number.isFinite(rows) || rows === pageSize)
|
|
187
|
+
return;
|
|
188
|
+
narrow({ size: rows }, false);
|
|
189
|
+
},
|
|
161
190
|
capped,
|
|
191
|
+
hold,
|
|
162
192
|
};
|
|
163
193
|
}
|
|
@@ -369,9 +369,10 @@ export function EntityList({ listing, api, columns: askedColumns, cells, yesNo,
|
|
|
369
369
|
};
|
|
370
370
|
const cardOfRow = cardFrom(look.card);
|
|
371
371
|
const tileOfRow = cardFrom(look.tile, "tile");
|
|
372
|
-
// Grouped: the SERVER counts the groups
|
|
373
|
-
// group starts closed. Every face of web-ui (1.4)
|
|
374
|
-
// each says what its next «more» brings (1.5)
|
|
372
|
+
// Grouped: the SERVER counts the groups, a page of them at a time; a level below and a group's
|
|
373
|
+
// rows are read when it is opened, so every group starts closed. Every face of web-ui (1.4)
|
|
374
|
+
// draws these piles, the table included; each says what its next «more» brings (1.5), and a
|
|
375
|
+
// level that holds fewer groups than it counts offers the next page of them (2.19).
|
|
375
376
|
const groups = useGroups(listing, sums === undefined ? {} : { sums: sums || [] });
|
|
376
377
|
const grouped = groups.levels.length > 0;
|
|
377
378
|
// A table and a sheet say a group's sums under their columns (web-ui 2.9): beside the group's
|
|
@@ -382,24 +383,20 @@ export function EntityList({ listing, api, columns: askedColumns, cells, yesNo,
|
|
|
382
383
|
? []
|
|
383
384
|
: [column.field])
|
|
384
385
|
: []);
|
|
385
|
-
const { piles, bucketAt } = pilesOf(groups.buckets, groups.levels, meta, locale, groups.rowsOf, groups.brings, underColumns);
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
const collapsed = [...bucketAt
|
|
386
|
+
const { piles, bucketAt } = pilesOf(groups.buckets, groups.levels, meta, locale, groups.rowsOf, groups.brings, underColumns, groups);
|
|
387
|
+
// What is open is the groups' (it survives a search); a group above another is drawn open
|
|
388
|
+
// only once its level arrived — an empty open group would read as one with no rows.
|
|
389
|
+
const collapsed = [...bucketAt].flatMap(([key, bucket]) => groups.isOpen(bucket) &&
|
|
390
|
+
(groups.isDeepest(bucket) || groups.groupsOf(bucket) !== undefined)
|
|
391
|
+
? []
|
|
392
|
+
: [key]);
|
|
389
393
|
const toggle = (key) => {
|
|
390
|
-
const
|
|
391
|
-
if (
|
|
392
|
-
|
|
393
|
-
else {
|
|
394
|
-
next.add(key);
|
|
395
|
-
const bucket = bucketAt.get(key);
|
|
396
|
-
const deepest = bucket !== undefined && (bucket.groups ?? []).length === 0;
|
|
397
|
-
if (deepest && groups.rowsOf(bucket) === undefined)
|
|
398
|
-
groups.open(bucket);
|
|
399
|
-
}
|
|
400
|
-
setOpened(next);
|
|
394
|
+
const bucket = bucketAt.get(key);
|
|
395
|
+
if (bucket !== undefined)
|
|
396
|
+
groups.toggle(bucket);
|
|
401
397
|
};
|
|
402
398
|
const failure = grouped && groups.status === "failed" ? explain(groups.error) : grouped ? null : failed;
|
|
399
|
+
const morePiles = groups.bringsGroups(null);
|
|
403
400
|
const grouping = grouped
|
|
404
401
|
? {
|
|
405
402
|
group: {
|
|
@@ -414,10 +411,22 @@ export function EntityList({ listing, api, columns: askedColumns, cells, yesNo,
|
|
|
414
411
|
if (bucket !== undefined)
|
|
415
412
|
groups.more(bucket);
|
|
416
413
|
},
|
|
414
|
+
// «Ver N grupos más» after the last group of a level: the first level, or the one
|
|
415
|
+
// under an open group — counted by the server (`groups_count`).
|
|
416
|
+
...(groups.groupsCount === null ? {} : { pilesCount: groups.groupsCount }),
|
|
417
|
+
...(morePiles === undefined ? {} : { morePiles }),
|
|
418
|
+
onMorePiles: (parent) => {
|
|
419
|
+
if (parent === null)
|
|
420
|
+
return groups.moreGroups(null);
|
|
421
|
+
const bucket = bucketAt.get(pathKey(parent.path));
|
|
422
|
+
if (bucket !== undefined)
|
|
423
|
+
groups.moreGroups(bucket);
|
|
424
|
+
},
|
|
417
425
|
},
|
|
418
426
|
}
|
|
419
427
|
: {};
|
|
420
|
-
|
|
428
|
+
// How many groups and records the SERVER counted — never the sum of the groups in hand.
|
|
429
|
+
const records = groups.records ?? groups.buckets.reduce((sum, bucket) => sum + bucket.count, 0);
|
|
421
430
|
// The foot adds up what the model says adds up (`total`) — over the rows in hand, never a
|
|
422
431
|
// grouped set (each group says its own, counted by the server). While the rows are not the
|
|
423
432
|
// whole set the list says the figures are this page's (`pageTotals`): the server's total over
|
|
@@ -456,9 +465,10 @@ export function EntityList({ listing, api, columns: askedColumns, cells, yesNo,
|
|
|
456
465
|
// The way through the set. Inside a record or a panel, only while there is more than what is
|
|
457
466
|
// on screen: «1–5 / 5» repeats the rows.
|
|
458
467
|
const fits = embedded && !partial;
|
|
459
|
-
const through = grouped ? (groups.status !== "ready" ? null : (_jsx("span", { className: "text-meta text-muted", children: groups.
|
|
460
|
-
|
|
461
|
-
|
|
468
|
+
const through = grouped ? (groups.status !== "ready" ? null : (_jsx("span", { className: "text-meta text-muted", children: copy.groups(groups.groupsCount ?? groups.buckets.length, records) }))) : fits ? null : listing.paging === "pages" ? (page.from === 0 ? null : (_jsx(Pagination, { from: page.from, to: page.to, total: count?.value ?? page.to,
|
|
469
|
+
/* The range is touched to choose how many rows fit — in both styles. Without these two
|
|
470
|
+
the modern pill and the plain range drew a size nobody could change. */
|
|
471
|
+
pageSize: page.size, onPageSizeChange: listing.resize, ...(page.hasPrev ? { onPrev: listing.prev } : {}), ...(page.hasNext ? { onNext: listing.next } : {}) }))) : (_jsxs(_Fragment, { children: [count === null ? null : (_jsx("span", { className: "text-meta text-muted", children: copy.shown(listing.rows.length, count.value, count.exact) })), listing.capped ? (_jsx("span", { className: "text-meta text-muted", children: copy.capped(listing.rows.length) })) : listing.hasMore ? (_jsx(Button, { size: "sm", role: "secondary", disabled: listing.status === "loadingMore", onClick: listing.more, children: listing.status === "loadingMore" ? copy.loading : copy.loadMore })) : null] }));
|
|
462
472
|
const pageTotals = totalled && partial && present === "table";
|
|
463
473
|
// Written only when it says something: web-ui draws an embedded list's bar for any slot
|
|
464
474
|
// written, an empty one too. Its parts side by side and apart: the slot does not space them,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type AnyRecord, type Bucket, type Meta } from "@sincpro/criteria";
|
|
2
|
-
import type { GroupLevel } from "../../react/useGroups.js";
|
|
2
|
+
import type { GroupLevel, Groups } from "../../react/useGroups.js";
|
|
3
3
|
import { type GroupableField, type Pile } from "@sincpro/web-ui";
|
|
4
4
|
/**
|
|
5
5
|
* The fields the model says may split a set (`groupable`), in its words. The identity splits
|
|
@@ -14,10 +14,18 @@ import { type GroupableField, type Pile } from "@sincpro/web-ui";
|
|
|
14
14
|
* names kept here.
|
|
15
15
|
*/
|
|
16
16
|
export declare function groupableOf(meta: Meta | null, locale?: string, only?: readonly string[]): GroupableField[];
|
|
17
|
+
/** The levels below the first, as `useGroups` reads them: each when its group opens, a page at
|
|
18
|
+
* a time. Without it, the levels below are the ones each bucket carries (`bucket.groups`). */
|
|
19
|
+
export type GroupTree<T> = Pick<Groups<T>, "groupsOf" | "groupsCountOf" | "bringsGroups" | "isDeepest" | "errorOf">;
|
|
17
20
|
/**
|
|
18
21
|
* The server's groups as piles, with the words a person reads, and each pile's group so that
|
|
19
22
|
* opening one reads its rows. Rows sit only on the deepest level, once read.
|
|
20
23
|
*
|
|
24
|
+
* With `tree` (a `useGroups`), the level below a pile is the one read when it opened, and the
|
|
25
|
+
* pile says how many groups that level makes in all (`pilesCount`) and what its next page
|
|
26
|
+
* brings (`morePiles`) — web-ui 2.19 draws «Ver N grupos más» from them. A group whose reading
|
|
27
|
+
* failed says why beside its name.
|
|
28
|
+
*
|
|
21
29
|
* What the server summed for a group is its `totals` — web-ui (2.9) says each figure under its
|
|
22
30
|
* column in a grouped table and a sheet, as Odoo's grouped list — and, in words beside the
|
|
23
31
|
* group's name (`detail`), what no column on screen says: a board's card has no columns.
|
|
@@ -26,7 +34,7 @@ export declare function pilesOf<T extends AnyRecord>(buckets: readonly Bucket<T>
|
|
|
26
34
|
/** What a group's next «more» brings (`Pile.more`); omitted, web-ui says nothing about it. */
|
|
27
35
|
brings?: (bucket: Bucket<T>) => number | false | undefined,
|
|
28
36
|
/** The figures a column on screen already says under itself: not said again in words. */
|
|
29
|
-
underColumns?: ReadonlySet<string>): {
|
|
37
|
+
underColumns?: ReadonlySet<string>, tree?: GroupTree<T>): {
|
|
30
38
|
piles: Pile<T>[];
|
|
31
39
|
bucketAt: ReadonlyMap<string, Bucket<T>>;
|
|
32
40
|
deepest: number;
|
package/dist/web/list/groups.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { labelOf } from "@sincpro/criteria";
|
|
2
2
|
import { frameworkCopy } from "../../core/copy.js";
|
|
3
|
+
import { explain } from "../../core/explain.js";
|
|
3
4
|
import { detailOf } from "../../core/measure.js";
|
|
4
5
|
import { textOf } from "../../core/present.js";
|
|
5
6
|
import { dateLabel, pathKey, } from "@sincpro/web-ui";
|
|
@@ -51,6 +52,11 @@ function figureOf(value) {
|
|
|
51
52
|
* The server's groups as piles, with the words a person reads, and each pile's group so that
|
|
52
53
|
* opening one reads its rows. Rows sit only on the deepest level, once read.
|
|
53
54
|
*
|
|
55
|
+
* With `tree` (a `useGroups`), the level below a pile is the one read when it opened, and the
|
|
56
|
+
* pile says how many groups that level makes in all (`pilesCount`) and what its next page
|
|
57
|
+
* brings (`morePiles`) — web-ui 2.19 draws «Ver N grupos más» from them. A group whose reading
|
|
58
|
+
* failed says why beside its name.
|
|
59
|
+
*
|
|
54
60
|
* What the server summed for a group is its `totals` — web-ui (2.9) says each figure under its
|
|
55
61
|
* column in a grouped table and a sheet, as Odoo's grouped list — and, in words beside the
|
|
56
62
|
* group's name (`detail`), what no column on screen says: a board's card has no columns.
|
|
@@ -59,7 +65,7 @@ export function pilesOf(buckets, levels, meta, locale, rowsOf,
|
|
|
59
65
|
/** What a group's next «more» brings (`Pile.more`); omitted, web-ui says nothing about it. */
|
|
60
66
|
brings,
|
|
61
67
|
/** The figures a column on screen already says under itself: not said again in words. */
|
|
62
|
-
underColumns = new Set()) {
|
|
68
|
+
underColumns = new Set(), tree) {
|
|
63
69
|
const bucketAt = new Map();
|
|
64
70
|
const none = frameworkCopy().list.noValue;
|
|
65
71
|
const labelFor = (bucket, depth, key) => {
|
|
@@ -81,7 +87,11 @@ underColumns = new Set()) {
|
|
|
81
87
|
return figure === undefined ? [] : [[field, figure]];
|
|
82
88
|
}));
|
|
83
89
|
const inWords = measures.filter(([field]) => !underColumns.has(field));
|
|
84
|
-
const
|
|
90
|
+
const failure = tree?.errorOf(bucket);
|
|
91
|
+
const refused = failure === null || failure === undefined
|
|
92
|
+
? undefined
|
|
93
|
+
: (({ heading, sentence }) => [heading, sentence].filter(Boolean).join(". "))(explain(failure));
|
|
94
|
+
const detail = refused ?? detailOf(meta, Object.fromEntries(inWords), locale);
|
|
85
95
|
return {
|
|
86
96
|
...(Object.keys(totals).length === 0 ? {} : { totals }),
|
|
87
97
|
...(detail === undefined ? {} : { detail }),
|
|
@@ -95,7 +105,10 @@ underColumns = new Set()) {
|
|
|
95
105
|
const key = bucket.value === null || bucket.value === undefined ? "" : String(bucket.value);
|
|
96
106
|
const path = [...above, key];
|
|
97
107
|
bucketAt.set(pathKey(path), bucket);
|
|
98
|
-
const
|
|
108
|
+
const deepest = tree === undefined ? (bucket.groups ?? []).length === 0 : tree.isDeepest(bucket);
|
|
109
|
+
const below = (tree === undefined ? bucket.groups : tree.groupsOf(bucket)) ?? [];
|
|
110
|
+
const counted = deepest ? undefined : tree?.groupsCountOf(bucket);
|
|
111
|
+
const next = deepest || tree === undefined ? undefined : tree.bringsGroups(bucket);
|
|
99
112
|
return {
|
|
100
113
|
path,
|
|
101
114
|
depth,
|
|
@@ -104,9 +117,11 @@ underColumns = new Set()) {
|
|
|
104
117
|
label: labelFor(bucket, depth, key),
|
|
105
118
|
count: bucket.count,
|
|
106
119
|
...summed(bucket),
|
|
107
|
-
items:
|
|
108
|
-
piles: walk(below, path, depth + 1),
|
|
109
|
-
...more(bucket,
|
|
120
|
+
items: deepest ? [...(rowsOf(bucket) ?? [])] : [],
|
|
121
|
+
piles: deepest ? [] : walk(below, path, depth + 1),
|
|
122
|
+
...more(bucket, deepest),
|
|
123
|
+
...(counted === null || counted === undefined ? {} : { pilesCount: counted }),
|
|
124
|
+
...(next === undefined ? {} : { morePiles: next }),
|
|
110
125
|
};
|
|
111
126
|
});
|
|
112
127
|
return { piles: walk(buckets, [], 0), bucketAt, deepest: levels.length - 1 };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sincpro/web-framework",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.16.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",
|
|
@@ -52,10 +52,10 @@
|
|
|
52
52
|
"@sentry/browser": "^11.11.0",
|
|
53
53
|
"@sentry/core": "^11.11.0",
|
|
54
54
|
"@sincpro/client-framework": "^2.7.0",
|
|
55
|
-
"@sincpro/criteria": "^2.
|
|
56
|
-
"@sincpro/criteria-react": "^2.
|
|
55
|
+
"@sincpro/criteria": "^2.5.1",
|
|
56
|
+
"@sincpro/criteria-react": "^2.5.1",
|
|
57
57
|
"@sincpro/web-composer": "^1.7.0",
|
|
58
|
-
"@sincpro/web-ui": "^2.
|
|
58
|
+
"@sincpro/web-ui": "^2.19.0",
|
|
59
59
|
"react": ">=19",
|
|
60
60
|
"react-dom": ">=19"
|
|
61
61
|
},
|
|
@@ -96,10 +96,10 @@
|
|
|
96
96
|
"@sentry/browser": "11.11.0",
|
|
97
97
|
"@sentry/core": "11.11.0",
|
|
98
98
|
"@sincpro/client-framework": "^2.7.0",
|
|
99
|
-
"@sincpro/criteria": "^2.5.
|
|
100
|
-
"@sincpro/criteria-react": "^2.5.
|
|
99
|
+
"@sincpro/criteria": "^2.5.1",
|
|
100
|
+
"@sincpro/criteria-react": "^2.5.1",
|
|
101
101
|
"@sincpro/web-composer": "^1.9.0",
|
|
102
|
-
"@sincpro/web-ui": "^2.
|
|
102
|
+
"@sincpro/web-ui": "^2.19.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",
|