@planningcenter/chat-react-native 4.2.1-qa-staging.7 → 4.2.1-qa-staging.9
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_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_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/__tests__/hooks/use_features.test.ts +10 -2
- package/src/__utils__/query_client.ts +3 -0
- 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/__tests__/use_message_create_or_update.test.tsx +2 -7
- 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_features.ts +3 -2
- 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 +3 -15
- package/src/utils/cache/__tests__/messages_cache_deletion.test.ts +2 -1
- 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 +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
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { renderHook } from '@testing-library/react-native'
|
|
2
|
+
import type { ConversationResource } from '../../types'
|
|
3
|
+
import { useMarkLatestMessageRead } from '../use_mark_latest_message_read'
|
|
4
|
+
|
|
5
|
+
const mockMarkRead = jest.fn()
|
|
6
|
+
jest.mock('../use_conversations_actions', () => ({
|
|
7
|
+
useConversationsMarkRead: () => ({ markRead: mockMarkRead }),
|
|
8
|
+
}))
|
|
9
|
+
jest.mock('../use_app_state', () => ({ useAppState: () => 'active' }))
|
|
10
|
+
|
|
11
|
+
const conversation = { id: 7, unreadCount: 3 } as ConversationResource
|
|
12
|
+
|
|
13
|
+
beforeEach(() => mockMarkRead.mockReset())
|
|
14
|
+
|
|
15
|
+
describe('useMarkLatestMessageRead', () => {
|
|
16
|
+
it('marks the conversation read on open', () => {
|
|
17
|
+
renderHook(() => useMarkLatestMessageRead({ conversation, messages: [] }))
|
|
18
|
+
|
|
19
|
+
expect(mockMarkRead).toHaveBeenCalledTimes(1)
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('waits while disabled and marks read once enabled', () => {
|
|
23
|
+
const { rerender } = renderHook<void, { enabled: boolean }>(
|
|
24
|
+
({ enabled }) => useMarkLatestMessageRead({ conversation, messages: [], enabled }),
|
|
25
|
+
{ initialProps: { enabled: false } }
|
|
26
|
+
)
|
|
27
|
+
expect(mockMarkRead).not.toHaveBeenCalled()
|
|
28
|
+
|
|
29
|
+
rerender({ enabled: true })
|
|
30
|
+
|
|
31
|
+
expect(mockMarkRead).toHaveBeenCalledTimes(1)
|
|
32
|
+
})
|
|
33
|
+
})
|
|
@@ -7,13 +7,8 @@ import { getMessagesQueryKey } from '../../utils/request/get_messages'
|
|
|
7
7
|
import { useMessageCreateOrUpdate } from '../use_message_create_or_update'
|
|
8
8
|
|
|
9
9
|
jest.mock('../../contexts/api_provider', () => {
|
|
10
|
-
const {
|
|
11
|
-
|
|
12
|
-
return {
|
|
13
|
-
chatQueryClient: new QueryClient({
|
|
14
|
-
defaultOptions: { queries: { gcTime: 0 }, mutations: { gcTime: 0 } },
|
|
15
|
-
}),
|
|
16
|
-
}
|
|
10
|
+
const { buildTestQueryClient } = require('../../__utils__/query_client')
|
|
11
|
+
return { chatQueryClient: buildTestQueryClient() }
|
|
17
12
|
})
|
|
18
13
|
|
|
19
14
|
const mockPost = jest.fn()
|
|
@@ -1,37 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { NextPageCursor } from '../types/api_primitives'
|
|
2
2
|
import { RequestData } from '../utils/client'
|
|
3
3
|
|
|
4
4
|
export const getNextPageParamFromMeta = (
|
|
5
5
|
next: NextPageCursor = {}
|
|
6
6
|
): Partial<RequestData> | undefined => {
|
|
7
|
-
if (next.idLt) return
|
|
8
|
-
if (next.idGt) return
|
|
9
|
-
if (next.idLte) return
|
|
10
|
-
if (next.idGte) return
|
|
7
|
+
if (next.idLt) return { where: { id_lt: next.idLt } }
|
|
8
|
+
if (next.idGt) return { where: { id_gt: next.idGt } }
|
|
9
|
+
if (next.idLte) return { where: { id_lte: next.idLte } }
|
|
10
|
+
if (next.idGte) return { where: { id_gte: next.idGte } }
|
|
11
11
|
if (next.offset) return { offset: Number(next.offset) }
|
|
12
12
|
return undefined
|
|
13
13
|
}
|
|
14
|
-
|
|
15
|
-
const withOrder = (param: Partial<RequestData>, order: NextPageCursor['order']) =>
|
|
16
|
-
order ? { ...param, order } : param
|
|
17
|
-
|
|
18
|
-
const BOUND_KEYS = ['id_lt', 'id_lte', 'id_gt', 'id_gte', 'id_around']
|
|
19
|
-
|
|
20
|
-
export const mergePageWhere = (
|
|
21
|
-
argsWhere: RequestData['where'] = {},
|
|
22
|
-
pageWhere: RequestData['where'] = {}
|
|
23
|
-
): RequestData['where'] => {
|
|
24
|
-
const pageHasCursor = BOUND_KEYS.some(key => key in pageWhere)
|
|
25
|
-
const base = pageHasCursor
|
|
26
|
-
? Object.fromEntries(Object.entries(argsWhere).filter(([key]) => !BOUND_KEYS.includes(key)))
|
|
27
|
-
: argsWhere
|
|
28
|
-
return { ...base, ...pageWhere }
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export const getNewerPageParam = (
|
|
32
|
-
page: ApiCollection<ResourceObject>,
|
|
33
|
-
pageParam: Partial<RequestData> | undefined
|
|
34
|
-
): Partial<RequestData> | undefined => {
|
|
35
|
-
const isAscendingPage = pageParam?.order === 'asc'
|
|
36
|
-
return getNextPageParamFromMeta(isAscendingPage ? page.meta?.next : page.meta?.prev)
|
|
37
|
-
}
|
package/src/hooks/use_api.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { ChatContext } from '../contexts/chat_context'
|
|
|
4
4
|
import { ApiCollection, ApiResource, ResourceObject } from '../types'
|
|
5
5
|
import { GetRequest, RequestData } from '../utils/client'
|
|
6
6
|
import { ResponseError, throwResponseError } from '../utils/response_error'
|
|
7
|
-
import { getNextPageParamFromMeta
|
|
7
|
+
import { getNextPageParamFromMeta } from './paginator_meta'
|
|
8
8
|
import { App, useApiClient } from './use_api_client'
|
|
9
9
|
import { getRequestQueryKey, RequestQueryKey } from './use_suspense_api'
|
|
10
10
|
|
|
@@ -61,11 +61,12 @@ export const useApiPaginator = <T extends ResourceObject>(
|
|
|
61
61
|
>({
|
|
62
62
|
queryKey: getRequestQueryKey(args),
|
|
63
63
|
queryFn: ({ pageParam }) => {
|
|
64
|
-
const
|
|
64
|
+
const pageParmWhere = pageParam?.where || {}
|
|
65
|
+
const argsWhere = args.data.where || {}
|
|
66
|
+
const where = { ...argsWhere, ...pageParmWhere }
|
|
65
67
|
const app = args.app || 'chat'
|
|
66
68
|
const offset = pageParam?.offset || args.data.offset
|
|
67
|
-
const
|
|
68
|
-
const data = { ...args.data, where, offset, order }
|
|
69
|
+
const data = { ...args.data, where, offset }
|
|
69
70
|
|
|
70
71
|
return apiClient[app].get<ApiCollection<T>>({ url: args.url, data }).catch(throwResponseError)
|
|
71
72
|
},
|
|
@@ -1,74 +1,119 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { useQueryClient } from '@tanstack/react-query'
|
|
2
|
+
import { useEffect, useMemo } from 'react'
|
|
3
|
+
import { chatQueryClient } from '../contexts/api_provider'
|
|
4
|
+
import { ApiCollection, MessageResource } from '../types'
|
|
5
|
+
import {
|
|
6
|
+
anchoredPageParam,
|
|
7
|
+
isBehindLiveEdge,
|
|
8
|
+
livePageParam,
|
|
9
|
+
markLiveIfCaughtUp,
|
|
10
|
+
MessagesWindow,
|
|
11
|
+
newerPageParam,
|
|
12
|
+
newestFirst,
|
|
13
|
+
} from '../utils/cache/messages_window'
|
|
3
14
|
import { replaceEqualById } from '../utils/replace_equal_by_id'
|
|
4
15
|
import { getMessagesQueryKey, getMessagesRequestArgs } from '../utils/request/get_messages'
|
|
5
|
-
import {
|
|
6
|
-
import { getNewerPageParam } from './paginator_meta'
|
|
16
|
+
import { useApiClient } from './use_api_client'
|
|
7
17
|
import { SuspensePaginatorOptions, useSuspensePaginator } from './use_suspense_api'
|
|
8
18
|
|
|
9
|
-
type
|
|
10
|
-
|
|
11
|
-
reply_root_id?: string | null
|
|
12
|
-
anchor?: string | null
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export const anchoredRetry = (failureCount: number, error: unknown) => {
|
|
16
|
-
const status = error instanceof ResponseError ? error.status : undefined
|
|
17
|
-
if (status === 404 || status === 429) return false
|
|
18
|
-
|
|
19
|
-
return failureCount < 3
|
|
20
|
-
}
|
|
19
|
+
type WindowOptions = { conversation_id: number; reply_root_id?: string | null }
|
|
20
|
+
type Options = WindowOptions & { anchor?: string | null }
|
|
21
21
|
|
|
22
22
|
export const useConversationMessages = (
|
|
23
23
|
{ conversation_id, reply_root_id, anchor }: Options,
|
|
24
24
|
opts?: SuspensePaginatorOptions
|
|
25
25
|
) => {
|
|
26
|
+
const queryClient = useQueryClient()
|
|
27
|
+
const apiClient = useApiClient()
|
|
28
|
+
const queryKey = getMessagesQueryKey({ conversation_id, reply_root_id })
|
|
29
|
+
const requestArgs = getMessagesRequestArgs({ conversation_id, reply_root_id })
|
|
26
30
|
const {
|
|
27
31
|
data,
|
|
32
|
+
infiniteData,
|
|
28
33
|
refetch,
|
|
29
34
|
isRefetching,
|
|
30
35
|
fetchNextPage,
|
|
31
|
-
hasNextPage,
|
|
32
36
|
fetchPreviousPage,
|
|
37
|
+
hasNextPage,
|
|
33
38
|
hasPreviousPage,
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
39
|
+
} = useSuspensePaginator<MessageResource>(requestArgs, {
|
|
40
|
+
structuralSharing: replaceEqualById,
|
|
41
|
+
initialPageParam: anchor ? anchoredPageParam(anchor) : livePageParam,
|
|
42
|
+
getPreviousPageParam: newerPageParam,
|
|
43
|
+
transformPage: newestFirst,
|
|
44
|
+
...opts,
|
|
45
|
+
})
|
|
46
|
+
const behind = isBehindLiveEdge(infiniteData)
|
|
47
|
+
|
|
48
|
+
const fetchOlder = () => fetchNextPage({ cancelRefetch: false })
|
|
49
|
+
|
|
50
|
+
const fetchNewer = async () => {
|
|
51
|
+
if (!hasPreviousPage) return
|
|
52
|
+
|
|
53
|
+
await fetchPreviousPage({ cancelRefetch: false })
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const fetchLiveWindow = async () => {
|
|
57
|
+
if (!behind) return
|
|
58
|
+
|
|
59
|
+
const page = await apiClient.chat.get<ApiCollection<MessageResource>>(requestArgs)
|
|
60
|
+
await queryClient.cancelQueries({ queryKey, exact: true })
|
|
61
|
+
queryClient.setQueryData<MessagesWindow>(queryKey, {
|
|
62
|
+
pages: [page],
|
|
63
|
+
pageParams: [livePageParam],
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
|
|
51
67
|
const messages = useMemo(
|
|
52
68
|
() =>
|
|
53
69
|
data
|
|
54
|
-
.filter(
|
|
55
|
-
message =>
|
|
56
|
-
(!message.deletedAt || message.replyRootId) &&
|
|
57
|
-
(message.attachments?.length || message.text?.length)
|
|
58
|
-
)
|
|
70
|
+
.filter(message => message.deletedAt || message.attachments?.length || message.text?.length)
|
|
59
71
|
.sort((a, b) => -a.id.localeCompare(b.id)),
|
|
60
72
|
[data]
|
|
61
73
|
)
|
|
62
74
|
|
|
75
|
+
// Once nothing newer remains, the top of the window is the live edge. Without this,
|
|
76
|
+
// a refetch would replay from the anchor and drop rows Jolt has added since.
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
const settled = markLiveIfCaughtUp(infiniteData)
|
|
79
|
+
|
|
80
|
+
if (settled !== infiniteData) {
|
|
81
|
+
queryClient.setQueryData(queryKey, settled)
|
|
82
|
+
}
|
|
83
|
+
}, [infiniteData, queryClient, queryKey])
|
|
84
|
+
|
|
85
|
+
useEnsureWindowHoldsAnchor({ conversation_id, reply_root_id, anchor })
|
|
86
|
+
|
|
63
87
|
return {
|
|
64
88
|
messages,
|
|
89
|
+
behind,
|
|
65
90
|
refetch,
|
|
66
91
|
isRefetching,
|
|
67
|
-
fetchNextPage,
|
|
92
|
+
fetchNextPage: fetchOlder,
|
|
93
|
+
fetchNewer,
|
|
94
|
+
fetchLiveWindow,
|
|
68
95
|
hasNextPage,
|
|
69
|
-
fetchPreviousPage,
|
|
70
|
-
hasPreviousPage,
|
|
71
|
-
isFetchingPreviousPage,
|
|
72
96
|
queryKey,
|
|
73
97
|
}
|
|
74
98
|
}
|
|
99
|
+
|
|
100
|
+
const useEnsureWindowHoldsAnchor = ({ conversation_id, reply_root_id, anchor }: Options) => {
|
|
101
|
+
const queryClient = useQueryClient()
|
|
102
|
+
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
if (!anchor) return
|
|
105
|
+
|
|
106
|
+
const queryKey = getMessagesQueryKey({ conversation_id, reply_root_id })
|
|
107
|
+
const cached = queryClient.getQueryData<MessagesWindow>(queryKey)
|
|
108
|
+
const holdsAnchor = cached?.pages.some(page => page.data.some(m => m.id === anchor))
|
|
109
|
+
if (cached && !holdsAnchor) {
|
|
110
|
+
queryClient.resetQueries({ queryKey, exact: true })
|
|
111
|
+
}
|
|
112
|
+
}, [anchor, conversation_id, reply_root_id, queryClient])
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export const dropMessagesWindowIfBehind = (options: WindowOptions) => {
|
|
116
|
+
const queryKey = getMessagesQueryKey(options)
|
|
117
|
+
const cached = chatQueryClient.getQueryData<MessagesWindow>(queryKey)
|
|
118
|
+
if (isBehindLiveEdge(cached)) chatQueryClient.removeQueries({ queryKey, exact: true })
|
|
119
|
+
}
|
|
@@ -12,80 +12,45 @@ import {
|
|
|
12
12
|
updateCacheWithIndividualMessage,
|
|
13
13
|
updateCacheWithReaction,
|
|
14
14
|
updateCacheReplyRoots,
|
|
15
|
+
getThreadedMessagesQueryKey,
|
|
15
16
|
} from '../utils/cache/messages_cache'
|
|
17
|
+
import { isBehindLiveEdge, MessagesWindow } from '../utils/cache/messages_window'
|
|
16
18
|
import { transformMessageEventDataToMessageResource } from '../utils/jolt/transform_message_event_data_to_message_resource'
|
|
17
19
|
import { completeMessageCreationTracking } from '../utils/performance_tracking'
|
|
18
20
|
import { getMessageQueryKey } from '../utils/request/get_message'
|
|
19
|
-
import {
|
|
21
|
+
import { getMessagesRequestArgs } from '../utils/request/get_messages'
|
|
20
22
|
import { useApiClient } from './use_api_client'
|
|
21
23
|
import { useCurrentPerson } from './use_current_person'
|
|
22
24
|
import { useJoltChannel, useJoltEvent } from './use_jolt'
|
|
25
|
+
import { getRequestQueryKey } from './use_suspense_api'
|
|
23
26
|
import { TYPING_TIMEOUT_INTERVAL, useTypingStatusCache } from './use_typing_status_cache'
|
|
24
27
|
|
|
25
|
-
export type MessagesJoltEvent =
|
|
26
|
-
| 'message.created'
|
|
27
|
-
| 'message.updated'
|
|
28
|
-
| 'message.destroyed'
|
|
29
|
-
| 'reaction'
|
|
30
|
-
|
|
31
|
-
export interface MessagesCache {
|
|
32
|
-
queryKey: unknown[]
|
|
33
|
-
canUpdate?: (event: MessagesJoltEvent) => boolean
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface MessagesView {
|
|
37
|
-
replyRootId: string | null
|
|
38
|
-
cache: MessagesCache
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
type MessageEventData = { sort_key: string; reply_root_id?: string | null }
|
|
42
|
-
|
|
43
28
|
interface Props {
|
|
44
29
|
conversationId: number
|
|
45
|
-
replyRootId?: string | null
|
|
46
|
-
messageCache?: MessagesCache
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const belongsToView = (replyRootId: string | null, data: MessageEventData) =>
|
|
50
|
-
replyRootId === null || data.reply_root_id === replyRootId || data.sort_key === replyRootId
|
|
51
|
-
|
|
52
|
-
export const queryKeysForEvent = (
|
|
53
|
-
event: MessagesJoltEvent,
|
|
54
|
-
view: MessagesView,
|
|
55
|
-
data: MessageEventData
|
|
56
|
-
) => {
|
|
57
|
-
const accepted = !view.cache.canUpdate || view.cache.canUpdate(event)
|
|
58
|
-
return belongsToView(view.replyRootId, data) && accepted ? [view.cache.queryKey] : []
|
|
59
30
|
}
|
|
60
31
|
|
|
61
|
-
export function useConversationMessagesJoltEvents({
|
|
62
|
-
conversationId,
|
|
63
|
-
replyRootId = null,
|
|
64
|
-
messageCache,
|
|
65
|
-
}: Props) {
|
|
32
|
+
export function useConversationMessagesJoltEvents({ conversationId }: Props) {
|
|
66
33
|
const queryClient = useQueryClient()
|
|
67
34
|
const currentPerson = useCurrentPerson()
|
|
68
35
|
const joltChannel = useJoltChannel(`chat.conversations.${conversationId}`)
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
cache: messageCache ?? {
|
|
72
|
-
queryKey: getMessagesQueryKey({
|
|
73
|
-
conversation_id: conversationId,
|
|
74
|
-
reply_root_id: replyRootId,
|
|
75
|
-
}),
|
|
76
|
-
},
|
|
77
|
-
}
|
|
36
|
+
const messagesRequestArgs = getMessagesRequestArgs({ conversation_id: conversationId })
|
|
37
|
+
const messagesQueryKey = getRequestQueryKey(messagesRequestArgs)
|
|
78
38
|
const apiClient = useApiClient()
|
|
79
39
|
|
|
80
40
|
const { addTypingEvent, removeTypingEventById, removeAllTypingEventsByAuthorId } =
|
|
81
41
|
useTypingStatusCache(conversationId)
|
|
82
42
|
|
|
43
|
+
// While behind, a new message would land after a gap; the newer fetch brings it in.
|
|
44
|
+
const acceptsCreates = (queryKey: unknown[]) =>
|
|
45
|
+
!isBehindLiveEdge(queryClient.getQueryData<MessagesWindow>(queryKey))
|
|
46
|
+
|
|
83
47
|
const handleMessageUpdateOrCreate = async (e: MessageCreatedEvent | MessageUpdatedEvent) => {
|
|
84
48
|
const { data } = e.data
|
|
85
49
|
const message = transformMessageEventDataToMessageResource({
|
|
86
50
|
data,
|
|
87
51
|
currentPersonId: currentPerson.id,
|
|
88
52
|
})
|
|
53
|
+
const isCreate = e.event === 'message.created'
|
|
89
54
|
const isReplyRootUpdate = e.event === 'message.updated' && data.reply_root_id === data.sort_key
|
|
90
55
|
|
|
91
56
|
if (e.event === 'message.created' && data.author_id) {
|
|
@@ -95,12 +60,25 @@ export function useConversationMessagesJoltEvents({
|
|
|
95
60
|
}
|
|
96
61
|
}
|
|
97
62
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
updateCacheWithMessage(queryClient,
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
63
|
+
// Update the main conversation cache
|
|
64
|
+
if (!isCreate || acceptsCreates(messagesQueryKey)) {
|
|
65
|
+
updateCacheWithMessage(queryClient, messagesQueryKey, message, e.event)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// If message has a reply_root_id, also update the threaded cache
|
|
69
|
+
if (data.reply_root_id) {
|
|
70
|
+
const threadedMessagesQueryKey = getThreadedMessagesQueryKey(
|
|
71
|
+
conversationId,
|
|
72
|
+
data.reply_root_id
|
|
73
|
+
)
|
|
74
|
+
if (!isCreate || acceptsCreates(threadedMessagesQueryKey)) {
|
|
75
|
+
updateCacheWithMessage(queryClient, threadedMessagesQueryKey, message, e.event)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (isReplyRootUpdate) {
|
|
79
|
+
updateCacheReplyRoots(queryClient, messagesQueryKey, message)
|
|
80
|
+
updateCacheReplyRoots(queryClient, threadedMessagesQueryKey, message)
|
|
81
|
+
}
|
|
104
82
|
}
|
|
105
83
|
|
|
106
84
|
// Update individual message query cache so reply shadow messages update immediately
|
|
@@ -120,21 +98,34 @@ export function useConversationMessagesJoltEvents({
|
|
|
120
98
|
currentPersonId: currentPerson.id,
|
|
121
99
|
})
|
|
122
100
|
|
|
123
|
-
|
|
124
|
-
|
|
101
|
+
// Update the main conversation cache
|
|
102
|
+
queryClient.setQueryData<QueryData>(messagesQueryKey, prev =>
|
|
103
|
+
deleteRecordInPagesData({ data: prev, record: message })
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
// If message has a reply_root_id, also update the threaded cache
|
|
107
|
+
if (data.reply_root_id) {
|
|
108
|
+
const threadedMessagesQueryKey = getThreadedMessagesQueryKey(
|
|
109
|
+
conversationId,
|
|
110
|
+
data.reply_root_id
|
|
111
|
+
)
|
|
112
|
+
queryClient.setQueryData<QueryData>(threadedMessagesQueryKey, prev =>
|
|
125
113
|
deleteRecordInPagesData({ data: prev, record: message })
|
|
126
114
|
)
|
|
127
|
-
|
|
115
|
+
}
|
|
128
116
|
}
|
|
129
117
|
|
|
130
118
|
const handleReactionJoltEvent = async (e: JoltReactionEvent) => {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
119
|
+
// Update the main conversation cache and capture the reply_root_id if present
|
|
120
|
+
updateCacheWithReaction(queryClient, messagesQueryKey, e, currentPerson.id)
|
|
121
|
+
|
|
122
|
+
const replyRootId = e.data.data.reply_root_id
|
|
123
|
+
|
|
124
|
+
// If the message has a reply_root_id, also update the threaded cache
|
|
125
|
+
if (replyRootId) {
|
|
126
|
+
const threadedMessagesQueryKey = getThreadedMessagesQueryKey(conversationId, replyRootId)
|
|
127
|
+
updateCacheWithReaction(queryClient, threadedMessagesQueryKey, e, currentPerson.id)
|
|
134
128
|
}
|
|
135
|
-
queryKeysForEvent('reaction', view, data).forEach(queryKey =>
|
|
136
|
-
updateCacheWithReaction(queryClient, queryKey, e, currentPerson.id)
|
|
137
|
-
)
|
|
138
129
|
}
|
|
139
130
|
|
|
140
131
|
const handleTypingEvent = async (e: JoltTypingEvent) => {
|
|
@@ -55,8 +55,9 @@ export const availableFeatures = {
|
|
|
55
55
|
conversation_safety_lock: 'ROLLOUT_conversation_safety_lock',
|
|
56
56
|
video_moderation: 'ROLLOUT_MOBILE_video_moderation',
|
|
57
57
|
dm_search_clear_fix: 'ROLLOUT_MOBILE_dm_search_clear_fix',
|
|
58
|
-
search: '
|
|
59
|
-
|
|
58
|
+
search: 'QA_MOBILE_search',
|
|
59
|
+
search_rollout: 'ROLLOUT_MOBILE_search',
|
|
60
|
+
} as const satisfies Record<string, `${'QA' | 'ROLLOUT'}_${string}`>
|
|
60
61
|
|
|
61
62
|
const stableEmptyFeatures: ApiCollection<FeatureResource> = {
|
|
62
63
|
data: [],
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { InfiniteData, useMutation } from '@tanstack/react-query'
|
|
2
2
|
import { chatQueryClient } from '../contexts/api_provider'
|
|
3
|
-
import { useConversationContext } from '../contexts/conversation_context'
|
|
4
3
|
import { ApiCollection, ApiResource, MessageResource } from '../types'
|
|
5
4
|
import { DenormalizedAttachmentResourceForCreate } from '../types/resources/denormalized_attachment_resource_for_create'
|
|
6
5
|
import {
|
|
@@ -36,12 +35,6 @@ export function useMessageCreateOrUpdate({ conversationId, message, replyRootId
|
|
|
36
35
|
const isEditing = !isNewMessage(message)
|
|
37
36
|
const apiClient = useApiClient()
|
|
38
37
|
const currentPerson = useCurrentPerson()
|
|
39
|
-
const { messagesQueryKey } = useConversationContext()
|
|
40
|
-
const liveQueryKey = getMessagesQueryKey({
|
|
41
|
-
conversation_id: conversationId,
|
|
42
|
-
reply_root_id: replyRootId,
|
|
43
|
-
})
|
|
44
|
-
const queryKey = isEditing ? (messagesQueryKey ?? liveQueryKey) : liveQueryKey
|
|
45
38
|
|
|
46
39
|
const mutation = useMutation({
|
|
47
40
|
mutationFn: ({ text, attachments, idempotentKey }: ResolvedMessageMutationVariables) => {
|
|
@@ -86,7 +79,6 @@ export function useMessageCreateOrUpdate({ conversationId, message, replyRootId
|
|
|
86
79
|
message,
|
|
87
80
|
text,
|
|
88
81
|
replyRootId,
|
|
89
|
-
queryKey,
|
|
90
82
|
})
|
|
91
83
|
|
|
92
84
|
return { message: optimisticMessage }
|
|
@@ -110,6 +102,10 @@ export function useMessageCreateOrUpdate({ conversationId, message, replyRootId
|
|
|
110
102
|
|
|
111
103
|
// Add error to the optimistic message from the cache on error
|
|
112
104
|
if (optimisticMessage) {
|
|
105
|
+
const queryKey = getMessagesQueryKey({
|
|
106
|
+
conversation_id: conversationId,
|
|
107
|
+
reply_root_id: replyRootId,
|
|
108
|
+
})
|
|
113
109
|
chatQueryClient.setQueryData(
|
|
114
110
|
queryKey,
|
|
115
111
|
(data: InfiniteData<ApiCollection<MessageResource>> | undefined) =>
|
|
@@ -129,6 +125,10 @@ export function useMessageCreateOrUpdate({ conversationId, message, replyRootId
|
|
|
129
125
|
const { message: optimisticMessage } = context || {}
|
|
130
126
|
const updatedMessage = result.data
|
|
131
127
|
type QueryData = InfiniteData<ApiCollection<MessageResource>>
|
|
128
|
+
const queryKey = getMessagesQueryKey({
|
|
129
|
+
conversation_id: conversationId,
|
|
130
|
+
reply_root_id: replyRootId,
|
|
131
|
+
})
|
|
132
132
|
|
|
133
133
|
// First remove the optimistic message if it exists
|
|
134
134
|
if (optimisticMessage) {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { InfiniteData, useMutation, useQueryClient } from '@tanstack/react-query'
|
|
2
2
|
import { useCallback } from 'react'
|
|
3
3
|
import { Alert } from 'react-native'
|
|
4
|
-
import { useConversationContext } from '../contexts/conversation_context'
|
|
5
4
|
import { ApiCollection, ApiResource, MessageResource } from '../types'
|
|
6
5
|
import { ReactionCountResource } from '../types/resources/reaction'
|
|
7
6
|
import { updateRecordInPagesData, REACTION_EMOJIS } from '../utils'
|
|
@@ -31,8 +30,7 @@ export function useMessageReactionToggle({
|
|
|
31
30
|
}: UseMessageReactionToggleProps) {
|
|
32
31
|
const apiClient = useApiClient()
|
|
33
32
|
const queryClient = useQueryClient()
|
|
34
|
-
const {
|
|
35
|
-
const queryKey = messagesQueryKey ?? getMessagesQueryKey({ conversation_id })
|
|
33
|
+
const queryKey = getMessagesQueryKey({ conversation_id })
|
|
36
34
|
const message_id = message.id
|
|
37
35
|
|
|
38
36
|
const toggleReaction = useCallback(
|
|
@@ -10,7 +10,7 @@ import { FailedResponse } from '../types/api_primitives'
|
|
|
10
10
|
import { Log } from '../utils'
|
|
11
11
|
import { GetRequest, RequestData } from '../utils/client'
|
|
12
12
|
import { throwResponseError } from '../utils/response_error'
|
|
13
|
-
import { getNextPageParamFromMeta
|
|
13
|
+
import { getNextPageParamFromMeta } from './paginator_meta'
|
|
14
14
|
import { App, useApiClient } from './use_api_client'
|
|
15
15
|
|
|
16
16
|
interface SuspenseGetOptions extends GetRequest {
|
|
@@ -47,17 +47,23 @@ export const useSuspenseGet = <T extends ResourceObject | ResourceObject[]>(
|
|
|
47
47
|
export type SuspensePaginatorOptions = Omit<
|
|
48
48
|
AnyUseSuspenseInfiniteQueryOptions,
|
|
49
49
|
'getNextPageParam' | 'initialPageParam' | 'queryFn' | 'queryKey'
|
|
50
|
-
>
|
|
50
|
+
> & {
|
|
51
|
+
initialPageParam?: Partial<RequestData>
|
|
52
|
+
transformPage?(
|
|
53
|
+
page: ApiCollection<ResourceObject>,
|
|
54
|
+
pageParam: Partial<RequestData> | undefined
|
|
55
|
+
): ApiCollection<ResourceObject>
|
|
56
|
+
}
|
|
51
57
|
|
|
52
58
|
export const useSuspensePaginator = <T extends ResourceObject>(
|
|
53
59
|
args: SuspenseGetOptions,
|
|
54
|
-
opts
|
|
60
|
+
{ transformPage, ...opts }: SuspensePaginatorOptions = {}
|
|
55
61
|
) => {
|
|
56
62
|
const apiClient = useApiClient()
|
|
57
63
|
const query = useSuspenseInfiniteQuery<
|
|
58
64
|
ApiCollection<T>,
|
|
59
65
|
Response,
|
|
60
|
-
InfiniteData<ApiCollection<T
|
|
66
|
+
InfiniteData<ApiCollection<T>, Partial<RequestData> | undefined>,
|
|
61
67
|
any,
|
|
62
68
|
Partial<RequestData> | undefined
|
|
63
69
|
>({
|
|
@@ -65,11 +71,13 @@ export const useSuspensePaginator = <T extends ResourceObject>(
|
|
|
65
71
|
queryFn: ({ pageParam }) => {
|
|
66
72
|
Log.info('useSuspenseGet', { pageParam })
|
|
67
73
|
|
|
68
|
-
const
|
|
74
|
+
const pageParmWhere = pageParam?.where || {}
|
|
75
|
+
const argsWhere = args.data.where || {}
|
|
76
|
+
const where = { ...argsWhere, ...pageParmWhere }
|
|
69
77
|
const app = args.app || 'chat'
|
|
70
78
|
const offset = pageParam?.offset || args.data.offset
|
|
71
79
|
const order = pageParam?.order || args.data.order
|
|
72
|
-
const data = { ...args.data, where, offset, order }
|
|
80
|
+
const data = { ...args.data, where, offset, ...(order && { order }) }
|
|
73
81
|
|
|
74
82
|
return apiClient[app]
|
|
75
83
|
.get<ApiCollection<T>>({
|
|
@@ -77,16 +85,17 @@ export const useSuspensePaginator = <T extends ResourceObject>(
|
|
|
77
85
|
data,
|
|
78
86
|
})
|
|
79
87
|
.catch(throwResponseError)
|
|
88
|
+
.then(page => (transformPage ? (transformPage(page, pageParam) as ApiCollection<T>) : page))
|
|
80
89
|
},
|
|
81
90
|
initialPageParam: {} as Partial<RequestData>,
|
|
82
91
|
getNextPageParam: lastPage => getNextPageParamFromMeta(lastPage.meta?.next),
|
|
83
|
-
...
|
|
92
|
+
...opts,
|
|
84
93
|
})
|
|
85
94
|
|
|
86
95
|
const data: T[] = query.data?.pages.flatMap(page => page.data) || []
|
|
87
96
|
const totalCount = query.data?.pages[0]?.meta?.totalCount || data.length
|
|
88
97
|
|
|
89
|
-
return { ...query, data, totalCount }
|
|
98
|
+
return { ...query, data, totalCount, infiniteData: query.data }
|
|
90
99
|
}
|
|
91
100
|
|
|
92
101
|
export type RequestQueryKey = [
|