@marktiderman/genesis-chat 0.8.0 → 0.10.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 +1 -1
- package/dist/native.d.ts +11 -6
- package/dist/native.js +167 -233
- package/dist/native.js.map +1 -1
- package/dist/web.d.ts +7 -3
- package/dist/web.js +269 -265
- package/dist/web.js.map +1 -1
- package/migrations/0005_genesis_chat_attachment_paths.sql +80 -0
- package/migrations/tests/0005_genesis_chat_attachment_paths.test.sql +111 -0
- package/package.json +13 -9
- package/src/__tests__/web-attachment-view.test.ts +33 -0
- package/src/__tests__/web.test.tsx +160 -3
- package/src/native/__tests__/composer.test.tsx +75 -0
- package/src/native/__tests__/organise-screens.test.tsx +1 -1
- package/src/native/__tests__/thread-screen.test.tsx +104 -2
- package/src/native/composer.tsx +53 -79
- package/src/native/context.tsx +9 -2
- package/src/native/message-bubble.tsx +3 -3
- package/src/native/modules.ts +13 -18
- package/src/native/organise-copy.ts +3 -0
- package/src/native/thread-screen.tsx +81 -205
- package/src/native/use-jump.ts +5 -2
- package/src/web/attachments.tsx +16 -3
- package/src/web/bubble.tsx +4 -2
- package/src/web/composer.tsx +2 -4
- package/src/web/dock.tsx +3 -3
- package/src/web/inbox.tsx +82 -90
- package/src/web/thread.tsx +248 -259
package/src/web/thread.tsx
CHANGED
|
@@ -9,10 +9,9 @@
|
|
|
9
9
|
* their hours a banner says so; search (in the header) jumps to a message,
|
|
10
10
|
* loading earlier ones as needed; links get preview cards.
|
|
11
11
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* builder; until then fix behaviour in both places.
|
|
12
|
+
* On genesis-ui's `ThreadPage`, which owns the scrolling, the list, going to a message and the pill
|
|
13
|
+
* back to the newest. This screen keeps its data wiring (the chat client, realtime, read state) and
|
|
14
|
+
* draws chat's rows and composer in the page's slots.
|
|
16
15
|
*/
|
|
17
16
|
import {
|
|
18
17
|
useCallback,
|
|
@@ -21,11 +20,12 @@ import {
|
|
|
21
20
|
useMemo,
|
|
22
21
|
useRef,
|
|
23
22
|
useState,
|
|
23
|
+
type ComponentProps,
|
|
24
24
|
type ComponentType,
|
|
25
25
|
type DragEvent,
|
|
26
|
-
type
|
|
26
|
+
type Ref,
|
|
27
27
|
} from "react";
|
|
28
|
-
import {
|
|
28
|
+
import { ArrowLeft, ImagePlus, MessageSquare, Pin, Search } from "lucide-react";
|
|
29
29
|
import {
|
|
30
30
|
AlertDialog,
|
|
31
31
|
AlertDialogAction,
|
|
@@ -51,13 +51,14 @@ import {
|
|
|
51
51
|
SheetDescription,
|
|
52
52
|
SheetHeader,
|
|
53
53
|
SheetTitle,
|
|
54
|
-
Skeleton,
|
|
55
|
-
Spinner,
|
|
56
54
|
Textarea,
|
|
55
|
+
ThreadPage,
|
|
57
56
|
UserAvatar,
|
|
58
57
|
cn,
|
|
59
58
|
toast,
|
|
59
|
+
type ThreadPageSlotMap,
|
|
60
60
|
} from "@marktiderman/genesis-ui";
|
|
61
|
+
import type { ThreadMessage } from "@marktiderman/genesis-core/conversation";
|
|
61
62
|
import { renderSlot, type SlotOverride } from "@marktiderman/genesis-core/slots";
|
|
62
63
|
import { ChatError } from "../core/client";
|
|
63
64
|
import { extractLinks } from "../core/extras";
|
|
@@ -70,29 +71,13 @@ import { MessageRow, plainText } from "./bubble";
|
|
|
70
71
|
import { ChatAwayBanner } from "./availability";
|
|
71
72
|
import type { LinkPreviewImages } from "./cards";
|
|
72
73
|
import { ChatErrorBoundary } from "./error-boundary";
|
|
73
|
-
import { ChatComposer, type ChatComposerHandle } from "./composer";
|
|
74
|
+
import { ChatComposer, type ChatComposerHandle, type ChatComposerProps } from "./composer";
|
|
74
75
|
import type { ResolveUser } from "./format";
|
|
75
76
|
import { ChatSearch } from "./search";
|
|
76
77
|
import { ChatTopicBar } from "./topics";
|
|
77
78
|
import { QUICK_REACTIONS, ThreadContext, type ThreadContextValue } from "./thread-context";
|
|
78
79
|
|
|
79
80
|
|
|
80
|
-
/**
|
|
81
|
-
* Centre a message in the thread by scrolling the thread alone. `scrollIntoView` would also scroll
|
|
82
|
-
* every scrollable ancestor (an inbox's other panes, the page), moving things the reader didn't.
|
|
83
|
-
* The same as genesis-ui's ThreadList, which chat's thread moves onto.
|
|
84
|
-
*/
|
|
85
|
-
function centerRow(scroller: HTMLElement, row: HTMLElement, smooth: boolean) {
|
|
86
|
-
const delta = row.getBoundingClientRect().top - scroller.getBoundingClientRect().top - (scroller.clientHeight - row.offsetHeight) / 2;
|
|
87
|
-
const top = Math.max(0, scroller.scrollTop + delta);
|
|
88
|
-
if (smooth && typeof scroller.scrollTo === "function") scroller.scrollTo({ top, behavior: "smooth" });
|
|
89
|
-
else scroller.scrollTop = top;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function prefersReducedMotion(): boolean {
|
|
93
|
-
return typeof window !== "undefined" && typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
81
|
export interface ChatThreadHeaderProps {
|
|
97
82
|
conversationId: string;
|
|
98
83
|
conversation: ChatConversation | null;
|
|
@@ -233,6 +218,113 @@ function DefaultEmpty({ canPost }: ChatThreadEmptyProps) {
|
|
|
233
218
|
return <EmptyState icon={MessageSquare} title="No messages yet" description={canPost ? "Say hello." : undefined} />;
|
|
234
219
|
}
|
|
235
220
|
|
|
221
|
+
/** What chat adds to a ThreadPage row, carried on the row itself: the page hands it on to the slot. */
|
|
222
|
+
interface ChatThreadRow extends ThreadMessage {
|
|
223
|
+
item: ThreadItem;
|
|
224
|
+
startsGroup: boolean;
|
|
225
|
+
/** Heads a new day. */
|
|
226
|
+
day: string | null;
|
|
227
|
+
firstUnread: boolean;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function ChatRowSlot(row: ChatThreadRow & { highlighted?: boolean }) {
|
|
231
|
+
return (
|
|
232
|
+
// ThreadPage spaces and insets its own bubbles; chat's row does both itself.
|
|
233
|
+
<div className="-mx-3 -my-1">
|
|
234
|
+
{row.day ? (
|
|
235
|
+
<div className="sticky top-1 z-10 flex justify-center py-2" role="separator" aria-label={row.day}>
|
|
236
|
+
<span className="rounded-full border bg-background/95 px-3 py-0.5 text-xs font-medium text-muted-foreground shadow-sm">{row.day}</span>
|
|
237
|
+
</div>
|
|
238
|
+
) : null}
|
|
239
|
+
{row.firstUnread ? (
|
|
240
|
+
<div className="my-2 flex items-center gap-2 px-4" role="separator" aria-label="New messages" data-testid="genesis-chat-new-line">
|
|
241
|
+
<span className="h-px flex-1 bg-primary/40" />
|
|
242
|
+
<span className="text-xs font-semibold text-primary">New</span>
|
|
243
|
+
<span className="h-px flex-1 bg-primary/40" />
|
|
244
|
+
</div>
|
|
245
|
+
) : null}
|
|
246
|
+
<ChatErrorBoundary
|
|
247
|
+
fallback={
|
|
248
|
+
<p className="px-4 py-1 text-xs italic text-muted-foreground" role="note" data-testid="genesis-chat-row-error">
|
|
249
|
+
This message couldn't be shown.
|
|
250
|
+
</p>
|
|
251
|
+
}
|
|
252
|
+
>
|
|
253
|
+
<MessageRow item={row.item} startsGroup={row.startsGroup} highlighted={Boolean(row.highlighted)} />
|
|
254
|
+
</ChatErrorBoundary>
|
|
255
|
+
</div>
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
interface ChatComposerSlotExtras {
|
|
260
|
+
chatComposer: ChatComposerProps;
|
|
261
|
+
composerRef: Ref<ChatComposerHandle>;
|
|
262
|
+
/** "Sam is typing…" */
|
|
263
|
+
live: string | null;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function ChatComposerSlot({ onSend, chatComposer, composerRef, live }: ThreadPageSlotMap["composer"] & ChatComposerSlotExtras) {
|
|
267
|
+
return (
|
|
268
|
+
<>
|
|
269
|
+
<div className="h-5 px-4 text-xs italic text-muted-foreground" data-testid="genesis-chat-activity">
|
|
270
|
+
{live ? (
|
|
271
|
+
<span className="inline-flex items-center gap-1.5">
|
|
272
|
+
<span className="inline-flex gap-0.5" aria-hidden>
|
|
273
|
+
<span className="size-1 animate-bounce rounded-full bg-muted-foreground [animation-delay:-0.3s]" />
|
|
274
|
+
<span className="size-1 animate-bounce rounded-full bg-muted-foreground [animation-delay:-0.15s]" />
|
|
275
|
+
<span className="size-1 animate-bounce rounded-full bg-muted-foreground" />
|
|
276
|
+
</span>
|
|
277
|
+
{live}
|
|
278
|
+
</span>
|
|
279
|
+
) : null}
|
|
280
|
+
</div>
|
|
281
|
+
{/* ThreadPage's onSend goes to the newest message: what you send is what you look at next. */}
|
|
282
|
+
<ChatComposer {...chatComposer} ref={composerRef} onSent={onSend} />
|
|
283
|
+
</>
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
interface ChatEmptySlotExtras {
|
|
288
|
+
override: SlotOverride<ChatThreadEmptyProps> | undefined;
|
|
289
|
+
conversationId: string;
|
|
290
|
+
canPost: boolean;
|
|
291
|
+
failed: string | null;
|
|
292
|
+
onRetry: () => void;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function ChatEmptySlot({ override, conversationId, canPost, failed, onRetry }: ThreadPageSlotMap["empty"] & ChatEmptySlotExtras) {
|
|
296
|
+
if (failed) {
|
|
297
|
+
return (
|
|
298
|
+
<EmptyState
|
|
299
|
+
icon={MessageSquare}
|
|
300
|
+
title="Couldn't load this conversation"
|
|
301
|
+
description={failed}
|
|
302
|
+
action={
|
|
303
|
+
<Button type="button" variant="outline" onClick={onRetry}>
|
|
304
|
+
Try again
|
|
305
|
+
</Button>
|
|
306
|
+
}
|
|
307
|
+
/>
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
return <>{renderSlot(DefaultEmpty, override, { conversationId, canPost })}</>;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// Module scope: a new slot each render would remount every row. The extras chat's slots take come
|
|
314
|
+
// through the row and `slotProps`, which the page's own types don't know.
|
|
315
|
+
const THREAD_SLOTS = {
|
|
316
|
+
message: ChatRowSlot,
|
|
317
|
+
event: ChatRowSlot,
|
|
318
|
+
// Chat's row draws it, under the day heading.
|
|
319
|
+
unreadDivider: null,
|
|
320
|
+
composer: ChatComposerSlot,
|
|
321
|
+
empty: ChatEmptySlot,
|
|
322
|
+
} as unknown as NonNullable<ComponentProps<typeof ThreadPage>["slots"]>;
|
|
323
|
+
|
|
324
|
+
// Chat's composer keeps its own text and sends for itself.
|
|
325
|
+
const THREAD_COMPOSER = { value: "", onChangeText: () => undefined, onSend: () => undefined };
|
|
326
|
+
const GONE = "That message isn't here any more";
|
|
327
|
+
|
|
236
328
|
export function ChatThread({
|
|
237
329
|
conversationId,
|
|
238
330
|
resolveUser,
|
|
@@ -276,12 +368,9 @@ export function ChatThread({
|
|
|
276
368
|
|
|
277
369
|
const [replyTo, setReplyTo] = useState<ChatMessage | null>(null);
|
|
278
370
|
const [editingId, setEditingId] = useState<string | null>(null);
|
|
279
|
-
const [highlightedId, setHighlightedId] = useState<string | null>(null);
|
|
280
371
|
const [confirmDelete, setConfirmDelete] = useState<ChatMessage | null>(null);
|
|
281
372
|
const [reporting, setReporting] = useState<ChatMessage | null>(null);
|
|
282
373
|
const [dragging, setDragging] = useState(false);
|
|
283
|
-
const [newCount, setNewCount] = useState(0);
|
|
284
|
-
const [announcement, setAnnouncement] = useState("");
|
|
285
374
|
const composer = useRef<ChatComposerHandle | null>(null);
|
|
286
375
|
useEffect(() => {
|
|
287
376
|
if (!autoFocus) return;
|
|
@@ -294,8 +383,8 @@ export function ChatThread({
|
|
|
294
383
|
useEffect(() => {
|
|
295
384
|
setReplyTo(null);
|
|
296
385
|
setEditingId(null);
|
|
297
|
-
setNewCount(0);
|
|
298
386
|
setChosenTopic(null);
|
|
387
|
+
setJump(null);
|
|
299
388
|
}, [conversationId]);
|
|
300
389
|
|
|
301
390
|
const topics = topicsState.topics;
|
|
@@ -345,97 +434,69 @@ export function ChatThread({
|
|
|
345
434
|
return awaySummary(people) ? people : null;
|
|
346
435
|
}, [availability, otherUserId, members, me, avail, resolveUser]);
|
|
347
436
|
|
|
348
|
-
//
|
|
349
|
-
|
|
350
|
-
const
|
|
351
|
-
const
|
|
352
|
-
const
|
|
353
|
-
const
|
|
354
|
-
const anchor = useRef<{ height: number; top: number } | null>(null);
|
|
355
|
-
|
|
356
|
-
const scrollToBottom = useCallback((smooth = false) => {
|
|
357
|
-
const el = scroller.current;
|
|
358
|
-
if (!el) return;
|
|
359
|
-
if (smooth && typeof el.scrollTo === "function") el.scrollTo({ top: el.scrollHeight, behavior: "smooth" });
|
|
360
|
-
else el.scrollTop = el.scrollHeight;
|
|
361
|
-
atBottom.current = true;
|
|
362
|
-
setNewCount(0);
|
|
363
|
-
}, []);
|
|
364
|
-
|
|
437
|
+
// Someone who just posted has stopped typing: their last signal no longer counts, and the line
|
|
438
|
+
// above the composer is worked out again now.
|
|
439
|
+
const lastItem = items[items.length - 1];
|
|
440
|
+
const lastKey = lastItem ? itemKey(lastItem) : null;
|
|
441
|
+
const seenLast = useRef(lastKey);
|
|
442
|
+
const [, noteArrival] = useState(0);
|
|
365
443
|
useLayoutEffect(() => {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
const prev = edges.current;
|
|
372
|
-
edges.current = { first, last };
|
|
373
|
-
|
|
374
|
-
if (initialised.current !== conversationId) {
|
|
375
|
-
initialised.current = conversationId;
|
|
376
|
-
const unread = state.firstUnreadId ? document.getElementById(`genesis-chat-msg-${state.firstUnreadId}`) : null;
|
|
377
|
-
if (unread) centerRow(el, unread, false);
|
|
378
|
-
else scrollToBottom();
|
|
379
|
-
return;
|
|
380
|
-
}
|
|
381
|
-
if (anchor.current && first !== prev.first) {
|
|
382
|
-
el.scrollTop = el.scrollHeight - anchor.current.height + anchor.current.top;
|
|
383
|
-
anchor.current = null;
|
|
384
|
-
}
|
|
385
|
-
if (last !== prev.last) {
|
|
386
|
-
const mine = itemSender(lastItem) === me;
|
|
387
|
-
if (atBottom.current || mine) scrollToBottom();
|
|
388
|
-
else setNewCount((n) => n + 1);
|
|
389
|
-
if (!mine && lastItem.type === "message" && lastItem.message.kind !== "system") {
|
|
390
|
-
const m = lastItem.message;
|
|
391
|
-
if (m.senderId) arrived.current.set(m.senderId, Date.now());
|
|
392
|
-
setAnnouncement(`${m.senderId ? resolveUser(m.senderId).name : "Someone"}: ${previewText(m.body, m.attachments[0]?.kind ?? null, Boolean(m.deletedAt))}`);
|
|
393
|
-
}
|
|
444
|
+
if (lastKey === seenLast.current) return;
|
|
445
|
+
seenLast.current = lastKey;
|
|
446
|
+
if (lastItem?.type === "message" && lastItem.message.kind !== "system" && lastItem.message.senderId && lastItem.message.senderId !== me) {
|
|
447
|
+
arrived.current.set(lastItem.message.senderId, Date.now());
|
|
448
|
+
noteArrival((n) => n + 1);
|
|
394
449
|
}
|
|
395
|
-
}, [
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
450
|
+
}, [lastKey, lastItem, me]);
|
|
451
|
+
|
|
452
|
+
// ---- rows -----------------------------------------------------------------
|
|
453
|
+
const firstUnreadId = state.firstUnreadId;
|
|
454
|
+
const rows = useMemo<ChatThreadRow[]>(
|
|
455
|
+
() =>
|
|
456
|
+
items.map((item, i) => {
|
|
457
|
+
const message = item.type === "message" ? item.message : null;
|
|
458
|
+
const sender = itemSender(item);
|
|
459
|
+
return {
|
|
460
|
+
// The client's id, so a message that lands keeps its row; a jump names the server's.
|
|
461
|
+
id: itemKey(item),
|
|
462
|
+
...(message ? { jumpId: message.id } : {}),
|
|
463
|
+
...(message?.kind === "system" ? { kind: "event" as const } : {}),
|
|
464
|
+
// Plain text: only the list's announcement reads it, and the slot draws the row.
|
|
465
|
+
body: message ? previewText(message.body, message.attachments[0]?.kind ?? null, Boolean(message.deletedAt)) : "",
|
|
466
|
+
mine: sender === me,
|
|
467
|
+
...(sender && sender !== me ? { authorName: resolveUser(sender).name } : {}),
|
|
468
|
+
timestamp: itemTime(item),
|
|
469
|
+
item,
|
|
470
|
+
startsGroup: startsGroup(items, i),
|
|
471
|
+
day: startsDay(items, i) ? dayLabel(itemTime(item)) : null,
|
|
472
|
+
firstUnread: message !== null && message.id === firstUnreadId,
|
|
473
|
+
};
|
|
474
|
+
}),
|
|
475
|
+
[items, me, resolveUser, firstUnreadId],
|
|
476
|
+
);
|
|
477
|
+
const firstUnreadRow = rows.find((r) => r.firstUnread)?.id ?? null;
|
|
408
478
|
|
|
409
479
|
const { hasEarlier, loadingEarlier, loadEarlier } = state;
|
|
410
480
|
const loadOlder = useCallback(() => {
|
|
411
|
-
const el = scroller.current;
|
|
412
|
-
if (!el || !hasEarlier || loadingEarlier) return;
|
|
413
|
-
anchor.current = { height: el.scrollHeight, top: el.scrollTop };
|
|
414
481
|
void loadEarlier().catch((e) => toast.error(errorText(e)));
|
|
415
|
-
}, [
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
if (el.scrollTop < 200) loadOlder();
|
|
424
|
-
};
|
|
425
|
-
|
|
426
|
-
const jumpTo = useCallback((id: string) => {
|
|
427
|
-
const el = document.getElementById(`genesis-chat-msg-${id}`);
|
|
428
|
-
if (!el) {
|
|
429
|
-
toast.info("That message is further up", { description: "Scroll up to load it." });
|
|
430
|
-
return;
|
|
431
|
-
}
|
|
432
|
-
if (scroller.current) centerRow(scroller.current, el, !prefersReducedMotion());
|
|
433
|
-
setHighlightedId(id);
|
|
434
|
-
setTimeout(() => setHighlightedId((h) => (h === id ? null : h)), 1600);
|
|
482
|
+
}, [loadEarlier]);
|
|
483
|
+
|
|
484
|
+
// ---- going to a message ---------------------------------------------------
|
|
485
|
+
// Cleared first, so going to the same message again goes again.
|
|
486
|
+
const [jump, setJump] = useState<{ messageId: string; seq?: number } | null>(null);
|
|
487
|
+
const goTo = useCallback((target: { messageId: string; seq?: number }) => {
|
|
488
|
+
setJump(null);
|
|
489
|
+
requestAnimationFrame(() => setJump(target));
|
|
435
490
|
}, []);
|
|
436
491
|
|
|
437
|
-
|
|
438
|
-
|
|
492
|
+
const jumpTo = useCallback(
|
|
493
|
+
(id: string) => {
|
|
494
|
+
if (byId.has(id)) goTo({ messageId: id });
|
|
495
|
+
else toast.info("That message is further up", { description: "Scroll up to load it." });
|
|
496
|
+
},
|
|
497
|
+
[byId, goTo],
|
|
498
|
+
);
|
|
499
|
+
|
|
439
500
|
/**
|
|
440
501
|
* A reply in a thread isn't on this timeline: open its thread (onOpenThread, scrolled to the
|
|
441
502
|
* reply) and come here to the thread's first message; without onOpenThread, just to that.
|
|
@@ -444,50 +505,48 @@ export function ChatThread({
|
|
|
444
505
|
(target: { messageId: string; seq?: number; threadRootId?: string | null }) => {
|
|
445
506
|
const root = target.threadRootId;
|
|
446
507
|
if (!root || root === threadRootId || root === target.messageId) {
|
|
447
|
-
|
|
508
|
+
goTo({ messageId: target.messageId, ...(target.seq != null ? { seq: target.seq } : {}) });
|
|
448
509
|
return;
|
|
449
510
|
}
|
|
450
511
|
void chatClient.message(root).then(
|
|
451
512
|
(m) => {
|
|
452
513
|
if (!m) {
|
|
453
|
-
toast.info(
|
|
514
|
+
toast.info(GONE);
|
|
454
515
|
return;
|
|
455
516
|
}
|
|
456
517
|
onOpenThread?.(m, target.seq != null ? { jumpTo: { messageId: target.messageId, seq: target.seq } } : undefined);
|
|
457
|
-
|
|
518
|
+
goTo({ messageId: m.id, seq: m.seq });
|
|
458
519
|
},
|
|
459
520
|
(e) => toast.error(errorText(e)),
|
|
460
521
|
);
|
|
461
522
|
},
|
|
462
|
-
[chatClient, onOpenThread, threadRootId],
|
|
523
|
+
[chatClient, goTo, onOpenThread, threadRootId],
|
|
463
524
|
);
|
|
464
|
-
const [pendingJump, setPendingJump] = useState<Jump | null>(jumpToProp && !jumpToProp.threadRootId ? { ...jumpToProp, phase: "new" } : null);
|
|
465
525
|
useEffect(() => {
|
|
466
526
|
if (jumpToProp) reach(jumpToProp);
|
|
467
527
|
}, [jumpToProp?.messageId, jumpToProp?.seq]); // eslint-disable-line react-hooks/exhaustive-deps
|
|
528
|
+
|
|
529
|
+
// Messages, not rows: a saved outbox row is drawn even when nothing could be read.
|
|
530
|
+
const firstPageFailed = Boolean(state.error) && messages.length === 0;
|
|
468
531
|
const { loadUntil } = state;
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
toast.error(errorText(e));
|
|
488
|
-
settle();
|
|
489
|
-
});
|
|
490
|
-
}, [pendingJump, byId, state.isLoading, loadUntil, jumpTo]);
|
|
532
|
+
// A load that failed has said so: the message isn't known to be gone.
|
|
533
|
+
const jumpLoadFailed = useRef(false);
|
|
534
|
+
const onLoadUntil = useCallback(
|
|
535
|
+
(id: string) => {
|
|
536
|
+
jumpLoadFailed.current = false;
|
|
537
|
+
if (jump?.messageId !== id || jump.seq == null) return Promise.resolve(false);
|
|
538
|
+
return loadUntil(jump.seq).catch((e) => {
|
|
539
|
+
jumpLoadFailed.current = true;
|
|
540
|
+
toast.error(errorText(e));
|
|
541
|
+
return false;
|
|
542
|
+
});
|
|
543
|
+
},
|
|
544
|
+
[jump, loadUntil],
|
|
545
|
+
);
|
|
546
|
+
const onJumpFailed = useCallback(() => {
|
|
547
|
+
if (jumpLoadFailed.current) jumpLoadFailed.current = false;
|
|
548
|
+
else toast.info(GONE);
|
|
549
|
+
}, []);
|
|
491
550
|
|
|
492
551
|
const openHit = useCallback(
|
|
493
552
|
(hit: ChatSearchHit) => {
|
|
@@ -526,7 +585,7 @@ export function ChatThread({
|
|
|
526
585
|
emojibaseUrl,
|
|
527
586
|
linkPreviewImages,
|
|
528
587
|
linkPreviewProxyUrl,
|
|
529
|
-
highlightedId,
|
|
588
|
+
highlightedId: null,
|
|
530
589
|
editingId,
|
|
531
590
|
reply: (m) => {
|
|
532
591
|
setReplyTo(m);
|
|
@@ -581,76 +640,6 @@ export function ChatThread({
|
|
|
581
640
|
|
|
582
641
|
// ---- render ---------------------------------------------------------------
|
|
583
642
|
const pinnedMessage = pinIds.length ? byId.get(pinIds[pinIds.length - 1]!) : undefined;
|
|
584
|
-
let body: ReactNode;
|
|
585
|
-
if (state.isLoading) {
|
|
586
|
-
body = (
|
|
587
|
-
<div className="flex flex-col gap-3 p-4" aria-busy aria-label="Loading messages" data-testid="genesis-chat-thread-loading">
|
|
588
|
-
{[40, 60, 30, 50].map((w, i) => (
|
|
589
|
-
<Skeleton key={i} className={cn("h-9 rounded-2xl", i % 2 ? "self-end" : "self-start")} style={{ width: `${w}%` }} />
|
|
590
|
-
))}
|
|
591
|
-
</div>
|
|
592
|
-
);
|
|
593
|
-
} else if (state.error && items.length === 0) {
|
|
594
|
-
body = (
|
|
595
|
-
<EmptyState
|
|
596
|
-
icon={MessageSquare}
|
|
597
|
-
title="Couldn't load this conversation"
|
|
598
|
-
description={errorText(state.error)}
|
|
599
|
-
action={
|
|
600
|
-
<Button type="button" variant="outline" onClick={() => void state.refetch()}>
|
|
601
|
-
Try again
|
|
602
|
-
</Button>
|
|
603
|
-
}
|
|
604
|
-
/>
|
|
605
|
-
);
|
|
606
|
-
} else if (items.length === 0) {
|
|
607
|
-
body = renderSlot(DefaultEmpty, slots?.empty, { conversationId, canPost });
|
|
608
|
-
} else {
|
|
609
|
-
body = (
|
|
610
|
-
<div ref={content} className="flex min-h-full flex-col justify-end pb-2 pt-1">
|
|
611
|
-
{hasEarlier ? (
|
|
612
|
-
<div className="flex justify-center py-2">
|
|
613
|
-
{loadingEarlier ? (
|
|
614
|
-
<Spinner size="sm" aria-label="Loading earlier messages" />
|
|
615
|
-
) : (
|
|
616
|
-
<Button type="button" variant="ghost" size="sm" className="h-7 text-xs text-muted-foreground" onClick={loadOlder}>
|
|
617
|
-
Load earlier messages
|
|
618
|
-
</Button>
|
|
619
|
-
)}
|
|
620
|
-
</div>
|
|
621
|
-
) : null}
|
|
622
|
-
{items.map((item, i) => {
|
|
623
|
-
const id = item.type === "message" ? item.message.id : null;
|
|
624
|
-
return (
|
|
625
|
-
<div key={itemKey(item)}>
|
|
626
|
-
{startsDay(items, i) ? (
|
|
627
|
-
<div className="sticky top-1 z-10 flex justify-center py-2" role="separator" aria-label={dayLabel(itemTime(item))}>
|
|
628
|
-
<span className="rounded-full border bg-background/95 px-3 py-0.5 text-xs font-medium text-muted-foreground shadow-sm">{dayLabel(itemTime(item))}</span>
|
|
629
|
-
</div>
|
|
630
|
-
) : null}
|
|
631
|
-
{id && id === state.firstUnreadId ? (
|
|
632
|
-
<div className="my-2 flex items-center gap-2 px-4" role="separator" aria-label="New messages" data-testid="genesis-chat-new-line">
|
|
633
|
-
<span className="h-px flex-1 bg-primary/40" />
|
|
634
|
-
<span className="text-xs font-semibold text-primary">New</span>
|
|
635
|
-
<span className="h-px flex-1 bg-primary/40" />
|
|
636
|
-
</div>
|
|
637
|
-
) : null}
|
|
638
|
-
<ChatErrorBoundary
|
|
639
|
-
fallback={
|
|
640
|
-
<p className="px-4 py-1 text-xs italic text-muted-foreground" role="note" data-testid="genesis-chat-row-error">
|
|
641
|
-
This message couldn't be shown.
|
|
642
|
-
</p>
|
|
643
|
-
}
|
|
644
|
-
>
|
|
645
|
-
<MessageRow item={item} startsGroup={startsGroup(items, i)} />
|
|
646
|
-
</ChatErrorBoundary>
|
|
647
|
-
</div>
|
|
648
|
-
);
|
|
649
|
-
})}
|
|
650
|
-
</div>
|
|
651
|
-
);
|
|
652
|
-
}
|
|
653
|
-
|
|
654
643
|
return (
|
|
655
644
|
<ThreadContext.Provider value={ctx}>
|
|
656
645
|
<section
|
|
@@ -697,21 +686,55 @@ export function ChatThread({
|
|
|
697
686
|
) : null}
|
|
698
687
|
|
|
699
688
|
<div className="relative min-h-0 flex-1">
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
{
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
689
|
+
<ThreadPage
|
|
690
|
+
testID="genesis-chat-thread-page"
|
|
691
|
+
conversationId={conversationId}
|
|
692
|
+
messages={rows}
|
|
693
|
+
composer={THREAD_COMPOSER}
|
|
694
|
+
hasEarlier={hasEarlier}
|
|
695
|
+
onLoadEarlier={loadOlder}
|
|
696
|
+
loadingEarlier={loadingEarlier}
|
|
697
|
+
firstUnreadId={firstUnreadRow}
|
|
698
|
+
// Not while the first page has failed: there's nothing to look in, the empty slot says so
|
|
699
|
+
// with Try again, and the jump goes ahead when the rows come.
|
|
700
|
+
jumpTo={firstPageFailed ? null : (jump?.messageId ?? null)}
|
|
701
|
+
onLoadUntil={onLoadUntil}
|
|
702
|
+
onJumpFailed={onJumpFailed}
|
|
703
|
+
loading={state.isLoading}
|
|
704
|
+
slots={THREAD_SLOTS}
|
|
705
|
+
slotProps={
|
|
706
|
+
{
|
|
707
|
+
composer: {
|
|
708
|
+
live,
|
|
709
|
+
composerRef: composer,
|
|
710
|
+
chatComposer: {
|
|
711
|
+
conversation: state,
|
|
712
|
+
replyTo,
|
|
713
|
+
onCancelReply: () => setReplyTo(null),
|
|
714
|
+
resolveUser,
|
|
715
|
+
placeholder: currentTopic ? `Message #${currentTopic.title}` : placeholder,
|
|
716
|
+
topic: topic ?? null,
|
|
717
|
+
disabledReason: topicClosed ? "This topic is closed" : null,
|
|
718
|
+
scheduling,
|
|
719
|
+
emojibaseUrl,
|
|
720
|
+
accessory: renderSlot(EmptyAccessory, slots?.composerAccessory, {
|
|
721
|
+
conversationId,
|
|
722
|
+
insertText: (t) => composer.current?.insertText(t),
|
|
723
|
+
addFiles: (f) => composer.current?.addFiles(f),
|
|
724
|
+
}),
|
|
725
|
+
},
|
|
726
|
+
},
|
|
727
|
+
empty: {
|
|
728
|
+
override: slots?.empty,
|
|
729
|
+
conversationId,
|
|
730
|
+
canPost,
|
|
731
|
+
failed: state.error ? errorText(state.error) : null,
|
|
732
|
+
onRetry: () => void state.refetch(),
|
|
733
|
+
},
|
|
734
|
+
} as NonNullable<ComponentProps<typeof ThreadPage>["slotProps"]>
|
|
735
|
+
}
|
|
736
|
+
className="h-full"
|
|
737
|
+
/>
|
|
715
738
|
{dragging ? (
|
|
716
739
|
<div className="pointer-events-none absolute inset-2 flex flex-col items-center justify-center gap-2 rounded-xl border-2 border-dashed border-primary bg-background/90 text-sm font-medium text-primary">
|
|
717
740
|
<ImagePlus className="size-6" aria-hidden />
|
|
@@ -719,40 +742,6 @@ export function ChatThread({
|
|
|
719
742
|
</div>
|
|
720
743
|
) : null}
|
|
721
744
|
</div>
|
|
722
|
-
|
|
723
|
-
<div className="h-5 px-4 text-xs italic text-muted-foreground" data-testid="genesis-chat-activity">
|
|
724
|
-
{live ? (
|
|
725
|
-
<span className="inline-flex items-center gap-1.5">
|
|
726
|
-
<span className="inline-flex gap-0.5" aria-hidden>
|
|
727
|
-
<span className="size-1 animate-bounce rounded-full bg-muted-foreground [animation-delay:-0.3s]" />
|
|
728
|
-
<span className="size-1 animate-bounce rounded-full bg-muted-foreground [animation-delay:-0.15s]" />
|
|
729
|
-
<span className="size-1 animate-bounce rounded-full bg-muted-foreground" />
|
|
730
|
-
</span>
|
|
731
|
-
{live}
|
|
732
|
-
</span>
|
|
733
|
-
) : null}
|
|
734
|
-
</div>
|
|
735
|
-
<div className="sr-only" aria-live="polite" aria-atomic="true" data-testid="genesis-chat-announcer">
|
|
736
|
-
{announcement}
|
|
737
|
-
</div>
|
|
738
|
-
|
|
739
|
-
<ChatComposer
|
|
740
|
-
ref={composer}
|
|
741
|
-
conversation={state}
|
|
742
|
-
replyTo={replyTo}
|
|
743
|
-
onCancelReply={() => setReplyTo(null)}
|
|
744
|
-
resolveUser={resolveUser}
|
|
745
|
-
placeholder={currentTopic ? `Message #${currentTopic.title}` : placeholder}
|
|
746
|
-
topic={topic ?? null}
|
|
747
|
-
disabledReason={topicClosed ? "This topic is closed" : null}
|
|
748
|
-
scheduling={scheduling}
|
|
749
|
-
emojibaseUrl={emojibaseUrl}
|
|
750
|
-
accessory={renderSlot(EmptyAccessory, slots?.composerAccessory, {
|
|
751
|
-
conversationId,
|
|
752
|
-
insertText: (t) => composer.current?.insertText(t),
|
|
753
|
-
addFiles: (f) => composer.current?.addFiles(f),
|
|
754
|
-
})}
|
|
755
|
-
/>
|
|
756
745
|
</section>
|
|
757
746
|
|
|
758
747
|
{search && !threadRootId ? (
|