@connextar/house 0.25.4 → 0.26.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/README.md +38 -0
- package/dist/inbox/inbox.d.ts +104 -0
- package/dist/inbox/inbox.d.ts.map +1 -0
- package/dist/inbox/inbox.js +178 -0
- package/dist/inbox/inbox.js.map +1 -0
- package/dist/inbox/index.d.ts +11 -0
- package/dist/inbox/index.d.ts.map +1 -0
- package/dist/inbox/index.js +11 -0
- package/dist/inbox/index.js.map +1 -0
- package/dist/inbox/use-selection.d.ts +23 -0
- package/dist/inbox/use-selection.d.ts.map +1 -0
- package/dist/inbox/use-selection.js +51 -0
- package/dist/inbox/use-selection.js.map +1 -0
- package/package.json +5 -1
- package/src/inbox/inbox.ts +200 -0
- package/src/inbox/index.ts +33 -0
- package/src/inbox/use-selection.ts +60 -0
package/README.md
CHANGED
|
@@ -2424,6 +2424,44 @@ filter is dropped rather than throwing when the menu entry is picked. Where view
|
|
|
2424
2424
|
are stored — a table keyed by account, or the browser — is the app's decision,
|
|
2425
2425
|
because only the app knows whether a view should follow someone to their phone.
|
|
2426
2426
|
|
|
2427
|
+
### `@connextar/house/inbox`
|
|
2428
|
+
|
|
2429
|
+
The rules of a notifications inbox — which list is showing, which page of it,
|
|
2430
|
+
which rows are ticked and what can be done to them — with no markup and no
|
|
2431
|
+
database in it. AltEd's learners were sent to the home page by a notification
|
|
2432
|
+
because there was nowhere else to go; the inbox it built for them, and for every
|
|
2433
|
+
other account, is the same thing any app with a bell needs, and the same small
|
|
2434
|
+
things go wrong in each: a page left empty after a delete, a tick still counted
|
|
2435
|
+
on a row that has turned the page, "Mark read" offered for rows already read.
|
|
2436
|
+
|
|
2437
|
+
- **Filters.** `INBOX_FILTERS` is `inbox` (everything not archived), `unread`
|
|
2438
|
+
and `archived`. `parseInboxQuery` reads `?filter=` and `?page=` from a
|
|
2439
|
+
request or a page's `searchParams`, and reads anything it doesn't recognise as
|
|
2440
|
+
the inbox's first page. `inboxHref` writes them back, leaving the defaults out.
|
|
2441
|
+
- **Pages.** Numbered, not "load more": `pageCount`, `clampPage` (the page before,
|
|
2442
|
+
once a delete empties the last), `pageRange` for "21–40 of 57", and
|
|
2443
|
+
`pageWindow`, which gives the numbers a pager shows — first, last, the current
|
|
2444
|
+
page and its neighbours, and a `"gap"` that never hides just one page.
|
|
2445
|
+
- **Ticks.** `useSelection(visibleKeys)` keeps only the ticked keys still on
|
|
2446
|
+
show, derived on every render, so turning a page or a row leaving after an
|
|
2447
|
+
action can't leave something ticked the reader can't see.
|
|
2448
|
+
- **Actions.** `INBOX_ACTIONS` is read, unread, archive, unarchive and delete.
|
|
2449
|
+
`actionsFor(rows)` offers only what would change at least one ticked row;
|
|
2450
|
+
`actionChanges` is the field each one sets, and `readsUnread` says when the
|
|
2451
|
+
unread ones are read as well (archiving: the archive never holds the unread
|
|
2452
|
+
count, and a row read earlier keeps its time); `afterAction` and `stillListed`
|
|
2453
|
+
are the same rules for an optimistic update; `actionLabel` and `actionDone`
|
|
2454
|
+
are the words. `inboxActionSchema` is a bulk request's body — at most
|
|
2455
|
+
`INBOX_MAX_BULK` ids, checked for shape. Ownership is the app's query: scope
|
|
2456
|
+
every update to the reader, and a stranger's id changes nothing.
|
|
2457
|
+
|
|
2458
|
+
```ts
|
|
2459
|
+
const { filter, page } = parseInboxQuery(new URL(request.url).searchParams);
|
|
2460
|
+
const parsed = inboxActionSchema.safeParse(await request.json());
|
|
2461
|
+
// …then, in the app's own storage:
|
|
2462
|
+
await db.notification.updateMany({ where: { id: { in: parsed.data.ids }, userId }, data: actionChanges(action) });
|
|
2463
|
+
```
|
|
2464
|
+
|
|
2427
2465
|
### `@connextar/house/web-search`
|
|
2428
2466
|
|
|
2429
2467
|
Google's organic results for a query, from whichever search API a deployment
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* The rules of a notifications inbox, with no markup and no database in them.
|
|
4
|
+
*
|
|
5
|
+
* Which list is showing (`filter`), which page of it (`page`), what can be done
|
|
6
|
+
* to the rows that are ticked (`actionsFor`), what a bulk request may carry
|
|
7
|
+
* (`inboxActionSchema`) and which page numbers a pager shows (`pageWindow`).
|
|
8
|
+
* The app owns the rows, the storage and the look; this owns the arithmetic
|
|
9
|
+
* that every inbox gets slightly wrong on its own.
|
|
10
|
+
*/
|
|
11
|
+
/** The lists an inbox shows: everything not archived, only the unread ones, or the archive. */
|
|
12
|
+
export declare const INBOX_FILTERS: readonly ["inbox", "unread", "archived"];
|
|
13
|
+
export type InboxFilter = (typeof INBOX_FILTERS)[number];
|
|
14
|
+
/** What can be done to ticked rows. `delete` is permanent; `archive` is the way to put something away. */
|
|
15
|
+
export declare const INBOX_ACTIONS: readonly ["read", "unread", "archive", "unarchive", "delete"];
|
|
16
|
+
export type InboxAction = (typeof INBOX_ACTIONS)[number];
|
|
17
|
+
/** Rows a page, unless the app says otherwise. */
|
|
18
|
+
export declare const INBOX_PAGE_SIZE = 20;
|
|
19
|
+
/** The most rows one bulk request may name — a page and then some, never "everything" by accident. */
|
|
20
|
+
export declare const INBOX_MAX_BULK = 100;
|
|
21
|
+
/**
|
|
22
|
+
* The body of a bulk request: which rows, and what to do to them. The ids are the reader's own only after the
|
|
23
|
+
* app's query scopes them to the reader — this checks shape, never ownership.
|
|
24
|
+
*/
|
|
25
|
+
export declare const inboxActionSchema: z.ZodObject<{
|
|
26
|
+
ids: z.ZodArray<z.ZodString>;
|
|
27
|
+
action: z.ZodEnum<{
|
|
28
|
+
delete: "delete";
|
|
29
|
+
read: "read";
|
|
30
|
+
archive: "archive";
|
|
31
|
+
unread: "unread";
|
|
32
|
+
unarchive: "unarchive";
|
|
33
|
+
}>;
|
|
34
|
+
}, z.core.$strip>;
|
|
35
|
+
export type InboxActionRequest = z.infer<typeof inboxActionSchema>;
|
|
36
|
+
type QueryInput = URLSearchParams | Record<string, string | string[] | undefined>;
|
|
37
|
+
/**
|
|
38
|
+
* `?filter=` and `?page=` from a request or a page's `searchParams`. Anything unrecognised reads as the inbox's
|
|
39
|
+
* first page rather than an error: an address someone edited, or one from before a filter was renamed, still opens.
|
|
40
|
+
*/
|
|
41
|
+
export declare function parseInboxQuery(input: QueryInput): {
|
|
42
|
+
filter: InboxFilter;
|
|
43
|
+
page: number;
|
|
44
|
+
};
|
|
45
|
+
/** The address of one page of one list, keeping the defaults out of it so the plain inbox is the plain path. */
|
|
46
|
+
export declare function inboxHref(path: string, filter: InboxFilter, page: number): string;
|
|
47
|
+
/** How many pages `total` rows fill; an empty list is one empty page, so "page 1 of 1" is always true. */
|
|
48
|
+
export declare function pageCount(total: number, pageSize?: number): number;
|
|
49
|
+
/** A requested page brought within the list — after a delete empties the last page, the one before it. */
|
|
50
|
+
export declare function clampPage(page: number, pages: number): number;
|
|
51
|
+
/** The first and last row numbers on a page, 1-based, for "21–40 of 57"; both 0 for an empty list. */
|
|
52
|
+
export declare function pageRange(page: number, total: number, pageSize?: number): {
|
|
53
|
+
from: number;
|
|
54
|
+
to: number;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* The page numbers a pager shows: always the first and last, the current one with `around` either side, and a
|
|
58
|
+
* `"gap"` where numbers are left out. A gap never stands for a single page — that page is shown instead, since a
|
|
59
|
+
* "…" hiding one number is longer than the number.
|
|
60
|
+
*/
|
|
61
|
+
export declare function pageWindow(page: number, pages: number, around?: number): Array<number | "gap">;
|
|
62
|
+
/** What an inbox needs to know about a row to offer actions on it. */
|
|
63
|
+
export interface InboxRowState {
|
|
64
|
+
read: boolean;
|
|
65
|
+
archived: boolean;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The actions worth offering for the ticked rows, in the order a toolbar shows them. Only what would change at
|
|
69
|
+
* least one of them: "Mark read" is offered while any is unread, "Unarchive" only in the archive. Delete is always
|
|
70
|
+
* offered for a selection, and never for an empty one.
|
|
71
|
+
*/
|
|
72
|
+
export declare function actionsFor(rows: readonly InboxRowState[]): InboxAction[];
|
|
73
|
+
/**
|
|
74
|
+
* The field an action sets on every named row. `delete` changes no field: the row goes.
|
|
75
|
+
*
|
|
76
|
+
* Archiving reads a row too — put away is dealt with, so the archive never holds the unread count — but only a row
|
|
77
|
+
* that is still unread: overwriting an earlier `readAt` would restart whatever the app keeps read rows for. That
|
|
78
|
+
* second, narrower update is `readsUnread`.
|
|
79
|
+
*/
|
|
80
|
+
export declare function actionChanges(action: Exclude<InboxAction, "delete">, now?: Date): {
|
|
81
|
+
readonly readAt: Date;
|
|
82
|
+
readonly archivedAt?: undefined;
|
|
83
|
+
} | {
|
|
84
|
+
readonly readAt: null;
|
|
85
|
+
readonly archivedAt?: undefined;
|
|
86
|
+
} | {
|
|
87
|
+
readonly archivedAt: Date;
|
|
88
|
+
readonly readAt?: undefined;
|
|
89
|
+
} | {
|
|
90
|
+
readonly archivedAt: null;
|
|
91
|
+
readonly readAt?: undefined;
|
|
92
|
+
};
|
|
93
|
+
/** Whether an action also sets `readAt` on the named rows that are unread (archiving does). */
|
|
94
|
+
export declare function readsUnread(action: InboxAction): boolean;
|
|
95
|
+
/** Whether a row still belongs in a list after an action — so a toolbar knows to take it out of view. */
|
|
96
|
+
export declare function stillListed(filter: InboxFilter, row: InboxRowState): boolean;
|
|
97
|
+
/** A row's state after an action, without asking the server — for an optimistic update. */
|
|
98
|
+
export declare function afterAction(action: Exclude<InboxAction, "delete">, row: InboxRowState): InboxRowState;
|
|
99
|
+
/** The button's words for an action. */
|
|
100
|
+
export declare function actionLabel(action: InboxAction): string;
|
|
101
|
+
/** What to say once an action has been applied to `n` rows. */
|
|
102
|
+
export declare function actionDone(action: InboxAction, n: number): string;
|
|
103
|
+
export {};
|
|
104
|
+
//# sourceMappingURL=inbox.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inbox.d.ts","sourceRoot":"","sources":["../../src/inbox/inbox.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK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GACjC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,CAI9B;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,SAAI,GAAG,KAAK,CAAC,MAAM,GAAG,KAAK,CAAC,CAezF;AAED,sEAAsE;AACtE,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,OAAO,CAAC;IACd,QAAQ,EAAE,OAAO,CAAC;CACnB;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,SAAS,aAAa,EAAE,GAAG,WAAW,EAAE,CASxE;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,MAAM,EAAE,OAAO,CAAC,WAAW,EAAE,QAAQ,CAAC,EAAE,GAAG,GAAE,IAAiB;;;;;;;;;;;;EAW3F;AAED,+FAA+F;AAC/F,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW,GAAG,OAAO,CAExD;AAED,yGAAyG;AACzG,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW,EAAE,GAAG,EAAE,aAAa,GAAG,OAAO,CAI5E;AAED,2FAA2F;AAC3F,wBAAgB,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,WAAW,EAAE,QAAQ,CAAC,EAAE,GAAG,EAAE,aAAa,GAAG,aAAa,CAWrG;AAcD,wCAAwC;AACxC,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,CAEvD;AAED,+DAA+D;AAC/D,wBAAgB,UAAU,CAAC,MAAM,EAAE,WAAW,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAEjE"}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* The rules of a notifications inbox, with no markup and no database in them.
|
|
4
|
+
*
|
|
5
|
+
* Which list is showing (`filter`), which page of it (`page`), what can be done
|
|
6
|
+
* to the rows that are ticked (`actionsFor`), what a bulk request may carry
|
|
7
|
+
* (`inboxActionSchema`) and which page numbers a pager shows (`pageWindow`).
|
|
8
|
+
* The app owns the rows, the storage and the look; this owns the arithmetic
|
|
9
|
+
* that every inbox gets slightly wrong on its own.
|
|
10
|
+
*/
|
|
11
|
+
/** The lists an inbox shows: everything not archived, only the unread ones, or the archive. */
|
|
12
|
+
export const INBOX_FILTERS = ["inbox", "unread", "archived"];
|
|
13
|
+
/** What can be done to ticked rows. `delete` is permanent; `archive` is the way to put something away. */
|
|
14
|
+
export const INBOX_ACTIONS = ["read", "unread", "archive", "unarchive", "delete"];
|
|
15
|
+
/** Rows a page, unless the app says otherwise. */
|
|
16
|
+
export const INBOX_PAGE_SIZE = 20;
|
|
17
|
+
/** The most rows one bulk request may name — a page and then some, never "everything" by accident. */
|
|
18
|
+
export const INBOX_MAX_BULK = 100;
|
|
19
|
+
/** The longest id accepted. A cuid is 25 characters, a UUID 36. */
|
|
20
|
+
const MAX_ID_LENGTH = 64;
|
|
21
|
+
/**
|
|
22
|
+
* The body of a bulk request: which rows, and what to do to them. The ids are the reader's own only after the
|
|
23
|
+
* app's query scopes them to the reader — this checks shape, never ownership.
|
|
24
|
+
*/
|
|
25
|
+
export const inboxActionSchema = z.object({
|
|
26
|
+
ids: z.array(z.string().min(1).max(MAX_ID_LENGTH)).min(1).max(INBOX_MAX_BULK),
|
|
27
|
+
action: z.enum(INBOX_ACTIONS),
|
|
28
|
+
});
|
|
29
|
+
function first(input, key) {
|
|
30
|
+
if (input instanceof URLSearchParams)
|
|
31
|
+
return input.get(key) ?? undefined;
|
|
32
|
+
const value = input[key];
|
|
33
|
+
return Array.isArray(value) ? value[0] : value;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* `?filter=` and `?page=` from a request or a page's `searchParams`. Anything unrecognised reads as the inbox's
|
|
37
|
+
* first page rather than an error: an address someone edited, or one from before a filter was renamed, still opens.
|
|
38
|
+
*/
|
|
39
|
+
export function parseInboxQuery(input) {
|
|
40
|
+
const rawFilter = first(input, "filter");
|
|
41
|
+
const filter = INBOX_FILTERS.includes(rawFilter) ? rawFilter : "inbox";
|
|
42
|
+
const rawPage = first(input, "page");
|
|
43
|
+
const parsed = rawPage && /^\d{1,6}$/.test(rawPage) ? Number(rawPage) : 1;
|
|
44
|
+
return { filter, page: Math.max(1, parsed) };
|
|
45
|
+
}
|
|
46
|
+
/** The address of one page of one list, keeping the defaults out of it so the plain inbox is the plain path. */
|
|
47
|
+
export function inboxHref(path, filter, page) {
|
|
48
|
+
const params = new URLSearchParams();
|
|
49
|
+
if (filter !== "inbox")
|
|
50
|
+
params.set("filter", filter);
|
|
51
|
+
if (page > 1)
|
|
52
|
+
params.set("page", String(page));
|
|
53
|
+
const query = params.toString();
|
|
54
|
+
return query ? `${path}?${query}` : path;
|
|
55
|
+
}
|
|
56
|
+
/** How many pages `total` rows fill; an empty list is one empty page, so "page 1 of 1" is always true. */
|
|
57
|
+
export function pageCount(total, pageSize = INBOX_PAGE_SIZE) {
|
|
58
|
+
return Math.max(1, Math.ceil(Math.max(0, total) / Math.max(1, pageSize)));
|
|
59
|
+
}
|
|
60
|
+
/** A requested page brought within the list — after a delete empties the last page, the one before it. */
|
|
61
|
+
export function clampPage(page, pages) {
|
|
62
|
+
return Math.min(Math.max(1, Math.floor(page) || 1), Math.max(1, pages));
|
|
63
|
+
}
|
|
64
|
+
/** The first and last row numbers on a page, 1-based, for "21–40 of 57"; both 0 for an empty list. */
|
|
65
|
+
export function pageRange(page, total, pageSize = INBOX_PAGE_SIZE) {
|
|
66
|
+
if (total <= 0)
|
|
67
|
+
return { from: 0, to: 0 };
|
|
68
|
+
const from = (page - 1) * pageSize + 1;
|
|
69
|
+
return { from, to: Math.min(page * pageSize, total) };
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The page numbers a pager shows: always the first and last, the current one with `around` either side, and a
|
|
73
|
+
* `"gap"` where numbers are left out. A gap never stands for a single page — that page is shown instead, since a
|
|
74
|
+
* "…" hiding one number is longer than the number.
|
|
75
|
+
*/
|
|
76
|
+
export function pageWindow(page, pages, around = 1) {
|
|
77
|
+
const last = Math.max(1, pages);
|
|
78
|
+
const current = clampPage(page, last);
|
|
79
|
+
const wanted = new Set([1, last]);
|
|
80
|
+
for (let p = current - around; p <= current + around; p++)
|
|
81
|
+
if (p >= 1 && p <= last)
|
|
82
|
+
wanted.add(p);
|
|
83
|
+
const sorted = [...wanted].sort((a, b) => a - b);
|
|
84
|
+
const out = [];
|
|
85
|
+
let previous = 0;
|
|
86
|
+
for (const p of sorted) {
|
|
87
|
+
if (p - previous === 2)
|
|
88
|
+
out.push(previous + 1);
|
|
89
|
+
else if (p - previous > 2)
|
|
90
|
+
out.push("gap");
|
|
91
|
+
out.push(p);
|
|
92
|
+
previous = p;
|
|
93
|
+
}
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* The actions worth offering for the ticked rows, in the order a toolbar shows them. Only what would change at
|
|
98
|
+
* least one of them: "Mark read" is offered while any is unread, "Unarchive" only in the archive. Delete is always
|
|
99
|
+
* offered for a selection, and never for an empty one.
|
|
100
|
+
*/
|
|
101
|
+
export function actionsFor(rows) {
|
|
102
|
+
if (rows.length === 0)
|
|
103
|
+
return [];
|
|
104
|
+
const actions = [];
|
|
105
|
+
if (rows.some((row) => !row.read))
|
|
106
|
+
actions.push("read");
|
|
107
|
+
if (rows.some((row) => row.read))
|
|
108
|
+
actions.push("unread");
|
|
109
|
+
if (rows.some((row) => !row.archived))
|
|
110
|
+
actions.push("archive");
|
|
111
|
+
if (rows.some((row) => row.archived))
|
|
112
|
+
actions.push("unarchive");
|
|
113
|
+
actions.push("delete");
|
|
114
|
+
return actions;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* The field an action sets on every named row. `delete` changes no field: the row goes.
|
|
118
|
+
*
|
|
119
|
+
* Archiving reads a row too — put away is dealt with, so the archive never holds the unread count — but only a row
|
|
120
|
+
* that is still unread: overwriting an earlier `readAt` would restart whatever the app keeps read rows for. That
|
|
121
|
+
* second, narrower update is `readsUnread`.
|
|
122
|
+
*/
|
|
123
|
+
export function actionChanges(action, now = new Date()) {
|
|
124
|
+
switch (action) {
|
|
125
|
+
case "read":
|
|
126
|
+
return { readAt: now };
|
|
127
|
+
case "unread":
|
|
128
|
+
return { readAt: null };
|
|
129
|
+
case "archive":
|
|
130
|
+
return { archivedAt: now };
|
|
131
|
+
case "unarchive":
|
|
132
|
+
return { archivedAt: null };
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/** Whether an action also sets `readAt` on the named rows that are unread (archiving does). */
|
|
136
|
+
export function readsUnread(action) {
|
|
137
|
+
return action === "archive";
|
|
138
|
+
}
|
|
139
|
+
/** Whether a row still belongs in a list after an action — so a toolbar knows to take it out of view. */
|
|
140
|
+
export function stillListed(filter, row) {
|
|
141
|
+
if (filter === "archived")
|
|
142
|
+
return row.archived;
|
|
143
|
+
if (filter === "unread")
|
|
144
|
+
return !row.archived && !row.read;
|
|
145
|
+
return !row.archived;
|
|
146
|
+
}
|
|
147
|
+
/** A row's state after an action, without asking the server — for an optimistic update. */
|
|
148
|
+
export function afterAction(action, row) {
|
|
149
|
+
switch (action) {
|
|
150
|
+
case "read":
|
|
151
|
+
return { ...row, read: true };
|
|
152
|
+
case "unread":
|
|
153
|
+
return { ...row, read: false };
|
|
154
|
+
case "archive":
|
|
155
|
+
return { read: true, archived: true };
|
|
156
|
+
case "unarchive":
|
|
157
|
+
return { ...row, archived: false };
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const VERBS = {
|
|
161
|
+
read: { label: "Mark read", done: (n) => `Marked ${count(n)} read.` },
|
|
162
|
+
unread: { label: "Mark unread", done: (n) => `Marked ${count(n)} unread.` },
|
|
163
|
+
archive: { label: "Archive", done: (n) => `Archived ${count(n)}.` },
|
|
164
|
+
unarchive: { label: "Move to inbox", done: (n) => `Moved ${count(n)} to the inbox.` },
|
|
165
|
+
delete: { label: "Delete", done: (n) => `Deleted ${count(n)}.` },
|
|
166
|
+
};
|
|
167
|
+
function count(n) {
|
|
168
|
+
return n === 1 ? "1 notification" : `${n} notifications`;
|
|
169
|
+
}
|
|
170
|
+
/** The button's words for an action. */
|
|
171
|
+
export function actionLabel(action) {
|
|
172
|
+
return VERBS[action].label;
|
|
173
|
+
}
|
|
174
|
+
/** What to say once an action has been applied to `n` rows. */
|
|
175
|
+
export function actionDone(action, n) {
|
|
176
|
+
return VERBS[action].done(n);
|
|
177
|
+
}
|
|
178
|
+
//# sourceMappingURL=inbox.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"inbox.js","sourceRoot":"","sources":["../../src/inbox/inbox.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB;;;;;;;;GAQG;AAEH,+FAA+F;AAC/F,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,OAAO,EAAE,QAAQ,EAAE,UAAU,CAAU,CAAC;AAGtE,0GAA0G;AAC1G,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,WAAW,EAAE,QAAQ,CAAU,CAAC;AAG3F,kDAAkD;AAClD,MAAM,CAAC,MAAM,eAAe,GAAG,EAAE,CAAC;AAElC,sGAAsG;AACtG,MAAM,CAAC,MAAM,cAAc,GAAG,GAAG,CAAC;AAElC,mEAAmE;AACnE,MAAM,aAAa,GAAG,EAAE,CAAC;AAEzB;;;GAGG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,CAAC,MAAM,CAAC;IACxC,GAAG,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,cAAc,CAAC;IAC7E,M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AE,IAAI,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,WAAW,KAAK,CAAC,CAAC,CAAC,GAAG,EAAE;CACjE,CAAC;AAEF,SAAS,KAAK,CAAC,CAAS;IACtB,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC,CAAC,GAAG,CAAC,gBAAgB,CAAC;AAC3D,CAAC;AAED,wCAAwC;AACxC,MAAM,UAAU,WAAW,CAAC,MAAmB;IAC7C,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC;AAC7B,CAAC;AAED,+DAA+D;AAC/D,MAAM,UAAU,UAAU,CAAC,MAAmB,EAAE,CAAS;IACvD,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAC/B,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/inbox` — the rules of a notifications inbox.
|
|
3
|
+
*
|
|
4
|
+
* Filters (inbox, unread, archived), numbered pages, ticked rows and the bulk
|
|
5
|
+
* actions on them — read, unread, archive, move back, delete — with no markup
|
|
6
|
+
* and no database in it: React and Zod, and nothing else. The app owns its rows,
|
|
7
|
+
* its storage and its look, the same bargain `grid` offers.
|
|
8
|
+
*/
|
|
9
|
+
export { INBOX_ACTIONS, INBOX_FILTERS, INBOX_MAX_BULK, INBOX_PAGE_SIZE, actionChanges, actionDone, actionLabel, actionsFor, afterAction, clampPage, inboxActionSchema, inboxHref, pageCount, pageRange, pageWindow, parseInboxQuery, readsUnread, stillListed, type InboxAction, type InboxActionRequest, type InboxFilter, type InboxRowState, } from "./inbox.js";
|
|
10
|
+
export { useSelection, type Selection } from "./use-selection.js";
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/inbox/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EACL,aAAa,EACb,aAAa,EACb,cAAc,EACd,eAAe,EACf,aAAa,EACb,UAAU,EACV,WAAW,EACX,UAAU,EACV,WAAW,EACX,SAAS,EACT,iBAAiB,EACjB,SAAS,EACT,SAAS,EACT,SAAS,EACT,UAAU,EACV,eAAe,EACf,WAAW,EACX,WAAW,EACX,KAAK,WAAW,EAChB,KAAK,kBAAkB,EACvB,KAAK,WAAW,EAChB,KAAK,aAAa,GACnB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,YAAY,EAAE,KAAK,SAAS,EAAE,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/inbox` — the rules of a notifications inbox.
|
|
3
|
+
*
|
|
4
|
+
* Filters (inbox, unread, archived), numbered pages, ticked rows and the bulk
|
|
5
|
+
* actions on them — read, unread, archive, move back, delete — with no markup
|
|
6
|
+
* and no database in it: React and Zod, and nothing else. The app owns its rows,
|
|
7
|
+
* its storage and its look, the same bargain `grid` offers.
|
|
8
|
+
*/
|
|
9
|
+
export { INBOX_ACTIONS, INBOX_FILTERS, INBOX_MAX_BULK, INBOX_PAGE_SIZE, actionChanges, actionDone, actionLabel, actionsFor, afterAction, clampPage, inboxActionSchema, inboxHref, pageCount, pageRange, pageWindow, parseInboxQuery, readsUnread, stillListed, } from "./inbox.js";
|
|
10
|
+
export { useSelection } from "./use-selection.js";
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/inbox/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,OAAO,EACL,aAAa,EACb,aAAa,EACb,cAAc,EACd,eAAe,EACf,aAAa,EACb,UAAU,EACV,WAAW,EACX,UAAU,EACV,WAAW,EACX,SAAS,EACT,iBAAiB,EACjB,SAAS,EACT,SAAS,EACT,SAAS,EACT,UAAU,EACV,eAAe,EACf,WAAW,EACX,WAAW,GAKZ,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,YAAY,EAAkB,MAAM,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ticked rows on a list that changes under them.
|
|
3
|
+
*
|
|
4
|
+
* Only the rows on show can be ticked: what is selected is always the ticked keys that are still in `visible`, so
|
|
5
|
+
* turning a page, changing the filter or a row leaving after an action can never leave something ticked that the
|
|
6
|
+
* reader can't see — and then act on it. Derived on every render rather than pruned by an effect, so there is no
|
|
7
|
+
* render in which a hidden row is still counted.
|
|
8
|
+
*/
|
|
9
|
+
export declare function useSelection(visible: readonly string[]): {
|
|
10
|
+
/** The ticked keys on show, in list order. */
|
|
11
|
+
keys: string[];
|
|
12
|
+
count: number;
|
|
13
|
+
/** Every row on show is ticked (and there is at least one). */
|
|
14
|
+
all: boolean;
|
|
15
|
+
/** Some, but not all, are ticked — a header checkbox's "indeterminate". */
|
|
16
|
+
some: boolean;
|
|
17
|
+
isSelected: (key: string) => boolean;
|
|
18
|
+
toggle: (key: string, on?: boolean) => void;
|
|
19
|
+
toggleAll: (on?: boolean) => void;
|
|
20
|
+
clear: () => void;
|
|
21
|
+
};
|
|
22
|
+
export type Selection = ReturnType<typeof useSelection>;
|
|
23
|
+
//# sourceMappingURL=use-selection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-selection.d.ts","sourceRoot":"","sources":["../../src/inbox/use-selection.ts"],"names":[],"mappings":"AAIA;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE;IAiCnD,8CAA8C;;;IAG9C,+DAA+D;;IAE/D,2EAA2E;;sBARlC,MAAM;kBAtBV,MAAM,OAAO,OAAO;qBAWnD,OAAO;;EA0BhB;AAED,MAAM,MAAM,SAAS,GAAG,UAAU,CAAC,OAAO,YAAY,CAAC,CAAC"}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
/**
|
|
4
|
+
* Ticked rows on a list that changes under them.
|
|
5
|
+
*
|
|
6
|
+
* Only the rows on show can be ticked: what is selected is always the ticked keys that are still in `visible`, so
|
|
7
|
+
* turning a page, changing the filter or a row leaving after an action can never leave something ticked that the
|
|
8
|
+
* reader can't see — and then act on it. Derived on every render rather than pruned by an effect, so there is no
|
|
9
|
+
* render in which a hidden row is still counted.
|
|
10
|
+
*/
|
|
11
|
+
export function useSelection(visible) {
|
|
12
|
+
const [ticked, setTicked] = React.useState(() => new Set());
|
|
13
|
+
const visibleSet = React.useMemo(() => new Set(visible), [visible]);
|
|
14
|
+
const keys = React.useMemo(() => visible.filter((key) => ticked.has(key)), [visible, ticked]);
|
|
15
|
+
const count = keys.length;
|
|
16
|
+
const all = visible.length > 0 && count === visible.length;
|
|
17
|
+
const toggle = React.useCallback((key, on) => {
|
|
18
|
+
setTicked((previous) => {
|
|
19
|
+
const next = new Set(previous);
|
|
20
|
+
const tick = on ?? !next.has(key);
|
|
21
|
+
if (tick)
|
|
22
|
+
next.add(key);
|
|
23
|
+
else
|
|
24
|
+
next.delete(key);
|
|
25
|
+
return next;
|
|
26
|
+
});
|
|
27
|
+
}, []);
|
|
28
|
+
const toggleAll = React.useCallback((on) => {
|
|
29
|
+
setTicked((previous) => {
|
|
30
|
+
const tickedVisible = [...visibleSet].filter((key) => previous.has(key)).length;
|
|
31
|
+
const tick = on ?? !(visibleSet.size > 0 && tickedVisible === visibleSet.size);
|
|
32
|
+
return tick ? new Set(visibleSet) : new Set();
|
|
33
|
+
});
|
|
34
|
+
}, [visibleSet]);
|
|
35
|
+
const clear = React.useCallback(() => setTicked(new Set()), []);
|
|
36
|
+
const isSelected = React.useCallback((key) => visibleSet.has(key) && ticked.has(key), [visibleSet, ticked]);
|
|
37
|
+
return {
|
|
38
|
+
/** The ticked keys on show, in list order. */
|
|
39
|
+
keys,
|
|
40
|
+
count,
|
|
41
|
+
/** Every row on show is ticked (and there is at least one). */
|
|
42
|
+
all,
|
|
43
|
+
/** Some, but not all, are ticked — a header checkbox's "indeterminate". */
|
|
44
|
+
some: count > 0 && !all,
|
|
45
|
+
isSelected,
|
|
46
|
+
toggle,
|
|
47
|
+
toggleAll,
|
|
48
|
+
clear,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
//# sourceMappingURL=use-selection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-selection.js","sourceRoot":"","sources":["../../src/inbox/use-selection.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAE/B;;;;;;;GAOG;AACH,MAAM,UAAU,YAAY,CAAC,OAA0B;IACrD,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAsB,GAAG,EAAE,CAAC,IAAI,GAAG,EAAE,CAAC,CAAC;IAEjF,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;IACpE,MAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC;IAC9F,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC;IAC1B,MAAM,GAAG,GAAG,OAAO,CAAC,MAAM,GAAG,CAAC,IAAI,KAAK,KAAK,OAAO,CAAC,MAAM,CAAC;IAE3D,MAAM,MAAM,GAAG,KAAK,CAAC,WAAW,CAAC,CAAC,GAAW,EAAE,EAAY,EAAE,EAAE;QAC7D,SAAS,CAAC,CAAC,QAAQ,EAAE,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AE,MAAM,CAAC,CAAC,CAAC;IAEpH,OAAO;QACL,8CAA8C;QAC9C,IAAI;QACJ,KAAK;QACL,+DAA+D;QAC/D,GAAG;QACH,2EAA2E;QAC3E,IAAI,EAAE,KAAK,GAAG,CAAC,IAAI,CAAC,GAAG;QACvB,UAAU;QACV,MAAM;QACN,SAAS;QACT,KAAK;KACN,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@connextar/house",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.26.0",
|
|
4
4
|
"description": "The pieces every app we build needs and none of them should own a copy of.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -104,6 +104,10 @@
|
|
|
104
104
|
"types": "./dist/grid/index.d.ts",
|
|
105
105
|
"default": "./dist/grid/index.js"
|
|
106
106
|
},
|
|
107
|
+
"./inbox": {
|
|
108
|
+
"types": "./dist/inbox/index.d.ts",
|
|
109
|
+
"default": "./dist/inbox/index.js"
|
|
110
|
+
},
|
|
107
111
|
"./jobs": {
|
|
108
112
|
"types": "./dist/jobs/index.d.ts",
|
|
109
113
|
"default": "./dist/jobs/index.js"
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The rules of a notifications inbox, with no markup and no database in them.
|
|
5
|
+
*
|
|
6
|
+
* Which list is showing (`filter`), which page of it (`page`), what can be done
|
|
7
|
+
* to the rows that are ticked (`actionsFor`), what a bulk request may carry
|
|
8
|
+
* (`inboxActionSchema`) and which page numbers a pager shows (`pageWindow`).
|
|
9
|
+
* The app owns the rows, the storage and the look; this owns the arithmetic
|
|
10
|
+
* that every inbox gets slightly wrong on its own.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** The lists an inbox shows: everything not archived, only the unread ones, or the archive. */
|
|
14
|
+
export const INBOX_FILTERS = ["inbox", "unread", "archived"] as const;
|
|
15
|
+
export type InboxFilter = (typeof INBOX_FILTERS)[number];
|
|
16
|
+
|
|
17
|
+
/** What can be done to ticked rows. `delete` is permanent; `archive` is the way to put something away. */
|
|
18
|
+
export const INBOX_ACTIONS = ["read", "unread", "archive", "unarchive", "delete"] as const;
|
|
19
|
+
export type InboxAction = (typeof INBOX_ACTIONS)[number];
|
|
20
|
+
|
|
21
|
+
/** Rows a page, unless the app says otherwise. */
|
|
22
|
+
export const INBOX_PAGE_SIZE = 20;
|
|
23
|
+
|
|
24
|
+
/** The most rows one bulk request may name — a page and then some, never "everything" by accident. */
|
|
25
|
+
export const INBOX_MAX_BULK = 100;
|
|
26
|
+
|
|
27
|
+
/** The longest id accepted. A cuid is 25 characters, a UUID 36. */
|
|
28
|
+
const MAX_ID_LENGTH = 64;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The body of a bulk request: which rows, and what to do to them. The ids are the reader's own only after the
|
|
32
|
+
* app's query scopes them to the reader — this checks shape, never ownership.
|
|
33
|
+
*/
|
|
34
|
+
export const inboxActionSchema = z.object({
|
|
35
|
+
ids: z.array(z.string().min(1).max(MAX_ID_LENGTH)).min(1).max(INBOX_MAX_BULK),
|
|
36
|
+
action: z.enum(INBOX_ACTIONS),
|
|
37
|
+
});
|
|
38
|
+
export type InboxActionRequest = z.infer<typeof inboxActionSchema>;
|
|
39
|
+
|
|
40
|
+
type QueryInput = URLSearchParams | Record<string, string | string[] | undefined>;
|
|
41
|
+
|
|
42
|
+
function first(input: QueryInput, key: string): string | undefined {
|
|
43
|
+
if (input instanceof URLSearchParams) return input.get(key) ?? undefined;
|
|
44
|
+
const value = input[key];
|
|
45
|
+
return Array.isArray(value) ? value[0] : value;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* `?filter=` and `?page=` from a request or a page's `searchParams`. Anything unrecognised reads as the inbox's
|
|
50
|
+
* first page rather than an error: an address someone edited, or one from before a filter was renamed, still opens.
|
|
51
|
+
*/
|
|
52
|
+
export function parseInboxQuery(input: QueryInput): { filter: InboxFilter; page: number } {
|
|
53
|
+
const rawFilter = first(input, "filter");
|
|
54
|
+
const filter = INBOX_FILTERS.includes(rawFilter as InboxFilter) ? (rawFilter as InboxFilter) : "inbox";
|
|
55
|
+
const rawPage = first(input, "page");
|
|
56
|
+
const parsed = rawPage && /^\d{1,6}$/.test(rawPage) ? Number(rawPage) : 1;
|
|
57
|
+
return { filter, page: Math.max(1, parsed) };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The address of one page of one list, keeping the defaults out of it so the plain inbox is the plain path. */
|
|
61
|
+
export function inboxHref(path: string, filter: InboxFilter, page: number): string {
|
|
62
|
+
const params = new URLSearchParams();
|
|
63
|
+
if (filter !== "inbox") params.set("filter", filter);
|
|
64
|
+
if (page > 1) params.set("page", String(page));
|
|
65
|
+
const query = params.toString();
|
|
66
|
+
return query ? `${path}?${query}` : path;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** How many pages `total` rows fill; an empty list is one empty page, so "page 1 of 1" is always true. */
|
|
70
|
+
export function pageCount(total: number, pageSize: number = INBOX_PAGE_SIZE): number {
|
|
71
|
+
return Math.max(1, Math.ceil(Math.max(0, total) / Math.max(1, pageSize)));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** A requested page brought within the list — after a delete empties the last page, the one before it. */
|
|
75
|
+
export function clampPage(page: number, pages: number): number {
|
|
76
|
+
return Math.min(Math.max(1, Math.floor(page) || 1), Math.max(1, pages));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The first and last row numbers on a page, 1-based, for "21–40 of 57"; both 0 for an empty list. */
|
|
80
|
+
export function pageRange(
|
|
81
|
+
page: number,
|
|
82
|
+
total: number,
|
|
83
|
+
pageSize: number = INBOX_PAGE_SIZE,
|
|
84
|
+
): { from: number; to: number } {
|
|
85
|
+
if (total <= 0) return { from: 0, to: 0 };
|
|
86
|
+
const from = (page - 1) * pageSize + 1;
|
|
87
|
+
return { from, to: Math.min(page * pageSize, total) };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* The page numbers a pager shows: always the first and last, the current one with `around` either side, and a
|
|
92
|
+
* `"gap"` where numbers are left out. A gap never stands for a single page — that page is shown instead, since a
|
|
93
|
+
* "…" hiding one number is longer than the number.
|
|
94
|
+
*/
|
|
95
|
+
export function pageWindow(page: number, pages: number, around = 1): Array<number | "gap"> {
|
|
96
|
+
const last = Math.max(1, pages);
|
|
97
|
+
const current = clampPage(page, last);
|
|
98
|
+
const wanted = new Set<number>([1, last]);
|
|
99
|
+
for (let p = current - around; p <= current + around; p++) if (p >= 1 && p <= last) wanted.add(p);
|
|
100
|
+
const sorted = [...wanted].sort((a, b) => a - b);
|
|
101
|
+
const out: Array<number | "gap"> = [];
|
|
102
|
+
let previous = 0;
|
|
103
|
+
for (const p of sorted) {
|
|
104
|
+
if (p - previous === 2) out.push(previous + 1);
|
|
105
|
+
else if (p - previous > 2) out.push("gap");
|
|
106
|
+
out.push(p);
|
|
107
|
+
previous = p;
|
|
108
|
+
}
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** What an inbox needs to know about a row to offer actions on it. */
|
|
113
|
+
export interface InboxRowState {
|
|
114
|
+
read: boolean;
|
|
115
|
+
archived: boolean;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* The actions worth offering for the ticked rows, in the order a toolbar shows them. Only what would change at
|
|
120
|
+
* least one of them: "Mark read" is offered while any is unread, "Unarchive" only in the archive. Delete is always
|
|
121
|
+
* offered for a selection, and never for an empty one.
|
|
122
|
+
*/
|
|
123
|
+
export function actionsFor(rows: readonly InboxRowState[]): InboxAction[] {
|
|
124
|
+
if (rows.length === 0) return [];
|
|
125
|
+
const actions: InboxAction[] = [];
|
|
126
|
+
if (rows.some((row) => !row.read)) actions.push("read");
|
|
127
|
+
if (rows.some((row) => row.read)) actions.push("unread");
|
|
128
|
+
if (rows.some((row) => !row.archived)) actions.push("archive");
|
|
129
|
+
if (rows.some((row) => row.archived)) actions.push("unarchive");
|
|
130
|
+
actions.push("delete");
|
|
131
|
+
return actions;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* The field an action sets on every named row. `delete` changes no field: the row goes.
|
|
136
|
+
*
|
|
137
|
+
* Archiving reads a row too — put away is dealt with, so the archive never holds the unread count — but only a row
|
|
138
|
+
* that is still unread: overwriting an earlier `readAt` would restart whatever the app keeps read rows for. That
|
|
139
|
+
* second, narrower update is `readsUnread`.
|
|
140
|
+
*/
|
|
141
|
+
export function actionChanges(action: Exclude<InboxAction, "delete">, now: Date = new Date()) {
|
|
142
|
+
switch (action) {
|
|
143
|
+
case "read":
|
|
144
|
+
return { readAt: now } as const;
|
|
145
|
+
case "unread":
|
|
146
|
+
return { readAt: null } as const;
|
|
147
|
+
case "archive":
|
|
148
|
+
return { archivedAt: now } as const;
|
|
149
|
+
case "unarchive":
|
|
150
|
+
return { archivedAt: null } as const;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Whether an action also sets `readAt` on the named rows that are unread (archiving does). */
|
|
155
|
+
export function readsUnread(action: InboxAction): boolean {
|
|
156
|
+
return action === "archive";
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Whether a row still belongs in a list after an action — so a toolbar knows to take it out of view. */
|
|
160
|
+
export function stillListed(filter: InboxFilter, row: InboxRowState): boolean {
|
|
161
|
+
if (filter === "archived") return row.archived;
|
|
162
|
+
if (filter === "unread") return !row.archived && !row.read;
|
|
163
|
+
return !row.archived;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** A row's state after an action, without asking the server — for an optimistic update. */
|
|
167
|
+
export function afterAction(action: Exclude<InboxAction, "delete">, row: InboxRowState): InboxRowState {
|
|
168
|
+
switch (action) {
|
|
169
|
+
case "read":
|
|
170
|
+
return { ...row, read: true };
|
|
171
|
+
case "unread":
|
|
172
|
+
return { ...row, read: false };
|
|
173
|
+
case "archive":
|
|
174
|
+
return { read: true, archived: true };
|
|
175
|
+
case "unarchive":
|
|
176
|
+
return { ...row, archived: false };
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const VERBS: Record<InboxAction, { label: string; done: (n: number) => string }> = {
|
|
181
|
+
read: { label: "Mark read", done: (n) => `Marked ${count(n)} read.` },
|
|
182
|
+
unread: { label: "Mark unread", done: (n) => `Marked ${count(n)} unread.` },
|
|
183
|
+
archive: { label: "Archive", done: (n) => `Archived ${count(n)}.` },
|
|
184
|
+
unarchive: { label: "Move to inbox", done: (n) => `Moved ${count(n)} to the inbox.` },
|
|
185
|
+
delete: { label: "Delete", done: (n) => `Deleted ${count(n)}.` },
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
function count(n: number): string {
|
|
189
|
+
return n === 1 ? "1 notification" : `${n} notifications`;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** The button's words for an action. */
|
|
193
|
+
export function actionLabel(action: InboxAction): string {
|
|
194
|
+
return VERBS[action].label;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** What to say once an action has been applied to `n` rows. */
|
|
198
|
+
export function actionDone(action: InboxAction, n: number): string {
|
|
199
|
+
return VERBS[action].done(n);
|
|
200
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@connextar/house/inbox` — the rules of a notifications inbox.
|
|
3
|
+
*
|
|
4
|
+
* Filters (inbox, unread, archived), numbered pages, ticked rows and the bulk
|
|
5
|
+
* actions on them — read, unread, archive, move back, delete — with no markup
|
|
6
|
+
* and no database in it: React and Zod, and nothing else. The app owns its rows,
|
|
7
|
+
* its storage and its look, the same bargain `grid` offers.
|
|
8
|
+
*/
|
|
9
|
+
export {
|
|
10
|
+
INBOX_ACTIONS,
|
|
11
|
+
INBOX_FILTERS,
|
|
12
|
+
INBOX_MAX_BULK,
|
|
13
|
+
INBOX_PAGE_SIZE,
|
|
14
|
+
actionChanges,
|
|
15
|
+
actionDone,
|
|
16
|
+
actionLabel,
|
|
17
|
+
actionsFor,
|
|
18
|
+
afterAction,
|
|
19
|
+
clampPage,
|
|
20
|
+
inboxActionSchema,
|
|
21
|
+
inboxHref,
|
|
22
|
+
pageCount,
|
|
23
|
+
pageRange,
|
|
24
|
+
pageWindow,
|
|
25
|
+
parseInboxQuery,
|
|
26
|
+
readsUnread,
|
|
27
|
+
stillListed,
|
|
28
|
+
type InboxAction,
|
|
29
|
+
type InboxActionRequest,
|
|
30
|
+
type InboxFilter,
|
|
31
|
+
type InboxRowState,
|
|
32
|
+
} from "./inbox.js";
|
|
33
|
+
export { useSelection, type Selection } from "./use-selection.js";
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Ticked rows on a list that changes under them.
|
|
7
|
+
*
|
|
8
|
+
* Only the rows on show can be ticked: what is selected is always the ticked keys that are still in `visible`, so
|
|
9
|
+
* turning a page, changing the filter or a row leaving after an action can never leave something ticked that the
|
|
10
|
+
* reader can't see — and then act on it. Derived on every render rather than pruned by an effect, so there is no
|
|
11
|
+
* render in which a hidden row is still counted.
|
|
12
|
+
*/
|
|
13
|
+
export function useSelection(visible: readonly string[]) {
|
|
14
|
+
const [ticked, setTicked] = React.useState<ReadonlySet<string>>(() => new Set());
|
|
15
|
+
|
|
16
|
+
const visibleSet = React.useMemo(() => new Set(visible), [visible]);
|
|
17
|
+
const keys = React.useMemo(() => visible.filter((key) => ticked.has(key)), [visible, ticked]);
|
|
18
|
+
const count = keys.length;
|
|
19
|
+
const all = visible.length > 0 && count === visible.length;
|
|
20
|
+
|
|
21
|
+
const toggle = React.useCallback((key: string, on?: boolean) => {
|
|
22
|
+
setTicked((previous) => {
|
|
23
|
+
const next = new Set(previous);
|
|
24
|
+
const tick = on ?? !next.has(key);
|
|
25
|
+
if (tick) next.add(key);
|
|
26
|
+
else next.delete(key);
|
|
27
|
+
return next;
|
|
28
|
+
});
|
|
29
|
+
}, []);
|
|
30
|
+
|
|
31
|
+
const toggleAll = React.useCallback(
|
|
32
|
+
(on?: boolean) => {
|
|
33
|
+
setTicked((previous) => {
|
|
34
|
+
const tickedVisible = [...visibleSet].filter((key) => previous.has(key)).length;
|
|
35
|
+
const tick = on ?? !(visibleSet.size > 0 && tickedVisible === visibleSet.size);
|
|
36
|
+
return tick ? new Set(visibleSet) : new Set();
|
|
37
|
+
});
|
|
38
|
+
},
|
|
39
|
+
[visibleSet],
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
const clear = React.useCallback(() => setTicked(new Set()), []);
|
|
43
|
+
const isSelected = React.useCallback((key: string) => visibleSet.has(key) && ticked.has(key), [visibleSet, ticked]);
|
|
44
|
+
|
|
45
|
+
return {
|
|
46
|
+
/** The ticked keys on show, in list order. */
|
|
47
|
+
keys,
|
|
48
|
+
count,
|
|
49
|
+
/** Every row on show is ticked (and there is at least one). */
|
|
50
|
+
all,
|
|
51
|
+
/** Some, but not all, are ticked — a header checkbox's "indeterminate". */
|
|
52
|
+
some: count > 0 && !all,
|
|
53
|
+
isSelected,
|
|
54
|
+
toggle,
|
|
55
|
+
toggleAll,
|
|
56
|
+
clear,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export type Selection = ReturnType<typeof useSelection>;
|