@planningcenter/chat-react-native 3.21.2-rc.3 → 3.21.2-rc.5

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.
Files changed (31) hide show
  1. package/build/contexts/session_context.d.ts +40 -0
  2. package/build/contexts/session_context.d.ts.map +1 -0
  3. package/build/contexts/session_context.js +131 -0
  4. package/build/contexts/session_context.js.map +1 -0
  5. package/build/hooks/use_async_storage.d.ts.map +1 -1
  6. package/build/hooks/use_async_storage.js +3 -44
  7. package/build/hooks/use_async_storage.js.map +1 -1
  8. package/build/hooks/use_storage.d.ts +9 -0
  9. package/build/hooks/use_storage.d.ts.map +1 -0
  10. package/build/hooks/use_storage.js +53 -0
  11. package/build/hooks/use_storage.js.map +1 -0
  12. package/build/utils/native_adapters/configuration.d.ts +2 -0
  13. package/build/utils/native_adapters/configuration.d.ts.map +1 -1
  14. package/build/utils/native_adapters/configuration.js +7 -0
  15. package/build/utils/native_adapters/configuration.js.map +1 -1
  16. package/build/utils/native_adapters/index.d.ts +1 -0
  17. package/build/utils/native_adapters/index.d.ts.map +1 -1
  18. package/build/utils/native_adapters/index.js +1 -0
  19. package/build/utils/native_adapters/index.js.map +1 -1
  20. package/build/utils/native_adapters/storage_adapter.d.ts +17 -0
  21. package/build/utils/native_adapters/storage_adapter.d.ts.map +1 -0
  22. package/build/utils/native_adapters/storage_adapter.js +16 -0
  23. package/build/utils/native_adapters/storage_adapter.js.map +1 -0
  24. package/package.json +2 -2
  25. package/src/__tests__/contexts/session_context.tsx +420 -0
  26. package/src/contexts/session_context.tsx +234 -0
  27. package/src/hooks/use_async_storage.ts +3 -52
  28. package/src/hooks/use_storage.ts +69 -0
  29. package/src/utils/native_adapters/configuration.ts +8 -0
  30. package/src/utils/native_adapters/index.ts +1 -0
  31. package/src/utils/native_adapters/storage_adapter.ts +23 -0
@@ -0,0 +1,69 @@
1
+ import { useSuspenseQuery } from '@tanstack/react-query'
2
+ import { noop } from 'lodash'
3
+ import { useCallback } from 'react'
4
+ import { StorageAdapter } from '../utils/native_adapters'
5
+
6
+ const cacheKeyGenerator = (key: string) => [`StorageResource:${key}`]
7
+
8
+ type SetValue<TCacheData> = (_itemValue?: TCacheData | null) => Promise<any>
9
+
10
+ /**
11
+ * Hook for using a storage adapter with React Query caching.
12
+ * Similar to useAsyncStorage but accepts a StorageAdapter instead of using AsyncStorage directly.
13
+ */
14
+ export function useStorage<TCacheData>(
15
+ storage: StorageAdapter,
16
+ key: string,
17
+ initialValue: TCacheData,
18
+ throwOnError: boolean = false
19
+ ): [TCacheData, SetValue<TCacheData>] {
20
+ const cacheKey = cacheKeyGenerator(key)
21
+
22
+ const { data: value, refetch } = useSuspenseQuery<TCacheData>({
23
+ queryKey: cacheKey,
24
+ queryFn: () =>
25
+ storage
26
+ .getItem(key)
27
+ .then(storedValue => {
28
+ if (!storedValue) return initialValue
29
+
30
+ try {
31
+ return JSON.parse(storedValue)
32
+ } catch {
33
+ return storedValue
34
+ }
35
+ })
36
+ .catch(e => {
37
+ if (!throwOnError) return initialValue
38
+
39
+ return Promise.reject(e)
40
+ }),
41
+ })
42
+
43
+ const setValue: SetValue<TCacheData> = useCallback(
44
+ itemValue => {
45
+ if (itemValue === null || itemValue === undefined) {
46
+ return storage
47
+ .removeItem(key)
48
+ .then(() => {
49
+ refetch()
50
+ })
51
+ .catch(noop)
52
+ }
53
+
54
+ return storage
55
+ .setItem(key, JSON.stringify(itemValue))
56
+ .then(() => {
57
+ refetch()
58
+ })
59
+ .catch(e => {
60
+ if (!throwOnError) return
61
+
62
+ return Promise.reject(e)
63
+ })
64
+ },
65
+ [throwOnError, key, refetch, storage]
66
+ )
67
+
68
+ return [value || initialValue, setValue]
69
+ }
@@ -6,6 +6,8 @@ import { VideoAdapter } from './video'
6
6
  import { Linking as RNLinking } from 'react-native'
7
7
  import { LinkingAdapter } from './linking'
8
8
  import { HapticAdapter } from './haptic'
9
+ import { StorageAdapter } from './storage_adapter'
10
+ import AsyncStorage from '@react-native-async-storage/async-storage'
9
11
 
10
12
  type ChatConfigurations = {
11
13
  clipboard: ClipboardAdapter
@@ -74,3 +76,9 @@ export let Log: LogAdapter = new LogAdapter()
74
76
  export let Linking: LinkingAdapter = new LinkingAdapter(RNLinking)
75
77
 
76
78
  export let Haptic: HapticAdapter = new HapticAdapter()
79
+
80
+ export let Storage = new StorageAdapter({
81
+ getItem: AsyncStorage.getItem,
82
+ setItem: AsyncStorage.setItem,
83
+ removeItem: AsyncStorage.removeItem,
84
+ })
@@ -6,3 +6,4 @@ export * from './linking'
6
6
  export * from './log'
7
7
  export * from './video'
8
8
  export * from './haptic'
9
+ export * from './storage_adapter'
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Storage adapter interface for abstracting storage implementations.
3
+ * Allows apps to use different storage backends (AsyncStorage, expo-secure-store, etc.)
4
+ * without requiring those dependencies in the shared package.
5
+ */
6
+
7
+ export type StorageAdapterMethods = {
8
+ getItem: (key: string) => Promise<string | null>
9
+ setItem: (key: string, value: string) => Promise<void>
10
+ removeItem: (key: string) => Promise<void>
11
+ }
12
+
13
+ export class StorageAdapter {
14
+ getItem: (key: string) => Promise<string | null>
15
+ setItem: (key: string, value: string) => Promise<void>
16
+ removeItem: (key: string) => Promise<void>
17
+
18
+ constructor(methods: StorageAdapterMethods) {
19
+ this.getItem = methods.getItem
20
+ this.setItem = methods.setItem
21
+ this.removeItem = methods.removeItem
22
+ }
23
+ }