From 4022e3aa064838acfcc5712d8b16d1417db77ad5 Mon Sep 17 00:00:00 2001 From: Santhosh Vaiyapuri Date: Fri, 10 Mar 2023 11:34:57 +0100 Subject: [PATCH 1/3] fix: missing userSyncStatus db upon clean install --- package/src/components/Chat/Chat.tsx | 22 +++++++++++-------- .../components/Chat/hooks/useAppSettings.ts | 4 +++- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/package/src/components/Chat/Chat.tsx b/package/src/components/Chat/Chat.tsx index 428d409388..c946864db8 100644 --- a/package/src/components/Chat/Chat.tsx +++ b/package/src/components/Chat/Chat.tsx @@ -167,6 +167,8 @@ const ChatWithContext = < closeConnectionOnBackground, ); + const [initialisingDatabase, setInitialisingDatabase] = useState(enableOfflineSupport); + /** * Setup muted user listener * TODO: reimplement @@ -196,7 +198,16 @@ const ChatWithContext = < const setActiveChannel = (newChannel?: Channel) => setChannel(newChannel); - const appSettings = useAppSettings(client, isOnline, enableOfflineSupport); + useEffect(() => { + if (client.user?.id && enableOfflineSupport) { + setInitialisingDatabase(true); + QuickSqliteClient.initializeDatabase(); + DBSyncManager.init(client as unknown as StreamChat); + setInitialisingDatabase(false); + } + }, [client?.user?.id, enableOfflineSupport]); + + const appSettings = useAppSettings(client, isOnline, enableOfflineSupport, initialisingDatabase); const chatContext = useCreateChatContext({ appSettings, @@ -210,19 +221,12 @@ const ChatWithContext = < setActiveChannel, }); - useEffect(() => { - if (client.user?.id && enableOfflineSupport) { - QuickSqliteClient.initializeDatabase(); - DBSyncManager.init(client as unknown as StreamChat); - } - }, [client?.user?.id]); - useSyncDatabase({ client, enableOfflineSupport, }); - if (loadingTranslators) return null; + if (loadingTranslators || initialisingDatabase) return null; return ( value={chatContext}> diff --git a/package/src/components/Chat/hooks/useAppSettings.ts b/package/src/components/Chat/hooks/useAppSettings.ts index 3eb4bbd4ce..93d57d541a 100644 --- a/package/src/components/Chat/hooks/useAppSettings.ts +++ b/package/src/components/Chat/hooks/useAppSettings.ts @@ -10,12 +10,14 @@ export const useAppSettings = < client: StreamChat, isOnline: boolean | null, enableOfflineSupport: boolean, + initialisingDatabase: boolean, ): AppSettingsAPIResponse | null => { const [appSettings, setAppSettings] = useState(null); const isMounted = useRef(true); useEffect(() => { async function enforeAppSettings() { + if (!initialisingDatabase) return; if (!client.userID) return; if (!isOnline && enableOfflineSupport) { @@ -46,7 +48,7 @@ export const useAppSettings = < return () => { isMounted.current = false; }; - }, [client, isOnline]); + }, [client, isOnline, initialisingDatabase]); return appSettings; }; From ee87d6cc34cc05d3a53d60a3866d5934a764bdf9 Mon Sep 17 00:00:00 2001 From: Santhosh Vaiyapuri Date: Fri, 10 Mar 2023 11:47:25 +0100 Subject: [PATCH 2/3] fix: broken test --- .../src/components/Chat/hooks/__tests__/useAppSettings.test.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/package/src/components/Chat/hooks/__tests__/useAppSettings.test.tsx b/package/src/components/Chat/hooks/__tests__/useAppSettings.test.tsx index 76ce016b0b..3f96bdab75 100644 --- a/package/src/components/Chat/hooks/__tests__/useAppSettings.test.tsx +++ b/package/src/components/Chat/hooks/__tests__/useAppSettings.test.tsx @@ -22,6 +22,7 @@ describe('useAppSettings', () => { } as unknown as StreamChat, isOnline, false, + false, ); return {JSON.stringify(appSettings)}; From da27ecc11fe1c278f04947c2ce341860fba98d24 Mon Sep 17 00:00:00 2001 From: Santhosh Vaiyapuri Date: Fri, 10 Mar 2023 12:00:06 +0100 Subject: [PATCH 3/3] fix: failing test --- package/src/components/Chat/hooks/useAppSettings.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/src/components/Chat/hooks/useAppSettings.ts b/package/src/components/Chat/hooks/useAppSettings.ts index 93d57d541a..b02965c3ae 100644 --- a/package/src/components/Chat/hooks/useAppSettings.ts +++ b/package/src/components/Chat/hooks/useAppSettings.ts @@ -17,7 +17,7 @@ export const useAppSettings = < useEffect(() => { async function enforeAppSettings() { - if (!initialisingDatabase) return; + if (initialisingDatabase) return; if (!client.userID) return; if (!isOnline && enableOfflineSupport) {