@remit/web-client 0.0.116 → 0.0.117
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/package.json +1 -1
- package/src/components/layout/MailTopBar.tsx +1 -2
- package/src/components/mail/MessageToolbar.tsx +1 -1
- package/src/components/mail/ThreadListInteraction.tsx +1 -2
- package/src/components/ui/KeyboardShortcutsModal.tsx +2 -3
- package/src/hooks/useTriageLayer.ts +1 -4
- package/src/hooks/useListCursor.test.ts +0 -273
- package/src/hooks/useListCursor.ts +0 -259
- package/src/hooks/useTriageKeyboard.ts +0 -108
- package/src/lib/keymap-dispatch.test.ts +0 -255
- package/src/lib/keymap-dispatch.ts +0 -244
- package/src/lib/keymap.test.ts +0 -68
- package/src/lib/keymap.ts +0 -210
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remit/web-client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.117",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Remit web client, published as composable primitives — the app shell, auth shells, and runtime config. A distributor imports what it composes and bundles it.",
|
|
6
6
|
"exports": {
|
|
@@ -7,13 +7,12 @@
|
|
|
7
7
|
* what wording is not decided here.
|
|
8
8
|
*/
|
|
9
9
|
import type { RemitImapAccountResponse } from "@remit/api-http-client/types.gen.ts";
|
|
10
|
-
import { ShellTopBar } from "@remit/ui";
|
|
10
|
+
import { ShellTopBar, shortcutHintForAction } from "@remit/ui";
|
|
11
11
|
import { useNavigate } from "@tanstack/react-router";
|
|
12
12
|
import { AccountMenu } from "@/auth/AccountMenu";
|
|
13
13
|
import { useGlobalCompose } from "@/hooks/useComposeTarget";
|
|
14
14
|
import { useSearchScope } from "@/hooks/useSearchScope";
|
|
15
15
|
import { openBugReport } from "@/lib/bug-report";
|
|
16
|
-
import { shortcutHintForAction } from "@/lib/keymap";
|
|
17
16
|
import { useMailContext } from "@/lib/mail-context";
|
|
18
17
|
|
|
19
18
|
interface MailTopBarProps {
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* are not rendered: focus stops moving, the highlight disappears, and the next
|
|
17
17
|
* verb acts on a message the user cannot see.
|
|
18
18
|
*/
|
|
19
|
-
import { SelectionTopBar, type Verb } from "@remit/ui";
|
|
19
|
+
import { SelectionTopBar, useListCursor, type Verb } from "@remit/ui";
|
|
20
20
|
import {
|
|
21
21
|
createContext,
|
|
22
22
|
type ReactNode,
|
|
@@ -30,7 +30,6 @@ import {
|
|
|
30
30
|
} from "react";
|
|
31
31
|
import { ConfirmDialog } from "@/components/ui/ConfirmDialog";
|
|
32
32
|
import { useFollowFocusOpen } from "@/hooks/useFollowFocusOpen";
|
|
33
|
-
import { useListCursor } from "@/hooks/useListCursor";
|
|
34
33
|
import { useIsDesktop } from "@/hooks/useMediaQuery";
|
|
35
34
|
import type { TriageContextUpdate } from "@/hooks/useTriageLayer";
|
|
36
35
|
import { formatDeleteToTrashTitle } from "@/lib/format";
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { Kbd } from "@remit/ui";
|
|
1
|
+
import { Kbd, KEY_HINT_GROUPS } from "@remit/ui";
|
|
2
2
|
import { X } from "lucide-react";
|
|
3
3
|
import { Fragment, useCallback, useEffect } from "react";
|
|
4
|
-
import { KEY_HINT_GROUPS } from "@/lib/keymap";
|
|
5
4
|
import { cn } from "@/lib/utils";
|
|
6
5
|
|
|
7
6
|
interface KeyboardShortcutsModalProps {
|
|
@@ -11,7 +10,7 @@ interface KeyboardShortcutsModalProps {
|
|
|
11
10
|
|
|
12
11
|
/**
|
|
13
12
|
* The `?` help overlay (#429). Renders the full triage key map from the single
|
|
14
|
-
* source of truth (
|
|
13
|
+
* source of truth (`@remit/ui`'s keymap) using the `Kbd` component, so the
|
|
15
14
|
* displayed bindings can never drift from what the dispatcher actually routes.
|
|
16
15
|
*/
|
|
17
16
|
export const KeyboardShortcutsModal = ({
|
|
@@ -14,12 +14,9 @@
|
|
|
14
14
|
* context comes first, the pane builds its handlers from it, and the keyboard
|
|
15
15
|
* registration comes last.
|
|
16
16
|
*/
|
|
17
|
+
import { type TriageHandlers, useTriageKeyboard } from "@remit/ui";
|
|
17
18
|
import { type RefObject, useCallback, useRef, useState } from "react";
|
|
18
19
|
import type { MessageListCommands } from "@/components/mail/MessageList";
|
|
19
|
-
import {
|
|
20
|
-
type TriageHandlers,
|
|
21
|
-
useTriageKeyboard,
|
|
22
|
-
} from "@/hooks/useTriageKeyboard";
|
|
23
20
|
import { adjacentMessageId } from "@/lib/adjacent-message";
|
|
24
21
|
|
|
25
22
|
export interface TriageContextUpdate {
|
|
@@ -1,273 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The shared list cursor (#149) — the roving keyboard cursor and multi-selection
|
|
3
|
-
* every thread list drives. Mounted against jsdom rather than `renderToString`,
|
|
4
|
-
* since the state only moves in response to real calls across renders.
|
|
5
|
-
*/
|
|
6
|
-
import assert from "node:assert/strict";
|
|
7
|
-
import { after, afterEach, before, beforeEach, describe, it } from "node:test";
|
|
8
|
-
import type { JSDOM } from "jsdom";
|
|
9
|
-
import { act, createElement } from "react";
|
|
10
|
-
import { createRoot, type Root } from "react-dom/client";
|
|
11
|
-
import { type ListCursor, useListCursor } from "./useListCursor";
|
|
12
|
-
|
|
13
|
-
const IDS = ["m1", "m2", "m3", "m4"];
|
|
14
|
-
|
|
15
|
-
let dom: JSDOM;
|
|
16
|
-
let container: HTMLElement;
|
|
17
|
-
let root: Root;
|
|
18
|
-
|
|
19
|
-
before(async () => {
|
|
20
|
-
const { JSDOM: JSDOMCtor } = await import("jsdom");
|
|
21
|
-
dom = new JSDOMCtor(
|
|
22
|
-
"<!doctype html><html><body><div id=root></div></body></html>",
|
|
23
|
-
{ url: "http://localhost/", pretendToBeVisual: true },
|
|
24
|
-
);
|
|
25
|
-
globalThis.window = dom.window as unknown as typeof globalThis.window;
|
|
26
|
-
globalThis.document = dom.window.document;
|
|
27
|
-
globalThis.HTMLElement = dom.window.HTMLElement;
|
|
28
|
-
globalThis.Element = dom.window.Element;
|
|
29
|
-
Object.defineProperty(globalThis, "navigator", {
|
|
30
|
-
value: dom.window.navigator,
|
|
31
|
-
configurable: true,
|
|
32
|
-
});
|
|
33
|
-
(
|
|
34
|
-
globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }
|
|
35
|
-
).IS_REACT_ACT_ENVIRONMENT = true;
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
after(() => dom.window.close());
|
|
39
|
-
|
|
40
|
-
beforeEach(() => {
|
|
41
|
-
container = dom.window.document.getElementById(
|
|
42
|
-
"root",
|
|
43
|
-
) as unknown as HTMLElement;
|
|
44
|
-
container.innerHTML = "";
|
|
45
|
-
root = createRoot(container);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
afterEach(() => {
|
|
49
|
-
act(() => root.unmount());
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Mount the hook and hand back a live handle. Every read goes through
|
|
54
|
-
* `current()` so a test never asserts against a stale render.
|
|
55
|
-
*/
|
|
56
|
-
function mountCursor(options: {
|
|
57
|
-
orderedIds?: string[];
|
|
58
|
-
isDesktop?: boolean;
|
|
59
|
-
initialFocusedId?: string;
|
|
60
|
-
}): () => ListCursor {
|
|
61
|
-
let latest: ListCursor | undefined;
|
|
62
|
-
const Probe = () => {
|
|
63
|
-
latest = useListCursor({
|
|
64
|
-
orderedIds: options.orderedIds ?? IDS,
|
|
65
|
-
isDesktop: options.isDesktop ?? true,
|
|
66
|
-
initialFocusedId: options.initialFocusedId,
|
|
67
|
-
});
|
|
68
|
-
return null;
|
|
69
|
-
};
|
|
70
|
-
act(() => root.render(createElement(Probe)));
|
|
71
|
-
return () => {
|
|
72
|
-
if (!latest) throw new Error("cursor not mounted");
|
|
73
|
-
return latest;
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
describe("useListCursor — the roving cursor", () => {
|
|
78
|
-
it("starts on the open thread and walks the list with next/previous", () => {
|
|
79
|
-
const cursor = mountCursor({ initialFocusedId: "m2" });
|
|
80
|
-
assert.equal(cursor().focusedMessageId, "m2");
|
|
81
|
-
|
|
82
|
-
act(() => cursor().focusNext());
|
|
83
|
-
assert.equal(cursor().focusedMessageId, "m3");
|
|
84
|
-
|
|
85
|
-
act(() => cursor().focusPrevious());
|
|
86
|
-
assert.equal(cursor().focusedMessageId, "m2");
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
it("starts at the top when nothing is focused yet", () => {
|
|
90
|
-
const cursor = mountCursor({});
|
|
91
|
-
assert.equal(cursor().focusedMessageId, undefined);
|
|
92
|
-
|
|
93
|
-
act(() => cursor().focusNext());
|
|
94
|
-
assert.equal(cursor().focusedMessageId, "m1");
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
it("clamps at both ends rather than wrapping", () => {
|
|
98
|
-
const cursor = mountCursor({ initialFocusedId: "m1" });
|
|
99
|
-
act(() => cursor().focusPrevious());
|
|
100
|
-
assert.equal(cursor().focusedMessageId, "m1");
|
|
101
|
-
|
|
102
|
-
act(() => cursor().focusLast());
|
|
103
|
-
assert.equal(cursor().focusedMessageId, "m4");
|
|
104
|
-
act(() => cursor().focusNext());
|
|
105
|
-
assert.equal(cursor().focusedMessageId, "m4");
|
|
106
|
-
|
|
107
|
-
act(() => cursor().focusFirst());
|
|
108
|
-
assert.equal(cursor().focusedMessageId, "m1");
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
it("moves nothing when the list is empty", () => {
|
|
112
|
-
const cursor = mountCursor({ orderedIds: [] });
|
|
113
|
-
act(() => cursor().focusNext());
|
|
114
|
-
act(() => cursor().focusLast());
|
|
115
|
-
assert.equal(cursor().focusedMessageId, undefined);
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
it("asks for DOM focus on its own moves, not on a pointer move", () => {
|
|
119
|
-
const cursor = mountCursor({ initialFocusedId: "m1" });
|
|
120
|
-
act(() => cursor().focusNext());
|
|
121
|
-
assert.equal(cursor().pendingDomFocusRef.current, "m2");
|
|
122
|
-
assert.equal(cursor().cursorMovedByPointerRef.current, false);
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
describe("useListCursor — selection", () => {
|
|
127
|
-
it("x toggles the row under the cursor", () => {
|
|
128
|
-
const cursor = mountCursor({ initialFocusedId: "m2" });
|
|
129
|
-
act(() => cursor().toggleFocusedSelection());
|
|
130
|
-
assert.deepEqual([...cursor().selection.selectedIds], ["m2"]);
|
|
131
|
-
|
|
132
|
-
act(() => cursor().toggleFocusedSelection());
|
|
133
|
-
assert.equal(cursor().selection.selectedCount, 0);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it("shift-extend moves the cursor and grows the range from the anchor", () => {
|
|
137
|
-
const cursor = mountCursor({ initialFocusedId: "m2" });
|
|
138
|
-
|
|
139
|
-
// The first press has no anchor yet, so the row it lands on becomes both
|
|
140
|
-
// the anchor and the whole range.
|
|
141
|
-
act(() => cursor().extendRangeDown());
|
|
142
|
-
assert.deepEqual([...cursor().selection.selectedIds], ["m3"]);
|
|
143
|
-
assert.equal(cursor().focusedMessageId, "m3");
|
|
144
|
-
assert.equal(cursor().selection.anchorId, "m3");
|
|
145
|
-
|
|
146
|
-
// Consecutive presses extend from that anchor.
|
|
147
|
-
act(() => cursor().extendRangeDown());
|
|
148
|
-
assert.deepEqual([...cursor().selection.selectedIds].sort(), ["m3", "m4"]);
|
|
149
|
-
assert.equal(cursor().focusedMessageId, "m4");
|
|
150
|
-
assert.equal(cursor().selection.anchorId, "m3");
|
|
151
|
-
});
|
|
152
|
-
|
|
153
|
-
it("shift-extend upward ranges back through the anchor", () => {
|
|
154
|
-
const cursor = mountCursor({ initialFocusedId: "m4" });
|
|
155
|
-
|
|
156
|
-
act(() => cursor().extendRangeUp());
|
|
157
|
-
assert.deepEqual([...cursor().selection.selectedIds], ["m3"]);
|
|
158
|
-
|
|
159
|
-
act(() => cursor().extendRangeUp());
|
|
160
|
-
assert.deepEqual([...cursor().selection.selectedIds].sort(), ["m2", "m3"]);
|
|
161
|
-
assert.equal(cursor().focusedMessageId, "m2");
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
it("at the last row, extending takes that row and the cursor stays", () => {
|
|
165
|
-
const cursor = mountCursor({ initialFocusedId: "m4" });
|
|
166
|
-
act(() => cursor().extendRangeDown());
|
|
167
|
-
assert.deepEqual([...cursor().selection.selectedIds], ["m4"]);
|
|
168
|
-
assert.equal(cursor().focusedMessageId, "m4");
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
it("select-all takes every row, and exiting clears it", () => {
|
|
172
|
-
const cursor = mountCursor({});
|
|
173
|
-
act(() => cursor().selectAllLoaded());
|
|
174
|
-
assert.equal(cursor().selection.selectedCount, IDS.length);
|
|
175
|
-
|
|
176
|
-
act(() => cursor().exitSelection());
|
|
177
|
-
assert.equal(cursor().selection.selectedCount, 0);
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
it("runs the caller's teardown before clearing", () => {
|
|
181
|
-
const order: string[] = [];
|
|
182
|
-
let latest: ListCursor | undefined;
|
|
183
|
-
const Probe = () => {
|
|
184
|
-
latest = useListCursor({
|
|
185
|
-
orderedIds: IDS,
|
|
186
|
-
isDesktop: true,
|
|
187
|
-
onExitSelection: () => order.push("teardown"),
|
|
188
|
-
});
|
|
189
|
-
return null;
|
|
190
|
-
};
|
|
191
|
-
act(() => root.render(createElement(Probe)));
|
|
192
|
-
act(() => latest?.selectAllLoaded());
|
|
193
|
-
act(() => latest?.exitSelection());
|
|
194
|
-
assert.deepEqual(order, ["teardown"]);
|
|
195
|
-
assert.equal(latest?.selection.selectedCount, 0);
|
|
196
|
-
});
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
describe("useListCursor — mouse selection semantics", () => {
|
|
200
|
-
it("shift-click ranges, cmd-click toggles, and both consume the click", () => {
|
|
201
|
-
const cursor = mountCursor({ initialFocusedId: "m1" });
|
|
202
|
-
|
|
203
|
-
let handled = false;
|
|
204
|
-
act(() => {
|
|
205
|
-
handled = cursor().handleRowSelect("m3", {
|
|
206
|
-
shiftKey: true,
|
|
207
|
-
metaKey: false,
|
|
208
|
-
ctrlKey: false,
|
|
209
|
-
});
|
|
210
|
-
});
|
|
211
|
-
assert.equal(handled, true);
|
|
212
|
-
assert.deepEqual([...cursor().selection.selectedIds].sort(), [
|
|
213
|
-
"m1",
|
|
214
|
-
"m2",
|
|
215
|
-
"m3",
|
|
216
|
-
]);
|
|
217
|
-
|
|
218
|
-
act(() => {
|
|
219
|
-
handled = cursor().handleRowSelect("m4", {
|
|
220
|
-
shiftKey: false,
|
|
221
|
-
metaKey: true,
|
|
222
|
-
ctrlKey: false,
|
|
223
|
-
});
|
|
224
|
-
});
|
|
225
|
-
assert.equal(handled, true);
|
|
226
|
-
assert.ok(cursor().selection.selectedIds.has("m4"));
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
it("a plain click collapses the selection and lets the row open", () => {
|
|
230
|
-
const cursor = mountCursor({ initialFocusedId: "m1" });
|
|
231
|
-
act(() => cursor().selectAllLoaded());
|
|
232
|
-
|
|
233
|
-
let handled = true;
|
|
234
|
-
act(() => {
|
|
235
|
-
handled = cursor().handleRowSelect("m2", {
|
|
236
|
-
shiftKey: false,
|
|
237
|
-
metaKey: false,
|
|
238
|
-
ctrlKey: false,
|
|
239
|
-
});
|
|
240
|
-
});
|
|
241
|
-
assert.equal(handled, false, "navigation must proceed on a plain click");
|
|
242
|
-
assert.equal(cursor().selection.selectedCount, 0);
|
|
243
|
-
});
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
describe("useListCursor — the device branch", () => {
|
|
247
|
-
it("is not in multi-select mode on desktop, whatever is selected", () => {
|
|
248
|
-
const cursor = mountCursor({ isDesktop: true });
|
|
249
|
-
act(() => cursor().selectAllLoaded());
|
|
250
|
-
assert.equal(cursor().isMultiSelectMode, false);
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
it("enters multi-select mode on touch as soon as something is selected", () => {
|
|
254
|
-
const cursor = mountCursor({ isDesktop: false });
|
|
255
|
-
assert.equal(cursor().isMultiSelectMode, false);
|
|
256
|
-
|
|
257
|
-
act(() => cursor().selection.select("m2"));
|
|
258
|
-
assert.equal(cursor().isMultiSelectMode, true);
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
it("on touch, in multi-select mode, next/previous toggle instead of moving", () => {
|
|
262
|
-
const cursor = mountCursor({ isDesktop: false, initialFocusedId: "m1" });
|
|
263
|
-
act(() => cursor().selection.select("m1"));
|
|
264
|
-
|
|
265
|
-
act(() => cursor().focusNext());
|
|
266
|
-
assert.equal(
|
|
267
|
-
cursor().focusedMessageId,
|
|
268
|
-
"m1",
|
|
269
|
-
"the cursor stays put in multi-select mode",
|
|
270
|
-
);
|
|
271
|
-
assert.deepEqual([...cursor().selection.selectedIds].sort(), ["m1", "m2"]);
|
|
272
|
-
});
|
|
273
|
-
});
|
|
@@ -1,259 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* useListCursor — the roving keyboard cursor and multi-selection of a thread
|
|
3
|
-
* list, independent of how the list renders.
|
|
4
|
-
*
|
|
5
|
-
* The mailbox list, the daily brief and Flagged all need it. It used to exist
|
|
6
|
-
* only inside `MessageList`, so the brief and Flagged had no cursor and no
|
|
7
|
-
* selection at all (#149). Everything here is list-shape agnostic: it works off
|
|
8
|
-
* the ordered message ids, so a virtualized flat list and a sectioned brief
|
|
9
|
-
* drive the same state.
|
|
10
|
-
*
|
|
11
|
-
* DOM concerns stay with the caller. `pendingDomFocusRef` names the row that
|
|
12
|
-
* should take real browser focus once it is rendered, and
|
|
13
|
-
* `cursorMovedByPointerRef` records whether the last move came from a click, so
|
|
14
|
-
* a list that scrolls its cursor into view can skip doing so for pointer moves.
|
|
15
|
-
*/
|
|
16
|
-
import {
|
|
17
|
-
deriveIsMultiSelectMode,
|
|
18
|
-
nextFocusId,
|
|
19
|
-
rowSelectIntent,
|
|
20
|
-
type SelectionModifiers,
|
|
21
|
-
useSelection,
|
|
22
|
-
} from "@remit/ui";
|
|
23
|
-
import { useCallback, useMemo, useRef, useState } from "react";
|
|
24
|
-
|
|
25
|
-
interface UseListCursorOptions {
|
|
26
|
-
/** Message ids in display order. */
|
|
27
|
-
orderedIds: string[];
|
|
28
|
-
isDesktop: boolean;
|
|
29
|
-
/** Seeds the cursor — normally the open thread. */
|
|
30
|
-
initialFocusedId?: string;
|
|
31
|
-
/** Extra teardown run before the selection is cleared. */
|
|
32
|
-
onExitSelection?: () => void;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export interface ListCursor {
|
|
36
|
-
focusedMessageId: string | undefined;
|
|
37
|
-
setFocusedMessageId: (id: string | undefined) => void;
|
|
38
|
-
/**
|
|
39
|
-
* The row a keyboard command last moved the cursor onto, and `undefined`
|
|
40
|
-
* whenever the cursor last moved some other way. Drives the reading pane
|
|
41
|
-
* following the cursor (`useFollowFocusOpen`) — a click and Enter open on
|
|
42
|
-
* their own, so only a bare cursor move is left to follow.
|
|
43
|
-
*/
|
|
44
|
-
keyboardFocusedMessageId: string | undefined;
|
|
45
|
-
focusIndex: number;
|
|
46
|
-
pendingDomFocusRef: React.RefObject<string | null>;
|
|
47
|
-
cursorMovedByPointerRef: React.RefObject<boolean>;
|
|
48
|
-
selection: ReturnType<typeof useSelection>;
|
|
49
|
-
isMultiSelectMode: boolean;
|
|
50
|
-
exitSelection: () => void;
|
|
51
|
-
moveFocusToIndex: (index: number) => void;
|
|
52
|
-
focusNext: () => void;
|
|
53
|
-
focusPrevious: () => void;
|
|
54
|
-
focusFirst: () => void;
|
|
55
|
-
focusLast: () => void;
|
|
56
|
-
toggleFocusedSelection: () => void;
|
|
57
|
-
extendRangeUp: () => void;
|
|
58
|
-
extendRangeDown: () => void;
|
|
59
|
-
selectAllLoaded: () => void;
|
|
60
|
-
/**
|
|
61
|
-
* Desktop mouse selection semantics (Apple Mail / Gmail model). Returns true
|
|
62
|
-
* when selection handled the click — the caller must then suppress the
|
|
63
|
-
* row's navigation; false for a plain click.
|
|
64
|
-
*/
|
|
65
|
-
handleRowSelect: (
|
|
66
|
-
messageId: string,
|
|
67
|
-
modifiers: SelectionModifiers,
|
|
68
|
-
) => boolean;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export const useListCursor = ({
|
|
72
|
-
orderedIds,
|
|
73
|
-
isDesktop,
|
|
74
|
-
initialFocusedId,
|
|
75
|
-
onExitSelection,
|
|
76
|
-
}: UseListCursorOptions): ListCursor => {
|
|
77
|
-
// The keyboard "where am I" pointer, distinct from the open thread
|
|
78
|
-
// (`selectedMessageId` in the URL). j/k move this cursor; Enter opens the
|
|
79
|
-
// focused row, and on desktop the reading pane follows the cursor of its own
|
|
80
|
-
// accord (`useFollowFocusOpen`). It seeds from the open thread so opening a
|
|
81
|
-
// message also focuses its row.
|
|
82
|
-
const [focusedMessageId, setFocusedId] = useState<string | undefined>(
|
|
83
|
-
initialFocusedId,
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
// Which of those moves came from a keyboard command, so the reading pane can
|
|
87
|
-
// follow the cursor without following a click that already opened its own row.
|
|
88
|
-
const [keyboardFocusedMessageId, setKeyboardFocusedMessageId] = useState<
|
|
89
|
-
string | undefined
|
|
90
|
-
>();
|
|
91
|
-
|
|
92
|
-
// Every non-keyboard move — a click, Tab, a thread opening, a refetch snapping
|
|
93
|
-
// the cursor to a survivor — drops the keyboard mark, so nothing follows it.
|
|
94
|
-
// A row taking DOM focus as the *consequence* of a keyboard move arrives here
|
|
95
|
-
// with the id that move just set; keeping the mark in that case is what stops
|
|
96
|
-
// the browser's own focus event from cancelling the load the move started.
|
|
97
|
-
const setFocusedMessageId = useCallback((id: string | undefined) => {
|
|
98
|
-
setKeyboardFocusedMessageId((current) =>
|
|
99
|
-
current === id ? current : undefined,
|
|
100
|
-
);
|
|
101
|
-
setFocusedId(id);
|
|
102
|
-
}, []);
|
|
103
|
-
|
|
104
|
-
const selection = useSelection();
|
|
105
|
-
const {
|
|
106
|
-
selectedCount,
|
|
107
|
-
toggle: toggleCheck,
|
|
108
|
-
clearSelection,
|
|
109
|
-
selectRange,
|
|
110
|
-
setAnchor,
|
|
111
|
-
selectAll,
|
|
112
|
-
} = selection;
|
|
113
|
-
|
|
114
|
-
// The selection count is the only source of truth for whether the list is in
|
|
115
|
-
// multi-select mode (#115). A separate flag needs an effect to reconcile it
|
|
116
|
-
// back to the count, and across that render the two disagree.
|
|
117
|
-
const isMultiSelectMode = deriveIsMultiSelectMode(selectedCount, isDesktop);
|
|
118
|
-
|
|
119
|
-
// Set when a keyboard command moves the cursor. Real DOM focus then follows
|
|
120
|
-
// it onto the row once rendered, so the browser's own focus — and therefore
|
|
121
|
-
// Tab, Shift+Tab and the focus ring — agree with what the list highlights
|
|
122
|
-
// (#43).
|
|
123
|
-
const pendingDomFocusRef = useRef<string | null>(null);
|
|
124
|
-
// Whether the cursor's last move came from a row taking DOM focus (a click)
|
|
125
|
-
// rather than a command. Scrolling for a click moves the row out from under
|
|
126
|
-
// the pointer between mousedown and click, so the click lands on empty space
|
|
127
|
-
// and nothing opens (#85).
|
|
128
|
-
const cursorMovedByPointerRef = useRef(false);
|
|
129
|
-
|
|
130
|
-
const exitSelection = useCallback(() => {
|
|
131
|
-
onExitSelection?.();
|
|
132
|
-
clearSelection();
|
|
133
|
-
}, [clearSelection, onExitSelection]);
|
|
134
|
-
|
|
135
|
-
const focusIndex = useMemo(
|
|
136
|
-
() => (focusedMessageId ? orderedIds.indexOf(focusedMessageId) : -1),
|
|
137
|
-
[orderedIds, focusedMessageId],
|
|
138
|
-
);
|
|
139
|
-
|
|
140
|
-
// Move the cursor by index. In multi-select mode (mobile) j/k toggle
|
|
141
|
-
// selection rather than moving a cursor.
|
|
142
|
-
const moveFocusToIndex = useCallback(
|
|
143
|
-
(index: number) => {
|
|
144
|
-
if (index < 0 || index >= orderedIds.length) return;
|
|
145
|
-
const messageId = orderedIds[index];
|
|
146
|
-
if (isMultiSelectMode) {
|
|
147
|
-
toggleCheck(messageId);
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
pendingDomFocusRef.current = messageId;
|
|
151
|
-
cursorMovedByPointerRef.current = false;
|
|
152
|
-
setKeyboardFocusedMessageId(messageId);
|
|
153
|
-
setFocusedId(messageId);
|
|
154
|
-
},
|
|
155
|
-
[orderedIds, isMultiSelectMode, toggleCheck],
|
|
156
|
-
);
|
|
157
|
-
|
|
158
|
-
const focusNext = useCallback(() => {
|
|
159
|
-
if (orderedIds.length === 0) return;
|
|
160
|
-
moveFocusToIndex(
|
|
161
|
-
focusIndex < 0 ? 0 : Math.min(focusIndex + 1, orderedIds.length - 1),
|
|
162
|
-
);
|
|
163
|
-
}, [orderedIds.length, focusIndex, moveFocusToIndex]);
|
|
164
|
-
|
|
165
|
-
const focusPrevious = useCallback(() => {
|
|
166
|
-
if (orderedIds.length === 0) return;
|
|
167
|
-
moveFocusToIndex(focusIndex <= 0 ? 0 : focusIndex - 1);
|
|
168
|
-
}, [orderedIds.length, focusIndex, moveFocusToIndex]);
|
|
169
|
-
|
|
170
|
-
const focusFirst = useCallback(() => moveFocusToIndex(0), [moveFocusToIndex]);
|
|
171
|
-
const focusLast = useCallback(
|
|
172
|
-
() => moveFocusToIndex(orderedIds.length - 1),
|
|
173
|
-
[moveFocusToIndex, orderedIds.length],
|
|
174
|
-
);
|
|
175
|
-
|
|
176
|
-
const toggleFocusedSelection = useCallback(() => {
|
|
177
|
-
if (focusedMessageId) toggleCheck(focusedMessageId);
|
|
178
|
-
}, [focusedMessageId, toggleCheck]);
|
|
179
|
-
|
|
180
|
-
const handleRowSelect = useCallback(
|
|
181
|
-
(messageId: string, modifiers: SelectionModifiers): boolean => {
|
|
182
|
-
const intent = rowSelectIntent(modifiers);
|
|
183
|
-
if (intent === "range") {
|
|
184
|
-
// The open/focused row is the fallback origin when the stored anchor
|
|
185
|
-
// has been filtered or searched out of the visible list, so the first
|
|
186
|
-
// shift-click still ranges from where the user is (#142, #144).
|
|
187
|
-
selectRange(orderedIds, messageId, focusedMessageId);
|
|
188
|
-
return true;
|
|
189
|
-
}
|
|
190
|
-
if (intent === "toggle") {
|
|
191
|
-
toggleCheck(messageId);
|
|
192
|
-
return true;
|
|
193
|
-
}
|
|
194
|
-
// Plain click: collapse any multi-selection and let navigation proceed.
|
|
195
|
-
// The clicked row becomes the next anchor for a subsequent shift-click,
|
|
196
|
-
// but is NOT added to the checkbox set (no toolbar on a plain open).
|
|
197
|
-
exitSelection();
|
|
198
|
-
setAnchor(messageId);
|
|
199
|
-
return false;
|
|
200
|
-
},
|
|
201
|
-
[
|
|
202
|
-
orderedIds,
|
|
203
|
-
focusedMessageId,
|
|
204
|
-
selectRange,
|
|
205
|
-
toggleCheck,
|
|
206
|
-
exitSelection,
|
|
207
|
-
setAnchor,
|
|
208
|
-
],
|
|
209
|
-
);
|
|
210
|
-
|
|
211
|
-
// Shift+arrow moves the cursor one row and adds the row it lands on to the
|
|
212
|
-
// range — the keyboard equivalent of shift-click. The first press seeds the
|
|
213
|
-
// anchor on that row; consecutive presses extend from it. The range only
|
|
214
|
-
// grows, so reversing direction ranges back through the anchor rather than
|
|
215
|
-
// giving rows up.
|
|
216
|
-
const extendRange = useCallback(
|
|
217
|
-
(direction: -1 | 1) => {
|
|
218
|
-
const target = nextFocusId(orderedIds, focusedMessageId, direction);
|
|
219
|
-
if (target === undefined) return;
|
|
220
|
-
selectRange(orderedIds, target);
|
|
221
|
-
pendingDomFocusRef.current = target;
|
|
222
|
-
cursorMovedByPointerRef.current = false;
|
|
223
|
-
// Shift+arrow is building a range, not reading. The reading pane stays on
|
|
224
|
-
// whatever is open rather than chasing the growing edge of the selection.
|
|
225
|
-
setKeyboardFocusedMessageId(undefined);
|
|
226
|
-
setFocusedId(target);
|
|
227
|
-
},
|
|
228
|
-
[orderedIds, focusedMessageId, selectRange],
|
|
229
|
-
);
|
|
230
|
-
|
|
231
|
-
const extendRangeUp = useCallback(() => extendRange(-1), [extendRange]);
|
|
232
|
-
const extendRangeDown = useCallback(() => extendRange(1), [extendRange]);
|
|
233
|
-
|
|
234
|
-
const selectAllLoaded = useCallback(() => {
|
|
235
|
-
if (orderedIds.length > 0) selectAll(orderedIds);
|
|
236
|
-
}, [orderedIds, selectAll]);
|
|
237
|
-
|
|
238
|
-
return {
|
|
239
|
-
focusedMessageId,
|
|
240
|
-
setFocusedMessageId,
|
|
241
|
-
keyboardFocusedMessageId,
|
|
242
|
-
focusIndex,
|
|
243
|
-
pendingDomFocusRef,
|
|
244
|
-
cursorMovedByPointerRef,
|
|
245
|
-
selection,
|
|
246
|
-
isMultiSelectMode,
|
|
247
|
-
exitSelection,
|
|
248
|
-
moveFocusToIndex,
|
|
249
|
-
focusNext,
|
|
250
|
-
focusPrevious,
|
|
251
|
-
focusFirst,
|
|
252
|
-
focusLast,
|
|
253
|
-
toggleFocusedSelection,
|
|
254
|
-
extendRangeUp,
|
|
255
|
-
extendRangeDown,
|
|
256
|
-
selectAllLoaded,
|
|
257
|
-
handleRowSelect,
|
|
258
|
-
};
|
|
259
|
-
};
|