@planningcenter/chat-react-native 4.8.0-rc.1 → 4.8.0-rc.3
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/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/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_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 +1 -3
- package/build/hooks/use_conversations_cache.d.ts.map +1 -1
- package/build/hooks/use_conversations_cache.js +79 -124
- 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/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/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/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__/hooks/use_group_chat_conversation_payload.test.tsx +31 -0
- package/src/contexts/api_provider.tsx +2 -0
- package/src/contexts/chat_context.tsx +2 -58
- package/src/contexts/session_context.tsx +3 -0
- package/src/hooks/__tests__/jolt_refresh_throttling.test.tsx +229 -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_conversation_jolt_events.ts +2 -1
- package/src/hooks/use_conversations_actions.ts +7 -4
- package/src/hooks/use_conversations_cache.ts +1 -23
- 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/screens/conversation_new/components/groups_form.tsx +11 -9
- 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__/throttled_refresh.test.ts +101 -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/product_analytics_token.ts +120 -0
- package/src/utils/throttled_refresh.ts +62 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { QueryClient } from '@tanstack/react-query'
|
|
2
|
+
import { currentPersonQueryKey } from '../hooks/use_current_person'
|
|
3
|
+
import { ApiResource, CurrentPersonResource } from '../types'
|
|
4
|
+
import { ProductAnalyticsConfig } from '../types/product_analytics'
|
|
5
|
+
import { Client } from './client'
|
|
6
|
+
import { Storage } from './native_adapters'
|
|
7
|
+
import { Session } from './session'
|
|
8
|
+
import { ONE_HOUR, toMs } from './time'
|
|
9
|
+
import { Uri } from './uri'
|
|
10
|
+
|
|
11
|
+
interface ProductAnalyticsTokenResponse {
|
|
12
|
+
type: 'ProductAnalyticsToken'
|
|
13
|
+
id: string
|
|
14
|
+
url: string
|
|
15
|
+
metadata: Record<string, unknown>
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface StoredProductAnalyticsConfig extends ProductAnalyticsConfig {
|
|
19
|
+
expiresAt: number
|
|
20
|
+
personId: string
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const TOKEN_EXPIRY_TIME = toMs(7 * 24, 'hours')
|
|
24
|
+
const EXPIRY_MARGIN = ONE_HOUR
|
|
25
|
+
|
|
26
|
+
// One slot per environment, tagged with the person it belongs to. Keying on
|
|
27
|
+
// session.id would miss on every access-token refresh and leave orphaned entries.
|
|
28
|
+
export const productAnalyticsStorageKey = (session: Session) =>
|
|
29
|
+
`product-analytics-token:${session.env}`
|
|
30
|
+
|
|
31
|
+
export const productAnalyticsQueryKey = (session: Session) => [
|
|
32
|
+
'product-analytics-token',
|
|
33
|
+
session.env,
|
|
34
|
+
session.id,
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
const renewalTime = (config: StoredProductAnalyticsConfig) => config.expiresAt - EXPIRY_MARGIN
|
|
38
|
+
|
|
39
|
+
const isStoredConfig = (value: unknown): value is StoredProductAnalyticsConfig =>
|
|
40
|
+
typeof value === 'object' &&
|
|
41
|
+
value !== null &&
|
|
42
|
+
typeof (value as StoredProductAnalyticsConfig).token === 'string' &&
|
|
43
|
+
typeof (value as StoredProductAnalyticsConfig).endpoint === 'string' &&
|
|
44
|
+
typeof (value as StoredProductAnalyticsConfig).expiresAt === 'number' &&
|
|
45
|
+
typeof (value as StoredProductAnalyticsConfig).personId === 'string'
|
|
46
|
+
|
|
47
|
+
async function readStoredConfig(key: string): Promise<StoredProductAnalyticsConfig | undefined> {
|
|
48
|
+
try {
|
|
49
|
+
const raw = await Storage.getItem(key)
|
|
50
|
+
if (!raw) return undefined
|
|
51
|
+
|
|
52
|
+
const parsed: unknown = JSON.parse(raw)
|
|
53
|
+
return isStoredConfig(parsed) ? parsed : undefined
|
|
54
|
+
} catch {
|
|
55
|
+
return undefined
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function writeStoredConfig(key: string, config: StoredProductAnalyticsConfig): Promise<void> {
|
|
60
|
+
return Storage.setItem(key, JSON.stringify(config)).catch(() => {})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function requestConfig(session: Session, personId: string): Promise<StoredProductAnalyticsConfig> {
|
|
64
|
+
const uri = new Uri({ session })
|
|
65
|
+
const client = new Client({
|
|
66
|
+
root: uri.api('/chat/v2'),
|
|
67
|
+
defaultHeaders: uri.headers,
|
|
68
|
+
version: '2018-11-01',
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
return client
|
|
72
|
+
.post<ApiResource<ProductAnalyticsTokenResponse>>({
|
|
73
|
+
url: '/me/product_analytics_authorize',
|
|
74
|
+
data: { data: { type: 'ProductAnalyticsToken', attributes: {} } },
|
|
75
|
+
})
|
|
76
|
+
.then(res => ({
|
|
77
|
+
token: res.data.id,
|
|
78
|
+
endpoint: res.data.url,
|
|
79
|
+
metadata: res.data.metadata,
|
|
80
|
+
expiresAt: Date.now() + TOKEN_EXPIRY_TIME,
|
|
81
|
+
personId,
|
|
82
|
+
}))
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// The chat screens that publish events already load /me, and the package's query
|
|
86
|
+
// client is cleared whenever the access token changes, so this is normally a cache hit.
|
|
87
|
+
async function currentPersonId(queryClient: QueryClient): Promise<string> {
|
|
88
|
+
const { data } = await queryClient.ensureQueryData<ApiResource<CurrentPersonResource>>({
|
|
89
|
+
queryKey: currentPersonQueryKey,
|
|
90
|
+
})
|
|
91
|
+
return String(data.id)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function loadConfig(
|
|
95
|
+
queryClient: QueryClient,
|
|
96
|
+
session: Session
|
|
97
|
+
): Promise<StoredProductAnalyticsConfig> {
|
|
98
|
+
const personId = await currentPersonId(queryClient)
|
|
99
|
+
const key = productAnalyticsStorageKey(session)
|
|
100
|
+
const stored = await readStoredConfig(key)
|
|
101
|
+
if (stored && stored.personId === personId && renewalTime(stored) > Date.now()) return stored
|
|
102
|
+
|
|
103
|
+
const config = await requestConfig(session, personId)
|
|
104
|
+
await writeStoredConfig(key, config)
|
|
105
|
+
return config
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function fetchProductAnalyticsConfig(
|
|
109
|
+
queryClient: QueryClient,
|
|
110
|
+
session: Session
|
|
111
|
+
): Promise<StoredProductAnalyticsConfig> {
|
|
112
|
+
return queryClient.fetchQuery({
|
|
113
|
+
queryKey: productAnalyticsQueryKey(session),
|
|
114
|
+
queryFn: () => loadConfig(queryClient, session),
|
|
115
|
+
staleTime: ({ state }) =>
|
|
116
|
+
state.data ? Math.max(0, renewalTime(state.data) - state.dataUpdatedAt) : 0,
|
|
117
|
+
gcTime: TOKEN_EXPIRY_TIME,
|
|
118
|
+
retry: false,
|
|
119
|
+
})
|
|
120
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { hashKey, QueryClient, QueryKey } from '@tanstack/react-query'
|
|
2
|
+
import { DebouncedFuncLeading, throttle } from 'lodash'
|
|
3
|
+
|
|
4
|
+
export const REFRESH_THROTTLE_MS = 3000
|
|
5
|
+
|
|
6
|
+
type Refresh = () => unknown
|
|
7
|
+
|
|
8
|
+
interface ThrottledRefresh {
|
|
9
|
+
run: DebouncedFuncLeading<(refresh: Refresh) => void>
|
|
10
|
+
lastEvent?: object
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const refreshesByClient = new WeakMap<QueryClient, Map<string, ThrottledRefresh>>()
|
|
14
|
+
|
|
15
|
+
const refreshesFor = (queryClient: QueryClient) => {
|
|
16
|
+
const existing = refreshesByClient.get(queryClient)
|
|
17
|
+
if (existing) return existing
|
|
18
|
+
|
|
19
|
+
const refreshes = new Map<string, ThrottledRefresh>()
|
|
20
|
+
refreshesByClient.set(queryClient, refreshes)
|
|
21
|
+
return refreshes
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const throttledRefreshFor = (queryClient: QueryClient, key: QueryKey) => {
|
|
25
|
+
const refreshes = refreshesFor(queryClient)
|
|
26
|
+
const hashedKey = hashKey(key)
|
|
27
|
+
const existing = refreshes.get(hashedKey)
|
|
28
|
+
if (existing) return existing
|
|
29
|
+
|
|
30
|
+
const created: ThrottledRefresh = {
|
|
31
|
+
run: throttle(
|
|
32
|
+
(refresh: Refresh) => {
|
|
33
|
+
refresh()
|
|
34
|
+
},
|
|
35
|
+
REFRESH_THROTTLE_MS,
|
|
36
|
+
{ leading: true, trailing: true }
|
|
37
|
+
),
|
|
38
|
+
}
|
|
39
|
+
refreshes.set(hashedKey, created)
|
|
40
|
+
return created
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const throttledRefresh = (
|
|
44
|
+
queryClient: QueryClient,
|
|
45
|
+
key: QueryKey,
|
|
46
|
+
event: object,
|
|
47
|
+
refresh: Refresh
|
|
48
|
+
) => {
|
|
49
|
+
const throttled = throttledRefreshFor(queryClient, key)
|
|
50
|
+
if (throttled.lastEvent === event) return
|
|
51
|
+
|
|
52
|
+
throttled.lastEvent = event
|
|
53
|
+
throttled.run(refresh)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export const cancelThrottledRefreshes = (queryClient: QueryClient) => {
|
|
57
|
+
const refreshes = refreshesByClient.get(queryClient)
|
|
58
|
+
if (!refreshes) return
|
|
59
|
+
|
|
60
|
+
refreshes.forEach(({ run }) => run.cancel())
|
|
61
|
+
refreshesByClient.delete(queryClient)
|
|
62
|
+
}
|