@marktiderman/genesis-ui-native 3.5.0 → 3.6.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.
Files changed (92) hide show
  1. package/README.md +71 -7
  2. package/dist/{chunk-LBVD7XXL.js → chunk-2Z72MYHB.js} +627 -20
  3. package/dist/chunk-2Z72MYHB.js.map +1 -0
  4. package/dist/chunk-4OQOJ2OC.js +80 -0
  5. package/dist/chunk-4OQOJ2OC.js.map +1 -0
  6. package/dist/{chunk-6KWEAGRD.js → chunk-6PGLX6ET.js} +101 -6
  7. package/dist/chunk-6PGLX6ET.js.map +1 -0
  8. package/dist/{chunk-ONH2PCGH.js → chunk-AGTBABAH.js} +9 -15
  9. package/dist/chunk-AGTBABAH.js.map +1 -0
  10. package/dist/{chunk-ABVSSJNT.js → chunk-FUMSL7VZ.js} +198 -168
  11. package/dist/chunk-FUMSL7VZ.js.map +1 -0
  12. package/dist/{chunk-GHYUNNNU.js → chunk-NDXNSSM6.js} +175 -93
  13. package/dist/chunk-NDXNSSM6.js.map +1 -0
  14. package/dist/chunk-SEJ6RBP7.js +361 -0
  15. package/dist/chunk-SEJ6RBP7.js.map +1 -0
  16. package/dist/{chunk-D5RKSPMH.js → chunk-ZRQAFFTN.js} +4 -3
  17. package/dist/chunk-ZRQAFFTN.js.map +1 -0
  18. package/dist/components/index.d.ts +331 -4
  19. package/dist/components/index.js +5 -5
  20. package/dist/data/index.d.ts +68 -4
  21. package/dist/data/index.js +235 -31
  22. package/dist/data/index.js.map +1 -1
  23. package/dist/hooks/index.d.ts +60 -1
  24. package/dist/hooks/index.js +2 -3
  25. package/dist/icons/lucide.d.ts +1 -1
  26. package/dist/icons/lucide.js +7 -1
  27. package/dist/icons/lucide.js.map +1 -1
  28. package/dist/{inbox-page-BEarNEIP.d.ts → inbox-page-B7_Oi2fS.d.ts} +2 -44
  29. package/dist/index.d.ts +8 -5
  30. package/dist/index.js +8 -8
  31. package/dist/layouts/index.d.ts +40 -51
  32. package/dist/layouts/index.js +5 -4
  33. package/dist/{native-modules-PwnGltOU.d.ts → native-modules-Cl3zw3kh.d.ts} +27 -1
  34. package/package.json +78 -4
  35. package/src/__tests__/doctor-rules.test.ts +60 -0
  36. package/src/__tests__/native-modules-lists.test.tsx +1 -0
  37. package/src/components/__tests__/composer-bar.test.tsx +55 -0
  38. package/src/components/__tests__/gallery-card.test.tsx +59 -0
  39. package/src/components/__tests__/gallery-grid.test.tsx +160 -0
  40. package/src/components/__tests__/lightbox-resnap.test.tsx +50 -0
  41. package/src/components/__tests__/lightbox-zoom.test.ts +33 -0
  42. package/src/components/__tests__/lightbox.test.tsx +141 -0
  43. package/src/components/__tests__/live-indicator.test.tsx +72 -0
  44. package/src/components/__tests__/rating.test.tsx +82 -0
  45. package/src/components/__tests__/thread-list.test.tsx +156 -0
  46. package/src/components/composer-bar.tsx +198 -0
  47. package/src/components/drawer.tsx +15 -9
  48. package/src/components/gallery-card.tsx +109 -0
  49. package/src/components/gallery-grid.tsx +285 -0
  50. package/src/components/gallery-layout.ts +2 -0
  51. package/src/components/gallery-types.ts +2 -0
  52. package/src/components/index.ts +44 -0
  53. package/src/components/lightbox-zoom.tsx +282 -0
  54. package/src/components/lightbox.tsx +342 -0
  55. package/src/components/live-indicator.tsx +139 -0
  56. package/src/components/rating.tsx +128 -0
  57. package/src/components/skeleton.tsx +9 -4
  58. package/src/components/thread-list.tsx +427 -0
  59. package/src/components/toast.tsx +7 -4
  60. package/src/data/NativeConflictSheet.tsx +181 -0
  61. package/src/data/NativeResourceForm.tsx +15 -1
  62. package/src/data/index.ts +2 -0
  63. package/src/data/resource-view/NativeLinkPicker.tsx +1 -1
  64. package/src/data/resource-view/NativeRecordView.tsx +85 -8
  65. package/src/data/resource-view/NativeResourceView.tsx +79 -32
  66. package/src/data/resource-view/__tests__/native-link-picker.test.tsx +16 -0
  67. package/src/data/resource-view/__tests__/native-record-view.test.tsx +95 -1
  68. package/src/data/resource-view/__tests__/native-resource-gallery.test.tsx +125 -0
  69. package/src/data/resource-view/mapping.ts +3 -0
  70. package/src/hooks/__tests__/use-motion.test.tsx +106 -0
  71. package/src/hooks/__tests__/use-thread-jump.test.tsx +88 -0
  72. package/src/hooks/index.ts +3 -0
  73. package/src/hooks/use-motion.ts +128 -0
  74. package/src/hooks/use-thread-jump.ts +110 -0
  75. package/src/icon-names.ts +6 -0
  76. package/src/icons/lucide.ts +12 -0
  77. package/src/layouts/__tests__/inbox-page.test.tsx +6 -0
  78. package/src/layouts/__tests__/thread-page.test.tsx +114 -14
  79. package/src/layouts/inbox-page.tsx +6 -55
  80. package/src/layouts/index.ts +2 -0
  81. package/src/layouts/thread-page.tsx +326 -262
  82. package/src/native-modules.tsx +16 -2
  83. package/dist/chunk-6KWEAGRD.js.map +0 -1
  84. package/dist/chunk-7PCWEY6R.js +0 -27
  85. package/dist/chunk-7PCWEY6R.js.map +0 -1
  86. package/dist/chunk-ABVSSJNT.js.map +0 -1
  87. package/dist/chunk-D5RKSPMH.js.map +0 -1
  88. package/dist/chunk-GHYUNNNU.js.map +0 -1
  89. package/dist/chunk-LBVD7XXL.js.map +0 -1
  90. package/dist/chunk-ONH2PCGH.js.map +0 -1
  91. package/dist/chunk-Z3AQYVUA.js +0 -36
  92. package/dist/chunk-Z3AQYVUA.js.map +0 -1
