Skip to content

Commit 92decc8

Browse files
feat: add appSettings to database
1 parent 8eb7879 commit 92decc8

File tree

6 files changed

+63
-6
lines changed

6 files changed

+63
-6
lines changed

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { useEffect, useRef, useState } from 'react';
22

33
import 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';
67
export 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;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const useIsOnline = <
2222
client: StreamChat<StreamChatGenerics>,
2323
closeConnectionOnBackground = true,
2424
) => {
25-
const [isOnline, setIsOnline] = useState(true);
25+
const [isOnline, setIsOnline] = useState<boolean | null>(null);
2626
const [connectionRecovering, setConnectionRecovering] = useState(false);
2727
const isMounted = useIsMountedRef();
2828
const clientExists = !!client;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { AppSettingsAPIResponse } from 'stream-chat';
2+
3+
import { QuickSqliteClient } from '../QuickSqliteClient';
4+
import { createSelectQuery } from '../sqlite-utils/createSelectQuery';
5+
6+
export const getAppSettings = ({
7+
currentUserId,
8+
}: {
9+
currentUserId: string;
10+
}): AppSettingsAPIResponse => {
11+
const result = QuickSqliteClient.executeSql.apply(
12+
null,
13+
createSelectQuery('userSyncStatus', ['*'], {
14+
userId: currentUserId,
15+
}),
16+
);
17+
18+
return result[0]?.appSettings ? JSON.parse(result[0].appSettings) : null;
19+
};

package/src/store/apis/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from './deleteMessage';
44
export * from './deleteMessagesForChannel';
55
export * from './deleteReactions';
66
export * from './getAllChannelIds';
7+
export * from './getAppSettings';
78
export * from './getChannelMessages';
89
export * from './getChannels';
910
export * from './getChannelsForFilterSort';
@@ -12,6 +13,7 @@ export * from './getMembers';
1213
export * from './getReads';
1314
export * from './updateMessage';
1415
export * from './updateReaction';
16+
export * from './upsertAppSettings';
1517
export * from './upsertChannelData';
1618
export * from './upsertChannels';
1719
export * from './upsertCidsForQuery';
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { AppSettingsAPIResponse } from 'stream-chat';
2+
3+
import { QuickSqliteClient } from '../QuickSqliteClient';
4+
import { createUpsertQuery } from '../sqlite-utils/createUpsertQuery';
5+
6+
export const upsertAppSettings = ({
7+
appSettings,
8+
currentUserId,
9+
flush = true,
10+
}: {
11+
appSettings: AppSettingsAPIResponse;
12+
currentUserId: string;
13+
flush?: boolean;
14+
}) => {
15+
const query = createUpsertQuery('userSyncStatus', {
16+
appSettings: JSON.stringify(appSettings),
17+
userId: currentUserId,
18+
});
19+
20+
if (flush) {
21+
QuickSqliteClient.executeSql.apply(null, query);
22+
}
23+
};

package/src/store/schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ export const tables: Tables = {
201201
},
202202
userSyncStatus: {
203203
columns: {
204+
appSettings: 'TEXT',
204205
lastSyncedAt: 'TEXT',
205206
userId: 'TEXT',
206207
},
@@ -302,6 +303,7 @@ export type Schema = {
302303
updatedAt?: string;
303304
};
304305
userSyncStatus: {
306+
appSettings: string;
305307
lastSyncedAt: string;
306308
userId: string;
307309
};

0 commit comments

Comments
 (0)