@planningcenter/chat-react-native 4.2.1-qa-staging.7 → 4.2.1-qa-staging.8
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 +1 -1
- package/build/components/conversation/message_form.d.ts.map +1 -1
- package/build/components/conversation/message_form.js +167 -373
- package/build/components/conversation/message_form.js.map +1 -1
- package/build/components/conversation/message_list.d.ts +4 -2
- package/build/components/conversation/message_list.d.ts.map +1 -1
- package/build/components/conversation/message_list.js +115 -81
- 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/contexts/api_provider.d.ts.map +1 -1
- package/build/contexts/api_provider.js +1 -2
- package/build/contexts/api_provider.js.map +1 -1
- package/build/contexts/conversation_context.d.ts +1 -3
- package/build/contexts/conversation_context.d.ts.map +1 -1
- package/build/contexts/conversation_context.js +18 -23
- package/build/contexts/conversation_context.js.map +1 -1
- 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/paginator_meta.d.ts +1 -3
- package/build/hooks/paginator_meta.d.ts.map +1 -1
- package/build/hooks/paginator_meta.js +8 -25
- package/build/hooks/paginator_meta.js.map +1 -1
- package/build/hooks/use_api.d.ts.map +1 -1
- package/build/hooks/use_api.js +8 -5
- package/build/hooks/use_api.js.map +1 -1
- package/build/hooks/use_conversation_messages.d.ts +10 -8
- package/build/hooks/use_conversation_messages.d.ts.map +1 -1
- package/build/hooks/use_conversation_messages.js +218 -81
- package/build/hooks/use_conversation_messages.js.map +1 -1
- package/build/hooks/use_conversation_messages_jolt_events.d.ts +1 -17
- package/build/hooks/use_conversation_messages_jolt_events.d.ts.map +1 -1
- package/build/hooks/use_conversation_messages_jolt_events.js +90 -83
- package/build/hooks/use_conversation_messages_jolt_events.js.map +1 -1
- package/build/hooks/use_message_create_or_update.d.ts.map +1 -1
- package/build/hooks/use_message_create_or_update.js +80 -91
- package/build/hooks/use_message_create_or_update.js.map +1 -1
- package/build/hooks/use_message_reaction_toggle.d.ts.map +1 -1
- package/build/hooks/use_message_reaction_toggle.js +40 -45
- package/build/hooks/use_message_reaction_toggle.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 +88 -65
- package/build/hooks/use_suspense_api.js.map +1 -1
- package/build/screens/conversation_screen.d.ts +1 -1
- package/build/screens/conversation_screen.d.ts.map +1 -1
- package/build/screens/conversation_screen.js +338 -463
- package/build/screens/conversation_screen.js.map +1 -1
- package/build/screens/message_actions_screen.d.ts.map +1 -1
- package/build/screens/message_actions_screen.js +26 -21
- package/build/screens/message_actions_screen.js.map +1 -1
- package/build/screens/message_report_screen.d.ts.map +1 -1
- package/build/screens/message_report_screen.js +44 -17
- package/build/screens/message_report_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/screens/reactions_screen.d.ts.map +1 -1
- package/build/screens/reactions_screen.js +88 -70
- package/build/screens/reactions_screen.js.map +1 -1
- package/build/types/api_primitives.d.ts +0 -1
- package/build/types/api_primitives.d.ts.map +1 -1
- package/build/types/api_primitives.js.map +1 -1
- package/build/utils/cache/messages_cache.d.ts +0 -1
- package/build/utils/cache/messages_cache.d.ts.map +1 -1
- package/build/utils/cache/messages_cache.js +0 -3
- package/build/utils/cache/messages_cache.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/cache/optimistically_update_message.d.ts +1 -2
- package/build/utils/cache/optimistically_update_message.d.ts.map +1 -1
- package/build/utils/cache/optimistically_update_message.js +4 -4
- package/build/utils/cache/optimistically_update_message.js.map +1 -1
- package/build/utils/generate_placeholder_ulid.d.ts +6 -0
- package/build/utils/generate_placeholder_ulid.d.ts.map +1 -1
- package/build/utils/generate_placeholder_ulid.js +2 -2
- package/build/utils/generate_placeholder_ulid.js.map +1 -1
- package/build/utils/request/get_messages.d.ts +6 -18
- package/build/utils/request/get_messages.d.ts.map +1 -1
- package/build/utils/request/get_messages.js +12 -10
- package/build/utils/request/get_messages.js.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/hooks/paginator_meta.test.ts +2 -73
- package/src/components/conversation/__tests__/message_list.test.tsx +0 -42
- package/src/components/conversation/message_form.tsx +30 -5
- package/src/components/conversation/message_list.tsx +44 -14
- package/src/components/conversation/missing_message.tsx +66 -0
- package/src/contexts/api_provider.tsx +1 -2
- package/src/contexts/conversation_context.tsx +1 -6
- 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/paginator_meta.ts +5 -29
- package/src/hooks/use_api.ts +5 -4
- package/src/hooks/use_conversation_messages.ts +88 -43
- package/src/hooks/use_conversation_messages_jolt_events.ts +53 -62
- package/src/hooks/use_message_create_or_update.ts +8 -8
- package/src/hooks/use_message_reaction_toggle.ts +1 -3
- package/src/hooks/use_suspense_api.ts +17 -8
- package/src/screens/conversation_screen.tsx +63 -123
- package/src/screens/message_actions_screen.tsx +5 -3
- package/src/screens/message_report_screen.tsx +3 -2
- package/src/screens/reactions_screen.tsx +4 -2
- package/src/types/api_primitives.ts +0 -1
- package/src/utils/cache/__tests__/messages_cache.test.ts +1 -14
- package/src/utils/cache/__tests__/messages_window.test.ts +78 -0
- package/src/utils/cache/messages_cache.ts +0 -3
- package/src/utils/cache/messages_window.ts +55 -0
- package/src/utils/cache/optimistically_update_message.ts +5 -2
- package/src/utils/generate_placeholder_ulid.ts +2 -2
- package/src/utils/request/get_messages.ts +14 -12
- package/build/components/conversation/anchor_not_found_boundary.d.ts +0 -18
- package/build/components/conversation/anchor_not_found_boundary.d.ts.map +0 -1
- package/build/components/conversation/anchor_not_found_boundary.js +0 -20
- package/build/components/conversation/anchor_not_found_boundary.js.map +0 -1
- package/build/hooks/use_conversation_message_lookup.d.ts +0 -7
- package/build/hooks/use_conversation_message_lookup.d.ts.map +0 -1
- package/build/hooks/use_conversation_message_lookup.js +0 -52
- package/build/hooks/use_conversation_message_lookup.js.map +0 -1
- package/build/hooks/use_fetch_newer_messages.d.ts +0 -9
- package/build/hooks/use_fetch_newer_messages.d.ts.map +0 -1
- package/build/hooks/use_fetch_newer_messages.js +0 -69
- package/build/hooks/use_fetch_newer_messages.js.map +0 -1
- package/build/hooks/use_fetch_newer_on_foreground.d.ts +0 -8
- package/build/hooks/use_fetch_newer_on_foreground.d.ts.map +0 -1
- package/build/hooks/use_fetch_newer_on_foreground.js +0 -42
- package/build/hooks/use_fetch_newer_on_foreground.js.map +0 -1
- package/build/hooks/use_held_back_messages.d.ts +0 -11
- package/build/hooks/use_held_back_messages.d.ts.map +0 -1
- package/build/hooks/use_held_back_messages.js +0 -24
- package/build/hooks/use_held_back_messages.js.map +0 -1
- package/build/utils/cache/find_message_in_caches.d.ts +0 -4
- package/build/utils/cache/find_message_in_caches.d.ts.map +0 -1
- package/build/utils/cache/find_message_in_caches.js +0 -16
- package/build/utils/cache/find_message_in_caches.js.map +0 -1
- package/build/utils/cache/invalidate_refetchable_queries.d.ts +0 -3
- package/build/utils/cache/invalidate_refetchable_queries.d.ts.map +0 -1
- package/build/utils/cache/invalidate_refetchable_queries.js +0 -8
- package/build/utils/cache/invalidate_refetchable_queries.js.map +0 -1
- package/build/utils/cache/prepend_newer_page.d.ts +0 -7
- package/build/utils/cache/prepend_newer_page.d.ts.map +0 -1
- package/build/utils/cache/prepend_newer_page.js +0 -15
- package/build/utils/cache/prepend_newer_page.js.map +0 -1
- package/build/utils/gated_message_cache.d.ts +0 -7
- package/build/utils/gated_message_cache.d.ts.map +0 -1
- package/build/utils/gated_message_cache.js +0 -13
- package/build/utils/gated_message_cache.js.map +0 -1
- package/build/utils/ulid.d.ts +0 -3
- package/build/utils/ulid.d.ts.map +0 -1
- package/build/utils/ulid.js +0 -7
- package/build/utils/ulid.js.map +0 -1
- package/src/__tests__/hooks/use_conversation_messages.test.ts +0 -22
- package/src/__tests__/hooks/use_conversation_messages_jolt_events.test.ts +0 -33
- package/src/__tests__/utils/gated_message_cache.test.ts +0 -26
- package/src/__tests__/utils/request/get_messages.test.ts +0 -24
- package/src/__tests__/utils/ulid.test.ts +0 -15
- package/src/components/conversation/__tests__/anchor_not_found_boundary.test.tsx +0 -51
- package/src/components/conversation/anchor_not_found_boundary.tsx +0 -21
- package/src/hooks/__tests__/use_fetch_newer_messages.test.tsx +0 -95
- package/src/hooks/__tests__/use_held_back_messages.test.tsx +0 -60
- package/src/hooks/__tests__/use_message_reaction_toggle.test.tsx +0 -73
- package/src/hooks/use_conversation_message_lookup.ts +0 -29
- package/src/hooks/use_fetch_newer_messages.ts +0 -52
- package/src/hooks/use_fetch_newer_on_foreground.ts +0 -25
- package/src/hooks/use_held_back_messages.ts +0 -33
- package/src/utils/cache/__tests__/find_message_in_caches.test.ts +0 -37
- package/src/utils/cache/__tests__/invalidate_refetchable_queries.test.ts +0 -23
- package/src/utils/cache/__tests__/prepend_newer_page.test.ts +0 -34
- package/src/utils/cache/find_message_in_caches.ts +0 -20
- package/src/utils/cache/invalidate_refetchable_queries.ts +0 -9
- package/src/utils/cache/prepend_newer_page.ts +0 -19
- package/src/utils/gated_message_cache.ts +0 -19
- package/src/utils/ulid.ts +0 -11
|
@@ -12,20 +12,23 @@ import { isSystemMessage } from '../../utils/system_messages'
|
|
|
12
12
|
import { Text } from '../display'
|
|
13
13
|
import { ConversationStartBanner } from './conversation_start_banner'
|
|
14
14
|
import { Message } from './message'
|
|
15
|
+
import { MissingMessage } from './missing_message'
|
|
15
16
|
import { ReplyShadowMessage } from './reply_shadow_message'
|
|
16
17
|
import { SystemMessage } from './system_message'
|
|
17
18
|
|
|
19
|
+
// While behind, the bottom of the window isn't the live edge. Following there pulls
|
|
20
|
+
// each newly paged-in page into view and chains the next fetch.
|
|
21
|
+
const HOLD_AND_FOLLOW = { minIndexForVisible: 0, autoscrollToTopThreshold: 100 }
|
|
22
|
+
const HOLD_ONLY = { minIndexForVisible: 0 }
|
|
23
|
+
|
|
18
24
|
type ScrollHandlers = Pick<
|
|
19
25
|
FlatListProps<EnrichedMessage>,
|
|
20
26
|
| 'onScroll'
|
|
21
27
|
| 'onLayout'
|
|
22
28
|
| 'onContentSizeChange'
|
|
29
|
+
| 'onScrollBeginDrag'
|
|
23
30
|
| 'onScrollEndDrag'
|
|
24
31
|
| 'onMomentumScrollEnd'
|
|
25
|
-
| 'onScrollBeginDrag'
|
|
26
|
-
| 'onStartReached'
|
|
27
|
-
| 'onStartReachedThreshold'
|
|
28
|
-
| 'maintainVisibleContentPosition'
|
|
29
32
|
>
|
|
30
33
|
|
|
31
34
|
interface MessageListProps extends ScrollHandlers {
|
|
@@ -35,10 +38,14 @@ interface MessageListProps extends ScrollHandlers {
|
|
|
35
38
|
messages: MessageResource[]
|
|
36
39
|
replyRootId?: string | null
|
|
37
40
|
anchorMessageId?: string | null
|
|
41
|
+
behind?: boolean
|
|
38
42
|
hasNextPage: boolean
|
|
39
43
|
onEndReached: () => void
|
|
44
|
+
onStartReached?: () => void
|
|
40
45
|
}
|
|
41
46
|
|
|
47
|
+
const ANCHOR_CLEARS_JUMP_BUTTON = 56
|
|
48
|
+
|
|
42
49
|
/** MessageList
|
|
43
50
|
* The inverted list of messages, reply previews, and date separators for a conversation
|
|
44
51
|
*/
|
|
@@ -49,17 +56,16 @@ export function MessageList({
|
|
|
49
56
|
messages,
|
|
50
57
|
replyRootId,
|
|
51
58
|
anchorMessageId,
|
|
59
|
+
behind = false,
|
|
52
60
|
hasNextPage,
|
|
53
61
|
onEndReached,
|
|
62
|
+
onStartReached,
|
|
54
63
|
onScroll,
|
|
55
64
|
onLayout,
|
|
56
65
|
onContentSizeChange,
|
|
66
|
+
onScrollBeginDrag,
|
|
57
67
|
onScrollEndDrag,
|
|
58
68
|
onMomentumScrollEnd,
|
|
59
|
-
onScrollBeginDrag,
|
|
60
|
-
onStartReached,
|
|
61
|
-
onStartReachedThreshold,
|
|
62
|
-
maintainVisibleContentPosition,
|
|
63
69
|
}: MessageListProps) {
|
|
64
70
|
const styles = useStyles()
|
|
65
71
|
const inReplyScreen = !!replyRootId
|
|
@@ -73,13 +79,23 @@ export function MessageList({
|
|
|
73
79
|
const layoutHeight = useRef(0)
|
|
74
80
|
const contentHeight = useRef(0)
|
|
75
81
|
const userHasScrolled = useRef(false)
|
|
82
|
+
const draggedSinceStartReached = useRef(false)
|
|
76
83
|
|
|
84
|
+
// Row heights keep settling after the first layout (images, attachments), so we
|
|
85
|
+
// re-pin on every layout pass until the reader takes over with a drag.
|
|
77
86
|
const pinAnchorToBottomEdge = () => {
|
|
78
|
-
|
|
87
|
+
const anchorIsLoaded = anchorIndex >= 0
|
|
88
|
+
if (!anchorIsLoaded || userHasScrolled.current) return
|
|
89
|
+
|
|
79
90
|
const scrollable = layoutHeight.current > 0 && contentHeight.current > layoutHeight.current
|
|
80
91
|
if (!scrollable) return
|
|
81
92
|
|
|
82
|
-
listRef.current?.scrollToIndex({
|
|
93
|
+
listRef.current?.scrollToIndex({
|
|
94
|
+
index: anchorIndex,
|
|
95
|
+
viewPosition: 0,
|
|
96
|
+
viewOffset: ANCHOR_CLEARS_JUMP_BUTTON,
|
|
97
|
+
animated: false,
|
|
98
|
+
})
|
|
83
99
|
}
|
|
84
100
|
|
|
85
101
|
const handleLayout: FlatListProps<EnrichedMessage>['onLayout'] = event => {
|
|
@@ -99,9 +115,19 @@ export function MessageList({
|
|
|
99
115
|
|
|
100
116
|
const handleScrollBeginDrag: FlatListProps<EnrichedMessage>['onScrollBeginDrag'] = event => {
|
|
101
117
|
userHasScrolled.current = true
|
|
118
|
+
draggedSinceStartReached.current = true
|
|
102
119
|
onScrollBeginDrag?.(event)
|
|
103
120
|
}
|
|
104
121
|
|
|
122
|
+
// VirtualizedList re-checks its edges on every content size change using a JS offset
|
|
123
|
+
// the native hold never updates, so without this each merged page re-fires it.
|
|
124
|
+
const handleStartReached = () => {
|
|
125
|
+
if (!draggedSinceStartReached.current) return
|
|
126
|
+
|
|
127
|
+
draggedSinceStartReached.current = false
|
|
128
|
+
onStartReached?.()
|
|
129
|
+
}
|
|
130
|
+
|
|
105
131
|
return (
|
|
106
132
|
<FlatList
|
|
107
133
|
inverted
|
|
@@ -112,10 +138,13 @@ export function MessageList({
|
|
|
112
138
|
onScroll={onScroll}
|
|
113
139
|
onLayout={handleLayout}
|
|
114
140
|
onContentSizeChange={handleContentSizeChange}
|
|
141
|
+
onScrollBeginDrag={handleScrollBeginDrag}
|
|
142
|
+
// Expected while rows are still measuring; the next layout pass pins again.
|
|
115
143
|
onScrollToIndexFailed={noop}
|
|
116
144
|
onScrollEndDrag={onScrollEndDrag}
|
|
117
145
|
onMomentumScrollEnd={onMomentumScrollEnd}
|
|
118
146
|
scrollEventThrottle={10}
|
|
147
|
+
maintainVisibleContentPosition={behind ? HOLD_ONLY : HOLD_AND_FOLLOW}
|
|
119
148
|
ListFooterComponent={
|
|
120
149
|
hasNextPage ? null : (
|
|
121
150
|
<ConversationStartBanner conversation={conversation} hasMessages={true} />
|
|
@@ -126,6 +155,10 @@ export function MessageList({
|
|
|
126
155
|
return <InlineDateSeparator {...item} />
|
|
127
156
|
}
|
|
128
157
|
|
|
158
|
+
if (item.type === 'Message' && item.deletedAt && !item.replyRootId) {
|
|
159
|
+
return <MissingMessage />
|
|
160
|
+
}
|
|
161
|
+
|
|
129
162
|
if (item.type === 'ReplyShadowMessage') {
|
|
130
163
|
return (
|
|
131
164
|
<ReplyShadowMessage
|
|
@@ -152,10 +185,7 @@ export function MessageList({
|
|
|
152
185
|
)
|
|
153
186
|
}}
|
|
154
187
|
onEndReached={onEndReached}
|
|
155
|
-
|
|
156
|
-
onStartReached={onStartReached}
|
|
157
|
-
onStartReachedThreshold={onStartReachedThreshold}
|
|
158
|
-
maintainVisibleContentPosition={maintainVisibleContentPosition}
|
|
188
|
+
onStartReached={handleStartReached}
|
|
159
189
|
ListHeaderComponent={<View style={styles.listHeader} />}
|
|
160
190
|
/>
|
|
161
191
|
)
|
|
@@ -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
|
+
}
|
|
@@ -13,7 +13,6 @@ import { appGrantsRequestArgs } from '../hooks/use_chat_permissions'
|
|
|
13
13
|
import { featuresQueryOptions } from '../hooks/use_features'
|
|
14
14
|
import { getRequestQueryKey, RequestQueryKey } from '../hooks/use_suspense_api'
|
|
15
15
|
import { emitAuthRefresh } from '../utils/auth_events'
|
|
16
|
-
import { invalidateRefetchableQueries } from '../utils/cache/invalidate_refetchable_queries'
|
|
17
16
|
import { ResponseError } from '../utils/response_error'
|
|
18
17
|
import { ChatContext, ChatContextValue } from './chat_context'
|
|
19
18
|
|
|
@@ -60,7 +59,7 @@ export function ApiProvider({ children }: ViewProps) {
|
|
|
60
59
|
emitAuthRefresh()
|
|
61
60
|
|
|
62
61
|
if (chatQueryClient.isFetching()) {
|
|
63
|
-
|
|
62
|
+
chatQueryClient.invalidateQueries()
|
|
64
63
|
return
|
|
65
64
|
}
|
|
66
65
|
|
|
@@ -3,34 +3,29 @@ import React, { createContext, PropsWithChildren, useContext, useMemo } from 're
|
|
|
3
3
|
interface ConversationContextValue {
|
|
4
4
|
conversationId: number
|
|
5
5
|
currentPageReplyRootId: string | null
|
|
6
|
-
messagesQueryKey: unknown[] | null
|
|
7
6
|
}
|
|
8
7
|
|
|
9
8
|
interface ConversationContextProviderProps extends PropsWithChildren {
|
|
10
9
|
conversationId: number
|
|
11
10
|
currentPageReplyRootId: string | null
|
|
12
|
-
messagesQueryKey?: unknown[] | null
|
|
13
11
|
}
|
|
14
12
|
|
|
15
13
|
const ConversationContext = createContext<ConversationContextValue>({
|
|
16
14
|
conversationId: 0,
|
|
17
15
|
currentPageReplyRootId: null,
|
|
18
|
-
messagesQueryKey: null,
|
|
19
16
|
})
|
|
20
17
|
|
|
21
18
|
export const ConversationContextProvider = ({
|
|
22
19
|
children,
|
|
23
20
|
conversationId,
|
|
24
21
|
currentPageReplyRootId,
|
|
25
|
-
messagesQueryKey = null,
|
|
26
22
|
}: ConversationContextProviderProps) => {
|
|
27
23
|
const value = useMemo(
|
|
28
24
|
() => ({
|
|
29
25
|
conversationId,
|
|
30
26
|
currentPageReplyRootId,
|
|
31
|
-
messagesQueryKey,
|
|
32
27
|
}),
|
|
33
|
-
[conversationId, currentPageReplyRootId
|
|
28
|
+
[conversationId, currentPageReplyRootId]
|
|
34
29
|
)
|
|
35
30
|
|
|
36
31
|
return <ConversationContext.Provider value={value}>{children}</ConversationContext.Provider>
|
|
@@ -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
|
+
})
|