@planningcenter/chat-react-native 4.3.0-rc.3 → 4.3.0-rc.4
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/build/components/conversation/message_form.d.ts +2 -1
- package/build/components/conversation/message_form.d.ts.map +1 -1
- package/build/components/conversation/message_form.js +167 -370
- package/build/components/conversation/message_form.js.map +1 -1
- package/build/components/conversation/message_list.d.ts +5 -2
- package/build/components/conversation/message_list.d.ts.map +1 -1
- package/build/components/conversation/message_list.js +187 -67
- package/build/components/conversation/message_list.js.map +1 -1
- package/build/components/conversation/missing_message.d.ts +2 -0
- package/build/components/conversation/missing_message.d.ts.map +1 -0
- package/build/components/conversation/missing_message.js +139 -0
- package/build/components/conversation/missing_message.js.map +1 -0
- package/build/hooks/groups/use_group_members_for_new_conversation.d.ts +4 -3
- package/build/hooks/groups/use_group_members_for_new_conversation.d.ts.map +1 -1
- package/build/hooks/use_conversation_messages.d.ts +14 -5
- package/build/hooks/use_conversation_messages.d.ts.map +1 -1
- package/build/hooks/use_conversation_messages.js +218 -42
- package/build/hooks/use_conversation_messages.js.map +1 -1
- package/build/hooks/use_conversation_messages_jolt_events.d.ts.map +1 -1
- package/build/hooks/use_conversation_messages_jolt_events.js +58 -42
- package/build/hooks/use_conversation_messages_jolt_events.js.map +1 -1
- package/build/hooks/use_mark_latest_message_read.d.ts +2 -1
- package/build/hooks/use_mark_latest_message_read.d.ts.map +1 -1
- package/build/hooks/use_mark_latest_message_read.js +4 -3
- package/build/hooks/use_mark_latest_message_read.js.map +1 -1
- package/build/hooks/use_read_receipts.d.ts +4 -3
- package/build/hooks/use_read_receipts.d.ts.map +1 -1
- package/build/hooks/use_suspense_api.d.ts +10 -6
- package/build/hooks/use_suspense_api.d.ts.map +1 -1
- package/build/hooks/use_suspense_api.js +83 -63
- package/build/hooks/use_suspense_api.js.map +1 -1
- package/build/screens/conversation_screen.d.ts +1 -0
- package/build/screens/conversation_screen.d.ts.map +1 -1
- package/build/screens/conversation_screen.js +305 -203
- package/build/screens/conversation_screen.js.map +1 -1
- package/build/screens/notification_settings/hooks/groups.d.ts +4 -3
- package/build/screens/notification_settings/hooks/groups.d.ts.map +1 -1
- package/build/types/api_primitives.d.ts +1 -0
- package/build/types/api_primitives.d.ts.map +1 -1
- package/build/types/api_primitives.js.map +1 -1
- package/build/utils/cache/messages_window.d.ts +11 -0
- package/build/utils/cache/messages_window.d.ts.map +1 -0
- package/build/utils/cache/messages_window.js +51 -0
- package/build/utils/cache/messages_window.js.map +1 -0
- package/build/utils/client/types.d.ts +1 -0
- package/build/utils/client/types.d.ts.map +1 -1
- package/build/utils/client/types.js.map +1 -1
- package/package.json +2 -2
- package/src/components/conversation/message_form.tsx +31 -3
- package/src/components/conversation/message_list.tsx +88 -4
- package/src/components/conversation/missing_message.tsx +66 -0
- package/src/hooks/__tests__/use_conversation_messages.test.tsx +333 -0
- package/src/hooks/__tests__/use_conversation_messages_jolt_events.test.tsx +87 -0
- package/src/hooks/__tests__/use_mark_latest_message_read.test.tsx +33 -0
- package/src/hooks/use_conversation_messages.ts +103 -17
- package/src/hooks/use_conversation_messages_jolt_events.ts +12 -2
- package/src/hooks/use_mark_latest_message_read.ts +4 -3
- package/src/hooks/use_suspense_api.ts +14 -6
- package/src/screens/conversation_screen.tsx +62 -13
- package/src/types/api_primitives.ts +1 -0
- package/src/utils/cache/__tests__/messages_window.test.ts +78 -0
- package/src/utils/cache/messages_window.ts +55 -0
- package/src/utils/client/types.ts +1 -0
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
updateCacheReplyRoots,
|
|
15
15
|
getThreadedMessagesQueryKey,
|
|
16
16
|
} from '../utils/cache/messages_cache'
|
|
17
|
+
import { isBehindLiveEdge, MessagesWindow } from '../utils/cache/messages_window'
|
|
17
18
|
import { transformMessageEventDataToMessageResource } from '../utils/jolt/transform_message_event_data_to_message_resource'
|
|
18
19
|
import { completeMessageCreationTracking } from '../utils/performance_tracking'
|
|
19
20
|
import { getMessageQueryKey } from '../utils/request/get_message'
|
|
@@ -39,12 +40,17 @@ export function useConversationMessagesJoltEvents({ conversationId }: Props) {
|
|
|
39
40
|
const { addTypingEvent, removeTypingEventById, removeAllTypingEventsByAuthorId } =
|
|
40
41
|
useTypingStatusCache(conversationId)
|
|
41
42
|
|
|
43
|
+
// While behind, a new message would land after a gap; the newer fetch brings it in.
|
|
44
|
+
const acceptsCreates = (queryKey: unknown[]) =>
|
|
45
|
+
!isBehindLiveEdge(queryClient.getQueryData<MessagesWindow>(queryKey))
|
|
46
|
+
|
|
42
47
|
const handleMessageUpdateOrCreate = async (e: MessageCreatedEvent | MessageUpdatedEvent) => {
|
|
43
48
|
const { data } = e.data
|
|
44
49
|
const message = transformMessageEventDataToMessageResource({
|
|
45
50
|
data,
|
|
46
51
|
currentPersonId: currentPerson.id,
|
|
47
52
|
})
|
|
53
|
+
const isCreate = e.event === 'message.created'
|
|
48
54
|
const isReplyRootUpdate = e.event === 'message.updated' && data.reply_root_id === data.sort_key
|
|
49
55
|
|
|
50
56
|
if (e.event === 'message.created' && data.author_id) {
|
|
@@ -55,7 +61,9 @@ export function useConversationMessagesJoltEvents({ conversationId }: Props) {
|
|
|
55
61
|
}
|
|
56
62
|
|
|
57
63
|
// Update the main conversation cache
|
|
58
|
-
|
|
64
|
+
if (!isCreate || acceptsCreates(messagesQueryKey)) {
|
|
65
|
+
updateCacheWithMessage(queryClient, messagesQueryKey, message, e.event)
|
|
66
|
+
}
|
|
59
67
|
|
|
60
68
|
// If message has a reply_root_id, also update the threaded cache
|
|
61
69
|
if (data.reply_root_id) {
|
|
@@ -63,7 +71,9 @@ export function useConversationMessagesJoltEvents({ conversationId }: Props) {
|
|
|
63
71
|
conversationId,
|
|
64
72
|
data.reply_root_id
|
|
65
73
|
)
|
|
66
|
-
|
|
74
|
+
if (!isCreate || acceptsCreates(threadedMessagesQueryKey)) {
|
|
75
|
+
updateCacheWithMessage(queryClient, threadedMessagesQueryKey, message, e.event)
|
|
76
|
+
}
|
|
67
77
|
|
|
68
78
|
if (isReplyRootUpdate) {
|
|
69
79
|
updateCacheReplyRoots(queryClient, messagesQueryKey, message)
|
|
@@ -7,9 +7,10 @@ import { useConversationsMarkRead } from './use_conversations_actions'
|
|
|
7
7
|
interface Props {
|
|
8
8
|
conversation: ConversationResource
|
|
9
9
|
messages: MessageResource[]
|
|
10
|
+
enabled?: boolean
|
|
10
11
|
}
|
|
11
12
|
|
|
12
|
-
export function useMarkLatestMessageRead({ conversation }: Props) {
|
|
13
|
+
export function useMarkLatestMessageRead({ conversation, enabled = true }: Props) {
|
|
13
14
|
const firedOnce = useRef<boolean>(false)
|
|
14
15
|
const { markRead } = useConversationsMarkRead({ conversation })
|
|
15
16
|
const debouncedMarkRead = useMemo(
|
|
@@ -24,10 +25,10 @@ export function useMarkLatestMessageRead({ conversation }: Props) {
|
|
|
24
25
|
const isActive = appState === 'active'
|
|
25
26
|
|
|
26
27
|
useEffect(() => {
|
|
27
|
-
if (!isActive || !shouldMarkRead) return
|
|
28
|
+
if (!enabled || !isActive || !shouldMarkRead) return
|
|
28
29
|
|
|
29
30
|
firedOnce.current = true
|
|
30
31
|
|
|
31
32
|
debouncedMarkRead(true)
|
|
32
|
-
}, [debouncedMarkRead, isActive, shouldMarkRead, unreadReaction])
|
|
33
|
+
}, [debouncedMarkRead, enabled, isActive, shouldMarkRead, unreadReaction])
|
|
33
34
|
}
|
|
@@ -47,17 +47,23 @@ export const useSuspenseGet = <T extends ResourceObject | ResourceObject[]>(
|
|
|
47
47
|
export type SuspensePaginatorOptions = Omit<
|
|
48
48
|
AnyUseSuspenseInfiniteQueryOptions,
|
|
49
49
|
'getNextPageParam' | 'initialPageParam' | 'queryFn' | 'queryKey'
|
|
50
|
-
>
|
|
50
|
+
> & {
|
|
51
|
+
initialPageParam?: Partial<RequestData>
|
|
52
|
+
transformPage?(
|
|
53
|
+
page: ApiCollection<ResourceObject>,
|
|
54
|
+
pageParam: Partial<RequestData> | undefined
|
|
55
|
+
): ApiCollection<ResourceObject>
|
|
56
|
+
}
|
|
51
57
|
|
|
52
58
|
export const useSuspensePaginator = <T extends ResourceObject>(
|
|
53
59
|
args: SuspenseGetOptions,
|
|
54
|
-
opts
|
|
60
|
+
{ transformPage, ...opts }: SuspensePaginatorOptions = {}
|
|
55
61
|
) => {
|
|
56
62
|
const apiClient = useApiClient()
|
|
57
63
|
const query = useSuspenseInfiniteQuery<
|
|
58
64
|
ApiCollection<T>,
|
|
59
65
|
Response,
|
|
60
|
-
InfiniteData<ApiCollection<T
|
|
66
|
+
InfiniteData<ApiCollection<T>, Partial<RequestData> | undefined>,
|
|
61
67
|
any,
|
|
62
68
|
Partial<RequestData> | undefined
|
|
63
69
|
>({
|
|
@@ -70,7 +76,8 @@ export const useSuspensePaginator = <T extends ResourceObject>(
|
|
|
70
76
|
const where = { ...argsWhere, ...pageParmWhere }
|
|
71
77
|
const app = args.app || 'chat'
|
|
72
78
|
const offset = pageParam?.offset || args.data.offset
|
|
73
|
-
const
|
|
79
|
+
const order = pageParam?.order || args.data.order
|
|
80
|
+
const data = { ...args.data, where, offset, ...(order && { order }) }
|
|
74
81
|
|
|
75
82
|
return apiClient[app]
|
|
76
83
|
.get<ApiCollection<T>>({
|
|
@@ -78,16 +85,17 @@ export const useSuspensePaginator = <T extends ResourceObject>(
|
|
|
78
85
|
data,
|
|
79
86
|
})
|
|
80
87
|
.catch(throwResponseError)
|
|
88
|
+
.then(page => (transformPage ? (transformPage(page, pageParam) as ApiCollection<T>) : page))
|
|
81
89
|
},
|
|
82
90
|
initialPageParam: {} as Partial<RequestData>,
|
|
83
91
|
getNextPageParam: lastPage => getNextPageParamFromMeta(lastPage.meta?.next),
|
|
84
|
-
...
|
|
92
|
+
...opts,
|
|
85
93
|
})
|
|
86
94
|
|
|
87
95
|
const data: T[] = query.data?.pages.flatMap(page => page.data) || []
|
|
88
96
|
const totalCount = query.data?.pages[0]?.meta?.totalCount || data.length
|
|
89
97
|
|
|
90
|
-
return { ...query, data, totalCount }
|
|
98
|
+
return { ...query, data, totalCount, infiniteData: query.data }
|
|
91
99
|
}
|
|
92
100
|
|
|
93
101
|
export type RequestQueryKey = [
|
|
@@ -28,7 +28,10 @@ import { ConversationContextProvider } from '../contexts/conversation_context'
|
|
|
28
28
|
import { useMessageAvailableMembersAction } from '../hooks/direct_messages/use_message_available_members_action'
|
|
29
29
|
import { useConversation } from '../hooks/use_conversation'
|
|
30
30
|
import { useConversationJoltEvents } from '../hooks/use_conversation_jolt_events'
|
|
31
|
-
import {
|
|
31
|
+
import {
|
|
32
|
+
dropMessagesWindowIfBehind,
|
|
33
|
+
useConversationMessages,
|
|
34
|
+
} from '../hooks/use_conversation_messages'
|
|
32
35
|
import { useConversationMessagesJoltEvents } from '../hooks/use_conversation_messages_jolt_events'
|
|
33
36
|
import { useMarkLatestMessageRead } from '../hooks/use_mark_latest_message_read'
|
|
34
37
|
import {
|
|
@@ -41,10 +44,12 @@ import { useVideoModerationJoltEvents } from '../hooks/use_video_moderation_jolt
|
|
|
41
44
|
import { ConversationResource } from '../types/resources/conversation'
|
|
42
45
|
import { ConversationBadgeResource } from '../types/resources/conversation_badge'
|
|
43
46
|
import { MessageResource } from '../types/resources/message'
|
|
47
|
+
import { Log } from '../utils'
|
|
44
48
|
|
|
45
49
|
export type ConversationRouteProps = {
|
|
46
50
|
conversation_id: number
|
|
47
51
|
reply_root_id?: string | null
|
|
52
|
+
initial_message_id?: string | null
|
|
48
53
|
reply_root_author_name?: string
|
|
49
54
|
chat_group_graph_id?: string
|
|
50
55
|
clear_input?: boolean
|
|
@@ -81,18 +86,25 @@ export function ConversationScreen({ route }: ConversationScreenProps) {
|
|
|
81
86
|
function ConversationScreenContent({ route }: ConversationScreenProps) {
|
|
82
87
|
const styles = useStyles()
|
|
83
88
|
const navigation = useNavigation()
|
|
84
|
-
const {
|
|
85
|
-
route.params
|
|
86
|
-
const { data: conversation } = useConversation(route.params)
|
|
87
|
-
const { messages, fetchNextPage, hasNextPage } = useConversationMessages({
|
|
89
|
+
const {
|
|
88
90
|
conversation_id,
|
|
91
|
+
editing_message_id,
|
|
89
92
|
reply_root_id,
|
|
90
|
-
|
|
93
|
+
reply_root_author_name,
|
|
94
|
+
initial_message_id,
|
|
95
|
+
} = route.params
|
|
96
|
+
const { data: conversation } = useConversation(route.params)
|
|
97
|
+
const { messages, behind, fetchNextPage, fetchNewer, fetchLiveWindow, hasNextPage } =
|
|
98
|
+
useConversationMessages({
|
|
99
|
+
conversation_id,
|
|
100
|
+
reply_root_id,
|
|
101
|
+
anchor: initial_message_id,
|
|
102
|
+
})
|
|
91
103
|
useConversationJoltEvents({ conversationId: conversation_id })
|
|
92
104
|
useConversationMessagesJoltEvents({ conversationId: conversation_id })
|
|
93
105
|
const { markHandledByMessageForm } = useVideoModerationJoltEvents()
|
|
94
106
|
useEnsureConversationsRouteExists()
|
|
95
|
-
useMarkLatestMessageRead({ conversation, messages })
|
|
107
|
+
useMarkLatestMessageRead({ conversation, messages, enabled: !behind })
|
|
96
108
|
const noMessages = messages.length === 0
|
|
97
109
|
|
|
98
110
|
const { repliesDisabled, memberAbility, badges, title } = conversation
|
|
@@ -120,11 +132,24 @@ function ConversationScreenContent({ route }: ConversationScreenProps) {
|
|
|
120
132
|
enabled: Platform.OS === 'ios',
|
|
121
133
|
})
|
|
122
134
|
|
|
123
|
-
const
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
135
|
+
const [anchorMessageId, setAnchorMessageId] = useState(initial_message_id)
|
|
136
|
+
const releaseAnchor = useCallback(() => setAnchorMessageId(null), [])
|
|
137
|
+
|
|
138
|
+
const handleReturnToBottom = useCallback(
|
|
139
|
+
(animated = true) => {
|
|
140
|
+
releaseAnchor()
|
|
141
|
+
listRef.current?.scrollToOffset({ offset: restOffset, animated })
|
|
142
|
+
},
|
|
143
|
+
[releaseAnchor, restOffset]
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
const logWindowFetchFailure = (error: unknown) =>
|
|
147
|
+
Log.error('conversation window fetch failed', error)
|
|
148
|
+
|
|
149
|
+
const returnToLatest = async () => {
|
|
150
|
+
if (behind) await fetchLiveWindow()
|
|
151
|
+
handleReturnToBottom(false)
|
|
152
|
+
}
|
|
128
153
|
|
|
129
154
|
useEffect(() => {
|
|
130
155
|
if (reply_root_id) {
|
|
@@ -141,6 +166,18 @@ function ConversationScreenContent({ route }: ConversationScreenProps) {
|
|
|
141
166
|
}
|
|
142
167
|
}, [navigation, title, badges, conversation?.deleted, reply_root_id, replyHeaderTitle, muted])
|
|
143
168
|
|
|
169
|
+
useEffect(() => {
|
|
170
|
+
if (!initial_message_id) return
|
|
171
|
+
|
|
172
|
+
navigation.setParams({ initial_message_id: null })
|
|
173
|
+
}, [initial_message_id, navigation])
|
|
174
|
+
|
|
175
|
+
// A window left behind the live edge would make the next open from the list start
|
|
176
|
+
// mid-history with no pill, so we only keep windows that are live.
|
|
177
|
+
useEffect(() => {
|
|
178
|
+
return () => dropMessagesWindowIfBehind({ conversation_id, reply_root_id })
|
|
179
|
+
}, [conversation_id, reply_root_id])
|
|
180
|
+
|
|
144
181
|
if (!conversation || conversation.deleted) {
|
|
145
182
|
return (
|
|
146
183
|
<View style={styles.container}>
|
|
@@ -174,8 +211,11 @@ function ConversationScreenContent({ route }: ConversationScreenProps) {
|
|
|
174
211
|
conversationId={conversation_id}
|
|
175
212
|
messages={messages}
|
|
176
213
|
replyRootId={reply_root_id}
|
|
214
|
+
anchorMessageId={anchorMessageId}
|
|
215
|
+
behind={behind}
|
|
177
216
|
hasNextPage={hasNextPage}
|
|
178
217
|
onEndReached={() => fetchNextPage()}
|
|
218
|
+
onStartReached={() => fetchNewer().catch(logWindowFetchFailure)}
|
|
179
219
|
onScroll={trackScroll}
|
|
180
220
|
onLayout={onLayout}
|
|
181
221
|
onContentSizeChange={onContentSizeChange}
|
|
@@ -183,7 +223,12 @@ function ConversationScreenContent({ route }: ConversationScreenProps) {
|
|
|
183
223
|
onMomentumScrollEnd={onMomentumScrollEnd}
|
|
184
224
|
/>
|
|
185
225
|
)}
|
|
186
|
-
<JumpToBottomButton
|
|
226
|
+
<JumpToBottomButton
|
|
227
|
+
onPress={() =>
|
|
228
|
+
behind ? returnToLatest().catch(logWindowFetchFailure) : handleReturnToBottom()
|
|
229
|
+
}
|
|
230
|
+
visible={showJumpToBottomButton || behind}
|
|
231
|
+
/>
|
|
187
232
|
{!noMessages && <TypingIndicator />}
|
|
188
233
|
{showLeaderDisabledReplyBanner && <LeaderMessagesDisabledBanner />}
|
|
189
234
|
<ConversationBottomBar
|
|
@@ -193,6 +238,7 @@ function ConversationScreenContent({ route }: ConversationScreenProps) {
|
|
|
193
238
|
replyRootId={reply_root_id}
|
|
194
239
|
currentlyEditingMessage={currentlyEditingMessage}
|
|
195
240
|
onFlaggedInMessageForm={markHandledByMessageForm}
|
|
241
|
+
onBeforeSubmit={returnToLatest}
|
|
196
242
|
/>
|
|
197
243
|
</KeyboardView>
|
|
198
244
|
</View>
|
|
@@ -206,6 +252,7 @@ interface ConversationBottomBarProps {
|
|
|
206
252
|
replyRootId: string | null | undefined
|
|
207
253
|
currentlyEditingMessage: MessageResource | undefined
|
|
208
254
|
onFlaggedInMessageForm?: (id: string) => void
|
|
255
|
+
onBeforeSubmit?: () => void
|
|
209
256
|
}
|
|
210
257
|
|
|
211
258
|
function ConversationBottomBar({
|
|
@@ -215,6 +262,7 @@ function ConversationBottomBar({
|
|
|
215
262
|
replyRootId,
|
|
216
263
|
currentlyEditingMessage,
|
|
217
264
|
onFlaggedInMessageForm,
|
|
265
|
+
onBeforeSubmit,
|
|
218
266
|
}: ConversationBottomBarProps) {
|
|
219
267
|
const messageLink = useMessageAvailableMembersAction(conversation)
|
|
220
268
|
|
|
@@ -233,6 +281,7 @@ function ConversationBottomBar({
|
|
|
233
281
|
replyRootId={replyRootId}
|
|
234
282
|
currentlyEditingMessage={currentlyEditingMessage}
|
|
235
283
|
onFlaggedInMessageForm={onFlaggedInMessageForm}
|
|
284
|
+
onBeforeSubmit={onBeforeSubmit}
|
|
236
285
|
key={
|
|
237
286
|
currentlyEditingMessage
|
|
238
287
|
? `edit-message-form-${currentlyEditingMessage.id}`
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { ApiCollection, MessageResource } from '../../../types'
|
|
2
|
+
import {
|
|
3
|
+
anchoredPageParam,
|
|
4
|
+
isBehindLiveEdge,
|
|
5
|
+
livePageParam,
|
|
6
|
+
markLiveIfCaughtUp,
|
|
7
|
+
MessagesWindow,
|
|
8
|
+
newestFirst,
|
|
9
|
+
} from '../messages_window'
|
|
10
|
+
|
|
11
|
+
const page = (meta: Partial<ApiCollection<MessageResource>['meta']> = {}) => ({
|
|
12
|
+
data: [],
|
|
13
|
+
links: {},
|
|
14
|
+
meta: { count: 0, totalCount: 0, ...meta },
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
const window = (
|
|
18
|
+
pageParam: MessagesWindow['pageParams'][number],
|
|
19
|
+
meta?: Partial<ApiCollection<MessageResource>['meta']>
|
|
20
|
+
): MessagesWindow => ({ pages: [page(meta)], pageParams: [pageParam] })
|
|
21
|
+
|
|
22
|
+
describe('isBehindLiveEdge', () => {
|
|
23
|
+
it('is behind while the first page reports newer messages', () => {
|
|
24
|
+
expect(isBehindLiveEdge(window(anchoredPageParam('A'), { prev: { idGt: 'A' } }))).toBe(true)
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
it('is live otherwise', () => {
|
|
28
|
+
expect(isBehindLiveEdge(window(livePageParam))).toBe(false)
|
|
29
|
+
expect(isBehindLiveEdge(window(anchoredPageParam('A')))).toBe(false)
|
|
30
|
+
expect(isBehindLiveEdge(undefined)).toBe(false)
|
|
31
|
+
})
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
describe('markLiveIfCaughtUp', () => {
|
|
35
|
+
it('turns an anchored window with no newer side into the live window', () => {
|
|
36
|
+
const data = window(anchoredPageParam('A'))
|
|
37
|
+
|
|
38
|
+
expect(markLiveIfCaughtUp(data)?.pageParams).toEqual([livePageParam])
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
it('turns a window topped by a newer page into the live window once it catches up', () => {
|
|
42
|
+
const data = window({ where: { id_gt: 'A' }, order: 'asc' })
|
|
43
|
+
|
|
44
|
+
expect(markLiveIfCaughtUp(data)?.pageParams).toEqual([livePageParam])
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
it('leaves an anchored window that is still behind untouched', () => {
|
|
48
|
+
const data = window(anchoredPageParam('A'), { prev: { idGt: 'A' } })
|
|
49
|
+
|
|
50
|
+
expect(markLiveIfCaughtUp(data)).toBe(data)
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('leaves a live window untouched', () => {
|
|
54
|
+
const data = window(livePageParam)
|
|
55
|
+
|
|
56
|
+
expect(markLiveIfCaughtUp(data)).toBe(data)
|
|
57
|
+
})
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
describe('newestFirst', () => {
|
|
61
|
+
const row = (id: string) => ({ id, type: 'Message' }) as MessageResource
|
|
62
|
+
const ascPage = {
|
|
63
|
+
...page({ next: { idGt: 'E' } }),
|
|
64
|
+
data: [row('D'), row('E')],
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
it('stores a newer page newest first, linking newer onward and back to older rows', () => {
|
|
68
|
+
const result = newestFirst(ascPage, { where: { id_gt: 'C' }, order: 'asc' })
|
|
69
|
+
|
|
70
|
+
expect(result.data.map(m => m.id)).toEqual(['E', 'D'])
|
|
71
|
+
expect(result.meta.prev).toEqual({ idGt: 'E' })
|
|
72
|
+
expect(result.meta.next).toEqual({ idLt: 'D' })
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('leaves newest-first pages alone', () => {
|
|
76
|
+
expect(newestFirst(ascPage, anchoredPageParam('A'))).toBe(ascPage)
|
|
77
|
+
})
|
|
78
|
+
})
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { InfiniteData } from '@tanstack/react-query'
|
|
2
|
+
import { ApiCollection, MessageResource } from '../../types'
|
|
3
|
+
import { RequestData } from '../client'
|
|
4
|
+
|
|
5
|
+
export type MessagesWindow = InfiniteData<
|
|
6
|
+
ApiCollection<MessageResource>,
|
|
7
|
+
Partial<RequestData> | undefined
|
|
8
|
+
>
|
|
9
|
+
|
|
10
|
+
export const livePageParam: Partial<RequestData> = {}
|
|
11
|
+
|
|
12
|
+
export const anchoredPageParam = (anchor: string): Partial<RequestData> => ({
|
|
13
|
+
where: { id_around: anchor },
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
export function isBehindLiveEdge(data: MessagesWindow | undefined): boolean {
|
|
17
|
+
return !!data?.pages[0]?.meta?.prev
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function newerPageParam(
|
|
21
|
+
firstPage: ApiCollection<MessageResource>
|
|
22
|
+
): Partial<RequestData> | undefined {
|
|
23
|
+
const newestId = firstPage.meta?.prev?.idGt
|
|
24
|
+
if (!newestId) return undefined
|
|
25
|
+
|
|
26
|
+
// Without asc the server hands back the newest page instead, leaving a gap.
|
|
27
|
+
return { where: { id_gt: newestId }, order: 'asc' }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// The server returns newer pages oldest first, linking only to the next newer page.
|
|
31
|
+
// Storing them newest first with an older link lets a refetch replay the window
|
|
32
|
+
// forward from its top page like any other.
|
|
33
|
+
export function newestFirst<Page extends ApiCollection<MessageResource>>(
|
|
34
|
+
page: Page,
|
|
35
|
+
pageParam: Partial<RequestData> | undefined
|
|
36
|
+
): Page {
|
|
37
|
+
if (pageParam?.order !== 'asc') return page
|
|
38
|
+
|
|
39
|
+
const data = [...page.data].reverse()
|
|
40
|
+
const oldest = data[data.length - 1]
|
|
41
|
+
return {
|
|
42
|
+
...page,
|
|
43
|
+
data,
|
|
44
|
+
meta: { ...page.meta, prev: page.meta?.next, next: oldest && { idLt: oldest.id } },
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function markLiveIfCaughtUp(data: MessagesWindow | undefined): MessagesWindow | undefined {
|
|
49
|
+
if (!data) return data
|
|
50
|
+
|
|
51
|
+
const topIsLive = !data.pageParams[0]?.where
|
|
52
|
+
if (topIsLive || isBehindLiveEdge(data)) return data
|
|
53
|
+
|
|
54
|
+
return { ...data, pageParams: [livePageParam, ...data.pageParams.slice(1)] }
|
|
55
|
+
}
|