Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missing userSyncStatus db table upon clean install #2001

Merged
merged 3 commits into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions package/src/components/Chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ const ChatWithContext = <
closeConnectionOnBackground,
);

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

/**
* Setup muted user listener
* TODO: reimplement
Expand Down Expand Up @@ -196,7 +198,16 @@ const ChatWithContext = <

const setActiveChannel = (newChannel?: Channel<StreamChatGenerics>) => 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,
Expand All @@ -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]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fact that we missed enableOfflineSupport is also a red flag


useSyncDatabase({
client,
enableOfflineSupport,
});

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

return (
<ChatProvider<StreamChatGenerics> value={chatContext}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('useAppSettings', () => {
} as unknown as StreamChat,
isOnline,
false,
false,
);

return <Text>{JSON.stringify(appSettings)}</Text>;
Expand Down
4 changes: 3 additions & 1 deletion package/src/components/Chat/hooks/useAppSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ export const useAppSettings = <
client: StreamChat<StreamChatGenerics>,
isOnline: boolean | null,
enableOfflineSupport: boolean,
initialisingDatabase: 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) {
Expand Down Expand Up @@ -46,7 +48,7 @@ export const useAppSettings = <
return () => {
isMounted.current = false;
};
}, [client, isOnline]);
}, [client, isOnline, initialisingDatabase]);

return appSettings;
};