Skip to content

Commit

Permalink
feat: add appSettings to database
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalnarkhede committed Nov 29, 2022
1 parent 8eb7879 commit 92decc8
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
21 changes: 16 additions & 5 deletions package/src/components/Chat/hooks/useAppSettings.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useEffect, useRef, useState } from 'react';

import type { AppSettingsAPIResponse, StreamChat } from 'stream-chat';
import type { DefaultStreamChatGenerics } from 'stream-chat-react-native';

import * as dbApi from '../../../store/apis';
import type { DefaultStreamChatGenerics } from '../../../types/types';
export const useAppSettings = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
>(
Expand All @@ -13,11 +14,23 @@ export const useAppSettings = <
const isMounted = useRef(true);

useEffect(() => {
async function getAppSettings() {
async function enforeAppSettings() {
if (!client.userID) return;

if (!isOnline) {
const appSettings = dbApi.getAppSettings({ currentUserId: client.userID });
setAppSettings(appSettings);
return;
}

try {
const appSettings = await client.getAppSettings();
if (isMounted.current) {
setAppSettings(appSettings);
dbApi.upsertAppSettings({
appSettings,
currentUserId: client.userID as string,
});
}
} catch (error: unknown) {
if (error instanceof Error) {
Expand All @@ -26,9 +39,7 @@ export const useAppSettings = <
}
}

if (isOnline && client.userID) {
getAppSettings();
}
enforeAppSettings();

return () => {
isMounted.current = false;
Expand Down
2 changes: 1 addition & 1 deletion package/src/components/Chat/hooks/useIsOnline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const useIsOnline = <
client: StreamChat<StreamChatGenerics>,
closeConnectionOnBackground = true,
) => {
const [isOnline, setIsOnline] = useState(true);
const [isOnline, setIsOnline] = useState<boolean | null>(null);
const [connectionRecovering, setConnectionRecovering] = useState(false);
const isMounted = useIsMountedRef();
const clientExists = !!client;
Expand Down
19 changes: 19 additions & 0 deletions package/src/store/apis/getAppSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { AppSettingsAPIResponse } from 'stream-chat';

import { QuickSqliteClient } from '../QuickSqliteClient';
import { createSelectQuery } from '../sqlite-utils/createSelectQuery';

export const getAppSettings = ({
currentUserId,
}: {
currentUserId: string;
}): AppSettingsAPIResponse => {
const result = QuickSqliteClient.executeSql.apply(
null,
createSelectQuery('userSyncStatus', ['*'], {
userId: currentUserId,
}),
);

return result[0]?.appSettings ? JSON.parse(result[0].appSettings) : null;
};
2 changes: 2 additions & 0 deletions package/src/store/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './deleteMessage';
export * from './deleteMessagesForChannel';
export * from './deleteReactions';
export * from './getAllChannelIds';
export * from './getAppSettings';
export * from './getChannelMessages';
export * from './getChannels';
export * from './getChannelsForFilterSort';
Expand All @@ -12,6 +13,7 @@ export * from './getMembers';
export * from './getReads';
export * from './updateMessage';
export * from './updateReaction';
export * from './upsertAppSettings';
export * from './upsertChannelData';
export * from './upsertChannels';
export * from './upsertCidsForQuery';
Expand Down
23 changes: 23 additions & 0 deletions package/src/store/apis/upsertAppSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { AppSettingsAPIResponse } from 'stream-chat';

import { QuickSqliteClient } from '../QuickSqliteClient';
import { createUpsertQuery } from '../sqlite-utils/createUpsertQuery';

export const upsertAppSettings = ({
appSettings,
currentUserId,
flush = true,
}: {
appSettings: AppSettingsAPIResponse;
currentUserId: string;
flush?: boolean;
}) => {
const query = createUpsertQuery('userSyncStatus', {
appSettings: JSON.stringify(appSettings),
userId: currentUserId,
});

if (flush) {
QuickSqliteClient.executeSql.apply(null, query);
}
};
2 changes: 2 additions & 0 deletions package/src/store/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export const tables: Tables = {
},
userSyncStatus: {
columns: {
appSettings: 'TEXT',
lastSyncedAt: 'TEXT',
userId: 'TEXT',
},
Expand Down Expand Up @@ -302,6 +303,7 @@ export type Schema = {
updatedAt?: string;
};
userSyncStatus: {
appSettings: string;
lastSyncedAt: string;
userId: string;
};
Expand Down

0 comments on commit 92decc8

Please sign in to comment.