@planningcenter/chat-react-native 4.2.0-rc.0 → 4.2.0-rc.2
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.d.ts +3 -2
- package/build/components/conversation/message.d.ts.map +1 -1
- package/build/components/conversation/message.js +23 -22
- package/build/components/conversation/message.js.map +1 -1
- package/build/components/conversation/message_list.d.ts +21 -0
- package/build/components/conversation/message_list.d.ts.map +1 -0
- package/build/components/conversation/message_list.js +273 -0
- package/build/components/conversation/message_list.js.map +1 -0
- package/build/components/conversation/message_read_receipts.d.ts +2 -4
- package/build/components/conversation/message_read_receipts.d.ts.map +1 -1
- package/build/components/conversation/message_read_receipts.js +2 -3
- package/build/components/conversation/message_read_receipts.js.map +1 -1
- package/build/components/conversation/reply_shadow_message.d.ts +2 -1
- package/build/components/conversation/reply_shadow_message.d.ts.map +1 -1
- package/build/components/conversation/reply_shadow_message.js +47 -46
- package/build/components/conversation/reply_shadow_message.js.map +1 -1
- package/build/contexts/api_provider.d.ts.map +1 -1
- package/build/contexts/api_provider.js +12 -1
- package/build/contexts/api_provider.js.map +1 -1
- package/build/hooks/use_conversation_messages.d.ts.map +1 -1
- package/build/hooks/use_conversation_messages.js +40 -28
- 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 +6 -1
- package/build/hooks/use_conversation_messages_jolt_events.js.map +1 -1
- package/build/hooks/use_features.d.ts +16 -0
- package/build/hooks/use_features.d.ts.map +1 -1
- package/build/hooks/use_features.js +43 -47
- package/build/hooks/use_features.js.map +1 -1
- package/build/screens/conversation_screen.d.ts.map +1 -1
- package/build/screens/conversation_screen.js +184 -365
- package/build/screens/conversation_screen.js.map +1 -1
- package/build/screens/conversations/components/search_list_header_component.d.ts +2 -0
- package/build/screens/conversations/components/search_list_header_component.d.ts.map +1 -0
- package/build/screens/conversations/components/search_list_header_component.js +496 -0
- package/build/screens/conversations/components/search_list_header_component.js.map +1 -0
- package/build/screens/conversations/conversations_screen.d.ts.map +1 -1
- package/build/screens/conversations/conversations_screen.js +28 -16
- package/build/screens/conversations/conversations_screen.js.map +1 -1
- package/build/types/resources/message.d.ts +1 -0
- package/build/types/resources/message.d.ts.map +1 -1
- package/build/types/resources/message.js.map +1 -1
- 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 +10 -1
- package/build/utils/cache/messages_cache.js.map +1 -1
- package/build/utils/client/transform_response.d.ts.map +1 -1
- package/build/utils/client/transform_response.js +9 -5
- package/build/utils/client/transform_response.js.map +1 -1
- package/build/utils/group_messages.d.ts +1 -0
- package/build/utils/group_messages.d.ts.map +1 -1
- package/build/utils/group_messages.js +10 -1
- package/build/utils/group_messages.js.map +1 -1
- package/build/utils/replace_equal_by_id.d.ts +6 -0
- package/build/utils/replace_equal_by_id.d.ts.map +1 -0
- package/build/utils/replace_equal_by_id.js +41 -0
- package/build/utils/replace_equal_by_id.js.map +1 -0
- package/build/utils/request/messages_data_options.d.ts.map +1 -1
- package/build/utils/request/messages_data_options.js +1 -1
- package/build/utils/request/messages_data_options.js.map +1 -1
- package/build/utils/time.d.ts +1 -0
- package/build/utils/time.d.ts.map +1 -1
- package/build/utils/time.js +1 -0
- package/build/utils/time.js.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/hooks/use_features.test.ts +61 -0
- package/src/components/conversation/__tests__/message_list.test.tsx +277 -0
- package/src/components/conversation/__tests__/reply_shadow_message.test.tsx +119 -0
- package/src/components/conversation/message.tsx +15 -18
- package/src/components/conversation/message_list.tsx +157 -0
- package/src/components/conversation/message_read_receipts.tsx +3 -5
- package/src/components/conversation/reply_shadow_message.tsx +9 -7
- package/src/contexts/api_provider.tsx +4 -0
- package/src/hooks/use_conversation_messages.ts +5 -1
- package/src/hooks/use_conversation_messages_jolt_events.ts +7 -0
- package/src/hooks/use_features.ts +26 -14
- package/src/screens/conversation_screen.tsx +11 -104
- package/src/screens/conversations/components/search_list_header_component.tsx +291 -0
- package/src/screens/conversations/conversations_screen.tsx +6 -1
- package/src/types/resources/message.ts +1 -0
- package/src/utils/__tests__/group_messages.test.ts +48 -0
- package/src/utils/__tests__/replace_equal_by_id.test.ts +73 -0
- package/src/utils/cache/__tests__/messages_cache_reply_roots.test.ts +70 -0
- package/src/utils/cache/messages_cache.ts +21 -1
- package/src/utils/client/__tests__/transform_response.test.ts +62 -0
- package/src/utils/client/transform_response.ts +7 -5
- package/src/utils/group_messages.ts +18 -1
- package/src/utils/replace_equal_by_id.ts +42 -0
- package/src/utils/request/messages_data_options.ts +1 -1
- package/src/utils/time.ts +1 -0
|
@@ -3,10 +3,12 @@ import React from 'react'
|
|
|
3
3
|
import { StyleSheet, View } from 'react-native'
|
|
4
4
|
import { Conversations } from '../../components'
|
|
5
5
|
import { ConversationsContextProvider } from '../../contexts/conversations_context'
|
|
6
|
+
import { availableFeatures, useFeatures } from '../../hooks/use_features'
|
|
6
7
|
import { analyticsEvents, usePublishProductAnalyticsEvent } from '../../hooks/use_product_analytics'
|
|
7
8
|
import { AppName } from '../../types/resources/app_name'
|
|
8
9
|
import { GraphId } from '../../types/resources/group_resource'
|
|
9
10
|
import { ListHeaderComponent } from './components/list_header_component'
|
|
11
|
+
import { SearchListHeaderComponent } from './components/search_list_header_component'
|
|
10
12
|
|
|
11
13
|
export type ConversationsScreenProps = StaticScreenProps<{
|
|
12
14
|
title?: string
|
|
@@ -17,13 +19,16 @@ export type ConversationsScreenProps = StaticScreenProps<{
|
|
|
17
19
|
|
|
18
20
|
export function ConversationsScreen({ route }: ConversationsScreenProps) {
|
|
19
21
|
const styles = useStyles()
|
|
22
|
+
const { featureEnabled } = useFeatures()
|
|
23
|
+
const searchEnabled = featureEnabled(availableFeatures.search)
|
|
24
|
+
const Header = searchEnabled ? SearchListHeaderComponent : ListHeaderComponent
|
|
20
25
|
|
|
21
26
|
usePublishProductAnalyticsEvent(analyticsEvents.conversation_index_opened)
|
|
22
27
|
|
|
23
28
|
return (
|
|
24
29
|
<View style={styles.container}>
|
|
25
30
|
<ConversationsContextProvider args={route.params} opts={{ throwOnError: true }}>
|
|
26
|
-
<Conversations ListHeaderComponent={
|
|
31
|
+
<Conversations ListHeaderComponent={Header} />
|
|
27
32
|
</ConversationsContextProvider>
|
|
28
33
|
</View>
|
|
29
34
|
)
|
|
@@ -17,6 +17,7 @@ export interface MessageResource {
|
|
|
17
17
|
reactionCounts: ReactionCountResource[]
|
|
18
18
|
replyCount: number
|
|
19
19
|
replyRootId?: string | null
|
|
20
|
+
replyRoot?: MessageResource | null
|
|
20
21
|
idempotentKey?: string | null
|
|
21
22
|
messageType: string
|
|
22
23
|
personIdsForSystemEvent?: number[] | null
|
|
@@ -124,6 +124,54 @@ describe('groupMessages — nextRendersAuthor mirrors the newer enriched neighbo
|
|
|
124
124
|
})
|
|
125
125
|
})
|
|
126
126
|
|
|
127
|
+
describe('groupMessages — reply roots', () => {
|
|
128
|
+
const findShadow = (items: ReturnType<typeof groupMessages>, id: string) =>
|
|
129
|
+
items.find(item => 'type' in item && item.type === 'ReplyShadowMessage' && item.id === id)
|
|
130
|
+
|
|
131
|
+
it('carries the root from the page onto the reply and its shadow', () => {
|
|
132
|
+
const messages = [
|
|
133
|
+
message('03', { replyRootId: 'rootA' }),
|
|
134
|
+
message('02'),
|
|
135
|
+
message('rootA', { replyRootId: 'rootA', text: 'fresh' }),
|
|
136
|
+
]
|
|
137
|
+
|
|
138
|
+
const enriched = groupMessages({ ms: messages })
|
|
139
|
+
|
|
140
|
+
expect((findEnriched(enriched, '03') as MessageResource).replyRoot?.text).toBe('fresh')
|
|
141
|
+
expect(findShadow(enriched, '03-rootA')).toMatchObject({ replyRoot: { id: 'rootA' } })
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
it('prefers the page copy of the root over the reply’s included snapshot', () => {
|
|
145
|
+
const snapshot = message('rootA', { replyRootId: 'rootA', text: 'stale' })
|
|
146
|
+
const messages = [
|
|
147
|
+
message('03', { replyRootId: 'rootA', replyRoot: snapshot }),
|
|
148
|
+
message('02'),
|
|
149
|
+
message('rootA', { replyRootId: 'rootA', text: 'fresh' }),
|
|
150
|
+
]
|
|
151
|
+
|
|
152
|
+
const enriched = groupMessages({ ms: messages })
|
|
153
|
+
|
|
154
|
+
expect(findShadow(enriched, '03-rootA')).toMatchObject({ replyRoot: { text: 'fresh' } })
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it('falls back to the included snapshot when the root is not on the page', () => {
|
|
158
|
+
const snapshot = message('rootA', { replyRootId: 'rootA', text: 'included' })
|
|
159
|
+
const messages = [message('02', { replyRootId: 'rootA', replyRoot: snapshot })]
|
|
160
|
+
|
|
161
|
+
const enriched = groupMessages({ ms: messages })
|
|
162
|
+
|
|
163
|
+
expect(findShadow(enriched, '02-rootA')).toMatchObject({ replyRoot: { text: 'included' } })
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
it('leaves replyRoot null when the root is neither on the page nor included', () => {
|
|
167
|
+
const messages = [message('02', { replyRootId: 'rootA' })]
|
|
168
|
+
|
|
169
|
+
const enriched = groupMessages({ ms: messages })
|
|
170
|
+
|
|
171
|
+
expect(findShadow(enriched, '02-rootA')).toMatchObject({ replyRoot: null })
|
|
172
|
+
})
|
|
173
|
+
})
|
|
174
|
+
|
|
127
175
|
describe('groupMessages — system messages', () => {
|
|
128
176
|
it('flags lastInGroup true and renderAuthor false on system messages', () => {
|
|
129
177
|
const messages = [
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { replaceEqualById } from '../replace_equal_by_id'
|
|
2
|
+
|
|
3
|
+
type Record = { type: 'Message'; id: string; text: string; attachments: string[] }
|
|
4
|
+
type Pages = { pages: { data: Record[]; links: {}; meta: {} }[]; pageParams: unknown[] }
|
|
5
|
+
|
|
6
|
+
const record = (id: string, text = id): Record => ({ type: 'Message', id, text, attachments: [] })
|
|
7
|
+
const pagesOf = (...pages: Record[][]): Pages => ({
|
|
8
|
+
pages: pages.map(data => ({ data, links: {}, meta: {} })),
|
|
9
|
+
pageParams: pages.map(() => ({})),
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
const share = (prev: Pages, next: Pages) => replaceEqualById(prev, next) as Pages
|
|
13
|
+
|
|
14
|
+
describe('replaceEqualById', () => {
|
|
15
|
+
it('returns the previous data when nothing changed', () => {
|
|
16
|
+
const prev = pagesOf([record('c'), record('b')], [record('a')])
|
|
17
|
+
const next = pagesOf([record('c'), record('b')], [record('a')])
|
|
18
|
+
|
|
19
|
+
expect(share(prev, next)).toBe(prev)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('keeps every existing record when one is inserted at the head', () => {
|
|
23
|
+
const [c, b] = [record('c'), record('b')]
|
|
24
|
+
const prev = pagesOf([c, b])
|
|
25
|
+
const next = pagesOf([record('d'), record('c'), record('b')])
|
|
26
|
+
|
|
27
|
+
const result = share(prev, next)
|
|
28
|
+
|
|
29
|
+
expect(result.pages[0].data[1]).toBe(c)
|
|
30
|
+
expect(result.pages[0].data[2]).toBe(b)
|
|
31
|
+
expect(result.pages[0].data[0].id).toBe('d')
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
it('keeps the survivors when a record is removed', () => {
|
|
35
|
+
const [c, b, a] = [record('c'), record('b'), record('a')]
|
|
36
|
+
const prev = pagesOf([c, b, a])
|
|
37
|
+
const next = pagesOf([record('c'), record('a')])
|
|
38
|
+
|
|
39
|
+
const result = share(prev, next)
|
|
40
|
+
|
|
41
|
+
expect(result.pages[0].data).toEqual([c, a])
|
|
42
|
+
expect(result.pages[0].data[0]).toBe(c)
|
|
43
|
+
expect(result.pages[0].data[1]).toBe(a)
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('replaces only the changed record and keeps its untouched nested references', () => {
|
|
47
|
+
const b = record('b')
|
|
48
|
+
const prev = pagesOf([record('c'), b])
|
|
49
|
+
const next = pagesOf([record('c'), { ...record('b', 'edited'), attachments: [] }])
|
|
50
|
+
|
|
51
|
+
const result = share(prev, next)
|
|
52
|
+
|
|
53
|
+
expect(result.pages[0].data[0]).toBe(prev.pages[0].data[0])
|
|
54
|
+
expect(result.pages[0].data[1]).not.toBe(b)
|
|
55
|
+
expect(result.pages[0].data[1].text).toBe('edited')
|
|
56
|
+
expect(result.pages[0].data[1].attachments).toBe(b.attachments)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
it('keeps untouched pages when only another page changed', () => {
|
|
60
|
+
const prev = pagesOf([record('c')], [record('a')])
|
|
61
|
+
const next = pagesOf([record('d'), record('c')], [record('a')])
|
|
62
|
+
|
|
63
|
+
const result = share(prev, next)
|
|
64
|
+
|
|
65
|
+
expect(result.pages[1]).toBe(prev.pages[1])
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
it('returns the new data when there is nothing to share with', () => {
|
|
69
|
+
const next = pagesOf([record('a')])
|
|
70
|
+
|
|
71
|
+
expect(replaceEqualById(undefined, next)).toBe(next)
|
|
72
|
+
})
|
|
73
|
+
})
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { InfiniteData, QueryClient } from '@tanstack/react-query'
|
|
2
|
+
import type { ApiCollection } from '../../../types'
|
|
3
|
+
import type { MessageResource } from '../../../types/resources/message'
|
|
4
|
+
import type { PersonResource } from '../../../types/resources/person'
|
|
5
|
+
import { updateCacheReplyRoots } from '../messages_cache'
|
|
6
|
+
|
|
7
|
+
const author = { id: 1, type: 'Person', name: 'A', avatar: null } as unknown as PersonResource
|
|
8
|
+
|
|
9
|
+
const message = (id: string, overrides: Partial<MessageResource> = {}): MessageResource =>
|
|
10
|
+
({
|
|
11
|
+
type: 'Message',
|
|
12
|
+
id,
|
|
13
|
+
text: `msg ${id}`,
|
|
14
|
+
html: '',
|
|
15
|
+
createdAt: '2026-01-01T00:00:00Z',
|
|
16
|
+
deletedAt: null,
|
|
17
|
+
textEditedAt: null,
|
|
18
|
+
mine: false,
|
|
19
|
+
attachments: [],
|
|
20
|
+
author,
|
|
21
|
+
reactionCounts: [],
|
|
22
|
+
replyCount: 0,
|
|
23
|
+
replyRootId: null,
|
|
24
|
+
messageType: 'message',
|
|
25
|
+
...overrides,
|
|
26
|
+
}) as MessageResource
|
|
27
|
+
|
|
28
|
+
const queryKey = ['messages']
|
|
29
|
+
|
|
30
|
+
const seedPages = (queryClient: QueryClient, messages: MessageResource[]) => {
|
|
31
|
+
queryClient.setQueryData<InfiniteData<ApiCollection<MessageResource>>>(queryKey, {
|
|
32
|
+
pageParams: [{}],
|
|
33
|
+
pages: [{ data: messages, links: {}, meta: { count: messages.length, totalCount: 1 } }],
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const pageMessages = (queryClient: QueryClient) =>
|
|
38
|
+
queryClient.getQueryData<InfiniteData<ApiCollection<MessageResource>>>(queryKey)!.pages[0].data
|
|
39
|
+
|
|
40
|
+
describe('updateCacheReplyRoots', () => {
|
|
41
|
+
it('merges a root update into every reply carrying that root', () => {
|
|
42
|
+
const queryClient = new QueryClient()
|
|
43
|
+
const root = message('rootA', { replyRootId: 'rootA', text: 'before' })
|
|
44
|
+
seedPages(queryClient, [
|
|
45
|
+
message('03', { replyRootId: 'rootA', replyRoot: root }),
|
|
46
|
+
message('02', { replyRootId: 'rootB', replyRoot: message('rootB', { text: 'other' }) }),
|
|
47
|
+
message('01', { replyRootId: 'rootA', replyRoot: root }),
|
|
48
|
+
])
|
|
49
|
+
|
|
50
|
+
updateCacheReplyRoots(
|
|
51
|
+
queryClient,
|
|
52
|
+
queryKey,
|
|
53
|
+
message('rootA', { replyRootId: 'rootA', text: 'after', deletedAt: '2026-01-02T00:00:00Z' })
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
const [newest, other, oldest] = pageMessages(queryClient)
|
|
57
|
+
expect(newest.replyRoot).toMatchObject({ text: 'after', deletedAt: '2026-01-02T00:00:00Z' })
|
|
58
|
+
expect(oldest.replyRoot).toMatchObject({ text: 'after' })
|
|
59
|
+
expect(other.replyRoot?.text).toBe('other')
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it('ignores replies without an inline root', () => {
|
|
63
|
+
const queryClient = new QueryClient()
|
|
64
|
+
seedPages(queryClient, [message('01', { replyRootId: 'rootA' })])
|
|
65
|
+
|
|
66
|
+
updateCacheReplyRoots(queryClient, queryKey, message('rootA', { replyRootId: 'rootA' }))
|
|
67
|
+
|
|
68
|
+
expect(pageMessages(queryClient)[0].replyRoot).toBeUndefined()
|
|
69
|
+
})
|
|
70
|
+
})
|
|
@@ -4,7 +4,11 @@ import { ApiCollection, ApiResource, MessageResource } from '../../types'
|
|
|
4
4
|
import { JoltReactionEvent } from '../../types/jolt_events'
|
|
5
5
|
import { transformReactionEventDataToReactionCountResource } from '../jolt/transform_reaction_event_data_to_reaction_count_resource'
|
|
6
6
|
import { getMessagesRequestArgs } from '../request/get_messages'
|
|
7
|
-
import {
|
|
7
|
+
import {
|
|
8
|
+
updateAllRecordsInPagesData,
|
|
9
|
+
updateOrCreateRecordInPagesData,
|
|
10
|
+
updateRecordInPagesData,
|
|
11
|
+
} from './page_mutations'
|
|
8
12
|
import { deleteRecordInPagesData } from './page_mutations'
|
|
9
13
|
|
|
10
14
|
export function updateCacheWithMessage(
|
|
@@ -66,6 +70,22 @@ export function updateCacheWithIndividualMessage(
|
|
|
66
70
|
})
|
|
67
71
|
}
|
|
68
72
|
|
|
73
|
+
export function updateCacheReplyRoots(
|
|
74
|
+
queryClient: QueryClient,
|
|
75
|
+
queryKey: unknown[],
|
|
76
|
+
root: MessageResource
|
|
77
|
+
) {
|
|
78
|
+
queryClient.setQueryData<MessagesQueryData>(queryKey, prev =>
|
|
79
|
+
updateAllRecordsInPagesData({
|
|
80
|
+
data: prev,
|
|
81
|
+
processRecord: message =>
|
|
82
|
+
message.replyRoot?.id === root.id
|
|
83
|
+
? { ...message, replyRoot: mergeMessageUpdate(root, message.replyRoot) }
|
|
84
|
+
: message,
|
|
85
|
+
})
|
|
86
|
+
)
|
|
87
|
+
}
|
|
88
|
+
|
|
69
89
|
export function updateCacheWithReaction(
|
|
70
90
|
queryClient: QueryClient,
|
|
71
91
|
queryKey: unknown[],
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import transformResponse from '../transform_response'
|
|
2
|
+
|
|
3
|
+
const person = (id: string, name: string) => ({
|
|
4
|
+
type: 'Person',
|
|
5
|
+
id,
|
|
6
|
+
attributes: { name, avatar: null },
|
|
7
|
+
})
|
|
8
|
+
|
|
9
|
+
const message = (
|
|
10
|
+
id: string,
|
|
11
|
+
{ authorId, replyRootId }: { authorId: string; replyRootId: string | null }
|
|
12
|
+
) => ({
|
|
13
|
+
type: 'Message',
|
|
14
|
+
id,
|
|
15
|
+
attributes: { text: `msg ${id}` },
|
|
16
|
+
relationships: {
|
|
17
|
+
author: { data: { type: 'Person', id: authorId } },
|
|
18
|
+
reply_root: { data: replyRootId ? { type: 'Message', id: replyRootId } : null },
|
|
19
|
+
reaction_counts: { data: [] },
|
|
20
|
+
},
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
const root = message('01ROOT', { authorId: '2', replyRootId: '01ROOT' })
|
|
24
|
+
const reply = message('01REPLY', { authorId: '1', replyRootId: '01ROOT' })
|
|
25
|
+
|
|
26
|
+
describe('transformResponse — reply_root include', () => {
|
|
27
|
+
it('attaches the included root and its author to a reply', () => {
|
|
28
|
+
const result = transformResponse({
|
|
29
|
+
data: [reply],
|
|
30
|
+
included: [root, person('1', 'Replier'), person('2', 'Root Author')],
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
const [transformedReply] = result.data
|
|
34
|
+
expect(transformedReply.replyRootId).toBe('01ROOT')
|
|
35
|
+
expect(transformedReply.replyRoot.id).toBe('01ROOT')
|
|
36
|
+
expect(transformedReply.replyRoot.text).toBe('msg 01ROOT')
|
|
37
|
+
expect(transformedReply.replyRoot.author.name).toBe('Root Author')
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('drops a record’s self reference so the result stays acyclic', () => {
|
|
41
|
+
const result = transformResponse({
|
|
42
|
+
data: [reply, root],
|
|
43
|
+
included: [root, person('1', 'Replier'), person('2', 'Root Author')],
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
const [transformedReply, transformedRoot] = result.data
|
|
47
|
+
expect(transformedReply.replyRoot.replyRoot).toBeNull()
|
|
48
|
+
expect(transformedRoot.replyRootId).toBe('01ROOT')
|
|
49
|
+
expect(transformedRoot.replyRoot).toBeNull()
|
|
50
|
+
expect(() => JSON.stringify(result)).not.toThrow()
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('leaves replyRoot null when the root is not included', () => {
|
|
54
|
+
const result = transformResponse({
|
|
55
|
+
data: [reply],
|
|
56
|
+
included: [person('1', 'Replier')],
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
expect(result.data[0].replyRootId).toBe('01ROOT')
|
|
60
|
+
expect(result.data[0].replyRoot).toBeNull()
|
|
61
|
+
})
|
|
62
|
+
})
|
|
@@ -32,7 +32,8 @@ const transformRelationships = relationships => {
|
|
|
32
32
|
}, {})
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
const addIncludedRecords = (
|
|
35
|
+
const addIncludedRecords = (record, included, seen, parent) => {
|
|
36
|
+
const { relationships } = record
|
|
36
37
|
return Object.keys(relationships || {}).reduce((acc, key) => {
|
|
37
38
|
const relationship = relationships[key]
|
|
38
39
|
if (_.isArray(relationship.data)) {
|
|
@@ -43,13 +44,14 @@ const addIncludedRecords = (relationships, included, seen, parent) => {
|
|
|
43
44
|
return _.set(
|
|
44
45
|
acc,
|
|
45
46
|
_.pluralize(key),
|
|
46
|
-
_.map(records,
|
|
47
|
+
_.map(records, related => transformRecord(related, included, seen))
|
|
47
48
|
)
|
|
48
49
|
} else {
|
|
49
50
|
const [id, type] = _.at(relationship, ['data.id', 'data.type'])
|
|
50
51
|
if (id && type) {
|
|
51
|
-
const
|
|
52
|
-
|
|
52
|
+
const isSelfReference = id === record.id && type === record.type
|
|
53
|
+
const related = isSelfReference ? undefined : _.find(included, { id, type })
|
|
54
|
+
return _.set(acc, key, related ? transformRecord(related, included, seen) : null)
|
|
53
55
|
} else {
|
|
54
56
|
return acc
|
|
55
57
|
}
|
|
@@ -78,7 +80,7 @@ const transformRecord = (record, included, seen) => {
|
|
|
78
80
|
|
|
79
81
|
seen.set(record, transformed)
|
|
80
82
|
|
|
81
|
-
return addIncludedRecords(
|
|
83
|
+
return addIncludedRecords(record, included, seen, transformed)
|
|
82
84
|
}
|
|
83
85
|
}
|
|
84
86
|
|
|
@@ -10,6 +10,7 @@ export type ReplyShadowMessage = {
|
|
|
10
10
|
type: 'ReplyShadowMessage'
|
|
11
11
|
id: string
|
|
12
12
|
messageId: string
|
|
13
|
+
replyRoot: MessageResource | null
|
|
13
14
|
isReplyShadowMessage: boolean
|
|
14
15
|
nextRendersAuthor: boolean
|
|
15
16
|
}
|
|
@@ -23,6 +24,7 @@ interface GroupMessagesProps {
|
|
|
23
24
|
|
|
24
25
|
export function groupMessages({ ms, inReplyScreen }: GroupMessagesProps): EnrichedMessage[] {
|
|
25
26
|
const items: EnrichedMessage[] = []
|
|
27
|
+
const messagesById = new Map(ms.map(message => [message.id, message]))
|
|
26
28
|
let myLatestSeen = false
|
|
27
29
|
let nextNeighborEnriched: MessageResource | undefined
|
|
28
30
|
|
|
@@ -41,7 +43,13 @@ export function groupMessages({ ms, inReplyScreen }: GroupMessagesProps): Enrich
|
|
|
41
43
|
const isMyLatest = !myLatestSeen && message.mine
|
|
42
44
|
if (isMyLatest) myLatestSeen = true
|
|
43
45
|
|
|
44
|
-
const enriched = enrichRegularMessage(
|
|
46
|
+
const enriched = enrichRegularMessage(
|
|
47
|
+
{ ...message, replyRoot: resolveReplyRoot(message, messagesById) },
|
|
48
|
+
prev,
|
|
49
|
+
next,
|
|
50
|
+
isMyLatest,
|
|
51
|
+
!!inReplyScreen
|
|
52
|
+
)
|
|
45
53
|
items.push(enriched)
|
|
46
54
|
|
|
47
55
|
const shadow = replyShadowFor(enriched, prev)
|
|
@@ -61,6 +69,14 @@ function neighborsOf<T>(arr: T[], i: number): { prev: T | undefined; next: T | u
|
|
|
61
69
|
return { prev: arr[i + 1], next: arr[i - 1] }
|
|
62
70
|
}
|
|
63
71
|
|
|
72
|
+
function resolveReplyRoot(
|
|
73
|
+
message: MessageResource,
|
|
74
|
+
messagesById: Map<string, MessageResource>
|
|
75
|
+
): MessageResource | null {
|
|
76
|
+
if (!message.replyRootId || message.replyRootId === message.id) return null
|
|
77
|
+
return messagesById.get(message.replyRootId) ?? message.replyRoot ?? null
|
|
78
|
+
}
|
|
79
|
+
|
|
64
80
|
function enrichSystemMessage(
|
|
65
81
|
message: MessageResource,
|
|
66
82
|
next: MessageResource | undefined
|
|
@@ -171,6 +187,7 @@ function replyShadowFor(
|
|
|
171
187
|
type: 'ReplyShadowMessage',
|
|
172
188
|
id: `${message.id}-${message.replyRootId}`,
|
|
173
189
|
messageId: message.replyRootId,
|
|
190
|
+
replyRoot: message.replyRoot ?? null,
|
|
174
191
|
isReplyShadowMessage: true,
|
|
175
192
|
nextRendersAuthor: message.renderAuthor ?? false,
|
|
176
193
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { InfiniteData, replaceEqualDeep } from '@tanstack/react-query'
|
|
2
|
+
import { ApiCollection, ResourceObject } from '../types'
|
|
3
|
+
|
|
4
|
+
type Pages = InfiniteData<ApiCollection<ResourceObject>>
|
|
5
|
+
|
|
6
|
+
/** replaceEqualById
|
|
7
|
+
* TanStack's default structural sharing compares arrays by index, so an insert or
|
|
8
|
+
* delete gives every shifted record a new identity. Match by id instead.
|
|
9
|
+
*/
|
|
10
|
+
export function replaceEqualById(oldData: unknown, newData: unknown): unknown {
|
|
11
|
+
const prev = oldData as Pages | undefined
|
|
12
|
+
const next = newData as Pages
|
|
13
|
+
if (!prev?.pages || !next?.pages) return replaceEqualDeep(prev, next)
|
|
14
|
+
|
|
15
|
+
const prevById = new Map<ResourceObject['id'], ResourceObject>()
|
|
16
|
+
prev.pages.forEach(page => page.data.forEach(record => prevById.set(record.id, record)))
|
|
17
|
+
|
|
18
|
+
let changed = prev.pages.length !== next.pages.length
|
|
19
|
+
const pages = next.pages.map((page, index) => {
|
|
20
|
+
const prevPage = prev.pages[index]
|
|
21
|
+
const data = page.data.map(record => {
|
|
22
|
+
const previous = prevById.get(record.id)
|
|
23
|
+
return previous ? replaceEqualDeep(previous, record) : record
|
|
24
|
+
})
|
|
25
|
+
const links = replaceEqualDeep(prevPage?.links, page.links)
|
|
26
|
+
const meta = replaceEqualDeep(prevPage?.meta, page.meta)
|
|
27
|
+
const sameRecords =
|
|
28
|
+
!!prevPage &&
|
|
29
|
+
data.length === prevPage.data.length &&
|
|
30
|
+
data.every((record, i) => record === prevPage.data[i])
|
|
31
|
+
|
|
32
|
+
if (prevPage && sameRecords && links === prevPage.links && meta === prevPage.meta) {
|
|
33
|
+
return prevPage
|
|
34
|
+
}
|
|
35
|
+
changed = true
|
|
36
|
+
return { ...page, data, links, meta }
|
|
37
|
+
})
|
|
38
|
+
const pageParams = replaceEqualDeep(prev.pageParams, next.pageParams)
|
|
39
|
+
|
|
40
|
+
if (!changed && pageParams === prev.pageParams) return prev
|
|
41
|
+
return { ...next, pages, pageParams }
|
|
42
|
+
}
|
|
@@ -19,4 +19,4 @@ export const getMessageFields = {
|
|
|
19
19
|
ReactionCount: ['value', 'count', 'mine', 'message_id', 'author_ids'],
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export const getMessagesInclude = ['author', 'reaction_counts']
|
|
22
|
+
export const getMessagesInclude = ['author', 'reaction_counts', 'reply_root', 'reply_root.author']
|
package/src/utils/time.ts
CHANGED
|
@@ -12,6 +12,7 @@ export function toMs(value: number, unit: TimeUnit): number {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
export const FIVE_SECONDS = toMs(5, 'seconds')
|
|
15
|
+
export const ONE_MINUTE = toMs(1, 'minutes')
|
|
15
16
|
export const FIVE_MINUTES = toMs(5, 'minutes')
|
|
16
17
|
export const THIRTY_MINUTES = toMs(30, 'minutes')
|
|
17
18
|
export const ONE_HOUR = toMs(1, 'hours')
|