Skip to content

Commit 9421a33

Browse files
authored
fix: issue with loading app settings when the connectUser is not called on app (#2654)
* fix: issue with loading app settings when the connectUser is not called on app * fix: add comments * fix: add comments
1 parent 2d596ed commit 9421a33

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

package/src/components/Chat/hooks/__tests__/useAppSettings.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ describe('useAppSettings', () => {
1818
auto_translation_enabled: true,
1919
}),
2020
),
21-
userID: 'some-user-id',
2221
} as unknown as StreamChat,
2322
isOnline,
2423
false,

package/src/components/Chat/hooks/useAppSettings.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,32 @@ export const useAppSettings = <
1818
const isMounted = useIsMountedRef();
1919

2020
useEffect(() => {
21-
async function enforeAppSettings() {
22-
if (!client.userID) return;
21+
/**
22+
* Fetches app settings from the backend when offline support is disabled.
23+
*/
24+
const enforceAppSettingsWithoutOfflineSupport = async () => {
25+
try {
26+
const appSettings = await client.getAppSettings();
27+
if (isMounted.current) {
28+
setAppSettings(appSettings);
29+
}
30+
} catch (error: unknown) {
31+
if (error instanceof Error) {
32+
console.error(`An error occurred while getting app settings: ${error}`);
33+
}
34+
}
35+
};
2336

24-
if (enableOfflineSupport && !initialisedDatabase) return;
37+
/**
38+
* Fetches app settings from the local database when offline support is enabled if internet is off else fetches from the backend.
39+
* 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`.
40+
*
41+
* TODO: Remove client.userID usage for offline support case.
42+
*/
43+
const enforceAppSettingsWithOfflineSupport = async () => {
44+
if (!client.userID) return;
2545

26-
if (!isOnline && enableOfflineSupport) {
46+
if (!isOnline) {
2747
const appSettings = dbApi.getAppSettings({ currentUserId: client.userID });
2848
setAppSettings(appSettings);
2949
return;
@@ -33,17 +53,24 @@ export const useAppSettings = <
3353
const appSettings = await client.getAppSettings();
3454
if (isMounted.current) {
3555
setAppSettings(appSettings);
36-
enableOfflineSupport &&
37-
dbApi.upsertAppSettings({
38-
appSettings,
39-
currentUserId: client.userID as string,
40-
});
56+
dbApi.upsertAppSettings({
57+
appSettings,
58+
currentUserId: client.userID as string,
59+
});
4160
}
4261
} catch (error: unknown) {
4362
if (error instanceof Error) {
4463
console.error(`An error occurred while getting app settings: ${error}`);
4564
}
4665
}
66+
};
67+
68+
async function enforeAppSettings() {
69+
if (enableOfflineSupport) {
70+
await enforceAppSettingsWithOfflineSupport();
71+
} else {
72+
await enforceAppSettingsWithoutOfflineSupport();
73+
}
4774
}
4875

4976
enforeAppSettings();

0 commit comments

Comments
 (0)