1- import { useEffect , useState } from 'react' ;
1+ import { useEffect , useRef , useState } from 'react' ;
22
33import type { AppSettingsAPIResponse , StreamChat } from 'stream-chat' ;
44
@@ -12,9 +12,18 @@ export const useAppSettings = (
1212 initialisedDatabase : boolean ,
1313) : AppSettingsAPIResponse | null => {
1414 const [ appSettings , setAppSettings ] = useState < AppSettingsAPIResponse | null > ( null ) ;
15+ const appSettingsPromise = useRef < Promise < AppSettingsAPIResponse | null > > ( null ) ;
16+ const fetchedAppSettings = useRef ( false ) ;
1517 const isMounted = useIsMountedRef ( ) ;
1618
1719 useEffect ( ( ) => {
20+ const fetchAppSettings = ( ) => {
21+ if ( appSettingsPromise . current ) {
22+ return appSettingsPromise . current ;
23+ }
24+ appSettingsPromise . current = client . getAppSettings ( ) ;
25+ return appSettingsPromise . current ;
26+ } ;
1827 /**
1928 * Fetches app settings from the backend when offline support is disabled.
2029 */
@@ -24,9 +33,10 @@ export const useAppSettings = (
2433 }
2534
2635 try {
27- const appSettings = await client . getAppSettings ( ) ;
36+ const appSettings = await fetchAppSettings ( ) ;
2837 if ( isMounted . current ) {
2938 setAppSettings ( appSettings ) ;
39+ fetchedAppSettings . current = true ;
3040 }
3141 } catch ( error : unknown ) {
3242 if ( error instanceof Error ) {
@@ -56,9 +66,10 @@ export const useAppSettings = (
5666 }
5767
5868 try {
59- const appSettings = await client . getAppSettings ( ) ;
60- if ( isMounted . current ) {
69+ const appSettings = await fetchAppSettings ( ) ;
70+ if ( isMounted . current && appSettings ) {
6171 setAppSettings ( appSettings ) ;
72+ fetchedAppSettings . current = true ;
6273 await dbApi . upsertAppSettings ( {
6374 appSettings,
6475 currentUserId : client . userID as string ,
@@ -72,6 +83,10 @@ export const useAppSettings = (
7283 } ;
7384
7485 async function enforeAppSettings ( ) {
86+ if ( fetchedAppSettings . current ) {
87+ return ;
88+ }
89+
7590 if ( enableOfflineSupport ) {
7691 await enforceAppSettingsWithOfflineSupport ( ) ;
7792 } else {
0 commit comments