@marktiderman/genesis-chat 0.11.4 → 0.12.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 +116 -3
- package/dist/{chunk-ZQFREERR.js → chunk-2HNQWDLF.js} +3 -3
- package/dist/{chunk-ZQFREERR.js.map → chunk-2HNQWDLF.js.map} +1 -1
- package/dist/{chunk-O2QSBRSH.js → chunk-3HQN2FZ5.js} +3 -3
- package/dist/{chunk-O2QSBRSH.js.map → chunk-3HQN2FZ5.js.map} +1 -1
- package/dist/{chunk-D4RPOYB7.js → chunk-K5GYNHNE.js} +61 -11
- package/dist/chunk-K5GYNHNE.js.map +1 -0
- package/dist/{chunk-5XYDVUPS.js → chunk-VBIYDXL2.js} +9 -3
- package/dist/chunk-VBIYDXL2.js.map +1 -0
- package/dist/{client-DP7rhJ1U.d.ts → client-DJs3ltZc.d.ts} +15 -0
- package/dist/index.d.ts +4 -4
- package/dist/index.js +3 -3
- package/dist/native.d.ts +339 -137
- package/dist/native.js +668 -192
- package/dist/native.js.map +1 -1
- package/dist/{outbox-C4LnqpKT.d.ts → outbox-YFTbGIAl.d.ts} +1 -1
- package/dist/push.d.ts +2 -2
- package/dist/react.d.ts +13 -5
- package/dist/react.js +3 -3
- package/dist/server.d.ts +1 -1
- package/dist/server.js +1 -1
- package/dist/{thread-CSzdqNR_.d.ts → thread-CgHP00Jw.d.ts} +1 -1
- package/dist/web.d.ts +3 -3
- package/dist/web.js +5 -5
- package/migrations/0006_genesis_chat_read_receipts.sql +110 -0
- package/migrations/tests/0006_genesis_chat_read_receipts.test.sql +131 -0
- package/package.json +7 -3
- package/src/core/client.ts +9 -0
- package/src/core/types.ts +7 -0
- package/src/native/__tests__/bubble-native-feel.test.tsx +153 -0
- package/src/native/__tests__/composer-attach-menu.test.tsx +47 -0
- package/src/native/__tests__/media-cache-key.test.tsx +21 -0
- package/src/native/__tests__/organise-screens.test.tsx +2 -0
- package/src/native/__tests__/receipts.test.ts +95 -0
- package/src/native/__tests__/review-lows.test.tsx +80 -0
- package/src/native/__tests__/sheets-fit.test.tsx +45 -0
- package/src/native/__tests__/stable-hooks.test.tsx +68 -0
- package/src/native/__tests__/thread-screen-audit.test.tsx +248 -0
- package/src/native/__tests__/thread-screen.test.tsx +111 -0
- package/src/native/__tests__/thread-title.test.tsx +59 -0
- package/src/native/composer.tsx +61 -26
- package/src/native/copy.ts +40 -0
- package/src/native/emoji-picker.tsx +1 -1
- package/src/native/index.ts +6 -1
- package/src/native/media.tsx +7 -1
- package/src/native/message-actions-sheet.tsx +1 -1
- package/src/native/message-bubble.tsx +171 -19
- package/src/native/modules.ts +3 -1
- package/src/native/receipts.ts +102 -0
- package/src/native/schedule-sheet.tsx +8 -3
- package/src/native/stack-options.ts +35 -3
- package/src/native/thread-screen.tsx +243 -17
- package/src/native/thread-times.tsx +89 -0
- package/src/native/thread-title.tsx +111 -0
- package/src/native/typing-bubble.tsx +85 -0
- package/src/react/index.tsx +74 -7
- package/dist/chunk-5XYDVUPS.js.map +0 -1
- package/dist/chunk-D4RPOYB7.js.map +0 -1
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
* the newest message, following new ones while you are at the bottom, going
|
|
7
7
|
* to the bottom when you send, and keeping its place while earlier ones load
|
|
8
8
|
* at the top; day separators, the "New messages" line,
|
|
9
|
-
* grouped bubbles with names and avatars in groups,
|
|
10
|
-
*
|
|
9
|
+
* grouped bubbles with names and avatars in groups, the status of the last
|
|
10
|
+
* message you sent ("Delivered", "Read 9:41", "Read by 2", with who read it),
|
|
11
|
+
* who is typing (iMessage's bubble of dots) or recording, retry and delete on a send that failed, the long-press sheet and
|
|
11
12
|
* swipe to reply, a message's photos in the full-screen viewer, and the composer above
|
|
12
13
|
* the keyboard (or, when your role couldn't be read, Retry). Render it as the screen's only content, under the
|
|
13
14
|
* navigator's header with `useChatThreadStackOptions` (an opaque bar).
|
|
@@ -19,11 +20,17 @@
|
|
|
19
20
|
* closed one takes no posts from members), the away line under the header, scheduled send
|
|
20
21
|
* (touch and hold Send) with the thread's scheduled messages over the composer, `jumpTo` a
|
|
21
22
|
* message (a search result), and moderation's markers.
|
|
23
|
+
*
|
|
24
|
+
* It opens at the first unread when the "New" line would be above the screen, says when it is
|
|
25
|
+
* waiting for the network or reconnecting, shows an error row with Retry when nothing could be
|
|
26
|
+
* read, and a "↓ 3 new messages" pill when messages come in below where you are. Drag the thread
|
|
27
|
+
* left for every message's time. An app's own strip goes in `header`, its tools over the composer
|
|
28
|
+
* in `accessory`; `active={false}` keeps a thread mounted off screen without marking it read.
|
|
22
29
|
*/
|
|
23
|
-
import { useCallback, useEffect, useMemo, useRef, useState, type ComponentType } from "react";
|
|
24
|
-
import { KeyboardAvoidingView, Platform, View } from "react-native";
|
|
30
|
+
import { useCallback, useEffect, useMemo, useRef, useState, type ComponentType, type ReactNode } from "react";
|
|
31
|
+
import { KeyboardAvoidingView, Platform, Pressable, View, useWindowDimensions } from "react-native";
|
|
25
32
|
import { SafeAreaView } from "react-native-safe-area-context";
|
|
26
|
-
import { NativeEmptyState, NativeText, NativeThreadList, useGenesisModules, useToast, type NativeThreadListHandle, type ThreadRowInfo } from "@marktiderman/genesis-ui-native/components";
|
|
33
|
+
import { NativeAvatar, NativeButton, NativeEmptyState, NativeIcon, NativeListItem, NativeSheet, NativeSheetScrollView, NativeText, NativeThreadList, useGenesisModules, useToast, type NativeThreadListHandle, type ThreadRowInfo } from "@marktiderman/genesis-ui-native/components";
|
|
27
34
|
import { useReducedMotion, useThemeRoles } from "@marktiderman/genesis-ui-native/hooks";
|
|
28
35
|
import { activityLine } from "../core/thread";
|
|
29
36
|
import type { ChatMessage, PendingFile } from "../core/types";
|
|
@@ -42,7 +49,9 @@ import type { JumpTarget } from "./use-jump";
|
|
|
42
49
|
import type { MessageActionId } from "./actions";
|
|
43
50
|
import { ChatComposer } from "./composer";
|
|
44
51
|
import { composerDisabledReason, filesFromAssets, replyTargetFor } from "./composer-state";
|
|
45
|
-
import { ChatPeopleScope, useChatNative } from "./context";
|
|
52
|
+
import { ChatPeopleScope, initials, useChatNative } from "./context";
|
|
53
|
+
import { lastSentReceipt, receiptText, type ChatReceiptsMode } from "./receipts";
|
|
54
|
+
import { ChatTypingBubble } from "./typing-bubble";
|
|
46
55
|
import { useChatComposer } from "./use-composer";
|
|
47
56
|
import { ChatAccessError } from "./access-error";
|
|
48
57
|
import { attachmentPaths } from "./media";
|
|
@@ -52,6 +61,10 @@ import { displayBody, MessageBubble } from "./message-bubble";
|
|
|
52
61
|
import { haptic, type KeyboardAvoidingLike } from "./modules";
|
|
53
62
|
import { pauseChatPlayback } from "./voice-player";
|
|
54
63
|
import { buildThreadRows, type ThreadRow } from "./rows";
|
|
64
|
+
import { ChatTimesProvider, useTimeReveal } from "./thread-times";
|
|
65
|
+
import { isOnline } from "./auto-retry";
|
|
66
|
+
import type { ChatComposerHandle } from "./use-composer";
|
|
67
|
+
import type { NetInfoLike } from "./modules";
|
|
55
68
|
|
|
56
69
|
/** How long an upload minted for a thrown-away take is kept for the next one. */
|
|
57
70
|
const PREPARED_UPLOAD_MS = 20 * 60_000;
|
|
@@ -92,9 +105,67 @@ export interface ChatThreadScreenProps {
|
|
|
92
105
|
* mounted. Default true.
|
|
93
106
|
*/
|
|
94
107
|
focused?: boolean;
|
|
108
|
+
/**
|
|
109
|
+
* Message status under the last message you sent: "Sending…", "Sent", "Delivered", then "Read
|
|
110
|
+
* 9:41" in a direct conversation or "Read by 2" (a button: who has read it) in a group. "hide"
|
|
111
|
+
* leaves it out. Default "show".
|
|
112
|
+
*/
|
|
113
|
+
receipts?: ChatReceiptsMode;
|
|
114
|
+
/**
|
|
115
|
+
* Whether this thread is the one being read. False keeps it mounted (a tab, a split view's other
|
|
116
|
+
* pane) without marking it read, and pauses what it plays, as `focused` does; turning true marks
|
|
117
|
+
* it read again. Default true.
|
|
118
|
+
*/
|
|
119
|
+
active?: boolean;
|
|
120
|
+
/** Above the messages, under the topic bar and away line: an app's own context strip. */
|
|
121
|
+
header?: ReactNode;
|
|
122
|
+
/**
|
|
123
|
+
* Over the composer: an app's own tools for this thread (quick replies, a template picker).
|
|
124
|
+
* Given the composer, so a tool can write into the draft, and whether you can post.
|
|
125
|
+
*/
|
|
126
|
+
accessory?: (context: ChatThreadAccessoryContext) => ReactNode;
|
|
127
|
+
/** The longest video message, in ms. Default: your role's recording limit (`recordingLimitMs`). */
|
|
128
|
+
videoLimitMs?: number;
|
|
95
129
|
testID?: string;
|
|
96
130
|
}
|
|
97
131
|
|
|
132
|
+
/** What `ChatThreadScreen`'s `accessory` is given. */
|
|
133
|
+
export interface ChatThreadAccessoryContext {
|
|
134
|
+
conversationId: string;
|
|
135
|
+
/** The composer's draft and reply (`dispatch({ type: "text", text })` writes into it). */
|
|
136
|
+
composer: ChatComposerHandle;
|
|
137
|
+
/** null while your role loads. */
|
|
138
|
+
canPost: boolean | null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** What the thread's status line says: nothing, offline (NetInfo), or the live channel dropped. */
|
|
142
|
+
export type ChatConnectionState = "online" | "offline" | "connecting";
|
|
143
|
+
|
|
144
|
+
/** Offline wins; then a live channel that dropped; a channel not yet heard from is online. */
|
|
145
|
+
export function connectionState(online: boolean | null, live: boolean | null): ChatConnectionState {
|
|
146
|
+
if (online === false) return "offline";
|
|
147
|
+
if (live === false) return "connecting";
|
|
148
|
+
return "online";
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Whether the phone is online, from the app's NetInfo (`modules.netInfo`); null without it or until it says. */
|
|
152
|
+
export function useOnline(netInfo: NetInfoLike | undefined): boolean | null {
|
|
153
|
+
const [online, setOnline] = useState<boolean | null>(null);
|
|
154
|
+
useEffect(() => {
|
|
155
|
+
if (!netInfo) return;
|
|
156
|
+
return netInfo.addEventListener((state) => setOnline(isOnline(state)));
|
|
157
|
+
}, [netInfo]);
|
|
158
|
+
return online;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** How many messages from others came in after `afterSeq` (the newest when you scrolled up). */
|
|
162
|
+
export function countUnseenBelow(messages: readonly { seq: number; senderId: string | null }[], afterSeq: number | null, me: string): number {
|
|
163
|
+
if (afterSeq === null) return 0;
|
|
164
|
+
let n = 0;
|
|
165
|
+
for (let i = messages.length - 1; i >= 0 && messages[i]!.seq > afterSeq; i--) if (messages[i]!.senderId !== me) n++;
|
|
166
|
+
return n;
|
|
167
|
+
}
|
|
168
|
+
|
|
98
169
|
export interface ChatThreadScreenSlots {
|
|
99
170
|
/** Draws the app's own attachment kinds; the card by default. */
|
|
100
171
|
attachment?: ChatAttachmentSlot | null;
|
|
@@ -133,9 +204,11 @@ export function nearBottom(m: { contentOffset: { y: number }; contentSize: { hei
|
|
|
133
204
|
}
|
|
134
205
|
|
|
135
206
|
export function ChatThreadScreen(props: ChatThreadScreenProps) {
|
|
136
|
-
const { conversationId, threadRootId = null, keyboardVerticalOffset, onOpenAttachment, jumpTo, topic: topicProp, showTopics = false, scheduling = true, availability = true,
|
|
207
|
+
const { conversationId, threadRootId = null, keyboardVerticalOffset, onOpenAttachment, jumpTo, topic: topicProp, showTopics = false, scheduling = true, availability = true, receipts = "show", active = true, header, accessory, videoLimitMs, testID = "chat-thread" } = props;
|
|
208
|
+
// Off screen either way: not focused (a tab), or not the thread being read.
|
|
209
|
+
const focused = (props.focused ?? true) && active;
|
|
137
210
|
const chat = useChat();
|
|
138
|
-
const { userId, videoUpload, attachmentKinds, outbox } = chat;
|
|
211
|
+
const { userId, videoUpload, attachmentKinds, outbox, queue } = chat;
|
|
139
212
|
const attachmentSlot = props.slots?.attachment;
|
|
140
213
|
const cardActionTimeoutMs = props.cardActionTimeoutMs;
|
|
141
214
|
// Off unless the app turns it on: an og:image fetched from the phone tells its site who reads it.
|
|
@@ -155,13 +228,14 @@ export function ChatThreadScreen(props: ChatThreadScreenProps) {
|
|
|
155
228
|
if (jumpTo) setChosenTopic(null);
|
|
156
229
|
}
|
|
157
230
|
const topic = topicProp ?? chosenTopic ?? undefined;
|
|
158
|
-
const conv = useConversation(conversationId, { threadRootId: threadRootId ?? undefined, topic });
|
|
231
|
+
const conv = useConversation(conversationId, { threadRootId: threadRootId ?? undefined, topic, markRead: active });
|
|
159
232
|
const pins = usePins(conversationId);
|
|
160
233
|
const messageActions = useMessageActions();
|
|
161
234
|
const { modules, copy, person } = useChatNative();
|
|
162
235
|
const toast = useToast();
|
|
163
236
|
const colors = useThemeRoles();
|
|
164
237
|
const reducedMotion = useReducedMotion();
|
|
238
|
+
const { height: windowHeight } = useWindowDimensions();
|
|
165
239
|
const genesis = useGenesisModules();
|
|
166
240
|
const listRef = useRef<NativeThreadListHandle | null>(null);
|
|
167
241
|
// What you send is what you look at next, wherever the list was.
|
|
@@ -187,6 +261,18 @@ export function ChatThreadScreen(props: ChatThreadScreenProps) {
|
|
|
187
261
|
[conv.members, conv.messages],
|
|
188
262
|
);
|
|
189
263
|
|
|
264
|
+
// The status of the last message you sent, and who has read it in a group.
|
|
265
|
+
const queued = useMemo(() => new Set(queue.map((q) => q.clientId)), [queue]);
|
|
266
|
+
const lastReceipt = useMemo(
|
|
267
|
+
() => lastSentReceipt(conv.items, { me: userId, members: conv.members, group, queued, receipts }),
|
|
268
|
+
[conv.items, userId, conv.members, group, queued, receipts],
|
|
269
|
+
);
|
|
270
|
+
const receiptLabel = lastReceipt ? receiptText(lastReceipt.receipt, copy) : null;
|
|
271
|
+
const receiptKey = lastReceipt?.key ?? null;
|
|
272
|
+
const readers = lastReceipt?.receipt.state === "read-by" ? lastReceipt.receipt.readers : null;
|
|
273
|
+
const [readersOpen, setReadersOpen] = useState(false);
|
|
274
|
+
const openReaders = useCallback(() => setReadersOpen(true), []);
|
|
275
|
+
|
|
190
276
|
const rows = useMemo(() => buildThreadRows(conv.items, { me: userId, firstUnreadId: conv.firstUnreadId, group }), [conv.items, userId, conv.firstUnreadId, group]);
|
|
191
277
|
const paths = useMemo(() => conv.items.flatMap((i) => (i.type === "message" ? attachmentPaths(i.message.attachments) : [])), [conv.items]);
|
|
192
278
|
const urls = useFileUrls(paths);
|
|
@@ -256,10 +342,10 @@ export function ChatThreadScreen(props: ChatThreadScreenProps) {
|
|
|
256
342
|
const { retry, discard, canPost: allowed } = conv;
|
|
257
343
|
const onLongPress = useCallback(
|
|
258
344
|
(message: ChatMessage) => {
|
|
259
|
-
haptic(modules.haptics, "
|
|
345
|
+
haptic(modules.haptics ?? genesis.haptics, "long-press");
|
|
260
346
|
setActionsFor(message);
|
|
261
347
|
},
|
|
262
|
-
[modules.haptics],
|
|
348
|
+
[modules.haptics, genesis.haptics],
|
|
263
349
|
);
|
|
264
350
|
const onSwipeReply = useCallback(() => haptic(modules.haptics, "select"), [modules.haptics]);
|
|
265
351
|
const onRetry = useCallback((clientId: string) => void retry(clientId), [retry]);
|
|
@@ -328,6 +414,37 @@ export function ChatThreadScreen(props: ChatThreadScreenProps) {
|
|
|
328
414
|
},
|
|
329
415
|
[copy.jumpedTo, nameOf, attachmentKinds],
|
|
330
416
|
);
|
|
417
|
+
// ---- Where you are: the "↓ N new messages" pill ----
|
|
418
|
+
const [scrolledAwayAt, setScrolledAwayAt] = useState<number | null>(null);
|
|
419
|
+
// Where you scrolled away is this timeline's: another conversation or thread starts at the bottom.
|
|
420
|
+
const timeline = `${conversationId}:${threadRootId ?? ""}`;
|
|
421
|
+
const [awayFor, setAwayFor] = useState(timeline);
|
|
422
|
+
if (awayFor !== timeline) {
|
|
423
|
+
setAwayFor(timeline);
|
|
424
|
+
setScrolledAwayAt(null);
|
|
425
|
+
}
|
|
426
|
+
const latestSeqNow = useRef(0);
|
|
427
|
+
latestSeqNow.current = conv.messages.length ? conv.messages[conv.messages.length - 1]!.seq : 0;
|
|
428
|
+
const onAtBottomChange = useCallback((atBottom: boolean) => setScrolledAwayAt(atBottom ? null : latestSeqNow.current), []);
|
|
429
|
+
const newBelow = countUnseenBelow(conv.messages, scrolledAwayAt, userId);
|
|
430
|
+
// Opens at the first unread, once that is known (the list keeps it at the bottom if it fits).
|
|
431
|
+
// At the "New" line itself (its own row), so it is the first thing you see.
|
|
432
|
+
const openAt = conv.firstUnreadKnown ? (conv.firstUnreadId ? UNREAD_ROW_ID : null) : undefined;
|
|
433
|
+
|
|
434
|
+
// ---- Status: offline, reconnecting ----
|
|
435
|
+
const online = useOnline(modules.netInfo);
|
|
436
|
+
const connection = connectionState(online, conv.live);
|
|
437
|
+
const { reload } = conv;
|
|
438
|
+
const [reloading, setReloading] = useState(false);
|
|
439
|
+
const retryLoad = useCallback(() => {
|
|
440
|
+
if (!reload) return;
|
|
441
|
+
setReloading(true);
|
|
442
|
+
void reload()
|
|
443
|
+
.catch(() => fail())
|
|
444
|
+
.finally(() => setReloading(false));
|
|
445
|
+
}, [reload, fail]);
|
|
446
|
+
const times = useTimeReveal(reducedMotion);
|
|
447
|
+
|
|
331
448
|
const onJumpFailed = useCallback(() => {
|
|
332
449
|
if (jumpLoadFailed.current) jumpLoadFailed.current = false;
|
|
333
450
|
else toast.show({ title: copy.messageGone });
|
|
@@ -364,11 +481,14 @@ export function ChatThreadScreen(props: ChatThreadScreenProps) {
|
|
|
364
481
|
onOpenAttachment={onOpenAttachment}
|
|
365
482
|
attachmentSlot={attachmentSlot}
|
|
366
483
|
cardActionTimeoutMs={cardActionTimeoutMs}
|
|
484
|
+
receipt={row.key === receiptKey ? receiptLabel : null}
|
|
485
|
+
backdrop={colors.groupedBackground as string}
|
|
486
|
+
onReceiptPress={row.key === receiptKey && readers ? openReaders : undefined}
|
|
367
487
|
testID={`${testID}-message-${m?.id ?? row.key}`}
|
|
368
488
|
/>
|
|
369
489
|
);
|
|
370
490
|
},
|
|
371
|
-
[testID, copy.newMessages, userId, urls, byId, group, reducedMotion, onLongPress, allowed, reply, onSwipeReply, onRetry, onDiscard, react, onOpenImage, picker, reattachable, onReattach, previews, linkPreviewImages, attachmentKinds, onOpenAttachment, attachmentSlot, cardActionTimeoutMs, observer],
|
|
491
|
+
[receiptKey, receiptLabel, readers, openReaders, testID, colors.groupedBackground, copy.newMessages, userId, urls, byId, group, reducedMotion, onLongPress, allowed, reply, onSwipeReply, onRetry, onDiscard, react, onOpenImage, picker, reattachable, onReattach, previews, linkPreviewImages, attachmentKinds, onOpenAttachment, attachmentSlot, cardActionTimeoutMs, observer],
|
|
372
492
|
);
|
|
373
493
|
|
|
374
494
|
// ---- Video messages ----
|
|
@@ -403,6 +523,14 @@ export function ChatThreadScreen(props: ChatThreadScreenProps) {
|
|
|
403
523
|
);
|
|
404
524
|
|
|
405
525
|
const activity = activityLine(conv.activity, (id) => nameOf(id));
|
|
526
|
+
// Typing is iMessage's bubble of dots; recording a voice note or video keeps its words.
|
|
527
|
+
const typers = conv.activity.filter((a) => a.activity === "typing");
|
|
528
|
+
const recording = conv.activity.some((a) => a.activity !== "typing");
|
|
529
|
+
const footer = !activity ? null : recording ? (
|
|
530
|
+
<ActivityLine text={activity} testID={`${testID}-activity`} />
|
|
531
|
+
) : (
|
|
532
|
+
<ChatTypingBubble label={activity} userId={typers[0]?.userId ?? null} group={group} reducedMotion={reducedMotion} testID={`${testID}-typing`} />
|
|
533
|
+
);
|
|
406
534
|
// Unknown while access loads: the composer waits quietly instead of saying you can't post. A
|
|
407
535
|
// read that failed (and no role known from before) offers Retry in the composer's place.
|
|
408
536
|
const topics = topicsState.topics;
|
|
@@ -437,8 +565,13 @@ export function ChatThreadScreen(props: ChatThreadScreenProps) {
|
|
|
437
565
|
<ChatTopicBar topics={topics} value={topic ?? null} onValueChange={setChosenTopic} canManage={canManageTopics(conv.role)} onSetTopic={topicsState.setTopic} testID={`${testID}-topics`} />
|
|
438
566
|
) : null}
|
|
439
567
|
<ChatAwayLine text={awayLine} testID={`${testID}-away`} />
|
|
440
|
-
<
|
|
568
|
+
<ChatConnectionLine state={connection} testID={`${testID}-connection`} />
|
|
569
|
+
{header}
|
|
570
|
+
<View style={{ flex: 1 }} {...times.panHandlers}>
|
|
571
|
+
<ChatTimesProvider value={times.value}>
|
|
441
572
|
<NativeThreadList<ThreadRow>
|
|
573
|
+
// A list per conversation: where it opens (the first unread) is decided once per conversation.
|
|
574
|
+
key={`${conversationId}:${threadRootId ?? ""}`}
|
|
442
575
|
ref={listRef}
|
|
443
576
|
testID={`${testID}-list`}
|
|
444
577
|
data={rows}
|
|
@@ -455,12 +588,24 @@ export function ChatThreadScreen(props: ChatThreadScreenProps) {
|
|
|
455
588
|
onLoadUntil={onLoadUntil}
|
|
456
589
|
jumpAnnouncement={jumpAnnouncement}
|
|
457
590
|
onJumpFailed={onJumpFailed}
|
|
458
|
-
footer={
|
|
459
|
-
|
|
591
|
+
footer={footer}
|
|
592
|
+
openAt={openAt}
|
|
593
|
+
onAtBottomChange={onAtBottomChange}
|
|
594
|
+
header={firstPageFailed && rows.length ? <ThreadLoadError onRetry={retryLoad} retrying={reloading} testID={`${testID}-load-error`} /> : null}
|
|
595
|
+
empty={
|
|
596
|
+
firstPageFailed ? (
|
|
597
|
+
<ThreadLoadError onRetry={retryLoad} retrying={reloading} testID={`${testID}-load-error`} />
|
|
598
|
+
) : (
|
|
599
|
+
<NativeEmptyState variant="first-run" title={copy.threadEmptyTitle} body={currentTopic?.closedAt ? copy.topicClosedNotice : copy.threadEmptyBody} testID={`${testID}-empty`} />
|
|
600
|
+
)
|
|
601
|
+
}
|
|
460
602
|
/>
|
|
603
|
+
</ChatTimesProvider>
|
|
604
|
+
{newBelow > 0 ? <NewMessagesPill count={newBelow} onPress={toNewest} testID={`${testID}-new-below`} /> : null}
|
|
461
605
|
</View>
|
|
462
606
|
{scheduling && !threadRootId ? <ChatScheduledBar conversationId={conversationId} testID={`${testID}-scheduled`} /> : null}
|
|
463
607
|
<VideoUploadTray conversationId={conversationId} userId={userId} testID={`${testID}-video-uploads`} />
|
|
608
|
+
{accessory && !accessFailed ? <View testID={`${testID}-accessory`}>{accessory({ conversationId, composer, canPost })}</View> : null}
|
|
464
609
|
{accessFailed ? (
|
|
465
610
|
<ChatAccessError onRetry={retryAccess} retrying={retryingAccess} testID={`${testID}-access-error`} />
|
|
466
611
|
) : (
|
|
@@ -492,12 +637,33 @@ export function ChatThreadScreen(props: ChatThreadScreenProps) {
|
|
|
492
637
|
attachmentKinds={attachmentKinds}
|
|
493
638
|
testID={`${testID}-actions`}
|
|
494
639
|
/>
|
|
640
|
+
<NativeSheet open={readersOpen && Boolean(readers?.length)} onOpenChange={setReadersOpen} fitContent testID={`${testID}-readers`}>
|
|
641
|
+
<View style={{ paddingTop: 20, paddingBottom: 8, paddingHorizontal: 16, gap: 8 }}>
|
|
642
|
+
<NativeText preset="label" accessibilityRole="header" style={{ color: colors.secondaryLabel, paddingHorizontal: 4 }}>
|
|
643
|
+
{copy.receiptReadersTitle}
|
|
644
|
+
</NativeText>
|
|
645
|
+
{/* A long group's readers scroll inside the sheet (which stops at 90% of the screen). */}
|
|
646
|
+
<NativeSheetScrollView testID={`${testID}-readers-list`} style={{ maxHeight: Math.round(windowHeight * 0.7) }}>
|
|
647
|
+
{(readers ?? []).map((id) => {
|
|
648
|
+
const p = person(id) ?? people[id];
|
|
649
|
+
return (
|
|
650
|
+
<NativeListItem
|
|
651
|
+
key={id}
|
|
652
|
+
testID={`${testID}-readers-${id}`}
|
|
653
|
+
title={nameOf(id)}
|
|
654
|
+
leading={<NativeAvatar size={32} src={p?.avatarUrl ?? null} fallback={initials(p?.name)} alt={p?.name} recyclingKey={id} />}
|
|
655
|
+
/>
|
|
656
|
+
);
|
|
657
|
+
})}
|
|
658
|
+
</NativeSheetScrollView>
|
|
659
|
+
</View>
|
|
660
|
+
</NativeSheet>
|
|
495
661
|
<ChatPhotoViewer photos={viewer?.photos ?? null} initialIndex={viewer?.index} onClose={() => setViewer(null)} testID={`${testID}-viewer`} />
|
|
496
662
|
{canSchedule ? <ChatScheduleSheet open={schedule.open} onOpenChange={schedule.setOpen} onSchedule={(at) => void schedule.scheduleAt(at)} testID={`${testID}-schedule`} /> : null}
|
|
497
663
|
<ChatVideoRecorder
|
|
498
664
|
open={videoOpen}
|
|
499
665
|
onClose={() => setVideoOpen(false)}
|
|
500
|
-
limitMs={recordingLimitMs(conv.role)}
|
|
666
|
+
limitMs={videoLimitMs ?? recordingLimitMs(conv.role)}
|
|
501
667
|
onStart={onVideoStart}
|
|
502
668
|
onTick={conv.signalRecordingVideo}
|
|
503
669
|
onRecorded={onVideoRecorded}
|
|
@@ -532,6 +698,64 @@ function UnreadDivider({ label, testID }: { label: string; testID: string }) {
|
|
|
532
698
|
);
|
|
533
699
|
}
|
|
534
700
|
|
|
701
|
+
/** "Waiting for network…" or "Connecting…" under the header; nothing while live. */
|
|
702
|
+
function ChatConnectionLine({ state, testID }: { state: ChatConnectionState; testID: string }) {
|
|
703
|
+
const colors = useThemeRoles();
|
|
704
|
+
const { copy } = useChatNative();
|
|
705
|
+
if (state === "online") return null;
|
|
706
|
+
const text = state === "offline" ? copy.waitingForNetwork : copy.connecting;
|
|
707
|
+
return (
|
|
708
|
+
<View testID={testID} accessible accessibilityRole="text" accessibilityLabel={text} accessibilityLiveRegion="polite" style={{ alignItems: "center", paddingVertical: 6, backgroundColor: colors.fill }}>
|
|
709
|
+
<NativeText preset="caption" style={{ color: colors.secondaryLabel, fontWeight: "600" }}>
|
|
710
|
+
{text}
|
|
711
|
+
</NativeText>
|
|
712
|
+
</View>
|
|
713
|
+
);
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
/** Nothing could be read: say so, with Retry, instead of "No messages yet". */
|
|
717
|
+
function ThreadLoadError({ onRetry, retrying, testID }: { onRetry: () => void; retrying: boolean; testID: string }) {
|
|
718
|
+
const { copy } = useChatNative();
|
|
719
|
+
return (
|
|
720
|
+
<NativeEmptyState
|
|
721
|
+
variant="error"
|
|
722
|
+
title={copy.threadErrorTitle}
|
|
723
|
+
body={copy.threadErrorBody}
|
|
724
|
+
testID={testID}
|
|
725
|
+
primaryAction={
|
|
726
|
+
<NativeButton onPress={onRetry} loading={retrying} disabled={retrying} testID={`${testID}-retry`}>
|
|
727
|
+
{copy.retry}
|
|
728
|
+
</NativeButton>
|
|
729
|
+
}
|
|
730
|
+
/>
|
|
731
|
+
);
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
/** "↓ 3 new messages", over the bottom of the list while you're scrolled up; goes to the newest. */
|
|
735
|
+
function NewMessagesPill({ count, onPress, testID }: { count: number; onPress: () => void; testID: string }) {
|
|
736
|
+
const colors = useThemeRoles();
|
|
737
|
+
const { copy } = useChatNative();
|
|
738
|
+
const label = count === 1 ? copy.newMessagesPillOne : fill(copy.newMessagesPill, { count });
|
|
739
|
+
return (
|
|
740
|
+
<View pointerEvents="box-none" style={{ position: "absolute", left: 0, right: 0, bottom: 8, alignItems: "center" }}>
|
|
741
|
+
<Pressable
|
|
742
|
+
testID={testID}
|
|
743
|
+
accessibilityRole="button"
|
|
744
|
+
accessibilityLabel={label}
|
|
745
|
+
accessibilityHint={copy.newMessagesPillHint}
|
|
746
|
+
accessibilityLiveRegion="polite"
|
|
747
|
+
onPress={onPress}
|
|
748
|
+
style={{ flexDirection: "row", alignItems: "center", gap: 6, minHeight: 36, paddingHorizontal: 14, borderRadius: 18, backgroundColor: colors.tint }}
|
|
749
|
+
>
|
|
750
|
+
<NativeIcon name="chevron-down" size={14} color={colors.surface} />
|
|
751
|
+
<NativeText preset="caption" style={{ color: colors.surface, fontWeight: "600" }}>
|
|
752
|
+
{label}
|
|
753
|
+
</NativeText>
|
|
754
|
+
</Pressable>
|
|
755
|
+
</View>
|
|
756
|
+
);
|
|
757
|
+
}
|
|
758
|
+
|
|
535
759
|
function ActivityLine({ text, testID }: { text: string; testID: string }) {
|
|
536
760
|
const colors = useThemeRoles();
|
|
537
761
|
return (
|
|
@@ -545,5 +769,7 @@ function ActivityLine({ text, testID }: { text: string; testID: string }) {
|
|
|
545
769
|
|
|
546
770
|
const threadRowKey = (row: ThreadRow) => row.key;
|
|
547
771
|
/** A message row is gone to by the server's id; a day heading, the unread line and a message still sending can't be. */
|
|
548
|
-
|
|
772
|
+
/** The "New messages" line, as a row the list can open at (no message id looks like it). */
|
|
773
|
+
const UNREAD_ROW_ID = "genesis-chat:unread";
|
|
774
|
+
const threadRowMessageId = (row: ThreadRow) => (row.type === "unread" ? UNREAD_ROW_ID : row.type === "message" && row.item.type === "message" ? row.item.message.id : null);
|
|
549
775
|
const threadRowType = (row: ThreadRow) => (row.type === "message" ? (row.mine ? "mine" : "theirs") : row.type);
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @feat:chat
|
|
3
|
+
* Every message's time, on demand, as iMessage shows it: drag the thread left and the bubbles
|
|
4
|
+
* slide over, each one's time appearing at the trailing edge; let go and they slide back. The
|
|
5
|
+
* last message of a run keeps its time under it all the time. VoiceOver already reads each time
|
|
6
|
+
* in the bubble's label, so the revealed times are hidden from it.
|
|
7
|
+
*
|
|
8
|
+
* One `Animated.Value` for the whole thread (how far it is pulled, 0 to `TIME_REVEAL_WIDTH`),
|
|
9
|
+
* given to the rows through context, so a drag re-renders nothing: each row's transform follows it
|
|
10
|
+
* on the native driver. Reduce Motion: it still follows the finger (that's direct manipulation),
|
|
11
|
+
* and snaps back on release instead of springing.
|
|
12
|
+
*/
|
|
13
|
+
import { createContext, useContext, useMemo, useRef, type ReactNode } from "react";
|
|
14
|
+
import { Animated, PanResponder, View, type GestureResponderHandlers } from "react-native";
|
|
15
|
+
import { NativeText } from "@marktiderman/genesis-ui-native/components";
|
|
16
|
+
import { useThemeRoles } from "@marktiderman/genesis-ui-native/hooks";
|
|
17
|
+
|
|
18
|
+
/** How far the bubbles slide to show the times, in points. */
|
|
19
|
+
export const TIME_REVEAL_WIDTH = 64;
|
|
20
|
+
|
|
21
|
+
/** A horizontal move counts as the reveal once it is this far left and mostly sideways. */
|
|
22
|
+
export function startsTimeReveal(dx: number, dy: number): boolean {
|
|
23
|
+
return dx < -12 && Math.abs(dx) > Math.abs(dy) * 2;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** How far the thread is pulled for a drag of `dx` (negative is left). */
|
|
27
|
+
export function timeRevealFor(dx: number): number {
|
|
28
|
+
return Math.max(0, Math.min(TIME_REVEAL_WIDTH, -dx));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const TimesContext = createContext<Animated.Value | null>(null);
|
|
32
|
+
|
|
33
|
+
/** The pan that pulls the times in, and the value rows read. Put `panHandlers` on the view around the list. */
|
|
34
|
+
export function useTimeReveal(reducedMotion: boolean): { value: Animated.Value; panHandlers: GestureResponderHandlers } {
|
|
35
|
+
const value = useRef(new Animated.Value(0)).current;
|
|
36
|
+
const reduced = useRef(reducedMotion);
|
|
37
|
+
reduced.current = reducedMotion;
|
|
38
|
+
const responder = useMemo(() => {
|
|
39
|
+
const back = () => {
|
|
40
|
+
if (reduced.current) value.setValue(0);
|
|
41
|
+
else Animated.spring(value, { toValue: 0, useNativeDriver: true, bounciness: 0, speed: 16 }).start();
|
|
42
|
+
};
|
|
43
|
+
return PanResponder.create({
|
|
44
|
+
onMoveShouldSetPanResponder: (_e, g) => startsTimeReveal(g.dx, g.dy),
|
|
45
|
+
onPanResponderMove: (_e, g) => value.setValue(timeRevealFor(g.dx)),
|
|
46
|
+
onPanResponderRelease: back,
|
|
47
|
+
onPanResponderTerminate: back,
|
|
48
|
+
});
|
|
49
|
+
}, [value]);
|
|
50
|
+
return { value, panHandlers: responder.panHandlers };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function ChatTimesProvider({ value, children }: { value: Animated.Value; children: ReactNode }) {
|
|
54
|
+
return <TimesContext.Provider value={value}>{children}</TimesContext.Provider>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** A row that slides with the reveal and shows its time at the trailing edge. Without a provider, a plain view. */
|
|
58
|
+
export function TimeRevealRow({ time, testID, children }: { time: string; testID: string; children: ReactNode }) {
|
|
59
|
+
const reveal = useContext(TimesContext);
|
|
60
|
+
const colors = useThemeRoles();
|
|
61
|
+
const styles = useMemo(
|
|
62
|
+
() =>
|
|
63
|
+
reveal
|
|
64
|
+
? {
|
|
65
|
+
row: { transform: [{ translateX: reveal.interpolate({ inputRange: [0, TIME_REVEAL_WIDTH], outputRange: [0, -TIME_REVEAL_WIDTH], extrapolate: "clamp" }) }] },
|
|
66
|
+
time: { opacity: reveal.interpolate({ inputRange: [0, TIME_REVEAL_WIDTH], outputRange: [0, 1], extrapolate: "clamp" }) },
|
|
67
|
+
}
|
|
68
|
+
: null,
|
|
69
|
+
[reveal],
|
|
70
|
+
);
|
|
71
|
+
if (!styles) return <View>{children}</View>;
|
|
72
|
+
return (
|
|
73
|
+
<Animated.View style={styles.row}>
|
|
74
|
+
{children}
|
|
75
|
+
<Animated.View
|
|
76
|
+
testID={`${testID}-time-reveal`}
|
|
77
|
+
pointerEvents="none"
|
|
78
|
+
accessibilityElementsHidden
|
|
79
|
+
importantForAccessibility="no-hide-descendants"
|
|
80
|
+
style={[{ position: "absolute", top: 0, bottom: 0, right: -TIME_REVEAL_WIDTH, width: TIME_REVEAL_WIDTH, alignItems: "center", justifyContent: "center" }, styles.time]}
|
|
81
|
+
>
|
|
82
|
+
{/* Shrinks to fit the column at the largest text sizes, rather than clip. */}
|
|
83
|
+
<NativeText preset="caption" numberOfLines={1} adjustsFontSizeToFit minimumFontScale={0.6} style={{ color: colors.tertiaryLabel }}>
|
|
84
|
+
{time}
|
|
85
|
+
</NativeText>
|
|
86
|
+
</Animated.View>
|
|
87
|
+
</Animated.View>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @feat:chat
|
|
3
|
+
* The thread's title in the navigation bar, as Messages draws it: who it is (an avatar over or
|
|
4
|
+
* beside the name), a quiet line under it (the away line by default), and, with `onPress`, a
|
|
5
|
+
* button that opens their details. `useChatThreadStackOptions` puts it in `headerTitle` when any
|
|
6
|
+
* of `avatar`, `subtitle`, `onPressTitle` or `conversationId` is given.
|
|
7
|
+
*
|
|
8
|
+
* The default subtitle (the away line) needs `ChatProvider` above the navigator, since the bar is
|
|
9
|
+
* drawn by the navigator, not the screen. Pass `subtitle` yourself (or `null` for none) otherwise.
|
|
10
|
+
*/
|
|
11
|
+
import { isValidElement, useCallback, useMemo, type ReactNode } from "react";
|
|
12
|
+
import { Pressable, View } from "react-native";
|
|
13
|
+
import { useQuery } from "@tanstack/react-query";
|
|
14
|
+
import { NativeAvatar, NativeText } from "@marktiderman/genesis-ui-native/components";
|
|
15
|
+
import { useThemeRoles } from "@marktiderman/genesis-ui-native/hooks";
|
|
16
|
+
import type { ChatMember } from "../core/types";
|
|
17
|
+
import { chatKeys, useChat } from "../react/index";
|
|
18
|
+
import { useAwayLine } from "./availability";
|
|
19
|
+
import { initials, useChatNative } from "./context";
|
|
20
|
+
|
|
21
|
+
/** Who the conversation is with, for the title's avatar. */
|
|
22
|
+
export interface ChatThreadTitleAvatar {
|
|
23
|
+
src?: string | null;
|
|
24
|
+
name?: string | null;
|
|
25
|
+
/** Keeps the picture when the bar redraws (expo-image's `recyclingKey`). */
|
|
26
|
+
id?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ChatThreadTitleProps {
|
|
30
|
+
title: string;
|
|
31
|
+
/** Under the title. `undefined`: the away line, from `conversationId`. `null`: none. */
|
|
32
|
+
subtitle?: string | null;
|
|
33
|
+
/** A picture beside the title: who it is, or your own element. */
|
|
34
|
+
avatar?: ChatThreadTitleAvatar | ReactNode;
|
|
35
|
+
/** Makes the title a button (to the conversation's details). */
|
|
36
|
+
onPress?: () => void;
|
|
37
|
+
/** VoiceOver's hint when it is a button. */
|
|
38
|
+
pressHint?: string;
|
|
39
|
+
/** For the default subtitle (the away line). */
|
|
40
|
+
conversationId?: string;
|
|
41
|
+
tintColor?: string;
|
|
42
|
+
testID?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* A plain object is avatar data (`{}` and `{ id }` included). Anything React renders stays the app's
|
|
47
|
+
* own node: an element, a portal, an array, a Set or any other iterable of nodes.
|
|
48
|
+
*/
|
|
49
|
+
function isAvatarData(a: unknown): a is ChatThreadTitleAvatar {
|
|
50
|
+
if (a === null || typeof a !== "object" || isValidElement(a)) return false;
|
|
51
|
+
if ("$$typeof" in a) return false;
|
|
52
|
+
return typeof (a as { [Symbol.iterator]?: unknown })[Symbol.iterator] !== "function";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function ChatThreadTitle({ title, subtitle, avatar, onPress, pressHint, conversationId, tintColor, testID = "chat-thread-title" }: ChatThreadTitleProps) {
|
|
56
|
+
if (subtitle === undefined && conversationId) {
|
|
57
|
+
return <AwayTitle conversationId={conversationId} title={title} avatar={avatar} onPress={onPress} pressHint={pressHint} tintColor={tintColor} testID={testID} />;
|
|
58
|
+
}
|
|
59
|
+
return <TitleBody title={title} subtitle={subtitle ?? null} avatar={avatar} onPress={onPress} pressHint={pressHint} tintColor={tintColor} testID={testID} />;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function AwayTitle({ conversationId, ...rest }: Omit<ChatThreadTitleProps, "subtitle"> & { conversationId: string; testID: string }) {
|
|
63
|
+
const { client, userId } = useChat();
|
|
64
|
+
const { person, copy } = useChatNative();
|
|
65
|
+
// The thread's own members query (the same key), so this reads its cache.
|
|
66
|
+
const members = useQuery<ChatMember[]>({ queryKey: chatKeys.members(conversationId), queryFn: () => client.members(conversationId), staleTime: 60_000 }).data;
|
|
67
|
+
const nameOf = useCallback((id: string) => person(id)?.name ?? copy.someone, [person, copy.someone]);
|
|
68
|
+
const away = useAwayLine(conversationId, useMemo(() => members ?? [], [members]), userId, nameOf);
|
|
69
|
+
return <TitleBody {...rest} subtitle={away} />;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function TitleBody({ title, subtitle, avatar, onPress, pressHint, tintColor, testID }: Omit<ChatThreadTitleProps, "subtitle" | "conversationId"> & { subtitle: string | null; testID: string }) {
|
|
73
|
+
const colors = useThemeRoles();
|
|
74
|
+
const picture = isAvatarData(avatar) ? (
|
|
75
|
+
<NativeAvatar size={28} src={avatar.src ?? null} fallback={initials(avatar.name ?? title)} alt={avatar.name ?? title} recyclingKey={avatar.id} />
|
|
76
|
+
) : (
|
|
77
|
+
(avatar as ReactNode) ?? null
|
|
78
|
+
);
|
|
79
|
+
const label = [title, subtitle].filter(Boolean).join(", ");
|
|
80
|
+
const body = (
|
|
81
|
+
<View style={{ flexDirection: "row", alignItems: "center", gap: 8, maxWidth: 260 }}>
|
|
82
|
+
{picture ? (
|
|
83
|
+
<View testID={`${testID}-avatar`} accessibilityElementsHidden importantForAccessibility="no-hide-descendants">
|
|
84
|
+
{picture}
|
|
85
|
+
</View>
|
|
86
|
+
) : null}
|
|
87
|
+
<View style={{ flexShrink: 1, alignItems: picture ? "flex-start" : "center" }}>
|
|
88
|
+
<NativeText numberOfLines={1} style={{ fontSize: 17, lineHeight: 22, fontWeight: "600", color: tintColor ?? colors.label }}>
|
|
89
|
+
{title}
|
|
90
|
+
</NativeText>
|
|
91
|
+
{subtitle ? (
|
|
92
|
+
<NativeText testID={`${testID}-subtitle`} numberOfLines={1} preset="caption" style={{ color: colors.secondaryLabel }}>
|
|
93
|
+
{subtitle}
|
|
94
|
+
</NativeText>
|
|
95
|
+
) : null}
|
|
96
|
+
</View>
|
|
97
|
+
</View>
|
|
98
|
+
);
|
|
99
|
+
if (!onPress) {
|
|
100
|
+
return (
|
|
101
|
+
<View testID={testID} accessible accessibilityRole="header" accessibilityLabel={label}>
|
|
102
|
+
{body}
|
|
103
|
+
</View>
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
return (
|
|
107
|
+
<Pressable testID={testID} accessibilityRole="button" accessibilityLabel={label} accessibilityHint={pressHint} onPress={onPress} hitSlop={8} style={{ minHeight: 44, justifyContent: "center" }}>
|
|
108
|
+
{body}
|
|
109
|
+
</Pressable>
|
|
110
|
+
);
|
|
111
|
+
}
|