@planningcenter/chat-react-native 4.2.1-qa-staging.9 → 4.2.1
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 -2
- package/build/components/conversation/message_form.d.ts.map +1 -1
- package/build/components/conversation/message_form.js +370 -167
- package/build/components/conversation/message_form.js.map +1 -1
- package/build/components/conversation/message_list.d.ts +2 -5
- package/build/components/conversation/message_list.d.ts.map +1 -1
- package/build/components/conversation/message_list.js +67 -187
- package/build/components/conversation/message_list.js.map +1 -1
- package/build/hooks/groups/use_group_members_for_new_conversation.d.ts +3 -4
- package/build/hooks/groups/use_group_members_for_new_conversation.d.ts.map +1 -1
- package/build/hooks/index.d.ts +0 -1
- package/build/hooks/index.d.ts.map +1 -1
- package/build/hooks/index.js +0 -1
- package/build/hooks/index.js.map +1 -1
- package/build/hooks/use_conversation_messages.d.ts +5 -14
- package/build/hooks/use_conversation_messages.d.ts.map +1 -1
- package/build/hooks/use_conversation_messages.js +42 -218
- 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 +42 -58
- package/build/hooks/use_conversation_messages_jolt_events.js.map +1 -1
- package/build/hooks/use_features.d.ts +1 -2
- package/build/hooks/use_features.d.ts.map +1 -1
- package/build/hooks/use_features.js +1 -2
- package/build/hooks/use_features.js.map +1 -1
- package/build/hooks/use_mark_latest_message_read.d.ts +1 -2
- package/build/hooks/use_mark_latest_message_read.d.ts.map +1 -1
- package/build/hooks/use_mark_latest_message_read.js +3 -4
- package/build/hooks/use_mark_latest_message_read.js.map +1 -1
- package/build/hooks/use_read_receipts.d.ts +3 -4
- package/build/hooks/use_read_receipts.d.ts.map +1 -1
- package/build/hooks/use_suspense_api.d.ts +6 -10
- package/build/hooks/use_suspense_api.d.ts.map +1 -1
- package/build/hooks/use_suspense_api.js +63 -83
- package/build/hooks/use_suspense_api.js.map +1 -1
- package/build/screens/conversation_new/components/services_form.js +8 -9
- package/build/screens/conversation_new/components/services_form.js.map +1 -1
- package/build/screens/conversation_screen.d.ts +0 -1
- package/build/screens/conversation_screen.d.ts.map +1 -1
- package/build/screens/conversation_screen.js +203 -305
- 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 +92 -103
- package/build/screens/message_actions_screen.js.map +1 -1
- package/build/screens/notification_settings/hooks/groups.d.ts +3 -4
- package/build/screens/notification_settings/hooks/groups.d.ts.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/types/resources/member.d.ts +0 -1
- package/build/types/resources/member.d.ts.map +1 -1
- package/build/types/resources/member.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 +1 -37
- package/build/utils/cache/messages_cache.js.map +1 -1
- package/build/utils/client/types.d.ts +0 -1
- package/build/utils/client/types.d.ts.map +1 -1
- package/build/utils/client/types.js.map +1 -1
- package/build/utils/group_messages.js +1 -4
- package/build/utils/group_messages.js.map +1 -1
- package/package.json +3 -3
- package/src/__tests__/hooks/use_features.test.ts +2 -10
- package/src/__utils__/query_client.ts +0 -3
- package/src/components/conversation/message_form.tsx +3 -31
- package/src/components/conversation/message_list.tsx +4 -88
- package/src/hooks/__tests__/use_message_create_or_update.test.tsx +7 -2
- package/src/hooks/index.ts +0 -1
- package/src/hooks/use_conversation_messages.ts +17 -103
- package/src/hooks/use_conversation_messages_jolt_events.ts +2 -12
- package/src/hooks/use_features.ts +2 -3
- package/src/hooks/use_mark_latest_message_read.ts +3 -4
- package/src/hooks/use_suspense_api.ts +6 -14
- package/src/screens/conversation_new/components/services_form.tsx +5 -5
- package/src/screens/conversation_screen.tsx +13 -62
- package/src/screens/message_actions_screen.tsx +1 -12
- package/src/types/api_primitives.ts +0 -1
- package/src/types/resources/member.ts +0 -1
- package/src/utils/__tests__/group_messages.test.ts +0 -29
- package/src/utils/cache/__tests__/messages_cache.test.ts +1 -2
- package/src/utils/cache/__tests__/messages_cache_reply_roots.test.ts +2 -3
- package/src/utils/cache/messages_cache.ts +1 -45
- package/src/utils/client/types.ts +0 -1
- package/src/utils/group_messages.ts +1 -4
- package/build/components/conversation/missing_message.d.ts +0 -2
- package/build/components/conversation/missing_message.d.ts.map +0 -1
- package/build/components/conversation/missing_message.js +0 -139
- package/build/components/conversation/missing_message.js.map +0 -1
- package/build/hooks/use_direct_message_eligibility.d.ts +0 -14
- package/build/hooks/use_direct_message_eligibility.d.ts.map +0 -1
- package/build/hooks/use_direct_message_eligibility.js +0 -87
- package/build/hooks/use_direct_message_eligibility.js.map +0 -1
- package/build/utils/cache/messages_window.d.ts +0 -11
- package/build/utils/cache/messages_window.d.ts.map +0 -1
- package/build/utils/cache/messages_window.js +0 -51
- package/build/utils/cache/messages_window.js.map +0 -1
- package/src/__tests__/hooks/use_direct_message_eligibility.test.tsx +0 -83
- package/src/__tests__/screens/conversation_new/components/services_form.test.tsx +0 -91
- package/src/components/conversation/missing_message.tsx +0 -66
- package/src/hooks/__tests__/use_conversation_messages.test.tsx +0 -333
- package/src/hooks/__tests__/use_conversation_messages_jolt_events.test.tsx +0 -87
- package/src/hooks/__tests__/use_mark_latest_message_read.test.tsx +0 -33
- package/src/hooks/use_direct_message_eligibility.ts +0 -40
- package/src/utils/cache/__tests__/messages_cache_deletion.test.ts +0 -97
- package/src/utils/cache/__tests__/messages_window.test.ts +0 -78
- package/src/utils/cache/messages_window.ts +0 -55
|
@@ -1,87 +0,0 @@
|
|
|
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
|
-
})
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { renderHook } from '@testing-library/react-native'
|
|
2
|
-
import type { ConversationResource } from '../../types'
|
|
3
|
-
import { useMarkLatestMessageRead } from '../use_mark_latest_message_read'
|
|
4
|
-
|
|
5
|
-
const mockMarkRead = jest.fn()
|
|
6
|
-
jest.mock('../use_conversations_actions', () => ({
|
|
7
|
-
useConversationsMarkRead: () => ({ markRead: mockMarkRead }),
|
|
8
|
-
}))
|
|
9
|
-
jest.mock('../use_app_state', () => ({ useAppState: () => 'active' }))
|
|
10
|
-
|
|
11
|
-
const conversation = { id: 7, unreadCount: 3 } as ConversationResource
|
|
12
|
-
|
|
13
|
-
beforeEach(() => mockMarkRead.mockReset())
|
|
14
|
-
|
|
15
|
-
describe('useMarkLatestMessageRead', () => {
|
|
16
|
-
it('marks the conversation read on open', () => {
|
|
17
|
-
renderHook(() => useMarkLatestMessageRead({ conversation, messages: [] }))
|
|
18
|
-
|
|
19
|
-
expect(mockMarkRead).toHaveBeenCalledTimes(1)
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
it('waits while disabled and marks read once enabled', () => {
|
|
23
|
-
const { rerender } = renderHook<void, { enabled: boolean }>(
|
|
24
|
-
({ enabled }) => useMarkLatestMessageRead({ conversation, messages: [], enabled }),
|
|
25
|
-
{ initialProps: { enabled: false } }
|
|
26
|
-
)
|
|
27
|
-
expect(mockMarkRead).not.toHaveBeenCalled()
|
|
28
|
-
|
|
29
|
-
rerender({ enabled: true })
|
|
30
|
-
|
|
31
|
-
expect(mockMarkRead).toHaveBeenCalledTimes(1)
|
|
32
|
-
})
|
|
33
|
-
})
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { useQuery } from '@tanstack/react-query'
|
|
2
|
-
import { ApiResource, MemberResource } from '../types'
|
|
3
|
-
import { useApiClient } from './use_api_client'
|
|
4
|
-
|
|
5
|
-
export interface DirectMessageEligibility {
|
|
6
|
-
canDirectMessage: boolean
|
|
7
|
-
directMessagesDisabled: boolean
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function useDirectMessageEligibility({
|
|
11
|
-
groupId,
|
|
12
|
-
personId,
|
|
13
|
-
enabled = true,
|
|
14
|
-
}: {
|
|
15
|
-
groupId: string
|
|
16
|
-
personId: number
|
|
17
|
-
enabled?: boolean
|
|
18
|
-
}) {
|
|
19
|
-
const apiClient = useApiClient()
|
|
20
|
-
const url = `/me/groups/${groupId}/members/${personId}`
|
|
21
|
-
|
|
22
|
-
const { data, isLoading, isError } = useQuery({
|
|
23
|
-
queryKey: ['chat', url, 'direct_message_eligibility'],
|
|
24
|
-
queryFn: () =>
|
|
25
|
-
apiClient.chat.get({
|
|
26
|
-
url,
|
|
27
|
-
data: { fields: { Member: ['can_direct_message', 'direct_messages_disabled'] } },
|
|
28
|
-
}) as Promise<ApiResource<MemberResource>>,
|
|
29
|
-
enabled,
|
|
30
|
-
retry: false,
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
const member = data?.data
|
|
34
|
-
const eligibility: DirectMessageEligibility | undefined = member && {
|
|
35
|
-
canDirectMessage: member.canDirectMessage ?? false,
|
|
36
|
-
directMessagesDisabled: member.directMessagesDisabled ?? false,
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return { eligibility, isLoading, isError }
|
|
40
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { InfiniteData, QueryClient } from '@tanstack/react-query'
|
|
2
|
-
import { buildTestQueryClient } from '../../../__utils__/query_client'
|
|
3
|
-
import type { ApiCollection, ApiResource, MessageResource } from '../../../types'
|
|
4
|
-
import type { PersonResource } from '../../../types/resources/person'
|
|
5
|
-
import { getMessageQueryKey } from '../../request/get_message'
|
|
6
|
-
import { getMessagesQueryKey } from '../../request/get_messages'
|
|
7
|
-
import { applyMessageDeletionToCaches, getThreadedMessagesQueryKey } from '../messages_cache'
|
|
8
|
-
|
|
9
|
-
type MessagesQueryData = InfiniteData<ApiCollection<MessageResource>>
|
|
10
|
-
|
|
11
|
-
const conversation_id = 7
|
|
12
|
-
const author = { id: 1, type: 'Person', name: 'Gina', avatar: null } as unknown as PersonResource
|
|
13
|
-
|
|
14
|
-
const message = (id: string, overrides: Partial<MessageResource> = {}): MessageResource =>
|
|
15
|
-
({
|
|
16
|
-
type: 'Message',
|
|
17
|
-
id,
|
|
18
|
-
text: `msg ${id}`,
|
|
19
|
-
html: '',
|
|
20
|
-
createdAt: '2026-01-01T00:00:00Z',
|
|
21
|
-
deletedAt: null,
|
|
22
|
-
textEditedAt: null,
|
|
23
|
-
mine: true,
|
|
24
|
-
attachments: [],
|
|
25
|
-
author,
|
|
26
|
-
reactionCounts: [],
|
|
27
|
-
replyCount: 0,
|
|
28
|
-
replyRootId: null,
|
|
29
|
-
messageType: 'message',
|
|
30
|
-
...overrides,
|
|
31
|
-
}) as MessageResource
|
|
32
|
-
|
|
33
|
-
const mainKey = getMessagesQueryKey({ conversation_id })
|
|
34
|
-
const threadKey = getThreadedMessagesQueryKey(conversation_id, 'root')
|
|
35
|
-
const rootKey = getMessageQueryKey({ conversation_id, messageId: 'root' })
|
|
36
|
-
|
|
37
|
-
const seedPages = (queryClient: QueryClient, queryKey: unknown[], messages: MessageResource[]) =>
|
|
38
|
-
queryClient.setQueryData<MessagesQueryData>(queryKey, {
|
|
39
|
-
pageParams: [{}],
|
|
40
|
-
pages: [{ data: messages, links: {}, meta: { count: messages.length, totalCount: 1 } }],
|
|
41
|
-
})
|
|
42
|
-
|
|
43
|
-
const pageMessages = (queryClient: QueryClient, queryKey: unknown[]) =>
|
|
44
|
-
queryClient.getQueryData<MessagesQueryData>(queryKey)!.pages[0].data
|
|
45
|
-
|
|
46
|
-
describe('applyMessageDeletionToCaches', () => {
|
|
47
|
-
let queryClient: QueryClient
|
|
48
|
-
const root = message('root', {
|
|
49
|
-
replyRootId: 'root',
|
|
50
|
-
replyCount: 1,
|
|
51
|
-
text: 'What are our prayer requests?',
|
|
52
|
-
})
|
|
53
|
-
const reply = message('reply', { replyRootId: 'root', replyRoot: root, text: 'Smith family' })
|
|
54
|
-
|
|
55
|
-
beforeEach(() => {
|
|
56
|
-
queryClient = buildTestQueryClient()
|
|
57
|
-
seedPages(queryClient, mainKey, [reply, message('other'), root])
|
|
58
|
-
seedPages(queryClient, threadKey, [reply, root])
|
|
59
|
-
queryClient.setQueryData<ApiResource<MessageResource>>(rootKey, {
|
|
60
|
-
data: root,
|
|
61
|
-
links: {},
|
|
62
|
-
meta: {},
|
|
63
|
-
})
|
|
64
|
-
})
|
|
65
|
-
|
|
66
|
-
it('marks a deleted reply root as deleted everywhere it is cached and keeps its replies', () => {
|
|
67
|
-
applyMessageDeletionToCaches(queryClient, conversation_id, root)
|
|
68
|
-
|
|
69
|
-
const [threadReply, threadRoot] = pageMessages(queryClient, threadKey)
|
|
70
|
-
expect(threadRoot.deletedAt).toEqual(expect.any(String))
|
|
71
|
-
expect(threadReply).toMatchObject({ id: 'reply', text: 'Smith family' })
|
|
72
|
-
expect(threadReply.replyRoot?.deletedAt).toEqual(expect.any(String))
|
|
73
|
-
|
|
74
|
-
const [mainReply, , mainRoot] = pageMessages(queryClient, mainKey)
|
|
75
|
-
expect(mainRoot.deletedAt).toEqual(expect.any(String))
|
|
76
|
-
expect(mainReply.replyRoot?.deletedAt).toEqual(expect.any(String))
|
|
77
|
-
|
|
78
|
-
expect(queryClient.getQueryData<ApiResource<MessageResource>>(rootKey)!.data.deletedAt).toEqual(
|
|
79
|
-
expect.any(String)
|
|
80
|
-
)
|
|
81
|
-
})
|
|
82
|
-
|
|
83
|
-
it('removes a deleted reply from the thread and conversation caches but leaves the root', () => {
|
|
84
|
-
applyMessageDeletionToCaches(queryClient, conversation_id, reply)
|
|
85
|
-
|
|
86
|
-
expect(pageMessages(queryClient, threadKey).map(m => m.id)).toEqual(['root'])
|
|
87
|
-
expect(pageMessages(queryClient, mainKey).map(m => m.id)).toEqual(['other', 'root'])
|
|
88
|
-
expect(pageMessages(queryClient, threadKey)[0].deletedAt).toBeNull()
|
|
89
|
-
})
|
|
90
|
-
|
|
91
|
-
it('removes a plain message from the conversation cache without touching thread caches', () => {
|
|
92
|
-
applyMessageDeletionToCaches(queryClient, conversation_id, message('other'))
|
|
93
|
-
|
|
94
|
-
expect(pageMessages(queryClient, mainKey).map(m => m.id)).toEqual(['reply', 'root'])
|
|
95
|
-
expect(pageMessages(queryClient, threadKey).map(m => m.id)).toEqual(['reply', 'root'])
|
|
96
|
-
})
|
|
97
|
-
})
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import type { ApiCollection, MessageResource } from '../../../types'
|
|
2
|
-
import {
|
|
3
|
-
anchoredPageParam,
|
|
4
|
-
isBehindLiveEdge,
|
|
5
|
-
livePageParam,
|
|
6
|
-
markLiveIfCaughtUp,
|
|
7
|
-
MessagesWindow,
|
|
8
|
-
newestFirst,
|
|
9
|
-
} from '../messages_window'
|
|
10
|
-
|
|
11
|
-
const page = (meta: Partial<ApiCollection<MessageResource>['meta']> = {}) => ({
|
|
12
|
-
data: [],
|
|
13
|
-
links: {},
|
|
14
|
-
meta: { count: 0, totalCount: 0, ...meta },
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
const window = (
|
|
18
|
-
pageParam: MessagesWindow['pageParams'][number],
|
|
19
|
-
meta?: Partial<ApiCollection<MessageResource>['meta']>
|
|
20
|
-
): MessagesWindow => ({ pages: [page(meta)], pageParams: [pageParam] })
|
|
21
|
-
|
|
22
|
-
describe('isBehindLiveEdge', () => {
|
|
23
|
-
it('is behind while the first page reports newer messages', () => {
|
|
24
|
-
expect(isBehindLiveEdge(window(anchoredPageParam('A'), { prev: { idGt: 'A' } }))).toBe(true)
|
|
25
|
-
})
|
|
26
|
-
|
|
27
|
-
it('is live otherwise', () => {
|
|
28
|
-
expect(isBehindLiveEdge(window(livePageParam))).toBe(false)
|
|
29
|
-
expect(isBehindLiveEdge(window(anchoredPageParam('A')))).toBe(false)
|
|
30
|
-
expect(isBehindLiveEdge(undefined)).toBe(false)
|
|
31
|
-
})
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
describe('markLiveIfCaughtUp', () => {
|
|
35
|
-
it('turns an anchored window with no newer side into the live window', () => {
|
|
36
|
-
const data = window(anchoredPageParam('A'))
|
|
37
|
-
|
|
38
|
-
expect(markLiveIfCaughtUp(data)?.pageParams).toEqual([livePageParam])
|
|
39
|
-
})
|
|
40
|
-
|
|
41
|
-
it('turns a window topped by a newer page into the live window once it catches up', () => {
|
|
42
|
-
const data = window({ where: { id_gt: 'A' }, order: 'asc' })
|
|
43
|
-
|
|
44
|
-
expect(markLiveIfCaughtUp(data)?.pageParams).toEqual([livePageParam])
|
|
45
|
-
})
|
|
46
|
-
|
|
47
|
-
it('leaves an anchored window that is still behind untouched', () => {
|
|
48
|
-
const data = window(anchoredPageParam('A'), { prev: { idGt: 'A' } })
|
|
49
|
-
|
|
50
|
-
expect(markLiveIfCaughtUp(data)).toBe(data)
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
it('leaves a live window untouched', () => {
|
|
54
|
-
const data = window(livePageParam)
|
|
55
|
-
|
|
56
|
-
expect(markLiveIfCaughtUp(data)).toBe(data)
|
|
57
|
-
})
|
|
58
|
-
})
|
|
59
|
-
|
|
60
|
-
describe('newestFirst', () => {
|
|
61
|
-
const row = (id: string) => ({ id, type: 'Message' }) as MessageResource
|
|
62
|
-
const ascPage = {
|
|
63
|
-
...page({ next: { idGt: 'E' } }),
|
|
64
|
-
data: [row('D'), row('E')],
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
it('stores a newer page newest first, linking newer onward and back to older rows', () => {
|
|
68
|
-
const result = newestFirst(ascPage, { where: { id_gt: 'C' }, order: 'asc' })
|
|
69
|
-
|
|
70
|
-
expect(result.data.map(m => m.id)).toEqual(['E', 'D'])
|
|
71
|
-
expect(result.meta.prev).toEqual({ idGt: 'E' })
|
|
72
|
-
expect(result.meta.next).toEqual({ idLt: 'D' })
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
it('leaves newest-first pages alone', () => {
|
|
76
|
-
expect(newestFirst(ascPage, anchoredPageParam('A'))).toBe(ascPage)
|
|
77
|
-
})
|
|
78
|
-
})
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { InfiniteData } from '@tanstack/react-query'
|
|
2
|
-
import { ApiCollection, MessageResource } from '../../types'
|
|
3
|
-
import { RequestData } from '../client'
|
|
4
|
-
|
|
5
|
-
export type MessagesWindow = InfiniteData<
|
|
6
|
-
ApiCollection<MessageResource>,
|
|
7
|
-
Partial<RequestData> | undefined
|
|
8
|
-
>
|
|
9
|
-
|
|
10
|
-
export const livePageParam: Partial<RequestData> = {}
|
|
11
|
-
|
|
12
|
-
export const anchoredPageParam = (anchor: string): Partial<RequestData> => ({
|
|
13
|
-
where: { id_around: anchor },
|
|
14
|
-
})
|
|
15
|
-
|
|
16
|
-
export function isBehindLiveEdge(data: MessagesWindow | undefined): boolean {
|
|
17
|
-
return !!data?.pages[0]?.meta?.prev
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function newerPageParam(
|
|
21
|
-
firstPage: ApiCollection<MessageResource>
|
|
22
|
-
): Partial<RequestData> | undefined {
|
|
23
|
-
const newestId = firstPage.meta?.prev?.idGt
|
|
24
|
-
if (!newestId) return undefined
|
|
25
|
-
|
|
26
|
-
// Without asc the server hands back the newest page instead, leaving a gap.
|
|
27
|
-
return { where: { id_gt: newestId }, order: 'asc' }
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// The server returns newer pages oldest first, linking only to the next newer page.
|
|
31
|
-
// Storing them newest first with an older link lets a refetch replay the window
|
|
32
|
-
// forward from its top page like any other.
|
|
33
|
-
export function newestFirst<Page extends ApiCollection<MessageResource>>(
|
|
34
|
-
page: Page,
|
|
35
|
-
pageParam: Partial<RequestData> | undefined
|
|
36
|
-
): Page {
|
|
37
|
-
if (pageParam?.order !== 'asc') return page
|
|
38
|
-
|
|
39
|
-
const data = [...page.data].reverse()
|
|
40
|
-
const oldest = data[data.length - 1]
|
|
41
|
-
return {
|
|
42
|
-
...page,
|
|
43
|
-
data,
|
|
44
|
-
meta: { ...page.meta, prev: page.meta?.next, next: oldest && { idLt: oldest.id } },
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function markLiveIfCaughtUp(data: MessagesWindow | undefined): MessagesWindow | undefined {
|
|
49
|
-
if (!data) return data
|
|
50
|
-
|
|
51
|
-
const topIsLive = !data.pageParams[0]?.where
|
|
52
|
-
if (topIsLive || isBehindLiveEdge(data)) return data
|
|
53
|
-
|
|
54
|
-
return { ...data, pageParams: [livePageParam, ...data.pageParams.slice(1)] }
|
|
55
|
-
}
|