@planningcenter/chat-react-native 4.3.0-rc.1 → 4.3.0-rc.10
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/components/display/conversation_avatar.d.ts +1 -2
- package/build/components/display/conversation_avatar.d.ts.map +1 -1
- package/build/components/display/conversation_avatar.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/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_features.d.ts +2 -1
- package/build/hooks/use_features.d.ts.map +1 -1
- package/build/hooks/use_features.js +2 -1
- package/build/hooks/use_features.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_search_results.d.ts +19 -0
- package/build/hooks/use_search_results.d.ts.map +1 -0
- package/build/hooks/use_search_results.js +145 -0
- package/build/hooks/use_search_results.js.map +1 -0
- 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/navigation/index.d.ts +5 -0
- package/build/navigation/index.d.ts.map +1 -1
- package/build/navigation/index.js +5 -0
- package/build/navigation/index.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/conversations/components/search_list_header_component.d.ts.map +1 -1
- package/build/screens/conversations/components/search_list_header_component.js +159 -133
- package/build/screens/conversations/components/search_list_header_component.js.map +1 -1
- package/build/screens/message_actions_screen.d.ts.map +1 -1
- package/build/screens/message_actions_screen.js +103 -92
- package/build/screens/message_actions_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/search/components/search_blank_states.d.ts +7 -0
- package/build/screens/search/components/search_blank_states.d.ts.map +1 -0
- package/build/screens/search/components/search_blank_states.js +72 -0
- package/build/screens/search/components/search_blank_states.js.map +1 -0
- package/build/screens/search/components/search_field.d.ts +7 -0
- package/build/screens/search/components/search_field.d.ts.map +1 -0
- package/build/screens/search/components/search_field.js +152 -0
- package/build/screens/search/components/search_field.js.map +1 -0
- package/build/screens/search/search_screen.d.ts +5 -0
- package/build/screens/search/search_screen.d.ts.map +1 -0
- package/build/screens/search/search_screen.js +59 -0
- package/build/screens/search/search_screen.js.map +1 -0
- 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/types/resources/conversation.d.ts +1 -0
- package/build/types/resources/conversation.d.ts.map +1 -1
- package/build/types/resources/conversation.js.map +1 -1
- package/build/types/resources/index.d.ts +1 -0
- package/build/types/resources/index.d.ts.map +1 -1
- package/build/types/resources/index.js +1 -0
- package/build/types/resources/index.js.map +1 -1
- package/build/types/resources/search_result.d.ts +29 -0
- package/build/types/resources/search_result.d.ts.map +1 -0
- package/build/types/resources/search_result.js +2 -0
- package/build/types/resources/search_result.js.map +1 -0
- package/build/utils/cache/messages_cache.d.ts +1 -0
- package/build/utils/cache/messages_cache.d.ts.map +1 -1
- package/build/utils/cache/messages_cache.js +37 -1
- 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/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/build/utils/request/get_search_results.d.ts +13 -0
- package/build/utils/request/get_search_results.d.ts.map +1 -0
- package/build/utils/request/get_search_results.js +20 -0
- package/build/utils/request/get_search_results.js.map +1 -0
- package/package.json +2 -2
- package/src/__tests__/hooks/use_features.test.ts +10 -2
- package/src/__utils__/query_client.ts +3 -0
- 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/components/display/conversation_avatar.tsx +1 -10
- 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/__tests__/use_message_create_or_update.test.tsx +2 -7
- package/src/hooks/__tests__/use_search_results.test.tsx +76 -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_features.ts +3 -2
- package/src/hooks/use_mark_latest_message_read.ts +4 -3
- package/src/hooks/use_search_results.ts +77 -0
- package/src/hooks/use_suspense_api.ts +14 -6
- package/src/navigation/index.tsx +5 -0
- package/src/screens/conversation_screen.tsx +62 -13
- package/src/screens/conversations/components/__tests__/search_list_header_component.test.tsx +62 -0
- package/src/screens/conversations/components/search_list_header_component.tsx +11 -0
- package/src/screens/message_actions_screen.tsx +12 -1
- package/src/screens/search/components/__tests__/search_blank_states.test.tsx +20 -0
- package/src/screens/search/components/__tests__/search_field.test.tsx +29 -0
- package/src/screens/search/components/search_blank_states.tsx +34 -0
- package/src/screens/search/components/search_field.tsx +81 -0
- package/src/screens/search/search_screen.tsx +41 -0
- package/src/types/api_primitives.ts +1 -0
- package/src/types/resources/conversation.ts +9 -0
- package/src/types/resources/index.ts +1 -0
- package/src/types/resources/search_result.ts +32 -0
- package/src/utils/__tests__/get_search_results.test.ts +24 -0
- package/src/utils/cache/__tests__/messages_cache.test.ts +2 -1
- package/src/utils/cache/__tests__/messages_cache_deletion.test.ts +97 -0
- package/src/utils/cache/__tests__/messages_cache_reply_roots.test.ts +3 -2
- package/src/utils/cache/__tests__/messages_window.test.ts +78 -0
- package/src/utils/cache/messages_cache.ts +45 -1
- package/src/utils/cache/messages_window.ts +55 -0
- package/src/utils/client/types.ts +1 -0
- package/src/utils/request/get_search_results.ts +23 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { date as formatDate } from '@planningcenter/datetime-fmt'
|
|
2
|
-
import
|
|
2
|
+
import { noop } from 'lodash'
|
|
3
|
+
import React, { RefObject, useRef } from 'react'
|
|
3
4
|
import { FlatList, FlatListProps, StyleSheet, View } from 'react-native'
|
|
4
5
|
import { useTheme } from '../../hooks'
|
|
5
6
|
import { ConversationResource } from '../../types/resources/conversation'
|
|
@@ -11,12 +12,23 @@ import { isSystemMessage } from '../../utils/system_messages'
|
|
|
11
12
|
import { Text } from '../display'
|
|
12
13
|
import { ConversationStartBanner } from './conversation_start_banner'
|
|
13
14
|
import { Message } from './message'
|
|
15
|
+
import { MissingMessage } from './missing_message'
|
|
14
16
|
import { ReplyShadowMessage } from './reply_shadow_message'
|
|
15
17
|
import { SystemMessage } from './system_message'
|
|
16
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
|
+
|
|
17
24
|
type ScrollHandlers = Pick<
|
|
18
25
|
FlatListProps<EnrichedMessage>,
|
|
19
|
-
|
|
26
|
+
| 'onScroll'
|
|
27
|
+
| 'onLayout'
|
|
28
|
+
| 'onContentSizeChange'
|
|
29
|
+
| 'onScrollBeginDrag'
|
|
30
|
+
| 'onScrollEndDrag'
|
|
31
|
+
| 'onMomentumScrollEnd'
|
|
20
32
|
>
|
|
21
33
|
|
|
22
34
|
interface MessageListProps extends ScrollHandlers {
|
|
@@ -25,10 +37,15 @@ interface MessageListProps extends ScrollHandlers {
|
|
|
25
37
|
conversationId: number
|
|
26
38
|
messages: MessageResource[]
|
|
27
39
|
replyRootId?: string | null
|
|
40
|
+
anchorMessageId?: string | null
|
|
41
|
+
behind?: boolean
|
|
28
42
|
hasNextPage: boolean
|
|
29
43
|
onEndReached: () => void
|
|
44
|
+
onStartReached?: () => void
|
|
30
45
|
}
|
|
31
46
|
|
|
47
|
+
const ANCHOR_CLEARS_JUMP_BUTTON = 56
|
|
48
|
+
|
|
32
49
|
/** MessageList
|
|
33
50
|
* The inverted list of messages, reply previews, and date separators for a conversation
|
|
34
51
|
*/
|
|
@@ -38,11 +55,15 @@ export function MessageList({
|
|
|
38
55
|
conversationId,
|
|
39
56
|
messages,
|
|
40
57
|
replyRootId,
|
|
58
|
+
anchorMessageId,
|
|
59
|
+
behind = false,
|
|
41
60
|
hasNextPage,
|
|
42
61
|
onEndReached,
|
|
62
|
+
onStartReached,
|
|
43
63
|
onScroll,
|
|
44
64
|
onLayout,
|
|
45
65
|
onContentSizeChange,
|
|
66
|
+
onScrollBeginDrag,
|
|
46
67
|
onScrollEndDrag,
|
|
47
68
|
onMomentumScrollEnd,
|
|
48
69
|
}: MessageListProps) {
|
|
@@ -52,6 +73,60 @@ export function MessageList({
|
|
|
52
73
|
conversation.memberAbility?.canDeleteNonAuthoredMessages ?? false
|
|
53
74
|
const latestRead = conversation.latestReadMessageSortKey
|
|
54
75
|
const messagesWithSeparators = groupMessages({ ms: messages, inReplyScreen })
|
|
76
|
+
const anchorIndex = anchorMessageId
|
|
77
|
+
? messagesWithSeparators.findIndex(item => item.id === anchorMessageId)
|
|
78
|
+
: -1
|
|
79
|
+
const layoutHeight = useRef(0)
|
|
80
|
+
const contentHeight = useRef(0)
|
|
81
|
+
const userHasScrolled = useRef(false)
|
|
82
|
+
const draggedSinceStartReached = useRef(false)
|
|
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.
|
|
86
|
+
const pinAnchorToBottomEdge = () => {
|
|
87
|
+
const anchorIsLoaded = anchorIndex >= 0
|
|
88
|
+
if (!anchorIsLoaded || userHasScrolled.current) return
|
|
89
|
+
|
|
90
|
+
const scrollable = layoutHeight.current > 0 && contentHeight.current > layoutHeight.current
|
|
91
|
+
if (!scrollable) return
|
|
92
|
+
|
|
93
|
+
listRef.current?.scrollToIndex({
|
|
94
|
+
index: anchorIndex,
|
|
95
|
+
viewPosition: 0,
|
|
96
|
+
viewOffset: ANCHOR_CLEARS_JUMP_BUTTON,
|
|
97
|
+
animated: false,
|
|
98
|
+
})
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const handleLayout: FlatListProps<EnrichedMessage>['onLayout'] = event => {
|
|
102
|
+
onLayout?.(event)
|
|
103
|
+
layoutHeight.current = event.nativeEvent.layout.height
|
|
104
|
+
pinAnchorToBottomEdge()
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const handleContentSizeChange: FlatListProps<EnrichedMessage>['onContentSizeChange'] = (
|
|
108
|
+
width,
|
|
109
|
+
height
|
|
110
|
+
) => {
|
|
111
|
+
onContentSizeChange?.(width, height)
|
|
112
|
+
contentHeight.current = height
|
|
113
|
+
pinAnchorToBottomEdge()
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const handleScrollBeginDrag: FlatListProps<EnrichedMessage>['onScrollBeginDrag'] = event => {
|
|
117
|
+
userHasScrolled.current = true
|
|
118
|
+
draggedSinceStartReached.current = true
|
|
119
|
+
onScrollBeginDrag?.(event)
|
|
120
|
+
}
|
|
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
|
+
}
|
|
55
130
|
|
|
56
131
|
return (
|
|
57
132
|
<FlatList
|
|
@@ -61,11 +136,15 @@ export function MessageList({
|
|
|
61
136
|
data={messagesWithSeparators}
|
|
62
137
|
keyExtractor={item => item.id}
|
|
63
138
|
onScroll={onScroll}
|
|
64
|
-
onLayout={
|
|
65
|
-
onContentSizeChange={
|
|
139
|
+
onLayout={handleLayout}
|
|
140
|
+
onContentSizeChange={handleContentSizeChange}
|
|
141
|
+
onScrollBeginDrag={handleScrollBeginDrag}
|
|
142
|
+
// Expected while rows are still measuring; the next layout pass pins again.
|
|
143
|
+
onScrollToIndexFailed={noop}
|
|
66
144
|
onScrollEndDrag={onScrollEndDrag}
|
|
67
145
|
onMomentumScrollEnd={onMomentumScrollEnd}
|
|
68
146
|
scrollEventThrottle={10}
|
|
147
|
+
maintainVisibleContentPosition={behind ? HOLD_ONLY : HOLD_AND_FOLLOW}
|
|
69
148
|
ListFooterComponent={
|
|
70
149
|
hasNextPage ? null : (
|
|
71
150
|
<ConversationStartBanner conversation={conversation} hasMessages={true} />
|
|
@@ -76,6 +155,10 @@ export function MessageList({
|
|
|
76
155
|
return <InlineDateSeparator {...item} />
|
|
77
156
|
}
|
|
78
157
|
|
|
158
|
+
if (item.type === 'Message' && item.deletedAt && !item.replyRootId) {
|
|
159
|
+
return <MissingMessage />
|
|
160
|
+
}
|
|
161
|
+
|
|
79
162
|
if (item.type === 'ReplyShadowMessage') {
|
|
80
163
|
return (
|
|
81
164
|
<ReplyShadowMessage
|
|
@@ -102,6 +185,7 @@ export function MessageList({
|
|
|
102
185
|
)
|
|
103
186
|
}}
|
|
104
187
|
onEndReached={onEndReached}
|
|
188
|
+
onStartReached={handleStartReached}
|
|
105
189
|
ListHeaderComponent={<View style={styles.listHeader} />}
|
|
106
190
|
/>
|
|
107
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
|
+
}
|
|
@@ -1,21 +1,12 @@
|
|
|
1
1
|
import React from 'react'
|
|
2
2
|
import type { ViewStyle } from 'react-native'
|
|
3
|
-
import type {
|
|
3
|
+
import type { ConversationAvatarData } from '../../types'
|
|
4
4
|
import AvatarPrimitive, { type AvatarRootProps } from '../primitive/avatar_primitive'
|
|
5
5
|
import { AvatarGroup } from './avatar_group'
|
|
6
6
|
import { EmojiAvatar } from './emoji_avatar'
|
|
7
7
|
import { type IconString } from './icon'
|
|
8
8
|
import { IconAvatar } from './icon_avatar'
|
|
9
9
|
|
|
10
|
-
type ConversationAvatarData = Pick<
|
|
11
|
-
ConversationResource,
|
|
12
|
-
| 'customAvatarType'
|
|
13
|
-
| 'customAvatarKey'
|
|
14
|
-
| 'customAvatarColor'
|
|
15
|
-
| 'customAvatarImageUrl'
|
|
16
|
-
| 'previewAvatarUrls'
|
|
17
|
-
>
|
|
18
|
-
|
|
19
10
|
type ResolvedAvatar =
|
|
20
11
|
| { kind: 'image'; url: string }
|
|
21
12
|
| { kind: 'icon'; iconKey: string; color: string | null }
|
|
@@ -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
|
+
})
|