Skip to content

Commit 56a3c80

Browse files
committed
feat: move app settings logic to LLC
1 parent bfc99de commit 56a3c80

File tree

2 files changed

+26
-44
lines changed

2 files changed

+26
-44
lines changed

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

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { useEffect, useState } from 'react';
33
import type { AppSettingsAPIResponse, StreamChat } from 'stream-chat';
44

55
import { useIsMountedRef } from '../../../hooks/useIsMountedRef';
6-
import * as dbApi from '../../../store/apis';
76

87
export const useAppSettings = (
98
client: StreamChat,
@@ -18,70 +17,37 @@ export const useAppSettings = (
1817
/**
1918
* Fetches app settings from the backend when offline support is disabled.
2019
*/
21-
const enforceAppSettingsWithoutOfflineSupport = async () => {
22-
if (!client.userID) {
23-
return;
24-
}
25-
26-
try {
27-
const appSettings = await client.getAppSettings();
28-
if (isMounted.current) {
29-
setAppSettings(appSettings);
30-
}
31-
} catch (error: unknown) {
32-
if (error instanceof Error) {
33-
console.error(`An error occurred while getting app settings: ${error}`);
34-
}
35-
}
36-
};
3720

3821
/**
3922
* Fetches app settings from the local database when offline support is enabled if internet is off else fetches from the backend.
4023
* 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`.
4124
*
4225
* TODO: Remove client.userID usage for offline support case.
4326
*/
44-
const enforceAppSettingsWithOfflineSupport = async () => {
27+
28+
async function enforceAppSettings() {
4529
if (!client.userID) {
4630
return;
4731
}
48-
if (!initialisedDatabase) {
49-
return;
50-
}
5132

52-
if (!isOnline) {
53-
const appSettings = await dbApi.getAppSettings({ currentUserId: client.userID });
54-
setAppSettings(appSettings);
33+
if (enableOfflineSupport && !initialisedDatabase) {
5534
return;
5635
}
5736

5837
try {
59-
const appSettings = await client.getAppSettings();
38+
const appSettings = (await client.getAppSettings()) as AppSettingsAPIResponse;
6039
if (isMounted.current) {
6140
setAppSettings(appSettings);
62-
await dbApi.upsertAppSettings({
63-
appSettings,
64-
currentUserId: client.userID as string,
65-
});
6641
}
6742
} catch (error: unknown) {
6843
if (error instanceof Error) {
6944
console.error(`An error occurred while getting app settings: ${error}`);
7045
}
7146
}
72-
};
73-
74-
async function enforeAppSettings() {
75-
if (enableOfflineSupport) {
76-
await enforceAppSettingsWithOfflineSupport();
77-
} else {
78-
await enforceAppSettingsWithoutOfflineSupport();
79-
}
8047
}
8148

82-
enforeAppSettings();
83-
// eslint-disable-next-line react-hooks/exhaustive-deps
84-
}, [client, isOnline, initialisedDatabase]);
49+
enforceAppSettings();
50+
}, [client, isOnline, initialisedDatabase, enableOfflineSupport, isMounted]);
8551

8652
return appSettings;
8753
};

package/src/store/OfflineDB.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { AbstractOfflineDB, StreamChat } from 'stream-chat';
1+
import {
2+
AbstractOfflineDB,
3+
GetAppSettingsType,
4+
StreamChat,
5+
UpsertAppSettingsType,
6+
} from 'stream-chat';
27
import type {
38
GetChannelsForQueryType,
49
GetChannelsType,
@@ -15,10 +20,21 @@ export class OfflineDB extends AbstractOfflineDB {
1520
}
1621

1722
upsertCidsForQuery = api.upsertCidsForQuery;
23+
1824
upsertChannels = api.upsertChannels;
25+
26+
// FIXME
27+
upsertUserSyncStatus = ({ userId, lastSyncedAt }: UpsertUserSyncStatusType) =>
28+
api.upsertUserSyncStatus({ currentUserId: userId, lastSyncedAt });
29+
30+
// FIXME
31+
upsertAppSettings = ({ appSettings, userId, flush }: UpsertAppSettingsType) =>
32+
api.upsertAppSettings({ appSettings, currentUserId: userId, flush });
33+
1934
// FIXME
2035
getChannels = ({ cids, userId }: GetChannelsType) =>
2136
api.getChannels({ channelIds: cids, currentUserId: userId });
37+
2238
// FIXME
2339
getChannelsForQuery = ({ userId, filters, sort }: GetChannelsForQueryType) =>
2440
api.getChannelsForFilterSort({ currentUserId: userId, filters, sort });
@@ -27,9 +43,9 @@ export class OfflineDB extends AbstractOfflineDB {
2743
// FIXME
2844
getLastSyncedAt = ({ userId }: GetLastSyncedAtType) =>
2945
api.getLastSyncedAt({ currentUserId: userId });
30-
// FIXME
31-
upsertUserSyncStatus = ({ userId, lastSyncedAt }: UpsertUserSyncStatusType) =>
32-
api.upsertUserSyncStatus({ currentUserId: userId, lastSyncedAt });
46+
47+
getAppSettings = ({ userId }: GetAppSettingsType) =>
48+
api.getAppSettings({ currentUserId: userId });
3349

3450
addPendingTask = api.addPendingTask;
3551

0 commit comments

Comments
 (0)