@planningcenter/chat-react-native 4.7.1-qa-staging.7 → 4.7.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/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/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_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_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/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/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 +3 -3
- 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_mark_latest_message_read.test.tsx +23 -2
- package/src/hooks/__tests__/use_product_analytics.test.tsx +304 -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_mark_latest_message_read.ts +6 -5
- package/src/hooks/use_product_analytics.ts +74 -41
- package/src/types/jolt_events/index.ts +2 -0
- package/src/types/jolt_events/person_events.ts +16 -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,16 @@
|
|
|
1
|
+
import type { CustomMessage } from '@planningcenter/jolt-client/dist/types/JoltConnection'
|
|
2
|
+
|
|
3
|
+
export type StreamUserEventType =
|
|
4
|
+
| 'message.created'
|
|
5
|
+
| 'conversation.mark_read'
|
|
6
|
+
| 'conversation.mark_unread'
|
|
7
|
+
| 'conversation.destroy'
|
|
8
|
+
| 'person.mark_all_read'
|
|
9
|
+
| 'app_grant.synced'
|
|
10
|
+
|
|
11
|
+
export interface StreamUserUpdatedEvent extends CustomMessage {
|
|
12
|
+
event: 'STREAM_USER_UPDATED'
|
|
13
|
+
data: {
|
|
14
|
+
data: string
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { QueryClient } from '@tanstack/react-query'
|
|
2
|
+
import { buildTestQueryClient } from '../../__utils__/query_client'
|
|
3
|
+
import {
|
|
4
|
+
cancelThrottledRefreshes,
|
|
5
|
+
REFRESH_THROTTLE_MS,
|
|
6
|
+
throttledRefresh,
|
|
7
|
+
} from '../throttled_refresh'
|
|
8
|
+
|
|
9
|
+
const key = ['/me/conversations/7']
|
|
10
|
+
let queryClient: QueryClient
|
|
11
|
+
let refresh: jest.Mock
|
|
12
|
+
|
|
13
|
+
beforeEach(() => {
|
|
14
|
+
jest.useFakeTimers()
|
|
15
|
+
queryClient = buildTestQueryClient()
|
|
16
|
+
refresh = jest.fn()
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
cancelThrottledRefreshes(queryClient)
|
|
21
|
+
jest.useRealTimers()
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
const fire = (events = 1) => {
|
|
25
|
+
for (let i = 0; i < events; i++) throttledRefresh(queryClient, key, {}, refresh)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe('throttledRefresh', () => {
|
|
29
|
+
it('runs a lone event immediately and never again', () => {
|
|
30
|
+
fire()
|
|
31
|
+
expect(refresh).toHaveBeenCalledTimes(1)
|
|
32
|
+
|
|
33
|
+
jest.advanceTimersByTime(REFRESH_THROTTLE_MS * 3)
|
|
34
|
+
expect(refresh).toHaveBeenCalledTimes(1)
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('collapses a burst into one immediate run and one when the window ends', () => {
|
|
38
|
+
fire(50)
|
|
39
|
+
expect(refresh).toHaveBeenCalledTimes(1)
|
|
40
|
+
|
|
41
|
+
jest.advanceTimersByTime(REFRESH_THROTTLE_MS - 1)
|
|
42
|
+
expect(refresh).toHaveBeenCalledTimes(1)
|
|
43
|
+
|
|
44
|
+
jest.advanceTimersByTime(1)
|
|
45
|
+
expect(refresh).toHaveBeenCalledTimes(2)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
it('gives an event inside the window opened by a trailing run its own trailing run', () => {
|
|
49
|
+
fire()
|
|
50
|
+
jest.advanceTimersByTime(1000)
|
|
51
|
+
fire()
|
|
52
|
+
jest.advanceTimersByTime(REFRESH_THROTTLE_MS - 1000)
|
|
53
|
+
expect(refresh).toHaveBeenCalledTimes(2)
|
|
54
|
+
|
|
55
|
+
jest.advanceTimersByTime(500)
|
|
56
|
+
fire()
|
|
57
|
+
expect(refresh).toHaveBeenCalledTimes(2)
|
|
58
|
+
|
|
59
|
+
jest.advanceTimersByTime(REFRESH_THROTTLE_MS)
|
|
60
|
+
expect(refresh).toHaveBeenCalledTimes(3)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('counts the same event delivered to several consumers once', () => {
|
|
64
|
+
const event = {}
|
|
65
|
+
const other = jest.fn()
|
|
66
|
+
throttledRefresh(queryClient, key, event, refresh)
|
|
67
|
+
throttledRefresh(queryClient, [...key], event, other)
|
|
68
|
+
|
|
69
|
+
jest.advanceTimersByTime(REFRESH_THROTTLE_MS * 2)
|
|
70
|
+
expect(refresh).toHaveBeenCalledTimes(1)
|
|
71
|
+
expect(other).not.toHaveBeenCalled()
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
it('shares one window across consumers using the same client and key', () => {
|
|
75
|
+
const other = jest.fn()
|
|
76
|
+
throttledRefresh(queryClient, key, {}, refresh)
|
|
77
|
+
throttledRefresh(queryClient, [...key], {}, other)
|
|
78
|
+
|
|
79
|
+
expect(refresh).toHaveBeenCalledTimes(1)
|
|
80
|
+
expect(other).not.toHaveBeenCalled()
|
|
81
|
+
|
|
82
|
+
jest.advanceTimersByTime(REFRESH_THROTTLE_MS)
|
|
83
|
+
expect(other).toHaveBeenCalledTimes(1)
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
it('keeps separate windows per key and per query client', () => {
|
|
87
|
+
throttledRefresh(queryClient, key, {}, refresh)
|
|
88
|
+
throttledRefresh(queryClient, ['/me'], {}, refresh)
|
|
89
|
+
throttledRefresh(buildTestQueryClient(), key, {}, refresh)
|
|
90
|
+
|
|
91
|
+
expect(refresh).toHaveBeenCalledTimes(3)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('drops pending trailing runs when cancelled', () => {
|
|
95
|
+
fire(2)
|
|
96
|
+
cancelThrottledRefreshes(queryClient)
|
|
97
|
+
|
|
98
|
+
jest.advanceTimersByTime(REFRESH_THROTTLE_MS)
|
|
99
|
+
expect(refresh).toHaveBeenCalledTimes(1)
|
|
100
|
+
})
|
|
101
|
+
})
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { StreamUserUpdatedEvent } from '../../../types/jolt_events'
|
|
2
|
+
import { parseStreamUserEventType } from '../parse_stream_user_event_type'
|
|
3
|
+
|
|
4
|
+
const event = (data: unknown) =>
|
|
5
|
+
({
|
|
6
|
+
event: 'STREAM_USER_UPDATED',
|
|
7
|
+
channel: 'chat.people.1',
|
|
8
|
+
data: { data },
|
|
9
|
+
}) as StreamUserUpdatedEvent
|
|
10
|
+
|
|
11
|
+
describe('parseStreamUserEventType', () => {
|
|
12
|
+
it('reads the event type out of the JSON payload', () => {
|
|
13
|
+
expect(parseStreamUserEventType(event('{"event_type":"message.created"}'))).toBe(
|
|
14
|
+
'message.created'
|
|
15
|
+
)
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
it('treats an empty payload as missing', () => {
|
|
19
|
+
expect(parseStreamUserEventType(event('{}'))).toBeUndefined()
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('treats a payload that fails to parse as missing', () => {
|
|
23
|
+
expect(parseStreamUserEventType(event('not json'))).toBeUndefined()
|
|
24
|
+
expect(parseStreamUserEventType(event(undefined))).toBeUndefined()
|
|
25
|
+
expect(parseStreamUserEventType(event('null'))).toBeUndefined()
|
|
26
|
+
})
|
|
27
|
+
})
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { StreamUserEventType, StreamUserUpdatedEvent } from '../../types/jolt_events'
|
|
2
|
+
|
|
3
|
+
interface StreamUserEventData {
|
|
4
|
+
event_type?: StreamUserEventType
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export const parseStreamUserEventType = (
|
|
8
|
+
event: StreamUserUpdatedEvent
|
|
9
|
+
): StreamUserEventType | undefined => {
|
|
10
|
+
try {
|
|
11
|
+
const parsed = JSON.parse(event.data.data) as StreamUserEventData | null
|
|
12
|
+
return parsed?.event_type
|
|
13
|
+
} catch {
|
|
14
|
+
return undefined
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -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
|
+
}
|