Skip to content

Commit 5f05794

Browse files
committed
fix: modify app settings fetching logic as per develop changes
1 parent 717e812 commit 5f05794

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed
Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useState } from 'react';
1+
import { useEffect, useRef, useState } from 'react';
22

33
import type { AppSettingsAPIResponse, StreamChat } from 'stream-chat';
44

@@ -11,43 +11,55 @@ export const useAppSettings = (
1111
initialisedDatabase: boolean,
1212
): AppSettingsAPIResponse | null => {
1313
const [appSettings, setAppSettings] = useState<AppSettingsAPIResponse | null>(null);
14+
const appSettingsPromise = useRef<Promise<AppSettingsAPIResponse | null>>(null);
15+
const fetchedAppSettings = useRef(false);
1416
const isMounted = useIsMountedRef();
1517

1618
useEffect(() => {
17-
/**
18-
* Fetches app settings from the backend when offline support is disabled.
19-
*/
20-
21-
/**
22-
* Fetches app settings from the local database when offline support is enabled if internet is off else fetches from the backend.
23-
* Note: We need to set the app settings from the local database when offline as the client will not have the app settings in memory. For this we store it for the `client.userID`.
24-
*
25-
* TODO: Remove client.userID usage for offline support case.
26-
*/
27-
28-
async function enforceAppSettings() {
29-
if (!client.userID) {
30-
return;
19+
if (fetchedAppSettings.current) {
20+
return;
21+
}
22+
23+
const fetchAppSettings = () => {
24+
if (appSettingsPromise.current) {
25+
return appSettingsPromise.current;
3126
}
27+
appSettingsPromise.current = client.getAppSettings();
28+
return appSettingsPromise.current;
29+
};
30+
31+
const enforceAppSettings = async () => {
32+
if (!client.userID) return;
3233

33-
if (enableOfflineSupport && !initialisedDatabase) {
34+
if (enableOfflineSupport && !initialisedDatabase) return;
35+
36+
const userId = client.userID as string;
37+
38+
if (!isOnline && client.offlineDb) {
39+
const appSettings = await client.offlineDb.getAppSettings({ userId });
40+
setAppSettings(appSettings);
3441
return;
3542
}
3643

3744
try {
38-
const appSettings = (await client.getAppSettings()) as AppSettingsAPIResponse;
39-
if (isMounted.current) {
45+
const appSettings = await fetchAppSettings();
46+
if (isMounted.current && appSettings) {
4047
setAppSettings(appSettings);
48+
fetchedAppSettings.current = true;
49+
client.offlineDb?.executeQuerySafely(
50+
(db) => db.upsertAppSettings({ appSettings, userId }),
51+
{ method: 'upsertAppSettings' },
52+
);
4153
}
4254
} catch (error: unknown) {
4355
if (error instanceof Error) {
4456
console.error(`An error occurred while getting app settings: ${error}`);
4557
}
4658
}
47-
}
59+
};
4860

4961
enforceAppSettings();
50-
}, [client, isOnline, initialisedDatabase, enableOfflineSupport, isMounted]);
62+
}, [client, isOnline, initialisedDatabase, isMounted, enableOfflineSupport]);
5163

5264
return appSettings;
5365
};

0 commit comments

Comments
 (0)