@planningcenter/chat-react-native 4.8.0-rc.1 → 4.8.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/conversations/bulk_conversation_actions_bar.d.ts +2 -0
- package/build/components/conversations/bulk_conversation_actions_bar.d.ts.map +1 -0
- package/build/components/conversations/bulk_conversation_actions_bar.js +215 -0
- package/build/components/conversations/bulk_conversation_actions_bar.js.map +1 -0
- package/build/components/conversations/conversation_actions.d.ts +2 -1
- package/build/components/conversations/conversation_actions.d.ts.map +1 -1
- package/build/components/conversations/conversation_actions.js +173 -130
- package/build/components/conversations/conversation_actions.js.map +1 -1
- package/build/components/conversations/conversation_preview.d.ts.map +1 -1
- package/build/components/conversations/conversation_preview.js +168 -110
- package/build/components/conversations/conversation_preview.js.map +1 -1
- package/build/components/conversations/conversations.d.ts.map +1 -1
- package/build/components/conversations/conversations.js +50 -48
- package/build/components/conversations/conversations.js.map +1 -1
- package/build/components/display/checkbox.d.ts +8 -0
- package/build/components/display/checkbox.d.ts.map +1 -0
- package/build/components/display/checkbox.js +87 -0
- package/build/components/display/checkbox.js.map +1 -0
- package/build/components/display/icon.d.ts +3 -1
- package/build/components/display/icon.d.ts.map +1 -1
- package/build/components/display/icon.js +2 -0
- package/build/components/display/icon.js.map +1 -1
- package/build/components/display/index.d.ts +1 -0
- package/build/components/display/index.d.ts.map +1 -1
- package/build/components/display/index.js +1 -0
- package/build/components/display/index.js.map +1 -1
- package/build/contexts/api_provider.d.ts.map +1 -1
- package/build/contexts/api_provider.js +2 -0
- package/build/contexts/api_provider.js.map +1 -1
- package/build/contexts/chat_context.d.ts +0 -2
- package/build/contexts/chat_context.d.ts.map +1 -1
- package/build/contexts/chat_context.js +16 -93
- package/build/contexts/chat_context.js.map +1 -1
- package/build/contexts/conversations_context.d.ts +5 -0
- package/build/contexts/conversations_context.d.ts.map +1 -1
- package/build/contexts/conversations_context.js +121 -32
- package/build/contexts/conversations_context.js.map +1 -1
- package/build/contexts/session_context.d.ts +0 -1
- package/build/contexts/session_context.d.ts.map +1 -1
- package/build/contexts/session_context.js +3 -0
- package/build/contexts/session_context.js.map +1 -1
- package/build/hooks/groups/use_group_chat_conversation_payload.d.ts +11 -1
- package/build/hooks/groups/use_group_chat_conversation_payload.d.ts.map +1 -1
- package/build/hooks/groups/use_group_chat_conversation_payload.js +92 -47
- package/build/hooks/groups/use_group_chat_conversation_payload.js.map +1 -1
- package/build/hooks/groups/use_group_members_for_new_conversation.d.ts +27 -25
- package/build/hooks/groups/use_group_members_for_new_conversation.d.ts.map +1 -1
- package/build/hooks/groups/use_group_members_for_new_conversation.js +125 -110
- package/build/hooks/groups/use_group_members_for_new_conversation.js.map +1 -1
- package/build/hooks/groups/use_groups_conversation_create.d.ts.map +1 -1
- package/build/hooks/groups/use_groups_conversation_create.js +39 -31
- package/build/hooks/groups/use_groups_conversation_create.js.map +1 -1
- package/build/hooks/groups/use_groups_proxy.d.ts +2 -0
- package/build/hooks/groups/use_groups_proxy.d.ts.map +1 -0
- package/build/hooks/groups/use_groups_proxy.js +18 -0
- package/build/hooks/groups/use_groups_proxy.js.map +1 -0
- package/build/hooks/use_bulk_conversation_actions.d.ts +8 -0
- package/build/hooks/use_bulk_conversation_actions.d.ts.map +1 -0
- package/build/hooks/use_bulk_conversation_actions.js +309 -0
- package/build/hooks/use_bulk_conversation_actions.js.map +1 -0
- package/build/hooks/use_conversation_jolt_events.d.ts.map +1 -1
- package/build/hooks/use_conversation_jolt_events.js +3 -2
- package/build/hooks/use_conversation_jolt_events.js.map +1 -1
- package/build/hooks/use_conversations_actions.d.ts +9 -9
- package/build/hooks/use_conversations_actions.d.ts.map +1 -1
- package/build/hooks/use_conversations_actions.js +12 -7
- package/build/hooks/use_conversations_actions.js.map +1 -1
- package/build/hooks/use_conversations_cache.d.ts +15 -5
- package/build/hooks/use_conversations_cache.d.ts.map +1 -1
- package/build/hooks/use_conversations_cache.js +170 -122
- package/build/hooks/use_conversations_cache.js.map +1 -1
- package/build/hooks/use_conversations_jolt_events.d.ts.map +1 -1
- package/build/hooks/use_conversations_jolt_events.js +47 -20
- package/build/hooks/use_conversations_jolt_events.js.map +1 -1
- package/build/hooks/use_features.d.ts +1 -0
- package/build/hooks/use_features.d.ts.map +1 -1
- package/build/hooks/use_features.js +1 -0
- package/build/hooks/use_features.js.map +1 -1
- package/build/hooks/use_groups_groups.d.ts.map +1 -1
- package/build/hooks/use_groups_groups.js +25 -9
- package/build/hooks/use_groups_groups.js.map +1 -1
- package/build/hooks/use_mark_latest_message_read.d.ts.map +1 -1
- package/build/hooks/use_mark_latest_message_read.js +5 -4
- package/build/hooks/use_mark_latest_message_read.js.map +1 -1
- package/build/hooks/use_new_conversation_from_filter.d.ts.map +1 -1
- package/build/hooks/use_new_conversation_from_filter.js +11 -1
- package/build/hooks/use_new_conversation_from_filter.js.map +1 -1
- package/build/hooks/use_product_analytics.d.ts.map +1 -1
- package/build/hooks/use_product_analytics.js +93 -75
- package/build/hooks/use_product_analytics.js.map +1 -1
- package/build/navigation/index.d.ts.map +1 -1
- package/build/navigation/index.js +5 -7
- package/build/navigation/index.js.map +1 -1
- package/build/screens/conversation_actions_screen.d.ts +2 -0
- package/build/screens/conversation_actions_screen.d.ts.map +1 -1
- package/build/screens/conversation_actions_screen.js +380 -46
- package/build/screens/conversation_actions_screen.js.map +1 -1
- package/build/screens/conversation_filter_recipients/components/checkbox_row.d.ts.map +1 -1
- package/build/screens/conversation_filter_recipients/components/checkbox_row.js +9 -64
- package/build/screens/conversation_filter_recipients/components/checkbox_row.js.map +1 -1
- package/build/screens/conversation_new/components/groups_form.d.ts.map +1 -1
- package/build/screens/conversation_new/components/groups_form.js +96 -93
- package/build/screens/conversation_new/components/groups_form.js.map +1 -1
- package/build/screens/conversations/conversations_screen.d.ts +5 -0
- package/build/screens/conversations/conversations_screen.d.ts.map +1 -1
- package/build/screens/conversations/conversations_screen.js +156 -31
- package/build/screens/conversations/conversations_screen.js.map +1 -1
- package/build/types/jolt_events/index.d.ts +2 -0
- package/build/types/jolt_events/index.d.ts.map +1 -1
- package/build/types/jolt_events/index.js.map +1 -1
- package/build/types/jolt_events/person_events.d.ts +9 -0
- package/build/types/jolt_events/person_events.d.ts.map +1 -0
- package/build/types/jolt_events/person_events.js +2 -0
- package/build/types/jolt_events/person_events.js.map +1 -0
- package/build/types/resources/groups/groups_group_resource.d.ts +1 -1
- package/build/types/resources/groups/groups_group_resource.d.ts.map +1 -1
- package/build/types/resources/groups/groups_group_resource.js.map +1 -1
- package/build/types/resources/groups/groups_membership_resource.d.ts +11 -0
- package/build/types/resources/groups/groups_membership_resource.d.ts.map +1 -0
- package/build/types/resources/groups/groups_membership_resource.js +2 -0
- package/build/types/resources/groups/groups_membership_resource.js.map +1 -0
- package/build/types/resources/groups/index.d.ts +1 -0
- package/build/types/resources/groups/index.d.ts.map +1 -1
- package/build/types/resources/groups/index.js +1 -0
- package/build/types/resources/groups/index.js.map +1 -1
- package/build/utils/jolt/parse_stream_user_event_type.d.ts +3 -0
- package/build/utils/jolt/parse_stream_user_event_type.d.ts.map +1 -0
- package/build/utils/jolt/parse_stream_user_event_type.js +9 -0
- package/build/utils/jolt/parse_stream_user_event_type.js.map +1 -0
- package/build/utils/native_adapters/configuration.d.ts +3 -0
- package/build/utils/native_adapters/configuration.d.ts.map +1 -1
- package/build/utils/native_adapters/configuration.js +3 -0
- package/build/utils/native_adapters/configuration.js.map +1 -1
- package/build/utils/native_adapters/index.d.ts +1 -0
- package/build/utils/native_adapters/index.d.ts.map +1 -1
- package/build/utils/native_adapters/index.js +1 -0
- package/build/utils/native_adapters/index.js.map +1 -1
- package/build/utils/native_adapters/updates.d.ts +11 -0
- package/build/utils/native_adapters/updates.d.ts.map +1 -0
- package/build/utils/native_adapters/updates.js +8 -0
- package/build/utils/native_adapters/updates.js.map +1 -0
- package/build/utils/product_analytics_token.d.ts +12 -0
- package/build/utils/product_analytics_token.d.ts.map +1 -0
- package/build/utils/product_analytics_token.js +84 -0
- package/build/utils/product_analytics_token.js.map +1 -0
- package/build/utils/throttled_refresh.d.ts +7 -0
- package/build/utils/throttled_refresh.d.ts.map +1 -0
- package/build/utils/throttled_refresh.js +42 -0
- package/build/utils/throttled_refresh.js.map +1 -0
- package/package.json +2 -2
- package/src/__tests__/contexts/session_context.tsx +19 -39
- package/src/__tests__/hooks/useTheme.tsx +0 -7
- package/src/__tests__/hooks/use_async_storage.test.tsx +1 -87
- package/src/__tests__/hooks/use_attachment_uploader.test.tsx +0 -11
- package/src/__tests__/hooks/use_font_scale.test.ts +0 -25
- package/src/__tests__/hooks/use_group_chat_conversation_payload.test.tsx +31 -0
- package/src/__tests__/utils/cache/page_mutations.ts +3 -68
- package/src/__tests__/utils/client.ts +6 -15
- package/src/__tests__/utils/native_adapters/configuration.ts +15 -50
- package/src/__tests__/utils/parse_simple_markdown.ts +0 -6
- package/src/__tests__/utils/session.ts +0 -11
- package/src/__tests__/utils/theme.ts +0 -17
- package/src/components/conversations/__tests__/bulk_conversation_actions_bar.test.tsx +95 -0
- package/src/components/conversations/__tests__/conversation_actions.test.tsx +35 -3
- package/src/components/conversations/__tests__/conversation_preview.test.tsx +69 -3
- package/src/components/conversations/__tests__/conversations.test.tsx +4 -1
- package/src/components/conversations/bulk_conversation_actions_bar.tsx +140 -0
- package/src/components/conversations/conversation_actions.tsx +17 -9
- package/src/components/conversations/conversation_preview.tsx +20 -2
- package/src/components/conversations/conversations.tsx +2 -1
- package/src/components/display/__tests__/filter_dropdown.test.tsx +1 -19
- package/src/components/display/__tests__/icon_tile.test.tsx +24 -7
- package/src/components/display/checkbox.tsx +47 -0
- package/src/components/display/icon.tsx +3 -0
- package/src/components/display/index.ts +1 -0
- package/src/components/page/__tests__/component_error_boundary.test.tsx +0 -12
- package/src/contexts/__tests__/conversations_context.test.tsx +86 -0
- package/src/contexts/api_provider.tsx +2 -0
- package/src/contexts/chat_context.tsx +2 -58
- package/src/contexts/conversations_context.tsx +69 -2
- package/src/contexts/session_context.tsx +3 -1
- package/src/hooks/__tests__/jolt_refresh_throttling.test.tsx +194 -0
- package/src/hooks/__tests__/use_bulk_conversation_actions.test.tsx +119 -0
- package/src/hooks/__tests__/use_conversations_jolt_events.test.tsx +1 -0
- package/src/hooks/__tests__/use_conversations_mark_read.test.tsx +38 -12
- package/src/hooks/__tests__/use_hide_conversation.test.tsx +15 -3
- package/src/hooks/__tests__/use_mark_latest_message_read.test.tsx +23 -2
- package/src/hooks/__tests__/use_product_analytics.test.tsx +304 -0
- package/src/hooks/groups/use_group_chat_conversation_payload.ts +30 -12
- package/src/hooks/groups/use_group_members_for_new_conversation.ts +49 -18
- package/src/hooks/groups/use_groups_conversation_create.ts +10 -14
- package/src/hooks/groups/use_groups_proxy.ts +6 -0
- package/src/hooks/use_bulk_conversation_actions.ts +173 -0
- package/src/hooks/use_conversation_jolt_events.ts +2 -1
- package/src/hooks/use_conversations_actions.ts +7 -4
- package/src/hooks/use_conversations_cache.ts +33 -26
- package/src/hooks/use_conversations_jolt_events.ts +24 -10
- package/src/hooks/use_features.ts +1 -0
- package/src/hooks/use_groups_groups.ts +21 -16
- package/src/hooks/use_mark_latest_message_read.ts +6 -5
- package/src/hooks/use_new_conversation_from_filter.ts +13 -5
- package/src/hooks/use_product_analytics.ts +74 -41
- package/src/navigation/index.tsx +8 -8
- package/src/screens/__tests__/conversation_actions_screen.test.tsx +48 -6
- package/src/screens/__tests__/conversation_details_screen.test.tsx +0 -19
- package/src/screens/avatar_picker/__tests__/avatar_picker_state.test.ts +0 -14
- package/src/screens/conversation_actions_screen.tsx +145 -12
- package/src/screens/conversation_filter_recipients/components/checkbox_row.tsx +3 -35
- package/src/screens/conversation_new/components/groups_form.tsx +11 -9
- package/src/screens/conversations/__tests__/conversations_screen.test.tsx +35 -9
- package/src/screens/conversations/components/__tests__/filter_dropdown_list_header_component.test.tsx +0 -8
- package/src/screens/conversations/components/__tests__/search_list_header_component.test.tsx +0 -10
- package/src/screens/conversations/conversations_screen.tsx +67 -3
- package/src/screens/search/components/__tests__/search_results_list.test.tsx +1 -0
- package/src/types/jolt_events/index.ts +2 -0
- package/src/types/jolt_events/person_events.ts +16 -0
- package/src/types/resources/groups/groups_group_resource.ts +1 -1
- package/src/types/resources/groups/groups_membership_resource.ts +10 -0
- package/src/types/resources/groups/index.ts +1 -0
- package/src/utils/__tests__/attachment_kind.test.ts +0 -4
- package/src/utils/__tests__/convert_attachments_for_create.test.ts +0 -28
- package/src/utils/__tests__/is_input_editable.test.ts +1 -2
- package/src/utils/__tests__/message_text.test.ts +0 -5
- package/src/utils/__tests__/session_manager.test.ts +0 -83
- package/src/utils/__tests__/sha_256.test.ts +0 -34
- package/src/utils/__tests__/throttled_refresh.test.ts +101 -0
- package/src/utils/__tests__/uri.test.ts +51 -0
- package/src/utils/jolt/__tests__/parse_stream_user_event_type.test.ts +27 -0
- package/src/utils/jolt/parse_stream_user_event_type.ts +16 -0
- package/src/utils/native_adapters/configuration.ts +5 -0
- package/src/utils/native_adapters/index.ts +1 -0
- package/src/utils/native_adapters/updates.ts +16 -0
- package/src/utils/product_analytics_token.ts +120 -0
- package/src/utils/throttled_refresh.ts +62 -0
- package/src/__tests__/event-polyfill.test.ts +0 -313
- package/src/__tests__/index.tsx +0 -5
- package/src/__tests__/jest.ts +0 -11
- package/src/__tests__/utils/uri.ts +0 -65
- package/src/components/display/__tests__/version_footer.test.tsx +0 -48
- package/src/screens/search/components/__tests__/search_blank_states.test.tsx +0 -20
- package/src/utils/__tests__/membership_sync_info_text.test.ts +0 -15
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { useQueryClient } from '@tanstack/react-query'
|
|
2
|
+
import { Alert } from 'react-native'
|
|
3
|
+
import { useConversationsContext } from '../contexts/conversations_context'
|
|
4
|
+
import { ApiResource, ConversationResource } from '../types'
|
|
5
|
+
import { pluralize } from '../utils/pluralize'
|
|
6
|
+
import { HIDDEN_CONVERSATIONS_FILTER } from '../utils/request/conversation'
|
|
7
|
+
import { showToast } from '../utils/toast_store'
|
|
8
|
+
import { useApiClient } from './use_api_client'
|
|
9
|
+
import { getConversationRequestArgs } from './use_conversation'
|
|
10
|
+
import { useConversationsCache } from './use_conversations_cache'
|
|
11
|
+
import { useCurrentPersonCache } from './use_current_person'
|
|
12
|
+
import { getRequestQueryKey } from './use_suspense_api'
|
|
13
|
+
|
|
14
|
+
const settle = async (
|
|
15
|
+
conversations: ConversationResource[],
|
|
16
|
+
request: (conversation: ConversationResource) => Promise<unknown>
|
|
17
|
+
) => {
|
|
18
|
+
const results = await Promise.allSettled(conversations.map(request))
|
|
19
|
+
const succeeded = conversations.filter((_, i) => results[i].status === 'fulfilled')
|
|
20
|
+
const failed = conversations.filter((_, i) => results[i].status === 'rejected')
|
|
21
|
+
|
|
22
|
+
return { succeeded, failed }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const alertFailures = (verb: string, failed: ConversationResource[]) => {
|
|
26
|
+
if (failed.length === 0) return
|
|
27
|
+
|
|
28
|
+
Alert.alert(
|
|
29
|
+
'Oops',
|
|
30
|
+
`Couldn't ${verb} ${pluralize(failed.length, 'conversation')}. Please try again.`
|
|
31
|
+
)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const sumUnread = (conversations: ConversationResource[]) =>
|
|
35
|
+
conversations.reduce((sum, { unreadCount }) => sum + (unreadCount || 0), 0)
|
|
36
|
+
|
|
37
|
+
export const useBulkConversationActions = () => {
|
|
38
|
+
const apiClient = useApiClient()
|
|
39
|
+
const queryClient = useQueryClient()
|
|
40
|
+
const { args } = useConversationsContext()
|
|
41
|
+
const currentPersonCache = useCurrentPersonCache()
|
|
42
|
+
const {
|
|
43
|
+
cancelAllLists,
|
|
44
|
+
invalidateAllLists,
|
|
45
|
+
removeFromAllLists,
|
|
46
|
+
restoreLists,
|
|
47
|
+
snapshotAllLists,
|
|
48
|
+
updateInAllLists,
|
|
49
|
+
} = useConversationsCache(args)
|
|
50
|
+
|
|
51
|
+
const setMembershipHidden = (conversations: ConversationResource[], hidden: boolean) =>
|
|
52
|
+
conversations.forEach(({ id }) =>
|
|
53
|
+
queryClient.setQueryData<ApiResource<ConversationResource>>(
|
|
54
|
+
getRequestQueryKey(getConversationRequestArgs({ conversation_id: id })),
|
|
55
|
+
prev => {
|
|
56
|
+
if (!prev?.data) return prev
|
|
57
|
+
|
|
58
|
+
return {
|
|
59
|
+
...prev,
|
|
60
|
+
data: {
|
|
61
|
+
...prev.data,
|
|
62
|
+
conversationMembership: { ...prev.data.conversationMembership, hidden },
|
|
63
|
+
},
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
)
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
const patchMembership =
|
|
70
|
+
(attributes: { hidden?: boolean; muted?: boolean }) =>
|
|
71
|
+
({ id }: ConversationResource) =>
|
|
72
|
+
apiClient.chat.patch({
|
|
73
|
+
url: `/me/conversations/${id}/conversation_membership`,
|
|
74
|
+
data: {
|
|
75
|
+
data: { type: 'ConversationMembership', attributes },
|
|
76
|
+
fields: { ConversationMembership: Object.keys(attributes).join(',') },
|
|
77
|
+
},
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
const applyHidden = async (conversations: ConversationResource[], hidden: boolean) => {
|
|
81
|
+
const filter = hidden ? undefined : HIDDEN_CONVERSATIONS_FILTER
|
|
82
|
+
await cancelAllLists()
|
|
83
|
+
const previousLists = snapshotAllLists()
|
|
84
|
+
|
|
85
|
+
removeFromAllLists(
|
|
86
|
+
conversations.map(({ id }) => id),
|
|
87
|
+
{ filter }
|
|
88
|
+
)
|
|
89
|
+
setMembershipHidden(conversations, hidden)
|
|
90
|
+
|
|
91
|
+
const result = await settle(conversations, patchMembership({ hidden }))
|
|
92
|
+
|
|
93
|
+
if (result.failed.length > 0) {
|
|
94
|
+
restoreLists(previousLists)
|
|
95
|
+
removeFromAllLists(
|
|
96
|
+
result.succeeded.map(({ id }) => id),
|
|
97
|
+
{ filter }
|
|
98
|
+
)
|
|
99
|
+
setMembershipHidden(result.failed, !hidden)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return { ...result, previousLists }
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const unhide = async (conversations: ConversationResource[]) => {
|
|
106
|
+
const { failed } = await applyHidden(conversations, false)
|
|
107
|
+
|
|
108
|
+
invalidateAllLists()
|
|
109
|
+
alertFailures('unhide', failed)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const hide = async (conversations: ConversationResource[]) => {
|
|
113
|
+
const { succeeded, failed, previousLists } = await applyHidden(conversations, true)
|
|
114
|
+
|
|
115
|
+
alertFailures('hide', failed)
|
|
116
|
+
if (succeeded.length === 0) return
|
|
117
|
+
|
|
118
|
+
invalidateAllLists({ filter: HIDDEN_CONVERSATIONS_FILTER })
|
|
119
|
+
showToast({
|
|
120
|
+
title: `${pluralize(succeeded.length, 'conversation')} hidden`,
|
|
121
|
+
action: {
|
|
122
|
+
label: 'Undo',
|
|
123
|
+
onPress: () => {
|
|
124
|
+
restoreLists(previousLists)
|
|
125
|
+
unhide(succeeded)
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const silence = async (conversations: ConversationResource[]) => {
|
|
132
|
+
const setMuted = (conversation: ConversationResource, muted: boolean) =>
|
|
133
|
+
updateInAllLists(conversation.id, {
|
|
134
|
+
muted,
|
|
135
|
+
conversationMembership: { ...conversation.conversationMembership, muted },
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
conversations.forEach(conversation => setMuted(conversation, true))
|
|
139
|
+
|
|
140
|
+
const { failed } = await settle(conversations, patchMembership({ muted: true }))
|
|
141
|
+
|
|
142
|
+
failed.forEach(conversation => setMuted(conversation, conversation.muted))
|
|
143
|
+
alertFailures('silence', failed)
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const adjustPersonUnread = (delta: number) =>
|
|
147
|
+
currentPersonCache.update({}, person => ({
|
|
148
|
+
...person,
|
|
149
|
+
unreadCount: Math.max(person.unreadCount + delta, 0),
|
|
150
|
+
}))
|
|
151
|
+
|
|
152
|
+
const markRead = async (conversations: ConversationResource[]) => {
|
|
153
|
+
conversations.forEach(({ id }) =>
|
|
154
|
+
updateInAllLists(id, { unreadCount: 0, unreadReaction: false })
|
|
155
|
+
)
|
|
156
|
+
adjustPersonUnread(-sumUnread(conversations))
|
|
157
|
+
|
|
158
|
+
const { failed } = await settle(conversations, ({ id }) =>
|
|
159
|
+
apiClient.chat.post({
|
|
160
|
+
url: `/me/conversations/${id}/mark_read`,
|
|
161
|
+
data: { data: { type: '', attributes: {} }, fields: { Conversation: 'unread_count,id' } },
|
|
162
|
+
})
|
|
163
|
+
)
|
|
164
|
+
|
|
165
|
+
failed.forEach(({ id, unreadCount, unreadReaction }) =>
|
|
166
|
+
updateInAllLists(id, { unreadCount, unreadReaction })
|
|
167
|
+
)
|
|
168
|
+
adjustPersonUnread(sumUnread(failed))
|
|
169
|
+
alertFailures('mark read', failed)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return { hide, unhide, silence, markRead }
|
|
173
|
+
}
|
|
@@ -3,6 +3,7 @@ import { useCallback, useMemo } from 'react'
|
|
|
3
3
|
import { ApiResource, ConversationResource } from '../types'
|
|
4
4
|
import { JoltConversationEvent, JoltReactionEvent } from '../types/jolt_events'
|
|
5
5
|
import { completeMessageCreationConversationTracking } from '../utils/performance_tracking'
|
|
6
|
+
import { throttledRefresh } from '../utils/throttled_refresh'
|
|
6
7
|
import { useApiClient } from './use_api_client'
|
|
7
8
|
import { getConversationRequestArgs } from './use_conversation'
|
|
8
9
|
import { useCurrentPerson } from './use_current_person'
|
|
@@ -54,7 +55,7 @@ export function useConversationJoltEvents({ conversationId }: Props) {
|
|
|
54
55
|
}
|
|
55
56
|
})
|
|
56
57
|
|
|
57
|
-
queryClient.invalidateQueries({ queryKey })
|
|
58
|
+
throttledRefresh(queryClient, queryKey, e, () => queryClient.invalidateQueries({ queryKey }))
|
|
58
59
|
},
|
|
59
60
|
[queryClient, queryKey, currentPersonId, apiClient]
|
|
60
61
|
)
|
|
@@ -17,7 +17,7 @@ export const useConversationsMarkRead = ({
|
|
|
17
17
|
const queryClient = useQueryClient()
|
|
18
18
|
const { args } = useConversationsContext()
|
|
19
19
|
const currentPersonCache = useCurrentPersonCache()
|
|
20
|
-
const { updateInAllLists, invalidateAllLists
|
|
20
|
+
const { updateInAllLists, invalidateAllLists } = useConversationsCache(args)
|
|
21
21
|
const conversationQueryKey = getRequestQueryKey(
|
|
22
22
|
getConversationRequestArgs({ conversation_id: conversation.id })
|
|
23
23
|
)
|
|
@@ -48,13 +48,16 @@ export const useConversationsMarkRead = ({
|
|
|
48
48
|
mutationFn: async (read: boolean) => {
|
|
49
49
|
const action = read ? 'mark_read' : 'mark_unread'
|
|
50
50
|
|
|
51
|
-
return apiClient.chat.post({
|
|
51
|
+
return apiClient.chat.post<ApiResource<ConversationResource>>({
|
|
52
52
|
url: `/me/conversations/${conversation.id}/${action}`,
|
|
53
53
|
data: { data: { type: '', attributes: {} }, fields: { Conversation: 'unread_count,id' } },
|
|
54
54
|
})
|
|
55
55
|
},
|
|
56
|
-
onSuccess:
|
|
57
|
-
|
|
56
|
+
onSuccess: response => {
|
|
57
|
+
const unreadCount = response.data?.unreadCount
|
|
58
|
+
if (unreadCount === undefined) return
|
|
59
|
+
|
|
60
|
+
updateInAllLists(conversation.id, { unreadCount })
|
|
58
61
|
},
|
|
59
62
|
onError: () => {
|
|
60
63
|
Alert.alert('Oops', 'Something went wrong updating this conversation, please try again')
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import { InfiniteData, useQueryClient } from '@tanstack/react-query'
|
|
1
|
+
import { InfiniteData, QueryKey, useQueryClient } from '@tanstack/react-query'
|
|
2
2
|
import { ApiCollection, ConversationResource } from '../types'
|
|
3
3
|
import {
|
|
4
4
|
deleteRecordInPagesData,
|
|
5
|
-
updateRecordInPagesData,
|
|
6
5
|
updateAllRecordsInPagesData,
|
|
7
6
|
updateOrCreateRecordInPagesData,
|
|
8
7
|
} from '../utils'
|
|
@@ -11,6 +10,7 @@ import { useApiClient } from './use_api_client'
|
|
|
11
10
|
import { getRequestQueryKey } from './use_suspense_api'
|
|
12
11
|
|
|
13
12
|
type QueryData = InfiniteData<ApiCollection<ConversationResource>>
|
|
13
|
+
export type ConversationListsSnapshot = Array<[QueryKey, QueryData | undefined]>
|
|
14
14
|
|
|
15
15
|
export function useConversationsCache(args?: Partial<ConversationRequestArgs>) {
|
|
16
16
|
const apiClient = useApiClient()
|
|
@@ -44,20 +44,6 @@ export function useConversationsCache(args?: Partial<ConversationRequestArgs>) {
|
|
|
44
44
|
)
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
const updateOnly = async (conversation: ConversationResource) => {
|
|
48
|
-
if (!conversation.id) return
|
|
49
|
-
|
|
50
|
-
queryClient.setQueryData<QueryData>(conversationQueryKey, prev =>
|
|
51
|
-
updateRecordInPagesData({
|
|
52
|
-
data: prev,
|
|
53
|
-
record: conversation,
|
|
54
|
-
processRecord: (record, current) => {
|
|
55
|
-
return { ...current, ...record }
|
|
56
|
-
},
|
|
57
|
-
})
|
|
58
|
-
)
|
|
59
|
-
}
|
|
60
|
-
|
|
61
47
|
const updateAll = async (update: Partial<ConversationResource>) => {
|
|
62
48
|
queryClient.setQueryData<QueryData>(conversationQueryKey, prev =>
|
|
63
49
|
updateAllRecordsInPagesData({
|
|
@@ -78,8 +64,32 @@ export function useConversationsCache(args?: Partial<ConversationRequestArgs>) {
|
|
|
78
64
|
})
|
|
79
65
|
}
|
|
80
66
|
|
|
81
|
-
const
|
|
82
|
-
|
|
67
|
+
const listsQueryKey = (filter?: string) =>
|
|
68
|
+
filter ? [conversationsRequestArgs.url, { filter }] : [conversationsRequestArgs.url]
|
|
69
|
+
|
|
70
|
+
const invalidateAllLists = ({ filter }: { filter?: string } = {}) =>
|
|
71
|
+
queryClient.invalidateQueries({ queryKey: listsQueryKey(filter) })
|
|
72
|
+
|
|
73
|
+
const cancelAllLists = () => queryClient.cancelQueries({ queryKey: listsQueryKey() })
|
|
74
|
+
|
|
75
|
+
const snapshotAllLists = (): ConversationListsSnapshot =>
|
|
76
|
+
queryClient.getQueriesData<QueryData>({ queryKey: listsQueryKey() })
|
|
77
|
+
|
|
78
|
+
const restoreLists = (lists: ConversationListsSnapshot) =>
|
|
79
|
+
lists.forEach(([queryKey, data]) => queryClient.setQueryData<QueryData>(queryKey, data))
|
|
80
|
+
|
|
81
|
+
const removeFromAllLists = (ids: number[], { filter }: { filter?: string } = {}) =>
|
|
82
|
+
queryClient.setQueriesData<QueryData>({ queryKey: listsQueryKey(filter) }, prev => {
|
|
83
|
+
if (!prev?.pages) return prev
|
|
84
|
+
|
|
85
|
+
return {
|
|
86
|
+
...prev,
|
|
87
|
+
pages: prev.pages.map(page => ({
|
|
88
|
+
...page,
|
|
89
|
+
data: page.data.filter(({ id }) => !ids.includes(id)),
|
|
90
|
+
})),
|
|
91
|
+
}
|
|
92
|
+
})
|
|
83
93
|
|
|
84
94
|
const handleConversationDestroy = ({ id }: { id: number }) => {
|
|
85
95
|
if (!id) return
|
|
@@ -92,13 +102,6 @@ export function useConversationsCache(args?: Partial<ConversationRequestArgs>) {
|
|
|
92
102
|
)
|
|
93
103
|
}
|
|
94
104
|
|
|
95
|
-
const fetchAndUpdate = async ({ id }: { id: number }) => {
|
|
96
|
-
const conversation = await fetchConversation(id).catch(() => undefined)
|
|
97
|
-
if (!conversation) return
|
|
98
|
-
|
|
99
|
-
updateOnly(conversation)
|
|
100
|
-
}
|
|
101
|
-
|
|
102
105
|
const fetchAndSync = async ({ id }: { id: number }) => {
|
|
103
106
|
let conversation: ConversationResource | undefined
|
|
104
107
|
try {
|
|
@@ -115,12 +118,16 @@ export function useConversationsCache(args?: Partial<ConversationRequestArgs>) {
|
|
|
115
118
|
}
|
|
116
119
|
|
|
117
120
|
return {
|
|
121
|
+
cancelAllLists,
|
|
122
|
+
queryKey: conversationQueryKey,
|
|
118
123
|
create: updateOrCreate,
|
|
119
124
|
destroy: handleConversationDestroy,
|
|
120
125
|
fetchSync: fetchAndSync,
|
|
121
|
-
fetchUpdate: fetchAndUpdate,
|
|
122
126
|
invalidate: () => queryClient.invalidateQueries({ queryKey: conversationQueryKey }),
|
|
123
127
|
invalidateAllLists,
|
|
128
|
+
removeFromAllLists,
|
|
129
|
+
restoreLists,
|
|
130
|
+
snapshotAllLists,
|
|
124
131
|
update: updateOrCreate,
|
|
125
132
|
updateAll,
|
|
126
133
|
updateInAllLists,
|
|
@@ -1,37 +1,48 @@
|
|
|
1
|
+
import { useQueryClient } from '@tanstack/react-query'
|
|
1
2
|
import { useConversationsContext } from '../contexts/conversations_context'
|
|
2
3
|
import { CurrentPersonResource } from '../types'
|
|
3
|
-
import { JoltConversationEvent } from '../types/jolt_events'
|
|
4
|
+
import { JoltConversationEvent, StreamUserUpdatedEvent } from '../types/jolt_events'
|
|
5
|
+
import { parseStreamUserEventType } from '../utils/jolt/parse_stream_user_event_type'
|
|
4
6
|
import { completeMessageCreationConversationTracking } from '../utils/performance_tracking'
|
|
7
|
+
import { throttledRefresh } from '../utils/throttled_refresh'
|
|
5
8
|
import { useApiGet } from './use_api'
|
|
6
9
|
import { useApiClient } from './use_api_client'
|
|
7
10
|
import { useConversationsCache } from './use_conversations_cache'
|
|
8
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
currentPersonQueryKey,
|
|
13
|
+
currentPersonRequestArgs,
|
|
14
|
+
useCurrentPersonCache,
|
|
15
|
+
} from './use_current_person'
|
|
9
16
|
import { useJoltChannel, useJoltEvent } from './use_jolt'
|
|
10
17
|
|
|
11
18
|
const useCurrentPerson = () => {
|
|
12
19
|
return useApiGet<CurrentPersonResource>(currentPersonRequestArgs)
|
|
13
20
|
}
|
|
14
21
|
|
|
22
|
+
const refreshesCurrentPerson = (event: StreamUserUpdatedEvent) => {
|
|
23
|
+
const eventType = parseStreamUserEventType(event)
|
|
24
|
+
return eventType === undefined || eventType === 'app_grant.synced'
|
|
25
|
+
}
|
|
26
|
+
|
|
15
27
|
export function useConversationsJoltEvents() {
|
|
16
28
|
const { data: currentPerson } = useCurrentPerson()
|
|
17
29
|
const { args } = useConversationsContext()
|
|
18
30
|
const cache = useConversationsCache(args)
|
|
19
31
|
const currentPersonCache = useCurrentPersonCache()
|
|
20
32
|
const apiClient = useApiClient()
|
|
33
|
+
const queryClient = useQueryClient()
|
|
21
34
|
const enabled = currentPerson?.id ? true : false
|
|
22
35
|
const joltChannel = useJoltChannel(`chat.people.${currentPerson?.id}`, enabled)
|
|
23
36
|
|
|
24
37
|
useJoltEvent(joltChannel, 'conversation.updated', (e: JoltConversationEvent) => {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
e.data.data.last_message_author_id === currentPerson?.id
|
|
28
|
-
) {
|
|
38
|
+
const { id, last_message_idempotent_key, last_message_author_id } = e.data.data
|
|
39
|
+
if (last_message_idempotent_key && last_message_author_id === currentPerson?.id) {
|
|
29
40
|
completeMessageCreationConversationTracking({
|
|
30
41
|
apiClient,
|
|
31
|
-
idempotentKey:
|
|
42
|
+
idempotentKey: last_message_idempotent_key,
|
|
32
43
|
})
|
|
33
44
|
}
|
|
34
|
-
cache.
|
|
45
|
+
throttledRefresh(queryClient, [...cache.queryKey, id], e, () => cache.fetchSync({ id }))
|
|
35
46
|
})
|
|
36
47
|
useJoltEvent(joltChannel, 'conversation.created', (e: JoltConversationEvent) =>
|
|
37
48
|
cache.fetchSync({ id: e.data.data.id })
|
|
@@ -40,6 +51,9 @@ export function useConversationsJoltEvents() {
|
|
|
40
51
|
cache.destroy({ id: e.data.data.id })
|
|
41
52
|
)
|
|
42
53
|
|
|
43
|
-
useJoltEvent(joltChannel, 'STREAM_USER_UPDATED',
|
|
44
|
-
|
|
54
|
+
useJoltEvent(joltChannel, 'STREAM_USER_UPDATED', (e: StreamUserUpdatedEvent) => {
|
|
55
|
+
if (!refreshesCurrentPerson(e)) return
|
|
56
|
+
|
|
57
|
+
throttledRefresh(queryClient, currentPersonQueryKey, e, currentPersonCache.invalidate)
|
|
58
|
+
})
|
|
45
59
|
}
|
|
@@ -61,6 +61,7 @@ export const availableFeatures = {
|
|
|
61
61
|
filter_dropdown_rollout: 'ROLLOUT_MOBILE_filter_dropdown',
|
|
62
62
|
manual_archiving: 'QA_MOBILE_manual_archiving',
|
|
63
63
|
manual_archiving_rollout: 'ROLLOUT_MOBILE_manual_archiving',
|
|
64
|
+
groups_proxy: 'ROLLOUT_groups_proxy',
|
|
64
65
|
hide_conversations: 'ROLLOUT_hide_conversations',
|
|
65
66
|
} as const satisfies Record<string, `${'QA' | 'ROLLOUT'}_${string}`>
|
|
66
67
|
|
|
@@ -1,26 +1,31 @@
|
|
|
1
1
|
import { GroupsGroupResource } from '../types'
|
|
2
2
|
import { FIVE_MINUTES } from '../utils/time'
|
|
3
|
+
import { useGroupsProxy } from './groups/use_groups_proxy'
|
|
3
4
|
import { useApiPaginator } from './use_api'
|
|
4
5
|
|
|
6
|
+
const GROUP_FIELDS = [
|
|
7
|
+
'can_create_conversation',
|
|
8
|
+
'can_create_conversation_reason_code',
|
|
9
|
+
'name',
|
|
10
|
+
'header_image',
|
|
11
|
+
'memberships_count',
|
|
12
|
+
]
|
|
13
|
+
|
|
5
14
|
export const useGroupsGroups = () => {
|
|
15
|
+
const proxied = useGroupsProxy()
|
|
16
|
+
|
|
6
17
|
return useApiPaginator<GroupsGroupResource>(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
'memberships_count',
|
|
18
|
-
],
|
|
18
|
+
proxied
|
|
19
|
+
? {
|
|
20
|
+
url: '/me/groups_groups',
|
|
21
|
+
data: { perPage: 100, fields: { GroupsGroup: GROUP_FIELDS } },
|
|
22
|
+
app: 'chat',
|
|
23
|
+
}
|
|
24
|
+
: {
|
|
25
|
+
url: '/me/groups',
|
|
26
|
+
data: { perPage: 100, fields: { Group: GROUP_FIELDS }, order: 'name' },
|
|
27
|
+
app: 'groups',
|
|
19
28
|
},
|
|
20
|
-
order: 'name',
|
|
21
|
-
},
|
|
22
|
-
app: 'groups',
|
|
23
|
-
},
|
|
24
29
|
{ staleTime: FIVE_MINUTES }
|
|
25
30
|
)
|
|
26
31
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { throttle } from 'lodash'
|
|
2
2
|
import { useEffect, useMemo, useRef } from 'react'
|
|
3
3
|
import { ConversationResource, MessageResource } from '../types'
|
|
4
|
+
import { REFRESH_THROTTLE_MS } from '../utils/throttled_refresh'
|
|
4
5
|
import { useAppState } from './use_app_state'
|
|
5
6
|
import { useConversationsMarkRead } from './use_conversations_actions'
|
|
6
7
|
|
|
@@ -13,8 +14,8 @@ interface Props {
|
|
|
13
14
|
export function useMarkLatestMessageRead({ conversation, enabled = true }: Props) {
|
|
14
15
|
const firedOnce = useRef<boolean>(false)
|
|
15
16
|
const { markRead } = useConversationsMarkRead({ conversation })
|
|
16
|
-
const
|
|
17
|
-
() =>
|
|
17
|
+
const throttledMarkRead = useMemo(
|
|
18
|
+
() => throttle(markRead, REFRESH_THROTTLE_MS, { leading: true, trailing: true }),
|
|
18
19
|
[markRead]
|
|
19
20
|
)
|
|
20
21
|
const unreadCount = conversation.unreadCount
|
|
@@ -30,6 +31,6 @@ export function useMarkLatestMessageRead({ conversation, enabled = true }: Props
|
|
|
30
31
|
|
|
31
32
|
firedOnce.current = true
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
}, [
|
|
34
|
+
throttledMarkRead(true)
|
|
35
|
+
}, [throttledMarkRead, isActive, shouldMarkRead, unreadReaction])
|
|
35
36
|
}
|
|
@@ -2,6 +2,7 @@ import { useMemo } from 'react'
|
|
|
2
2
|
import { GroupResource, GraphId } from '../types/resources/group_resource'
|
|
3
3
|
import { GroupsGroupResource } from '../types/resources/groups/groups_group_resource'
|
|
4
4
|
import { destructureChatGroupGraphId } from '../utils'
|
|
5
|
+
import { useGroupsProxy } from './groups/use_groups_proxy'
|
|
5
6
|
import { useApiGet } from './use_api'
|
|
6
7
|
import { useNewConversationEntry } from './use_new_conversation_entry'
|
|
7
8
|
|
|
@@ -22,6 +23,7 @@ export function useNewConversationFromFilter(
|
|
|
22
23
|
): NewConversationFromFilter | undefined {
|
|
23
24
|
const { sourceAppName, sourceType, sourceId } = destructureChatGroupGraphId(chatGroupGraphId)
|
|
24
25
|
const entryMode = useNewConversationEntry()
|
|
26
|
+
const proxied = useGroupsProxy()
|
|
25
27
|
const isGroupsGroup = sourceAppName === 'Groups' && sourceType === 'Group'
|
|
26
28
|
const isServicesTeam = sourceAppName === 'Services' && sourceType === 'Team'
|
|
27
29
|
|
|
@@ -31,11 +33,17 @@ export function useNewConversationFromFilter(
|
|
|
31
33
|
)
|
|
32
34
|
|
|
33
35
|
const { data: groupsGroup } = useApiGet<GroupsGroupResource>(
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
proxied
|
|
37
|
+
? {
|
|
38
|
+
url: `/me/groups_groups/${sourceId}`,
|
|
39
|
+
data: { fields: { GroupsGroup: ['can_create_conversation'] } },
|
|
40
|
+
app: 'chat',
|
|
41
|
+
}
|
|
42
|
+
: {
|
|
43
|
+
url: `/me/groups/${sourceId}`,
|
|
44
|
+
data: { fields: { Group: ['can_create_conversation'] } },
|
|
45
|
+
app: 'groups',
|
|
46
|
+
},
|
|
39
47
|
{ enabled: isGroupsGroup && !!sourceId }
|
|
40
48
|
)
|
|
41
49
|
|
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isCancelledError, useQueryClient } from '@tanstack/react-query'
|
|
2
|
+
import { useCallback, useEffect, useRef } from 'react'
|
|
2
3
|
import { useChatContext } from '../contexts/chat_context'
|
|
3
4
|
import { ProductAnalyticsConfig } from '../types/product_analytics'
|
|
4
5
|
import { AnalyticsMetadataResource } from '../types/resources/analytics_metadata'
|
|
5
6
|
import { keysToSnakeCase } from '../utils/client/utils'
|
|
7
|
+
import { Log } from '../utils/native_adapters/configuration'
|
|
8
|
+
import { fetchProductAnalyticsConfig } from '../utils/product_analytics_token'
|
|
9
|
+
import { isResponseLike, ResponseError } from '../utils/response_error'
|
|
6
10
|
import { SHA256, enc } from '../utils/sha_256'
|
|
7
11
|
|
|
8
12
|
export interface EventMetadata {
|
|
@@ -27,6 +31,46 @@ function contentSha256(payload: string): string {
|
|
|
27
31
|
return SHA256(payload).toString(enc.Hex)
|
|
28
32
|
}
|
|
29
33
|
|
|
34
|
+
type AnalyticsStage = 'prepare' | 'send'
|
|
35
|
+
|
|
36
|
+
const REPORT_TAGS = {
|
|
37
|
+
team: 'chat',
|
|
38
|
+
package: 'chat-react-native',
|
|
39
|
+
feature: 'product-analytics',
|
|
40
|
+
} as const
|
|
41
|
+
|
|
42
|
+
const isNetworkFailure = (error: unknown) =>
|
|
43
|
+
error instanceof TypeError && /network/i.test(error.message)
|
|
44
|
+
|
|
45
|
+
// HTTP failures from the API client are already reported by instrumentedFetch,
|
|
46
|
+
// network failures are expected on flaky connections, and queries are cancelled
|
|
47
|
+
// whenever the session changes. Anything else is a bug worth seeing in Sentry.
|
|
48
|
+
const isExpectedFailure = (error: unknown) =>
|
|
49
|
+
isResponseLike(error) ||
|
|
50
|
+
error instanceof ResponseError ||
|
|
51
|
+
isCancelledError(error) ||
|
|
52
|
+
isNetworkFailure(error)
|
|
53
|
+
|
|
54
|
+
function reportAnalyticsError(error: unknown, stage: AnalyticsStage) {
|
|
55
|
+
if (__DEV__) {
|
|
56
|
+
console.warn(`Product Analytics ${stage} failed:`, error)
|
|
57
|
+
}
|
|
58
|
+
if (isExpectedFailure(error)) return
|
|
59
|
+
|
|
60
|
+
const reported = error instanceof Error ? error : new Error(String(error))
|
|
61
|
+
Log.reportError(reported, { tags: { ...REPORT_TAGS, 'analytics.stage': stage } })
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// The events endpoint is outside the chat API, so it isn't instrumented.
|
|
65
|
+
// A rejected batch here usually means the stored token is no longer accepted.
|
|
66
|
+
function reportRejectedEvents(response: Response) {
|
|
67
|
+
const error = new Error(`HTTP ${response.status} POST product analytics events`)
|
|
68
|
+
error.name = `HTTPError${response.status}`
|
|
69
|
+
Log.reportError(error, {
|
|
70
|
+
tags: { ...REPORT_TAGS, 'analytics.stage': 'send', 'http.status': String(response.status) },
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
|
|
30
74
|
function sendEvents(events: AnalyticsEvent[], config: ProductAnalyticsConfig): Promise<Response[]> {
|
|
31
75
|
// Split into chunks of 10 and make parallel requests
|
|
32
76
|
const chunks: AnalyticsEvent[][] = []
|
|
@@ -37,7 +81,7 @@ function sendEvents(events: AnalyticsEvent[], config: ProductAnalyticsConfig): P
|
|
|
37
81
|
return Promise.all(
|
|
38
82
|
chunks.map(async chunk => {
|
|
39
83
|
const payload = JSON.stringify({ events: chunk })
|
|
40
|
-
|
|
84
|
+
const response = await fetch(config.endpoint, {
|
|
41
85
|
method: 'POST',
|
|
42
86
|
headers: {
|
|
43
87
|
'Content-Type': 'application/json; charset=utf-8',
|
|
@@ -46,56 +90,45 @@ function sendEvents(events: AnalyticsEvent[], config: ProductAnalyticsConfig): P
|
|
|
46
90
|
},
|
|
47
91
|
body: payload,
|
|
48
92
|
})
|
|
93
|
+
if (!response.ok) reportRejectedEvents(response)
|
|
94
|
+
return response
|
|
49
95
|
})
|
|
50
96
|
).catch(error => {
|
|
51
|
-
|
|
52
|
-
console.warn('Product Analytics request failed:', error)
|
|
53
|
-
}
|
|
97
|
+
reportAnalyticsError(error, 'send')
|
|
54
98
|
return [] as Response[]
|
|
55
99
|
})
|
|
56
100
|
}
|
|
57
101
|
|
|
58
102
|
export const useProductAnalytics = () => {
|
|
59
|
-
const {
|
|
60
|
-
|
|
61
|
-
const snakeCasedBaseMeta = useMemo(
|
|
62
|
-
() => keysToSnakeCase(productAnalyticsConfig?.metadata || {}),
|
|
63
|
-
[productAnalyticsConfig?.metadata]
|
|
64
|
-
)
|
|
65
|
-
|
|
66
|
-
const publishEvent = useCallback(
|
|
67
|
-
(name: AnalyticsEventName, meta: EventMetadata = {}): Promise<Response[]> => {
|
|
68
|
-
if (!productAnalyticsConfig) {
|
|
69
|
-
if (__DEV__) {
|
|
70
|
-
console.warn('Product Analytics not available')
|
|
71
|
-
}
|
|
72
|
-
return Promise.resolve([])
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const mergedMeta = { ...snakeCasedBaseMeta, ...meta } as EventMetadata
|
|
76
|
-
|
|
77
|
-
return sendEvents([{ name, meta: mergedMeta }], productAnalyticsConfig)
|
|
78
|
-
},
|
|
79
|
-
[productAnalyticsConfig, snakeCasedBaseMeta]
|
|
80
|
-
)
|
|
103
|
+
const { session } = useChatContext()
|
|
104
|
+
const queryClient = useQueryClient()
|
|
81
105
|
|
|
82
106
|
const publishEvents = useCallback(
|
|
83
107
|
(events: Array<{ name: AnalyticsEventName; meta?: EventMetadata }>): Promise<Response[]> => {
|
|
84
|
-
if (!
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
108
|
+
if (!session.isAuthenticated) return Promise.resolve([])
|
|
109
|
+
|
|
110
|
+
return fetchProductAnalyticsConfig(queryClient, session)
|
|
111
|
+
.then(config => {
|
|
112
|
+
const baseMeta = keysToSnakeCase(config.metadata || {})
|
|
113
|
+
const eventsWithMeta = events.map(event => ({
|
|
114
|
+
name: event.name,
|
|
115
|
+
meta: { ...baseMeta, ...event.meta } as EventMetadata,
|
|
116
|
+
}))
|
|
117
|
+
|
|
118
|
+
return sendEvents(eventsWithMeta, config)
|
|
119
|
+
})
|
|
120
|
+
.catch(error => {
|
|
121
|
+
reportAnalyticsError(error, 'prepare')
|
|
122
|
+
return [] as Response[]
|
|
123
|
+
})
|
|
97
124
|
},
|
|
98
|
-
[
|
|
125
|
+
[queryClient, session]
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
const publishEvent = useCallback(
|
|
129
|
+
(name: AnalyticsEventName, meta: EventMetadata = {}): Promise<Response[]> =>
|
|
130
|
+
publishEvents([{ name, meta }]),
|
|
131
|
+
[publishEvents]
|
|
99
132
|
)
|
|
100
133
|
|
|
101
134
|
return { publishEvent, publishEvents }
|