diff --git a/web/database/sqlite-data-handler.js b/web/database/sqlite-data-handler.js index 6b8274f9a..88648fc67 100644 --- a/web/database/sqlite-data-handler.js +++ b/web/database/sqlite-data-handler.js @@ -1,62 +1,88 @@ // @flow import * as React from 'react'; +import { useDispatch } from 'react-redux'; + +import { setClientDBStoreActionType } from 'lib/actions/client-db-store-actions.js'; import { databaseModule } from './database-module-provider.js'; import { useSelector } from '../redux/redux-utils.js'; import { workerRequestMessageTypes } from '../types/worker-types.js'; function SQLiteDataHandler(): React.Node { + const dispatch = useDispatch(); const rehydrateConcluded = useSelector( state => !!(state._persist && state._persist.rehydrated), ); const currentLoggedInUserID = useSelector(state => state.currentUserInfo?.anonymous ? undefined : state.currentUserInfo?.id, ); const handleSensitiveData = React.useCallback(async () => { try { const currentUserData = await databaseModule.schedule({ type: workerRequestMessageTypes.GET_CURRENT_USER_ID, }); const currentDBUserID = currentUserData?.userID; if (currentDBUserID && currentDBUserID !== currentLoggedInUserID) { await databaseModule.clearSensitiveData(); } if ( currentLoggedInUserID && (currentDBUserID || currentDBUserID !== currentLoggedInUserID) ) { await databaseModule.schedule({ type: workerRequestMessageTypes.SET_CURRENT_USER_ID, userID: currentLoggedInUserID, }); } } catch (error) { console.error(error); throw error; } }, [currentLoggedInUserID]); React.useEffect(() => { (async () => { if (currentLoggedInUserID) { await databaseModule.initDBForLoggedInUser(currentLoggedInUserID); } + if (!rehydrateConcluded) { return; } const isSupported = await databaseModule.isDatabaseSupported(); if (!isSupported) { return; } await handleSensitiveData(); + if (!currentLoggedInUserID) { + return; + } + const data = await databaseModule.schedule({ + type: workerRequestMessageTypes.GET_CLIENT_STORE, + }); + + if (!data?.store?.drafts) { + return; + } + dispatch({ + type: setClientDBStoreActionType, + payload: { + drafts: data.store.drafts, + }, + }); })(); - }, [currentLoggedInUserID, handleSensitiveData, rehydrateConcluded]); + }, [ + currentLoggedInUserID, + dispatch, + handleSensitiveData, + rehydrateConcluded, + ]); return null; } export { SQLiteDataHandler };