Changeset View
Changeset View
Standalone View
Standalone View
web/database/sqlite-data-handler.js
// @flow | // @flow | ||||
import * as React from 'react'; | 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 { databaseModule } from './database-module-provider.js'; | ||||
import { useSelector } from '../redux/redux-utils.js'; | import { useSelector } from '../redux/redux-utils.js'; | ||||
import { workerRequestMessageTypes } from '../types/worker-types.js'; | import { workerRequestMessageTypes } from '../types/worker-types.js'; | ||||
function SQLiteDataHandler(): React.Node { | function SQLiteDataHandler(): React.Node { | ||||
const dispatch = useDispatch(); | |||||
const rehydrateConcluded = useSelector( | const rehydrateConcluded = useSelector( | ||||
state => !!(state._persist && state._persist.rehydrated), | state => !!(state._persist && state._persist.rehydrated), | ||||
); | ); | ||||
const currentLoggedInUserID = useSelector(state => | const currentLoggedInUserID = useSelector(state => | ||||
state.currentUserInfo?.anonymous ? undefined : state.currentUserInfo?.id, | state.currentUserInfo?.anonymous ? undefined : state.currentUserInfo?.id, | ||||
); | ); | ||||
const handleSensitiveData = React.useCallback(async () => { | const handleSensitiveData = React.useCallback(async () => { | ||||
Show All 21 Lines | const handleSensitiveData = React.useCallback(async () => { | ||||
} | } | ||||
}, [currentLoggedInUserID]); | }, [currentLoggedInUserID]); | ||||
React.useEffect(() => { | React.useEffect(() => { | ||||
(async () => { | (async () => { | ||||
if (currentLoggedInUserID) { | if (currentLoggedInUserID) { | ||||
await databaseModule.initDBForLoggedInUser(currentLoggedInUserID); | await databaseModule.initDBForLoggedInUser(currentLoggedInUserID); | ||||
} | } | ||||
if (!rehydrateConcluded) { | if (!rehydrateConcluded) { | ||||
return; | return; | ||||
} | } | ||||
const isSupported = await databaseModule.isDatabaseSupported(); | const isSupported = await databaseModule.isDatabaseSupported(); | ||||
if (!isSupported) { | if (!isSupported) { | ||||
return; | return; | ||||
} | } | ||||
await handleSensitiveData(); | 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; | return null; | ||||
} | } | ||||
export { SQLiteDataHandler }; | export { SQLiteDataHandler }; |