@@ -1,95 +1,87 @@
1
1
  /**
2
2
  * ThreadPage — one conversation: messages oldest first, a composer at the
3
- * bottom that stays above the keyboard, attachments, a read receipt, and
4
- * "load earlier" paging.
3
+ * bottom that follows the keyboard, attachments, a read receipt, earlier
4
+ * pages, going to a message, and the first unread.
5
5
  *
6
- * Generic: a support ticket with staff replies, a direct message, a comment
7
- * thread. Mine are on the trailing side in the tint, theirs on the leading
8
- * side on `surface`, over the `groupedBackground` page (SettingsPage's iOS
9
- * palette).
6
+ * Generic: a support ticket with staff replies, a help desk thread with
7
+ * internal notes, a direct message, a comment thread. Mine are on the
8
+ * trailing side in the tint, theirs on the leading side on `surface`, over the
9
+ * `groupedBackground` page (SettingsPage's iOS palette). Events ("Sam was
10
+ * assigned") are centred, muted lines between them.
10
11
  *
11
- * Keyboard: with `KeyboardScrollProvider` (the native app shell's
12
- * `keyboardScroll`, react-native-keyboard-controller's
13
- * `KeyboardAwareScrollView`), the messages and the composer scroll together
14
- * and the composer is kept above the keyboard. Without it, a
15
- * `KeyboardAvoidingView` does the same on iOS. Either way the thread stays
16
- * scrolled to the newest message as it grows.
12
+ * Built from the messaging atoms, so an app can take this whole or build its
13
+ * own from the same parts: `NativeThreadList` (bottom-anchored, holds its
14
+ * place while earlier messages load, follows the newest only at the bottom)
15
+ * and `NativeComposerBar` (grows as you type, modes with a tone, 44pt send).
16
+ *
17
+ * Keyboard: the page sits in `GenesisModules.keyboardAvoidingView`
18
+ * (react-native-keyboard-controller's, `translate-with-padding`: the composer
19
+ * follows the keyboard frame by frame) when the app passed it, else React
20
+ * Native's `KeyboardAvoidingView` (`padding` on iOS, `height` on Android, so
21
+ * under edge-to-edge the composer still clears the keyboard). Dragging the
22
+ * list down dismisses the keyboard (interactively on iOS). The app's
23
+ * `KeyboardScrollProvider` no longer applies here, since a virtualized list
24
+ * can't live in a scroll view: an app that passes it but not
25
+ * `keyboardAvoidingView` gets a development warning saying so.
26
+ *
27
+ * Data: rows arrive as props and the page never fetches. A row's send state
28
+ * is data (`sending`, `pending`, `paused`, `failed`, `conflict`) with
29
+ * `onRetry` / `onDiscard`, whatever feeds it (an outbox, an offline queue).
30
+ * Ids must stay the same across refreshes (a cache, then the server).
17
31
  *
18
32
  * Read-only: pass `composer={null}` (a closed ticket, a viewer) and an
19
33
  * optional `closedNotice`.
20
34
  *
21
- * Slots: `message`, `attachment`, `readReceipt`, `composer`,
22
- * `composerAccessory` (beside the input: attach a screenshot), `header`
23
- * (above the messages: what the thread is about), `earlier` and `empty`.
35
+ * `loading` shows a skeleton instead of the empty state; `updating` a quiet
36
+ * line while fresher rows arrive.
37
+ *
38
+ * Slots: `message`, `event`, `attachment`, `readReceipt`, `unreadDivider`,
39
+ * `composer`, `composerAccessory` (beside the input: attach a screenshot),
40
+ * `headerRegion` (pinned above the list: a ticket's status and assignee),
41
+ * `header` (scrolls above the first message: what the thread is about),
42
+ * `earlier` and `empty`.
24
43
  *
25
44
  * @stability Beta
26
45
  */
27
- import { createElement, useEffect, useRef, type ReactNode } from "react";
46
+ import { memo, useCallback, useEffect, useMemo, useRef, useState, type ComponentType, type ReactNode } from "react";
28
47
  import { renderSlot, type SlotPropsFor, type Slots } from "@marktiderman/genesis-core/slots";
29
- import { ActivityIndicator, Image, KeyboardAvoidingView, Platform, Pressable, ScrollView, TextInput, View } from "react-native";
48
+ import { AccessibilityInfo, Image, KeyboardAvoidingView, Platform, Pressable, View } from "react-native";
30
49
  import { SafeAreaView } from "react-native-safe-area-context";
31
50
  import { useThemeRoles } from "../hooks/use-theme-roles";
32
- import { NativeButton } from "../components/button";
51
+ import { useGenesisModules } from "../native-modules";
52
+ import { useKeyboardScroll } from "./keyboard";
33
53
  import { NativeEmptyState } from "../components/empty-state";
34
54
  import { NativeText } from "../components/text";
