Changeset View
Standalone View
web/database/sqlite-data-handler.js
- This file was added.
// @flow | |||||
import * as React from 'react'; | |||||
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 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, | |||||
}); | |||||
if ( | |||||
currentUserData?.userID && | |||||
currentUserData.userID !== currentLoggedInUserID | |||||
) { | |||||
await databaseModule.clearSensitiveData(); | |||||
} | |||||
if (currentLoggedInUserID) { | |||||
await databaseModule.schedule({ | |||||
type: workerRequestMessageTypes.SET_CURRENT_USER_ID, | |||||
userID: currentLoggedInUserID, | |||||
}); | |||||
} | |||||
tomek: Do we have to always call this code? Maybe we should call it only when `currentUserData.userID ! | |||||
kamilAuthorUnsubmitted Done Inline Actionsright, makes sense kamil: right, makes sense | |||||
} catch (error) { | |||||
console.error(error); | |||||
kamilAuthorUnsubmitted Done Inline ActionsI think we could try to manually clear indexedDB with persistent content here if clearSensitiveData fails, I can introduce this in next differential kamil: I think we could try to manually clear indexedDB with persistent content here if… | |||||
tomekUnsubmitted Not Done Inline ActionsBetter error handling is a good idea - manual clearing sounds reasonable. At least, we shouldn't swallow the exception. tomek: Better error handling is a good idea - manual clearing sounds reasonable. At least, we… | |||||
kamilAuthorUnsubmitted Done Inline Actionsrethrowing error for now - creating a task for better error handling ENG-3610. kamil: rethrowing error for now - creating a task for better error handling [ENG-3610](https://linear. | |||||
} | |||||
}, [currentLoggedInUserID]); | |||||
React.useEffect(() => { | |||||
(async () => { | |||||
if (currentLoggedInUserID) { | |||||
await databaseModule.userLoggedIn(currentLoggedInUserID); | |||||
tomekUnsubmitted Not Done Inline ActionsCan we find a better name than userLoggedIn? A function that performs an action should contain a verb in its name. tomek: Can we find a better name than `userLoggedIn`? A function that performs an action should… | |||||
kamilAuthorUnsubmitted Done Inline Actionsrenaming to initDBForLoggedInUser kamil: renaming to `initDBForLoggedInUser` | |||||
} | |||||
if (!rehydrateConcluded) { | |||||
return; | |||||
} | |||||
const isWorkable = await databaseModule.isDatabaseWorkable(); | |||||
if (!isWorkable) { | |||||
return; | |||||
} | |||||
tomekUnsubmitted Not Done Inline ActionsIs this logic correct? Is it possible to call userLoggedIn, then return because !rehydrateConcluded, then in the next render, call userLoggedIn again? tomek: Is this logic correct? Is it possible to call `userLoggedIn`, then return because `! | |||||
kamilAuthorUnsubmitted Done Inline ActionsReplying but after renaming userLoggedIn => initDBForLoggedInUser I know it's confusing (probably because of the naming), but we will move the entire redux to the database, which means we might want to call initDBForLoggedInUser before rehydration. kamil: Replying but after renaming `userLoggedIn` => `initDBForLoggedInUser`
I know it's confusing… | |||||
await handleSensitiveData(); | |||||
})(); | |||||
}, [currentLoggedInUserID, handleSensitiveData, rehydrateConcluded]); | |||||
return null; | |||||
} | |||||
export { SQLiteDataHandler }; |
Do we have to always call this code? Maybe we should call it only when currentUserData.userID !== currentLoggedInUserID?