@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
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { StyleSheet, useWindowDimensions, View } from 'react-native'
|
|
3
|
+
import { useTheme } from '../../hooks'
|
|
4
|
+
import { CONVERSATION_MESSAGE_LIST_PADDING_HORIZONTAL } from '../../utils/styles'
|
|
5
|
+
import { Avatar, Text } from '../display'
|
|
6
|
+
import { AVATAR_CONNECTOR_SPACING } from './reply_connectors'
|
|
7
|
+
|
|
8
|
+
export function MissingMessage() {
|
|
9
|
+
const styles = useStyles()
|
|
10
|
+
|
|
11
|
+
return (
|
|
12
|
+
<View style={styles.message} accessibilityLabel="Message deleted">
|
|
13
|
+
<Avatar
|
|
14
|
+
size="md"
|
|
15
|
+
sourceUri=""
|
|
16
|
+
style={styles.avatar}
|
|
17
|
+
maxFontSizeMultiplier={1}
|
|
18
|
+
minFontSizeMultiplier={1}
|
|
19
|
+
fallbackIconName="publishing.trash"
|
|
20
|
+
showFallback
|
|
21
|
+
/>
|
|
22
|
+
<View style={styles.messageContent}>
|
|
23
|
+
<View style={styles.messageBubble}>
|
|
24
|
+
<View style={styles.messageText}>
|
|
25
|
+
<Text style={styles.deletedText}>Message deleted</Text>
|
|
26
|
+
</View>
|
|
27
|
+
</View>
|
|
28
|
+
</View>
|
|
29
|
+
</View>
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const useStyles = () => {
|
|
34
|
+
const { colors } = useTheme()
|
|
35
|
+
const { width } = useWindowDimensions()
|
|
36
|
+
const tabletWidth = width >= 744
|
|
37
|
+
|
|
38
|
+
return StyleSheet.create({
|
|
39
|
+
message: {
|
|
40
|
+
gap: 8,
|
|
41
|
+
flexDirection: 'row',
|
|
42
|
+
paddingHorizontal: CONVERSATION_MESSAGE_LIST_PADDING_HORIZONTAL,
|
|
43
|
+
},
|
|
44
|
+
avatar: {
|
|
45
|
+
marginBottom: AVATAR_CONNECTOR_SPACING,
|
|
46
|
+
},
|
|
47
|
+
messageContent: {
|
|
48
|
+
flex: 1,
|
|
49
|
+
marginBottom: 16,
|
|
50
|
+
},
|
|
51
|
+
messageBubble: {
|
|
52
|
+
backgroundColor: colors.fillColorNeutral070,
|
|
53
|
+
borderRadius: 8,
|
|
54
|
+
maxWidth: tabletWidth ? 360 : '80%',
|
|
55
|
+
alignSelf: 'flex-start',
|
|
56
|
+
},
|
|
57
|
+
messageText: {
|
|
58
|
+
paddingVertical: 6,
|
|
59
|
+
paddingHorizontal: 8,
|
|
60
|
+
},
|
|
61
|
+
deletedText: {
|
|
62
|
+
color: colors.textColorDefaultSecondary,
|
|
63
|
+
fontStyle: 'italic',
|
|
64
|
+
},
|
|
65
|
+
})
|
|
66
|
+
}
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import { notifyManager, QueryClientProvider } from '@tanstack/react-query'
|
|
2
|
+
import { act, renderHook, waitFor } from '@testing-library/react-native'
|
|
3
|
+
import React, { Suspense } from 'react'
|
|
4
|
+
import { chatQueryClient } from '../../contexts/api_provider'
|
|
5
|
+
import type { MessageResource } from '../../types'
|
|
6
|
+
import { anchoredPageParam, livePageParam, MessagesWindow } from '../../utils/cache/messages_window'
|
|
7
|
+
import { getMessagesQueryKey } from '../../utils/request/get_messages'
|
|
8
|
+
import { dropMessagesWindowIfBehind, useConversationMessages } from '../use_conversation_messages'
|
|
9
|
+
|
|
10
|
+
jest.mock('../../contexts/api_provider', () => {
|
|
11
|
+
const { QueryClient } = require('@tanstack/react-query')
|
|
12
|
+
return {
|
|
13
|
+
chatQueryClient: new QueryClient({
|
|
14
|
+
defaultOptions: { queries: { retry: false, gcTime: Infinity } },
|
|
15
|
+
}),
|
|
16
|
+
}
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
const mockGet = jest.fn()
|
|
20
|
+
jest.mock('../use_api_client', () => ({
|
|
21
|
+
useApiClient: () => ({ chat: { get: mockGet } }),
|
|
22
|
+
}))
|
|
23
|
+
|
|
24
|
+
notifyManager.setNotifyFunction(fn => act(fn))
|
|
25
|
+
|
|
26
|
+
const conversation_id = 7
|
|
27
|
+
const queryKey = getMessagesQueryKey({ conversation_id })
|
|
28
|
+
const anchor = 'B'
|
|
29
|
+
|
|
30
|
+
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
|
31
|
+
<QueryClientProvider client={chatQueryClient}>
|
|
32
|
+
<Suspense fallback={null}>{children}</Suspense>
|
|
33
|
+
</QueryClientProvider>
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
const message = (id: string) => ({ id, type: 'Message', text: id }) as MessageResource
|
|
37
|
+
|
|
38
|
+
const page = (ids: string[], meta: Record<string, unknown> = {}) => ({
|
|
39
|
+
data: ids.map(message),
|
|
40
|
+
links: {},
|
|
41
|
+
meta: { count: ids.length, totalCount: ids.length, ...meta },
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const seed = (
|
|
45
|
+
pageParam: MessagesWindow['pageParams'][number],
|
|
46
|
+
ids: string[],
|
|
47
|
+
meta: Record<string, unknown> = {}
|
|
48
|
+
) =>
|
|
49
|
+
chatQueryClient.setQueryData<MessagesWindow>(queryKey, {
|
|
50
|
+
pages: [page(ids, meta)],
|
|
51
|
+
pageParams: [pageParam],
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
const behindMeta = { prev: { idGt: 'B', order: 'asc' } }
|
|
55
|
+
|
|
56
|
+
const cachedWindow = () => chatQueryClient.getQueryData<MessagesWindow>(queryKey)
|
|
57
|
+
|
|
58
|
+
const requestedWhere = () => mockGet.mock.calls.map(([request]) => request.data.where)
|
|
59
|
+
|
|
60
|
+
const render = (options: Parameters<typeof useConversationMessages>[0]) =>
|
|
61
|
+
renderHook(() => useConversationMessages(options, { refetchOnMount: false }), { wrapper })
|
|
62
|
+
|
|
63
|
+
beforeEach(() => {
|
|
64
|
+
chatQueryClient.clear()
|
|
65
|
+
mockGet.mockReset()
|
|
66
|
+
mockGet.mockResolvedValue(page(['C', 'B', 'A']))
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
describe('useConversationMessages', () => {
|
|
70
|
+
it('opens around the anchor', async () => {
|
|
71
|
+
render({ conversation_id, anchor })
|
|
72
|
+
|
|
73
|
+
await waitFor(() => expect(requestedWhere()).toEqual([{ id_around: anchor }]))
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
it('reads the loaded window as is', async () => {
|
|
77
|
+
seed(anchoredPageParam('A'), ['B', 'A'], behindMeta)
|
|
78
|
+
|
|
79
|
+
const { result } = render({ conversation_id })
|
|
80
|
+
|
|
81
|
+
await waitFor(() => expect(result.current.messages.map(m => m.id)).toEqual(['B', 'A']))
|
|
82
|
+
expect(result.current.behind).toBe(true)
|
|
83
|
+
expect(mockGet).not.toHaveBeenCalled()
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('reports behind and keeps the anchor while newer messages exist', async () => {
|
|
87
|
+
mockGet.mockResolvedValue(page(['C', 'B', 'A'], { prev: { idGt: 'C', order: 'asc' } }))
|
|
88
|
+
|
|
89
|
+
const { result } = render({ conversation_id, anchor })
|
|
90
|
+
|
|
91
|
+
await waitFor(() => expect(result.current.behind).toBe(true))
|
|
92
|
+
expect(cachedWindow()?.pageParams[0]).toEqual({ where: { id_around: anchor } })
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
it('refetches around the anchor when the cached window stops short of it', async () => {
|
|
96
|
+
seed(livePageParam, ['Z', 'Y'], { next: { idLt: 'Y' } })
|
|
97
|
+
|
|
98
|
+
const { result } = render({ conversation_id, anchor })
|
|
99
|
+
|
|
100
|
+
await waitFor(() => expect(requestedWhere()).toEqual([{ id_around: anchor }]))
|
|
101
|
+
await waitFor(() => expect(result.current.messages.map(m => m.id)).toContain(anchor))
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('reuses a cached window that already holds the anchor', async () => {
|
|
105
|
+
seed(livePageParam, ['Z', anchor])
|
|
106
|
+
|
|
107
|
+
const { result } = render({ conversation_id, anchor })
|
|
108
|
+
|
|
109
|
+
await waitFor(() => expect(result.current.messages.map(m => m.id)).toContain(anchor))
|
|
110
|
+
expect(mockGet).not.toHaveBeenCalled()
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
it('keeps a deleted anchor so the list can mark where it was', async () => {
|
|
114
|
+
const deleted = {
|
|
115
|
+
...message(anchor),
|
|
116
|
+
text: null,
|
|
117
|
+
author: null,
|
|
118
|
+
deletedAt: '2026-10-07T00:00:00Z',
|
|
119
|
+
} as unknown as MessageResource
|
|
120
|
+
mockGet.mockResolvedValue({ ...page(['C', 'A']), data: [message('C'), deleted, message('A')] })
|
|
121
|
+
|
|
122
|
+
const { result } = render({ conversation_id, anchor })
|
|
123
|
+
|
|
124
|
+
await waitFor(() => expect(result.current.messages.map(m => m.id)).toEqual(['C', 'B', 'A']))
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
it('settles into the live window when the anchored page is already caught up', async () => {
|
|
128
|
+
const { result } = render({ conversation_id, anchor })
|
|
129
|
+
|
|
130
|
+
await waitFor(() => expect(cachedWindow()?.pageParams[0]).toEqual(livePageParam))
|
|
131
|
+
expect(result.current.behind).toBe(false)
|
|
132
|
+
})
|
|
133
|
+
})
|
|
134
|
+
|
|
135
|
+
describe('fetchNewer', () => {
|
|
136
|
+
it('asks for the rows after the newest loaded one, oldest first, and merges them in', async () => {
|
|
137
|
+
mockGet.mockResolvedValueOnce(page(['C', 'B', 'A'], { prev: { idGt: 'C', order: 'asc' } }))
|
|
138
|
+
mockGet.mockResolvedValueOnce(page(['D', 'E']))
|
|
139
|
+
const { result } = render({ conversation_id, anchor })
|
|
140
|
+
await waitFor(() => expect(result.current.behind).toBe(true))
|
|
141
|
+
|
|
142
|
+
await act(() => result.current.fetchNewer())
|
|
143
|
+
|
|
144
|
+
const [request] = mockGet.mock.calls[1]
|
|
145
|
+
expect(request.data).toMatchObject({ where: { id_gt: 'C' }, order: 'asc' })
|
|
146
|
+
await waitFor(() =>
|
|
147
|
+
expect(result.current.messages.map(m => m.id)).toEqual(['E', 'D', 'C', 'B', 'A'])
|
|
148
|
+
)
|
|
149
|
+
expect(result.current.behind).toBe(false)
|
|
150
|
+
await waitFor(() => expect(cachedWindow()?.pageParams[0]).toEqual(livePageParam))
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
it('refetches the same window after paging newer, instead of snapping to the anchor', async () => {
|
|
154
|
+
mockGet.mockResolvedValueOnce(page(['C', 'B', 'A'], { prev: { idGt: 'C', order: 'asc' } }))
|
|
155
|
+
mockGet.mockResolvedValueOnce(page(['D', 'E'], { next: { idGt: 'E', order: 'asc' } }))
|
|
156
|
+
const { result } = render({ conversation_id, anchor })
|
|
157
|
+
await waitFor(() => expect(result.current.behind).toBe(true))
|
|
158
|
+
await act(() => result.current.fetchNewer())
|
|
159
|
+
|
|
160
|
+
mockGet.mockResolvedValueOnce(page(['D', 'E'], { next: { idGt: 'E', order: 'asc' } }))
|
|
161
|
+
mockGet.mockResolvedValueOnce(page(['C', 'B', 'A']))
|
|
162
|
+
await act(() => result.current.refetch())
|
|
163
|
+
|
|
164
|
+
expect(requestedWhere().slice(2)).toEqual([{ id_gt: 'C' }, { id_lt: 'D' }])
|
|
165
|
+
await waitFor(() =>
|
|
166
|
+
expect(result.current.messages.map(m => m.id)).toEqual(['E', 'D', 'C', 'B', 'A'])
|
|
167
|
+
)
|
|
168
|
+
expect(result.current.behind).toBe(true)
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
it('does nothing while the window is live', async () => {
|
|
172
|
+
const { result } = render({ conversation_id, anchor })
|
|
173
|
+
await waitFor(() => expect(result.current.behind).toBe(false))
|
|
174
|
+
|
|
175
|
+
await act(() => result.current.fetchNewer())
|
|
176
|
+
|
|
177
|
+
expect(mockGet).toHaveBeenCalledTimes(1)
|
|
178
|
+
})
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
describe('fetchNewer and older paging', () => {
|
|
182
|
+
const deferred = () => {
|
|
183
|
+
let resolve: (value: unknown) => void = () => {}
|
|
184
|
+
const promise = new Promise(r => (resolve = r))
|
|
185
|
+
return { promise, resolve }
|
|
186
|
+
}
|
|
187
|
+
const newerRequests = () =>
|
|
188
|
+
mockGet.mock.calls.filter(([request]) => request.data.where?.id_gt).length
|
|
189
|
+
const olderRequests = () =>
|
|
190
|
+
mockGet.mock.calls.filter(([request]) => request.data.where?.id_lt).length
|
|
191
|
+
|
|
192
|
+
const openBehind = async () => {
|
|
193
|
+
mockGet.mockResolvedValueOnce(
|
|
194
|
+
page(['C', 'B', 'A'], { prev: { idGt: 'C', order: 'asc' }, next: { idLt: 'A' } })
|
|
195
|
+
)
|
|
196
|
+
const hook = render({ conversation_id, anchor })
|
|
197
|
+
await waitFor(() => expect(hook.result.current.behind).toBe(true))
|
|
198
|
+
return hook
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
it('skips a newer fetch while an older page is loading', async () => {
|
|
202
|
+
const { result } = await openBehind()
|
|
203
|
+
const older = deferred()
|
|
204
|
+
mockGet.mockReturnValueOnce(older.promise)
|
|
205
|
+
|
|
206
|
+
act(() => {
|
|
207
|
+
result.current.fetchNextPage()
|
|
208
|
+
})
|
|
209
|
+
act(() => {
|
|
210
|
+
result.current.fetchNewer()
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
expect(newerRequests()).toBe(0)
|
|
214
|
+
await act(async () => older.resolve(page(['0'])))
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
it('skips an older page while a newer fetch is in flight', async () => {
|
|
218
|
+
const { result } = await openBehind()
|
|
219
|
+
const newer = deferred()
|
|
220
|
+
mockGet.mockReturnValueOnce(newer.promise)
|
|
221
|
+
|
|
222
|
+
let pendingNewer: Promise<void> = Promise.resolve()
|
|
223
|
+
act(() => {
|
|
224
|
+
pendingNewer = result.current.fetchNewer()
|
|
225
|
+
})
|
|
226
|
+
act(() => {
|
|
227
|
+
result.current.fetchNextPage()
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
expect(olderRequests()).toBe(0)
|
|
231
|
+
await act(async () => {
|
|
232
|
+
newer.resolve(page(['D']))
|
|
233
|
+
await pendingNewer
|
|
234
|
+
})
|
|
235
|
+
})
|
|
236
|
+
})
|
|
237
|
+
|
|
238
|
+
describe('fetchLiveWindow and in-flight fetches', () => {
|
|
239
|
+
const deferred = () => {
|
|
240
|
+
let resolve: (value: unknown) => void = () => {}
|
|
241
|
+
const promise = new Promise(r => (resolve = r))
|
|
242
|
+
return { promise, resolve }
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const openBehind = async () => {
|
|
246
|
+
mockGet.mockResolvedValueOnce(
|
|
247
|
+
page(['C', 'B', 'A'], { prev: { idGt: 'C', order: 'asc' }, next: { idLt: 'A' } })
|
|
248
|
+
)
|
|
249
|
+
const hook = render({ conversation_id, anchor })
|
|
250
|
+
await waitFor(() => expect(hook.result.current.behind).toBe(true))
|
|
251
|
+
return hook
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
it('keeps the live window when an older page started first', async () => {
|
|
255
|
+
const { result } = await openBehind()
|
|
256
|
+
const older = deferred()
|
|
257
|
+
mockGet.mockReturnValueOnce(older.promise)
|
|
258
|
+
mockGet.mockResolvedValueOnce(page(['Z', 'Y']))
|
|
259
|
+
|
|
260
|
+
act(() => {
|
|
261
|
+
result.current.fetchNextPage()
|
|
262
|
+
})
|
|
263
|
+
await act(() => result.current.fetchLiveWindow())
|
|
264
|
+
await act(async () => older.resolve(page(['0'])))
|
|
265
|
+
|
|
266
|
+
expect(cachedWindow()?.pageParams).toEqual([livePageParam])
|
|
267
|
+
await waitFor(() => expect(result.current.messages.map(m => m.id)).toEqual(['Z', 'Y']))
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
it('keeps the live window when a newer page lands after it', async () => {
|
|
271
|
+
const { result } = await openBehind()
|
|
272
|
+
const newer = deferred()
|
|
273
|
+
mockGet.mockReturnValueOnce(newer.promise)
|
|
274
|
+
mockGet.mockResolvedValueOnce(page(['Z', 'Y']))
|
|
275
|
+
|
|
276
|
+
let pendingNewer: Promise<void> = Promise.resolve()
|
|
277
|
+
act(() => {
|
|
278
|
+
pendingNewer = result.current.fetchNewer()
|
|
279
|
+
})
|
|
280
|
+
await act(() => result.current.fetchLiveWindow())
|
|
281
|
+
await act(async () => {
|
|
282
|
+
newer.resolve(page(['D']))
|
|
283
|
+
await pendingNewer
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
expect(cachedWindow()?.pageParams).toEqual([livePageParam])
|
|
287
|
+
await waitFor(() => expect(result.current.messages.map(m => m.id)).toEqual(['Z', 'Y']))
|
|
288
|
+
})
|
|
289
|
+
})
|
|
290
|
+
|
|
291
|
+
describe('fetchLiveWindow', () => {
|
|
292
|
+
it('replaces a behind window with the newest page', async () => {
|
|
293
|
+
mockGet.mockResolvedValueOnce(page(['C', 'B', 'A'], { prev: { idGt: 'C', order: 'asc' } }))
|
|
294
|
+
mockGet.mockResolvedValueOnce(page(['Z', 'Y']))
|
|
295
|
+
const { result } = render({ conversation_id, anchor })
|
|
296
|
+
await waitFor(() => expect(result.current.behind).toBe(true))
|
|
297
|
+
|
|
298
|
+
await act(() => result.current.fetchLiveWindow())
|
|
299
|
+
|
|
300
|
+
const [request] = mockGet.mock.calls[1]
|
|
301
|
+
expect(request.data.where).toBeUndefined()
|
|
302
|
+
await waitFor(() => expect(result.current.messages.map(m => m.id)).toEqual(['Z', 'Y']))
|
|
303
|
+
expect(cachedWindow()?.pageParams).toEqual([livePageParam])
|
|
304
|
+
expect(result.current.behind).toBe(false)
|
|
305
|
+
})
|
|
306
|
+
|
|
307
|
+
it('does nothing while the window is live', async () => {
|
|
308
|
+
const { result } = render({ conversation_id, anchor })
|
|
309
|
+
await waitFor(() => expect(result.current.behind).toBe(false))
|
|
310
|
+
|
|
311
|
+
await act(() => result.current.fetchLiveWindow())
|
|
312
|
+
|
|
313
|
+
expect(mockGet).toHaveBeenCalledTimes(1)
|
|
314
|
+
})
|
|
315
|
+
})
|
|
316
|
+
|
|
317
|
+
describe('dropMessagesWindowIfBehind', () => {
|
|
318
|
+
it('drops a window that is behind the live edge', () => {
|
|
319
|
+
seed(anchoredPageParam('A'), ['B', 'A'], behindMeta)
|
|
320
|
+
|
|
321
|
+
dropMessagesWindowIfBehind({ conversation_id })
|
|
322
|
+
|
|
323
|
+
expect(cachedWindow()).toBeUndefined()
|
|
324
|
+
})
|
|
325
|
+
|
|
326
|
+
it('keeps a live window', () => {
|
|
327
|
+
seed(livePageParam, ['Z'])
|
|
328
|
+
|
|
329
|
+
dropMessagesWindowIfBehind({ conversation_id })
|
|
330
|
+
|
|
331
|
+
expect(cachedWindow()?.pages[0].data.map(m => m.id)).toEqual(['Z'])
|
|
332
|
+
})
|
|
333
|
+
})
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
|
2
|
+
import { renderHook } from '@testing-library/react-native'
|
|
3
|
+
import React from 'react'
|
|
4
|
+
import { buildTestQueryClient } from '../../__utils__/query_client'
|
|
5
|
+
import type { MessageResource } from '../../types'
|
|
6
|
+
import { anchoredPageParam, livePageParam, MessagesWindow } from '../../utils/cache/messages_window'
|
|
7
|
+
import { getMessagesQueryKey } from '../../utils/request/get_messages'
|
|
8
|
+
import { useConversationMessagesJoltEvents } from '../use_conversation_messages_jolt_events'
|
|
9
|
+
|
|
10
|
+
const handlers: Record<string, (event: unknown) => void> = {}
|
|
11
|
+
jest.mock('../use_jolt', () => ({
|
|
12
|
+
useJoltChannel: () => 'channel',
|
|
13
|
+
useJoltEvent: (_channel: unknown, name: string, handler: (event: unknown) => void) => {
|
|
14
|
+
handlers[name] = handler
|
|
15
|
+
},
|
|
16
|
+
}))
|
|
17
|
+
jest.mock('../use_current_person', () => ({ useCurrentPerson: () => ({ id: 1 }) }))
|
|
18
|
+
jest.mock('../use_api_client', () => ({ useApiClient: () => ({ chat: {} }) }))
|
|
19
|
+
jest.mock('../use_typing_status_cache', () => ({
|
|
20
|
+
TYPING_TIMEOUT_INTERVAL: 0,
|
|
21
|
+
useTypingStatusCache: () => ({
|
|
22
|
+
addTypingEvent: jest.fn(),
|
|
23
|
+
removeTypingEventById: jest.fn(),
|
|
24
|
+
removeAllTypingEventsByAuthorId: jest.fn(),
|
|
25
|
+
}),
|
|
26
|
+
}))
|
|
27
|
+
|
|
28
|
+
const conversationId = 7
|
|
29
|
+
const queryKey = getMessagesQueryKey({ conversation_id: conversationId })
|
|
30
|
+
let queryClient: QueryClient
|
|
31
|
+
|
|
32
|
+
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
|
33
|
+
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
const message = (id: string, text = id) => ({ id, type: 'Message', text }) as MessageResource
|
|
37
|
+
|
|
38
|
+
const seed = (
|
|
39
|
+
pageParam: MessagesWindow['pageParams'][number],
|
|
40
|
+
ids: string[],
|
|
41
|
+
meta: MessagesWindow['pages'][number]['meta'] = { count: 0, totalCount: 0 }
|
|
42
|
+
) =>
|
|
43
|
+
queryClient.setQueryData<MessagesWindow>(queryKey, {
|
|
44
|
+
pages: [{ data: ids.map(m => message(m)), links: {}, meta }],
|
|
45
|
+
pageParams: [pageParam],
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
const cachedRows = () =>
|
|
49
|
+
queryClient.getQueryData<MessagesWindow>(queryKey)!.pages[0].data.map(m => `${m.id}:${m.text}`)
|
|
50
|
+
|
|
51
|
+
const event = (name: 'message.created' | 'message.updated', sort_key: string, text: string) => ({
|
|
52
|
+
event: name,
|
|
53
|
+
data: { data: { sort_key, text, author_id: 2, reply_root_id: null } },
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
beforeEach(() => {
|
|
57
|
+
queryClient = buildTestQueryClient()
|
|
58
|
+
renderHook(() => useConversationMessagesJoltEvents({ conversationId }), { wrapper })
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
describe('useConversationMessagesJoltEvents while behind the live edge', () => {
|
|
62
|
+
beforeEach(() =>
|
|
63
|
+
seed(anchoredPageParam('A'), ['B', 'A'], { count: 0, totalCount: 0, prev: { idGt: 'B' } })
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
it('does not insert a created message into the window', () => {
|
|
67
|
+
handlers['message.created'](event('message.created', 'Z', 'new'))
|
|
68
|
+
|
|
69
|
+
expect(cachedRows()).toEqual(['B:B', 'A:A'])
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
it('still applies an update to a loaded message', () => {
|
|
73
|
+
handlers['message.updated'](event('message.updated', 'A', 'edited'))
|
|
74
|
+
|
|
75
|
+
expect(cachedRows()).toEqual(['B:B', 'A:edited'])
|
|
76
|
+
})
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
describe('useConversationMessagesJoltEvents at the live edge', () => {
|
|
80
|
+
it('inserts a created message', () => {
|
|
81
|
+
seed(livePageParam, ['B', 'A'])
|
|
82
|
+
|
|
83
|
+
handlers['message.created'](event('message.created', 'Z', 'new'))
|
|
84
|
+
|
|
85
|
+
expect(cachedRows()).toEqual(['Z:new', 'B:B', 'A:A'])
|
|
86
|
+
})
|
|
87
|
+
})
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react-native'
|
|
2
|
+
import type { ConversationResource } from '../../types'
|
|
3
|
+
import { useMarkLatestMessageRead } from '../use_mark_latest_message_read'
|
|
4
|
+
|
|
5
|
+
const mockMarkRead = jest.fn()
|
|
6
|
+
jest.mock('../use_conversations_actions', () => ({
|
|
7
|
+
useConversationsMarkRead: () => ({ markRead: mockMarkRead }),
|
|
8
|
+
}))
|
|
9
|
+
jest.mock('../use_app_state', () => ({ useAppState: () => 'active' }))
|
|
10
|
+
|
|
11
|
+
const conversation = { id: 7, unreadCount: 3 } as ConversationResource
|
|
12
|
+
|
|
13
|
+
beforeEach(() => mockMarkRead.mockReset())
|
|
14
|
+
|
|
15
|
+
describe('useMarkLatestMessageRead', () => {
|
|
16
|
+
it('marks the conversation read on open', () => {
|
|
17
|
+
renderHook(() => useMarkLatestMessageRead({ conversation, messages: [] }))
|
|
18
|
+
|
|
19
|
+
expect(mockMarkRead).toHaveBeenCalledTimes(1)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('waits while disabled and marks read once enabled', () => {
|
|
23
|
+
const { rerender } = renderHook<void, { enabled: boolean }>(
|
|
24
|
+
({ enabled }) => useMarkLatestMessageRead({ conversation, messages: [], enabled }),
|
|
25
|
+
{ initialProps: { enabled: false } }
|
|
26
|
+
)
|
|
27
|
+
expect(mockMarkRead).not.toHaveBeenCalled()
|
|
28
|
+
|
|
29
|
+
rerender({ enabled: true })
|
|
30
|
+
|
|
31
|
+
expect(mockMarkRead).toHaveBeenCalledTimes(1)
|
|
32
|
+
})
|
|
33
|
+
})
|
|
@@ -1,33 +1,119 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { useQueryClient } from '@tanstack/react-query'
|
|
2
|
+
import { useEffect, useMemo } from 'react'
|
|
3
|
+
import { chatQueryClient } from '../contexts/api_provider'
|
|
4
|
+
import { ApiCollection, MessageResource } from '../types'
|
|
5
|
+
import {
|
|
6
|
+
anchoredPageParam,
|
|
7
|
+
isBehindLiveEdge,
|
|
8
|
+
livePageParam,
|
|
9
|
+
markLiveIfCaughtUp,
|
|
10
|
+
MessagesWindow,
|
|
11
|
+
newerPageParam,
|
|
12
|
+
newestFirst,
|
|
13
|
+
} from '../utils/cache/messages_window'
|
|
3
14
|
import { replaceEqualById } from '../utils/replace_equal_by_id'
|
|
4
15
|
import { getMessagesQueryKey, getMessagesRequestArgs } from '../utils/request/get_messages'
|
|
16
|
+
import { useApiClient } from './use_api_client'
|
|
5
17
|
import { SuspensePaginatorOptions, useSuspensePaginator } from './use_suspense_api'
|
|
6
18
|
|
|
19
|
+
type WindowOptions = { conversation_id: number; reply_root_id?: string | null }
|
|
20
|
+
type Options = WindowOptions & { anchor?: string | null }
|
|
21
|
+
|
|
7
22
|
export const useConversationMessages = (
|
|
8
|
-
{ conversation_id, reply_root_id }:
|
|
23
|
+
{ conversation_id, reply_root_id, anchor }: Options,
|
|
9
24
|
opts?: SuspensePaginatorOptions
|
|
10
25
|
) => {
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
getMessagesRequestArgs({ conversation_id, reply_root_id }),
|
|
14
|
-
{
|
|
15
|
-
structuralSharing: replaceEqualById,
|
|
16
|
-
...opts,
|
|
17
|
-
}
|
|
18
|
-
)
|
|
26
|
+
const queryClient = useQueryClient()
|
|
27
|
+
const apiClient = useApiClient()
|
|
19
28
|
const queryKey = getMessagesQueryKey({ conversation_id, reply_root_id })
|
|
29
|
+
const requestArgs = getMessagesRequestArgs({ conversation_id, reply_root_id })
|
|
30
|
+
const {
|
|
31
|
+
data,
|
|
32
|
+
infiniteData,
|
|
33
|
+
refetch,
|
|
34
|
+
isRefetching,
|
|
35
|
+
fetchNextPage,
|
|
36
|
+
fetchPreviousPage,
|
|
37
|
+
hasNextPage,
|
|
38
|
+
hasPreviousPage,
|
|
39
|
+
} = useSuspensePaginator<MessageResource>(requestArgs, {
|
|
40
|
+
structuralSharing: replaceEqualById,
|
|
41
|
+
initialPageParam: anchor ? anchoredPageParam(anchor) : livePageParam,
|
|
42
|
+
getPreviousPageParam: newerPageParam,
|
|
43
|
+
transformPage: newestFirst,
|
|
44
|
+
...opts,
|
|
45
|
+
})
|
|
46
|
+
const behind = isBehindLiveEdge(infiniteData)
|
|
47
|
+
|
|
48
|
+
const fetchOlder = () => fetchNextPage({ cancelRefetch: false })
|
|
49
|
+
|
|
50
|
+
const fetchNewer = async () => {
|
|
51
|
+
if (!hasPreviousPage) return
|
|
52
|
+
|
|
53
|
+
await fetchPreviousPage({ cancelRefetch: false })
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const fetchLiveWindow = async () => {
|
|
57
|
+
if (!behind) return
|
|
58
|
+
|
|
59
|
+
const page = await apiClient.chat.get<ApiCollection<MessageResource>>(requestArgs)
|
|
60
|
+
await queryClient.cancelQueries({ queryKey, exact: true })
|
|
61
|
+
queryClient.setQueryData<MessagesWindow>(queryKey, {
|
|
62
|
+
pages: [page],
|
|
63
|
+
pageParams: [livePageParam],
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
20
67
|
const messages = useMemo(
|
|
21
68
|
() =>
|
|
22
69
|
data
|
|
23
|
-
.filter(
|
|
24
|
-
message =>
|
|
25
|
-
(!message.deletedAt || message.replyRootId) &&
|
|
26
|
-
(message.attachments?.length || message.text?.length)
|
|
27
|
-
)
|
|
70
|
+
.filter(message => message.deletedAt || message.attachments?.length || message.text?.length)
|
|
28
71
|
.sort((a, b) => -a.id.localeCompare(b.id)),
|
|
29
72
|
[data]
|
|
30
73
|
)
|
|
31
74
|
|
|
32
|
-
|
|
75
|
+
// Once nothing newer remains, the top of the window is the live edge. Without this,
|
|
76
|
+
// a refetch would replay from the anchor and drop rows Jolt has added since.
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
const settled = markLiveIfCaughtUp(infiniteData)
|
|
79
|
+
|
|
80
|
+
if (settled !== infiniteData) {
|
|
81
|
+
queryClient.setQueryData(queryKey, settled)
|
|
82
|
+
}
|
|
83
|
+
}, [infiniteData, queryClient, queryKey])
|
|
84
|
+
|
|
85
|
+
useEnsureWindowHoldsAnchor({ conversation_id, reply_root_id, anchor })
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
messages,
|
|
89
|
+
behind,
|
|
90
|
+
refetch,
|
|
91
|
+
isRefetching,
|
|
92
|
+
fetchNextPage: fetchOlder,
|
|
93
|
+
fetchNewer,
|
|
94
|
+
fetchLiveWindow,
|
|
95
|
+
hasNextPage,
|
|
96
|
+
queryKey,
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const useEnsureWindowHoldsAnchor = ({ conversation_id, reply_root_id, anchor }: Options) => {
|
|
101
|
+
const queryClient = useQueryClient()
|
|
102
|
+
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
if (!anchor) return
|
|
105
|
+
|
|
106
|
+
const queryKey = getMessagesQueryKey({ conversation_id, reply_root_id })
|
|
107
|
+
const cached = queryClient.getQueryData<MessagesWindow>(queryKey)
|
|
108
|
+
const holdsAnchor = cached?.pages.some(page => page.data.some(m => m.id === anchor))
|
|
109
|
+
if (cached && !holdsAnchor) {
|
|
110
|
+
queryClient.resetQueries({ queryKey, exact: true })
|
|
111
|
+
}
|
|
112
|
+
}, [anchor, conversation_id, reply_root_id, queryClient])
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export const dropMessagesWindowIfBehind = (options: WindowOptions) => {
|
|
116
|
+
const queryKey = getMessagesQueryKey(options)
|
|
117
|
+
const cached = chatQueryClient.getQueryData<MessagesWindow>(queryKey)
|
|
118
|
+
if (isBehindLiveEdge(cached)) chatQueryClient.removeQueries({ queryKey, exact: true })
|
|
33
119
|
}
|