35
- import { composeHeaderOptions, type ScreenHeaderProps } from "./screen-container";
36
- import { useKeyboardScroll } from "./keyboard";
37
-
38
- export interface ThreadAttachment {
39
- id: string;
40
- /** An image URI (a signed URL, a file:// path). */
41
- uri: string;
42
- width?: number;
43
- height?: number;
44
- accessibilityLabel?: string;
45
- onPress?: () => void;
46
- }
47
-
48
- export interface ThreadMessage {
49
- id: string;
50
- body: string;
51
- /** Sent by the person using the app: trailing side, tint. */
52
- mine: boolean;
53
- /** Shown over theirs ("Support"); never over mine. */
54
- authorName?: string;
55
- timestamp: string | Date;
56
- /** An optimistic message: `sending`, then `sent`, or `failed` with `onRetry`. */
57
- status?: "sending" | "sent" | "failed";
58
- onRetry?: () => void;
59
- attachments?: ThreadAttachment[];
60
- }
55
+ import { NativeComposerBar, type ComposerMode } from "../components/composer-bar";
56
+ import { NativeThreadList, type NativeThreadListHandle, type ThreadRowInfo } from "../components/thread-list";
57
+ import {
58
+ DEFAULT_THREAD_LABELS,
59
+ formatMessageTime,
60
+ readReceiptFor,
61
+ type ThreadAttachment,
62
+ type ThreadComposer,
63
+ type ThreadLabels,
64
+ type ThreadMessage,
65
+ } from "@marktiderman/genesis-core/conversation";
61
66
 
62
- export interface ThreadComposer {
63
- value: string;
64
- onChangeText: (text: string) => void;
65
- onSend: () => void;
66
- /** A send is in flight: the button spins and won't send twice. */
67
- sending?: boolean;
68
- placeholder?: string;
69
- sendLabel?: string;
70
- /** Turns the send button off (besides an empty or blank value). */
71
- disabled?: boolean;
72
- maxLength?: number;
73
- }
74
-
75
- /** What the default parts say. */
76
- export interface ThreadLabels {
77
- /** Who "mine" is, to VoiceOver. */
78
- you: string;
79
- sending: string;
80
- notSent: string;
81
- retryHint: string;
82
- }
67
+ const announce = (text: string) => (AccessibilityInfo as { announceForAccessibility?: (t: string) => void }).announceForAccessibility?.(text);
68
+ import { composeHeaderOptions, type ScreenHeaderProps } from "./screen-container";
83
69
 
84
- const DEFAULT_LABELS: ThreadLabels = { you: "You", sending: "Sending", notSent: "Not sent. Tap to retry.", retryHint: "Double tap to send again" };
70
+ // The conversation contract is genesis-core's, shared with genesis-ui's ThreadPage.
71
+ export type { MessageTone, ThreadAttachment, ThreadComposer, ThreadLabels, ThreadMessage, ThreadMessageStatus } from "@marktiderman/genesis-core/conversation";
72
+ export { readReceiptFor } from "@marktiderman/genesis-core/conversation";
85
73
 
