11import { useEffect , useRef , useState } from 'react' ;
22
33import type { AppSettingsAPIResponse , StreamChat } from 'stream-chat' ;
4- import type { DefaultStreamChatGenerics } from 'stream-chat-react-native' ;
54
5+ import * as dbApi from '../../../store/apis' ;
6+ import type { DefaultStreamChatGenerics } from '../../../types/types' ;
67export const useAppSettings = <
78 StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics ,
89> (
@@ -13,11 +14,23 @@ export const useAppSettings = <
1314 const isMounted = useRef ( true ) ;
1415
1516 useEffect ( ( ) => {
16- async function getAppSettings ( ) {
17+ async function enforeAppSettings ( ) {
18+ if ( ! client . userID ) return ;
19+
20+ if ( ! isOnline ) {
21+ const appSettings = dbApi . getAppSettings ( { currentUserId : client . userID } ) ;
22+ setAppSettings ( appSettings ) ;
23+ return ;
24+ }
25+
1726 try {
1827 const appSettings = await client . getAppSettings ( ) ;
1928 if ( isMounted . current ) {
2029 setAppSettings ( appSettings ) ;
30+ dbApi . upsertAppSettings ( {
31+ appSettings,
32+ currentUserId : client . userID as string ,
33+ } ) ;
2134 }
2235 } catch ( error : unknown ) {
2336 if ( error instanceof Error ) {
@@ -26,9 +39,7 @@ export const useAppSettings = <
2639 }
2740 }
2841
29- if ( isOnline && client . userID ) {
30- getAppSettings ( ) ;
31- }
42+ enforeAppSettings ( ) ;
3243
3344 return ( ) => {
3445 isMounted . current = false ;
0 commit comments