Skip to content

Commit

Permalink
fix: blank screen on offline support if user id is lazily set (#2003)
Browse files Browse the repository at this point in the history
* fix: blank screen on offline support if user id is lazily set

* chore: change the initial state value of initialisedDb
  • Loading branch information
santhoshvai authored Mar 13, 2023
1 parent 36eb866 commit 2cc2d4a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
12 changes: 6 additions & 6 deletions package/src/components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ const ChatWithContext = <
closeConnectionOnBackground,
);

const [initialisingDatabase, setInitialisingDatabase] = useState(enableOfflineSupport);
const [initialisedDatabase, setInitialisedDatabase] = useState(false);

/**
* Setup muted user listener
Expand All @@ -194,20 +194,20 @@ const ChatWithContext = <
data: client.user,
});
}
}, [client]);
}, [client, enableOfflineSupport]);

const setActiveChannel = (newChannel?: Channel<StreamChatGenerics>) => setChannel(newChannel);

useEffect(() => {
if (client.user?.id && enableOfflineSupport) {
setInitialisingDatabase(true);
setInitialisedDatabase(false);
QuickSqliteClient.initializeDatabase();
DBSyncManager.init(client as unknown as StreamChat);
setInitialisingDatabase(false);
setInitialisedDatabase(true);
}
}, [client?.user?.id, enableOfflineSupport]);

const appSettings = useAppSettings(client, isOnline, enableOfflineSupport, initialisingDatabase);
const appSettings = useAppSettings(client, isOnline, enableOfflineSupport, initialisedDatabase);

const chatContext = useCreateChatContext({
appSettings,
Expand All @@ -226,7 +226,7 @@ const ChatWithContext = <
enableOfflineSupport,
});

if (loadingTranslators || initialisingDatabase) return null;
if (loadingTranslators) return null;

return (
<ChatProvider<StreamChatGenerics> value={chatContext}>
Expand Down
7 changes: 3 additions & 4 deletions package/src/components/Chat/hooks/useAppSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ export const useAppSettings = <
client: StreamChat<StreamChatGenerics>,
isOnline: boolean | null,
enableOfflineSupport: boolean,
initialisingDatabase: boolean,
initialisedDatabase: boolean,
): AppSettingsAPIResponse | null => {
const [appSettings, setAppSettings] = useState<AppSettingsAPIResponse | null>(null);
const isMounted = useRef(true);

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

if (!isOnline && enableOfflineSupport) {
if (!isOnline && enableOfflineSupport && initialisedDatabase) {
const appSettings = dbApi.getAppSettings({ currentUserId: client.userID });
setAppSettings(appSettings);
return;
Expand Down Expand Up @@ -48,7 +47,7 @@ export const useAppSettings = <
return () => {
isMounted.current = false;
};
}, [client, isOnline, initialisingDatabase]);
}, [client, isOnline, initialisedDatabase]);

return appSettings;
};

0 comments on commit 2cc2d4a

Please sign in to comment.