86
74
  export type ThreadPageSlotMap = {
87
- message: ThreadMessage & { testID: string; timeLabel: string; showReadReceipt: boolean; labels: ThreadLabels };
75
+ message: ThreadMessage & { testID: string; timeLabel: string; showReadReceipt: boolean; labels: ThreadLabels; /** A jump just landed here (two seconds). */ highlighted: boolean };
76
+ event: ThreadMessage & { testID: string; timeLabel: string };
88
77
  attachment: ThreadAttachment & { testID: string; mine: boolean };
89
78
  readReceipt: { label: string; testID: string };
79
+ unreadDivider: { label: string; testID: string };
90
80
  /** `accessory` is the `composerAccessory` slot, already rendered. */
91
81
  composer: ThreadComposer & { testID: string; accessory?: ReactNode };
92
82
  composerAccessory: { testID: string };
83
+ /** Pinned above the list: a ticket's status, assignee and priority, and "more". */
84
+ headerRegion: { testID: string };
93
85
  header: { testID: string };
94
86
  earlier: { loading: boolean; onPress: () => void; label: string; testID: string };
95
87
  empty: { title: string; body: string; testID: string };
@@ -102,7 +94,7 @@ export interface ThreadPageProps extends ScreenHeaderProps {
102
94
  composer: ThreadComposer | null;
103
95
  /** Shown instead of the composer when it is `null` ("This conversation is closed"). */
104
96
  closedNotice?: string;
105
- /** Above the messages (what the thread is about). Prefer `slots.header` for a component. */
97
+ /** Above the first message, scrolling with them. Prefer `slots.header` for a component. */
106
98
  header?: ReactNode;
107
99
  /**
108
100
  * When the other side last read the thread. My newest message sent before
@@ -110,11 +102,29 @@ export interface ThreadPageProps extends ScreenHeaderProps {
110
102
  */
111
103
  readAt?: string | Date | null;
112
104
  readLabel?: string;
113
- /** There are older messages: shows a "load earlier" button at the top. */
105
+ /** There are older messages: they load near the top, and a "load earlier" button shows. */
114
106
  hasEarlier?: boolean;
115
107
  onLoadEarlier?: () => void;
116
108
  loadingEarlier?: boolean;
117
109
  earlierLabel?: string;
110
+ /** The first message the reader hasn't seen: a "New messages" divider above it. */
111
+ firstUnreadId?: string | null;
112
+ /** Go to this message (a search result, a pinned one), loading with `onLoadUntil` until it's here. */
113
+ jumpTo?: string | null;
114
+ /** Load earlier pages until the message is held; resolve false when it can't be found. */
115
+ onLoadUntil?: (id: string) => Promise<boolean>;
116
+ /** A `jumpTo` message couldn't be found (the list goes back to the newest). Default: VoiceOver says `labels.messageGone`. */
117
+ onJumpFailed?: (id: string) => void;
118
+ /** The first page is loading: a skeleton instead of the empty state, and a jump waits for it. */
119
+ loading?: boolean;
120
+ /** Fresher rows are on their way (the cache showed first): a quiet "Updating…" line under the header region, announced once. */
121
+ updating?: boolean;
122
+ /**
123
+ * For the keyboard. With the keyboard-controller module the page finds its own place on screen
124
+ * (`automaticOffset`) and this is extra space (default 0); with React Native's, it's where the
125
+ * page starts below the window's top (default 100 on iOS: a standard header).
126
+ */
127
+ keyboardVerticalOffset?: number;
118
128
  emptyTitle?: string;
119
129
  emptyBody?: string;
120
130
  formatTime?: (date: Date) => string;
@@ -140,23 +150,25 @@ export function getThreadPageStackOptions(props: ScreenHeaderProps & { headerTit
140
150
 
141
151
  const toDate = (value: string | Date) => (typeof value === "string" ? new Date(value) : value);
142
152
 
143
- function defaultTime(date: Date): string {
144
- return date.toLocaleString(undefined, { month: "short", day: "numeric", hour: "numeric", minute: "2-digit" });
145
- }
146
153
 
147
- /** The newest of my messages the other side has read, or null. */
148
- export function readReceiptFor(messages: readonly ThreadMessage[], readAt: string | Date | null | undefined): string | null {
149
- if (!readAt) return null;
150
- const read = toDate(readAt).getTime();
151
- for (let i = messages.length - 1; i >= 0; i--) {
152
- const m = messages[i]!;
153
- if (m.mine && m.status !== "sending" && m.status !== "failed" && toDate(m.timestamp).getTime() <= read) return m.id;
154
- }
155
- return null;
156
- }
157
154
 
158
- interface ScrollLike {
159
- scrollToEnd?: (options?: { animated?: boolean }) => void;
155
+ const NO_SLOTS: Slots<ThreadPageSlotMap> = {};
156
+ const NO_SLOT_PROPS: SlotPropsFor<ThreadPageSlotMap> = {};
157
+ const messageKey = (m: ThreadMessage) => m.id;
158
+ const messageType = (m: ThreadMessage) => (m.kind === "event" ? "event" : m.mine ? "mine" : "theirs");
159
+
160
+ declare const __DEV__: boolean | undefined;
161
+ declare const process: { env: Record<string, string | undefined> } | undefined;
162
+ let warnedNoKeyboardModule = false;
163
+ /** Development-only: the app has keyboard-controller (it passed a keyboard scroll view) but not its KeyboardAvoidingView. */
164
+ function warnNoKeyboardModule() {
165
+ const dev = typeof __DEV__ !== "undefined" ? __DEV__ : typeof process !== "undefined" && process.env.NODE_ENV !== "production";
166
+ if (!dev || warnedNoKeyboardModule) return;
167
+ warnedNoKeyboardModule = true;
168
+ // genesis-allow-console: a development-only setup warning, like AuthScreen's
169
+ console.warn(
170
+ "[genesis] ThreadPage: react-native-keyboard-controller is set up (keyboardScroll) but GenesisModules.keyboardAvoidingView isn't, so the composer falls back to React Native's KeyboardAvoidingView. Pass keyboard-controller's KeyboardAvoidingView as `keyboardAvoidingView` in your Genesis modules.",
171
+ );
160
172
  }
161
173
 
162
174
  export function ThreadPage({
@@ -170,88 +182,105 @@ export function ThreadPage({
170
182
  onLoadEarlier,
171
183
  loadingEarlier = false,
172
184
  earlierLabel = "Load earlier messages",
185
+ firstUnreadId,
186
+ jumpTo,
187
+ onLoadUntil,
188
+ onJumpFailed,
189
+ loading = false,
190
+ updating = false,
191
+ keyboardVerticalOffset,
173
192
  emptyTitle,
174
193
  emptyBody,
175
- formatTime = defaultTime,
194
+ formatTime = formatMessageTime,
176
195
  labels: labelOverrides,
177
- slots = {},
178
- slotProps = {},
196
+ slots = NO_SLOTS,
197
+ slotProps = NO_SLOT_PROPS,
179
198
  testID = "thread-page",
180
199
  }: ThreadPageProps) {
181
200
  const colors = useThemeRoles();
182
- const KeyboardScroll = useKeyboardScroll();
183
- const scrollRef = useRef<ScrollLike | null>(null);
201
+ const genesis = useGenesisModules();
202
+ const listRef = useRef<NativeThreadListHandle | null>(null);
203
+ const [atBottom, setAtBottom] = useState(true);
184
204
  const receiptFor = readReceiptFor(messages, readAt);
185
- const labels: ThreadLabels = { ...DEFAULT_LABELS, ...labelOverrides };
186
- const newest = messages[messages.length - 1]?.id;
205
+ // Kept stable across renders (typing in the composer) unless the words change, so rows don't re-render.
206
+ const labelsKey = labelOverrides ? JSON.stringify(labelOverrides) : "";
207
+ // eslint-disable-next-line react-hooks/exhaustive-deps
208
+ const labels: ThreadLabels = useMemo(() => ({ ...DEFAULT_THREAD_LABELS, ...labelOverrides }), [labelsKey]);
209
+ const keyboardScroll = useKeyboardScroll();
210
+ useEffect(() => {
211
+ if (keyboardScroll && !genesis.keyboardAvoidingView) warnNoKeyboardModule();
212
+ }, [keyboardScroll, genesis.keyboardAvoidingView]);
187
213
 
188
- // Keep the newest message in view when one arrives (or is sent).
214
+ // "Updating…" on iOS too (a live region is Android-only).
189
215
  useEffect(() => {
190
- scrollRef.current?.scrollToEnd?.({ animated: true });
191
- }, [newest]);
216
+ if (updating && Platform.OS === "ios") announce(labels.updating);
217
+ // labels.updating is read when updating turns on.
218
+ // eslint-disable-next-line react-hooks/exhaustive-deps
219
+ }, [updating]);
192
220
 
193
- const body = (
194
- <View style={{ flexGrow: 1 }}>
195
- {header ?? renderSlot(Nothing, slots.header, { testID: `${testID}-header` }, slotProps.header)}
196
- {hasEarlier && onLoadEarlier
197
- ? renderSlot(
198
- DefaultEarlier,
199
- slots.earlier,
200
- { loading: loadingEarlier, onPress: onLoadEarlier, label: earlierLabel, testID: `${testID}-earlier` },
201
- slotProps.earlier,
202
- )
203
- : null}
204
- <View testID={`${testID}-messages`} accessibilityRole="list" style={{ flexGrow: 1, justifyContent: "flex-end", paddingHorizontal: 12, paddingVertical: 8, gap: 6 }}>
205
- {messages.length === 0
206
- ? renderSlot(
207
- DefaultEmpty,
208
- slots.empty,
209
- { title: emptyTitle ?? "No messages yet", body: emptyBody ?? "Write the first one below.", testID: `${testID}-empty` },
210
- slotProps.empty,
211
- )
212
- : messages.map((message) => (
213
- <View key={message.id}>
214
- {renderSlot(
215
- DefaultMessage,
216
- slots.message,
217
- {
218
- ...message,
219
- testID: `${testID}-message-${message.id}`,
220
- timeLabel: formatTime(toDate(message.timestamp)),
221
- showReadReceipt: message.id === receiptFor,
222
- labels,
223
- },
224
- slotProps.message,
225
- )}
226
- {message.attachments?.length ? (
227
- <View style={{ flexDirection: "row", flexWrap: "wrap", gap: 6, justifyContent: message.mine ? "flex-end" : "flex-start", marginTop: 4 }}>
228
- {message.attachments.map((attachment) => (
229
- <View key={attachment.id}>
230
- {renderSlot(
231
- DefaultAttachment,
232
- slots.attachment,
233
- { ...attachment, mine: message.mine, testID: `${testID}-attachment-${attachment.id}` },
234
- slotProps.attachment,
235
- )}
236
- </View>
237
- ))}
238
- </View>
239
- ) : null}
240
- {message.id === receiptFor
241
- ? renderSlot(DefaultReadReceipt, slots.readReceipt, { label: readLabel, testID: `${testID}-read-receipt` }, slotProps.readReceipt)
242
- : null}
243
- </View>
244
- ))}
245
- </View>
246
- </View>
221
+ const renderRow = useCallback(
222
+ (message: ThreadMessage, { highlighted }: ThreadRowInfo) => {
223
+ const timeLabel = formatTime(toDate(message.timestamp));
224
+ const divider =
225
+ message.id === firstUnreadId
226
+ ? renderSlot(DefaultUnreadDivider, slots.unreadDivider, { label: labels.newMessages, testID: `${testID}-unread` }, slotProps.unreadDivider)
227
+ : null;
228
+ if (message.kind === "event") {
229
+ return (
230
+ <View style={{ paddingVertical: 3 }}>
231
+ {divider}
232
+ {renderSlot(DefaultEvent, slots.event, { ...message, testID: `${testID}-event-${message.id}`, timeLabel }, slotProps.event)}
233
+ </View>
234
+ );
235
+ }
236
+ return (
237
+ <View style={{ paddingVertical: 3 }}>
238
+ {divider}
239
+ {renderSlot(
240
+ DefaultMessage,
241
+ slots.message,
242
+ { ...message, testID: `${testID}-message-${message.id}`, timeLabel, showReadReceipt: message.id === receiptFor, labels, highlighted },
243
+ slotProps.message,
244
+ )}
245
+ {message.attachments?.length ? (
246
+ <View style={{ flexDirection: "row", flexWrap: "wrap", gap: 6, justifyContent: message.mine ? "flex-end" : "flex-start", marginTop: 4 }}>
247
+ {message.attachments.map((attachment) => (
248
+ <View key={attachment.id}>
249
+ {renderSlot(
250
+ DefaultAttachment,
251
+ slots.attachment,
252
+ { ...attachment, mine: message.mine, testID: `${testID}-attachment-${attachment.id}` },
253
+ slotProps.attachment,
254
+ )}
255
+ </View>
256
+ ))}
257
+ </View>
258
+ ) : null}
259
+ {message.id === receiptFor
260
+ ? renderSlot(DefaultReadReceipt, slots.readReceipt, { label: readLabel, testID: `${testID}-read-receipt` }, slotProps.readReceipt)
261
+ : null}
262
+ </View>
263
+ );
264
+ },
265
+ [firstUnreadId, formatTime, receiptFor, readLabel, slots, slotProps, testID, labels],
247
266
  );
248
267
 
268
+ const send = composer
269
+ ? () => {
270
+ composer.onSend();
271
+ // What you send is what you look at next, wherever the list was.
272
+ listRef.current?.scrollToEnd({ animated: true });
273
+ }
274
+ : undefined;
275
+
249
276
  const footer = composer ? (
250
277
  renderSlot(
251
278
  DefaultComposer,
252
279
  slots.composer,
253
280
  {
254
281
  ...composer,
282
+ sendingLabel: composer.sendingLabel ?? labels.sending,
283
+ onSend: send!,
255
284
  testID: `${testID}-composer`,
256
285
  accessory: renderSlot(Nothing, slots.composerAccessory, { testID: `${testID}-composer-accessory` }, slotProps.composerAccessory),
257
286
  },
@@ -263,45 +292,66 @@ export function ThreadPage({
263
292
  </NativeText>
264
293
  ) : null;
265
294
 
266
- const page = { flex: 1, backgroundColor: colors.groupedBackground };
267
- const onContentSizeChange = () => scrollRef.current?.scrollToEnd?.({ animated: false });
295
+ const listHeader = header ?? renderSlot(Nothing, slots.header, { testID: `${testID}-header` }, slotProps.header);
296
+
297
+ const KeyboardAvoiding = (genesis.keyboardAvoidingView ?? KeyboardAvoidingView) as ComponentType<Record<string, unknown>>;
298
+ const keyboardProps = genesis.keyboardAvoidingView
299
+ ? { behavior: "translate-with-padding", automaticOffset: true, keyboardVerticalOffset: keyboardVerticalOffset ?? 0 }
300
+ : { behavior: Platform.OS === "ios" ? "padding" : "height", keyboardVerticalOffset: keyboardVerticalOffset ?? (Platform.OS === "ios" ? 100 : 0) };
268
301
 
269
- if (KeyboardScroll) {
270
- // The composer scrolls with the messages, so the keyboard-aware view keeps it (the focused input) above the keyboard.
271
- return (
272
- <SafeAreaView testID={testID} edges={["left", "right", "bottom"]} style={page}>
273
- {createElement(
274
- KeyboardScroll as never,
275
- {
276
- ref: scrollRef,
277
- testID: `${testID}-keyboard`,
278
- keyboardShouldPersistTaps: "handled",
279
- contentInsetAdjustmentBehavior: "automatic",
280
- contentContainerStyle: { flexGrow: 1 },
281
- bottomOffset: 8,
282
- onContentSizeChange,
283
- } as never,
284
- body,
285
- footer,
286
- )}
287
- </SafeAreaView>
288
- );
289
- }
290
302
  return (
291
- <SafeAreaView testID={testID} edges={["left", "right", "bottom"]} style={page}>
292
- <KeyboardAvoidingView testID={`${testID}-kav`} style={{ flex: 1 }} behavior={Platform.OS === "ios" ? "padding" : undefined} keyboardVerticalOffset={Platform.OS === "ios" ? 100 : 0}>
293
- <ScrollView
294
- ref={scrollRef as never}
295
- testID={`${testID}-scroll`}
296
- keyboardShouldPersistTaps="handled"
297
- contentInsetAdjustmentBehavior="automatic"
298
- contentContainerStyle={{ flexGrow: 1 }}
299
- onContentSizeChange={onContentSizeChange}
300
- >
301
- {body}
302
- </ScrollView>
303
+ <SafeAreaView testID={testID} edges={["left", "right", "bottom"]} style={{ flex: 1, backgroundColor: colors.groupedBackground }}>
304
+ {/* Pinned above the keyboard-avoiding view, so it doesn't move with the keyboard. */}
305
+ {renderSlot(Nothing, slots.headerRegion, { testID: `${testID}-header-region` }, slotProps.headerRegion)}
306
+ {updating ? (
307
+ <View testID={`${testID}-updating`} accessibilityLiveRegion="polite" accessible accessibilityLabel={labels.updating}>
308
+ <NativeText style={{ textAlign: "center", paddingVertical: 4, color: colors.tertiaryLabel, fontSize: 12, lineHeight: 16 }}>{labels.updating}</NativeText>
309
+ </View>
310
+ ) : null}
311
+ <KeyboardAvoiding testID={`${testID}-keyboard`} style={{ flex: 1 }} {...keyboardProps}>
312
+ <View style={{ flex: 1 }}>
313
+ <NativeThreadList<ThreadMessage>
314
+ ref={listRef}
315
+ testID={`${testID}-messages`}
316
+ data={messages}
317
+ keyExtractor={messageKey}
318
+ getItemType={messageType}
319
+ renderItem={renderRow}
320
+ loading={loading}
321
+ hasEarlier={hasEarlier}
322
+ onLoadEarlier={onLoadEarlier}
323
+ loadingEarlier={loadingEarlier}
324
+ earlierLabel={earlierLabel}
325
+ renderEarlier={(p) => renderSlot(DefaultEarlierButton, slots.earlier, { ...p, testID: `${testID}-earlier` }, slotProps.earlier)}
326
+ jumpTo={jumpTo}
327
+ onLoadUntil={onLoadUntil}
328
+ jumpAnnouncement={(m) => [m.mine ? labels.you : m.authorName, m.body].filter(Boolean).join(", ")}
329
+ onJumpFailed={onJumpFailed ?? (() => announce(labels.messageGone))}
330
+ onAtBottomChange={setAtBottom}
331
+ header={listHeader}
332
+ empty={renderSlot(
333
+ DefaultEmpty,
334
+ slots.empty,
335
+ { title: emptyTitle ?? "No messages yet", body: emptyBody ?? "Write the first one below.", testID: `${testID}-empty` },
336
+ slotProps.empty,
337
+ )}
338
+ />
339
+ {!atBottom && messages.length > 0 ? (
340
+ <Pressable
341
+ testID={`${testID}-jump-latest`}
342
+ accessibilityRole="button"
343
+ accessibilityLabel={labels.jumpToLatest}
344
+ onPress={() => listRef.current?.scrollToEnd({ animated: true })}
345
+ style={{ position: "absolute", bottom: 8, alignSelf: "center", minHeight: 44, justifyContent: "center", paddingHorizontal: 16, borderRadius: 22, backgroundColor: colors.surface }}
346
+ >
347
+ <NativeText preset="label" style={{ color: colors.tint }}>
348
+ {labels.jumpToLatest}
349
+ </NativeText>
350
+ </Pressable>
351
+ ) : null}
352
+ </View>
303
353
  {footer}
304
- </KeyboardAvoidingView>
354
+ </KeyboardAvoiding>
305
355
  </SafeAreaView>
306
356
  );
307
357
  }
@@ -310,13 +360,14 @@ function Nothing(_props: { testID: string }) {
310
360
  return null;
311
361
  }
312
362
 
313
- function DefaultEarlier({ loading, onPress, label, testID }: ThreadPageSlotMap["earlier"]) {
363
+ function DefaultEarlierButton({ loading, onPress, label, testID }: ThreadPageSlotMap["earlier"]) {
364
+ const colors = useThemeRoles();
314
365
  return (
315
- <View style={{ alignItems: "center", paddingTop: 8 }}>
316
- <NativeButton variant="ghost" size="sm" onPress={onPress} loading={loading} testID={testID}>
366
+ <Pressable testID={testID} accessibilityRole="button" accessibilityLabel={label} accessibilityState={{ busy: loading }} onPress={onPress} disabled={loading} style={{ minHeight: 44, alignItems: "center", justifyContent: "center" }}>
367
+ <NativeText preset="label" style={{ color: colors.tint }}>
317
368
  {label}
318
- </NativeButton>
319
- </View>
369
+ </NativeText>
370
+ </Pressable>
320
371
  );
321
372
  }
322
373
 
@@ -324,16 +375,62 @@ function DefaultEmpty({ title, body, testID }: ThreadPageSlotMap["empty"]) {
324
375
  return <NativeEmptyState variant="first-run" title={title} body={body} testID={testID} />;
325
376
  }
326
377
 
378
+ function DefaultUnreadDivider({ label, testID }: ThreadPageSlotMap["unreadDivider"]) {
379
+ const colors = useThemeRoles();
380
+ return (
381
+ <View testID={testID} accessible accessibilityRole="header" accessibilityLabel={label} style={{ flexDirection: "row", alignItems: "center", gap: 8, paddingVertical: 8 }}>
382
+ <View style={{ flex: 1, height: 1, backgroundColor: colors.tint }} />
383
+ <NativeText style={{ color: colors.tint, fontSize: 12, lineHeight: 16 }}>{label}</NativeText>
384
+ <View style={{ flex: 1, height: 1, backgroundColor: colors.tint }} />
385
+ </View>
386
+ );
387
+ }
388
+
389
+ function DefaultEvent({ body, timeLabel, testID }: ThreadPageSlotMap["event"]) {
390
+ const colors = useThemeRoles();
391
+ return (
392
+ <View testID={testID} accessible accessibilityLabel={`${body}, ${timeLabel}`} style={{ alignSelf: "center", paddingHorizontal: 12, paddingVertical: 4, borderRadius: 12, backgroundColor: colors.fill }}>
393
+ <NativeText style={{ color: colors.secondaryLabel, fontSize: 12, lineHeight: 16, textAlign: "center" }}>{body}</NativeText>
394
+ </View>
395
+ );
396
+ }
397
+
327
398
  // No `color` key on mine: an undefined color in the style would override the class's.
328
399
  const MINE_TEXT = { fontSize: 17, lineHeight: 22 } as const;
329
400
 
330
- function DefaultMessage(message: ThreadPageSlotMap["message"]) {
401
+ /** What a message's state says under it, or null when it's sent. */
402
+ function stateLabel(message: ThreadPageSlotMap["message"]): string | null {
403
+ const { labels } = message;
404
+ switch (message.status) {
405
+ case "sending":
406
+ return labels.sending;
407
+ case "pending":
408
+ return labels.pending;
409
+ case "paused":
410
+ return labels.paused;
411
+ case "failed":
412
+ return labels.notSent;
413
+ case "conflict":
414
+ return labels.conflict;
415
+ default:
416
+ return null;
417
+ }
418
+ }
419
+
420
+ const DefaultMessage = memo(function DefaultMessage(message: ThreadPageSlotMap["message"]) {
331
421
  const colors = useThemeRoles();
332
- const failed = message.status === "failed";
333
422
  const { labels } = message;
334
- const label = [message.mine ? labels.you : (message.authorName ?? ""), message.body, message.timeLabel, failed ? labels.notSent : message.status === "sending" ? labels.sending : null]
335
- .filter(Boolean)
336
- .join(", ");
423
+ const broken = message.status === "failed" || message.status === "conflict";
424
+ const waiting = message.status === "sending" || message.status === "pending" || message.status === "paused";
425
+ const internal = message.tone === "internal";
426
+ const state = stateLabel(message);
427
+ const label = [message.mine ? labels.you : (message.authorName ?? ""), internal ? labels.internal : null, message.body, message.timeLabel, state].filter(Boolean).join(", ");
428
+ // Discard is offered for a message that hasn't gone: not while it's sending (it may already be on the server).
429
+ const canDiscard = Boolean(message.onDiscard) && (broken || message.status === "pending" || message.status === "paused");
430
+ const actions = [
431
+ ...(broken && message.onRetry ? [{ name: "activate", label: labels.retryHint }] : []),
432
+ ...(canDiscard ? [{ name: "discard", label: labels.discard }] : []),
433
+ ];
337
434
  return (
338
435
  <View style={{ alignItems: message.mine ? "flex-end" : "flex-start" }}>
339
436
  {!message.mine && message.authorName ? (
@@ -342,38 +439,57 @@ function DefaultMessage(message: ThreadPageSlotMap["message"]) {
342
439
  <Pressable
343
440
  testID={message.testID}
344
441
  accessible
442
+ accessibilityRole={broken && message.onRetry ? "button" : undefined}
345
443
  accessibilityLabel={label}
346
- accessibilityHint={failed && message.onRetry ? labels.retryHint : undefined}
347
- onPress={failed ? message.onRetry : undefined}
444
+ accessibilityHint={broken && message.onRetry ? labels.retryHint : undefined}
445
+ accessibilityActions={actions.length ? actions : undefined}
446
+ onAccessibilityAction={(e) => {
447
+ if (e.nativeEvent.actionName === "discard" && canDiscard) message.onDiscard?.();
448
+ else if (e.nativeEvent.actionName === "activate" && broken) message.onRetry?.();
449
+ }}
450
+ onPress={broken ? message.onRetry : undefined}
348
451
  style={{
349
452
  maxWidth: "80%",
350
453
  paddingHorizontal: 14,
351
454
  paddingVertical: 8,
352
455
  borderRadius: 18,
353
- backgroundColor: message.mine ? colors.tint : colors.surface,
354
- opacity: message.status === "sending" ? 0.6 : 1,
456
+ backgroundColor: internal ? colors.surface : message.mine ? colors.tint : colors.surface,
457
+ borderWidth: internal || message.highlighted ? 2 : 0,
458
+ borderColor: internal ? colors.warning : colors.tint,
459
+ opacity: waiting ? 0.6 : 1,
355
460
  }}
356
461
  >
357
- <NativeText className={message.mine ? "text-primary-foreground" : undefined} style={message.mine ? MINE_TEXT : { ...MINE_TEXT, color: colors.label }}>
462
+ <NativeText className={message.mine && !internal ? "text-primary-foreground" : undefined} style={message.mine && !internal ? MINE_TEXT : { ...MINE_TEXT, color: colors.label }}>
358
463
  {message.body}
359
464
  </NativeText>
360
465
  </Pressable>
361
- <NativeText
362
- testID={`${message.testID}-time`}
363
- style={{ marginTop: 2, marginHorizontal: 12, color: failed ? colors.destructive : colors.tertiaryLabel, fontSize: 11, lineHeight: 14 }}
364
- >
365
- {failed ? labels.notSent : message.timeLabel}
366
- </NativeText>
466
+ <View style={{ flexDirection: "row", alignItems: "center", gap: 8, marginTop: 2, marginHorizontal: 12 }}>
467
+ <NativeText testID={`${message.testID}-time`} style={{ color: broken ? colors.destructive : colors.tertiaryLabel, fontSize: 11, lineHeight: 14 }}>
468
+ {state ?? message.timeLabel}
469
+ </NativeText>
470
+ {canDiscard ? (
471
+ <Pressable testID={`${message.testID}-discard`} accessibilityRole="button" accessibilityLabel={labels.discard} onPress={message.onDiscard} hitSlop={{ top: 15, bottom: 15, left: 12, right: 12 }}>
472
+ <NativeText style={{ color: colors.secondaryLabel, fontSize: 11, lineHeight: 14 }}>{labels.discard}</NativeText>
473
+ </Pressable>
474
+ ) : null}
475
+ </View>
367
476
  </View>
368
477
  );
369
- }
478
+ });
370
479
 
371
- function DefaultAttachment({ uri, width, height, accessibilityLabel, onPress, testID }: ThreadPageSlotMap["attachment"]) {
480
+ function DefaultAttachment({ id, uri, width, height, accessibilityLabel, onPress, testID }: ThreadPageSlotMap["attachment"]) {
481
+ const { image: ExpoImage } = useGenesisModules();
372
482
  const ratio = width && height ? width / height : 0.6;
373
483
  const w = 140;
484
+ const style = { width: w, height: Math.round(w / ratio), borderRadius: 12 };
374
485
  return (
375
486
  <Pressable testID={testID} accessibilityRole="image" accessibilityLabel={accessibilityLabel ?? "Attachment"} onPress={onPress} disabled={!onPress}>
376
- <Image source={{ uri }} style={{ width: w, height: Math.round(w / ratio), borderRadius: 12 }} resizeMode="cover" />
487
+ {/* A recycled row (FlashList) must not show the last row's picture while this one loads. */}
488
+ {ExpoImage ? (
489
+ <ExpoImage source={{ uri }} style={style} contentFit="cover" recyclingKey={id} />
490
+ ) : (
491
+ <Image key={uri} source={{ uri }} style={style} resizeMode="cover" />
492
+ )}
377
493
  </Pressable>
378
494
  );
379
495
  }
@@ -387,58 +503,6 @@ function DefaultReadReceipt({ label, testID }: ThreadPageSlotMap["readReceipt"])
387
503
  );
388
504
  }
389
505
 
390
- function DefaultComposer({
391
- value,
392
- onChangeText,
393
- onSend,
394
- sending = false,
395
- placeholder = "Message",
396
- sendLabel = "Send",
397
- disabled = false,
398
- maxLength,
399
- testID,
400
- accessory,
401
- }: ThreadPageSlotMap["composer"]) {
402
- const colors = useThemeRoles();
403
- const blank = !value.trim();
404
- return (
405
- <View
406
- testID={testID}
407
- style={{ flexDirection: "row", alignItems: "flex-end", gap: 8, paddingHorizontal: 12, paddingTop: 8, paddingBottom: 8, backgroundColor: colors.groupedBackground }}
408
- >
409
- {accessory}
410
- <TextInput
411
- testID={`${testID}-input`}
412
- value={value}
413
- onChangeText={onChangeText}
414
- placeholder={placeholder}
415
- placeholderTextColor={colors.tertiaryLabel as string}
416
- accessibilityLabel={placeholder}
417
- multiline
418
- maxLength={maxLength}
419
- editable={!sending}
420
- style={{
421
- flex: 1,
422
- minHeight: 36,
423
- maxHeight: 120,
424
- paddingHorizontal: 14,
425
- paddingTop: 8,
426
- paddingBottom: 8,
427
- borderRadius: 18,
428
- fontSize: 17,
429
- color: colors.label as string,
430
- backgroundColor: colors.surface,
431
- }}
432
- />
433
- {sending ? (
434
- <View style={{ height: 36, justifyContent: "center", paddingHorizontal: 8 }}>
435
- <ActivityIndicator testID={`${testID}-sending`} accessibilityLabel="Sending" />
436
- </View>
437
- ) : (
438
- <NativeButton size="sm" onPress={onSend} disabled={disabled || blank} testID={`${testID}-send`}>
439
- {sendLabel}
440
- </NativeButton>
441
- )}
442
- </View>
443
- );
506
+ function DefaultComposer({ accessory, testID, ...composer }: ThreadPageSlotMap["composer"]) {
507
+ return <NativeComposerBar {...composer} leading={accessory} testID={testID} />;
444
508
  }