Changeset View
Changeset View
Standalone View
Standalone View
native/data/sqlite-data-handler.js
// @flow | // @flow | ||||
import * as React from 'react'; | import * as React from 'react'; | ||||
import { Alert } from 'react-native'; | import { Alert } from 'react-native'; | ||||
import { useDispatch } from 'react-redux'; | import { useDispatch } from 'react-redux'; | ||||
import { setClientDBStoreActionType } from 'lib/actions/client-db-store-actions.js'; | import { setClientDBStoreActionType } from 'lib/actions/client-db-store-actions.js'; | ||||
import { MediaCacheContext } from 'lib/components/media-cache-provider.react.js'; | |||||
import { isLoggedIn } from 'lib/selectors/user-selectors.js'; | import { isLoggedIn } from 'lib/selectors/user-selectors.js'; | ||||
import { | import { | ||||
logInActionSources, | logInActionSources, | ||||
type LogInActionSource, | type LogInActionSource, | ||||
} from 'lib/types/account-types.js'; | } from 'lib/types/account-types.js'; | ||||
import { fetchNewCookieFromNativeCredentials } from 'lib/utils/action-utils.js'; | import { fetchNewCookieFromNativeCredentials } from 'lib/utils/action-utils.js'; | ||||
import { getMessageForException } from 'lib/utils/errors.js'; | import { getMessageForException } from 'lib/utils/errors.js'; | ||||
import { convertClientDBThreadInfosToRawThreadInfos } from 'lib/utils/thread-ops-utils.js'; | import { convertClientDBThreadInfosToRawThreadInfos } from 'lib/utils/thread-ops-utils.js'; | ||||
import { filesystemMediaCache } from '../media/media-cache.js'; | |||||
import { commCoreModule } from '../native-modules.js'; | import { commCoreModule } from '../native-modules.js'; | ||||
import { setStoreLoadedActionType } from '../redux/action-types.js'; | import { setStoreLoadedActionType } from '../redux/action-types.js'; | ||||
import { useSelector } from '../redux/redux-utils.js'; | import { useSelector } from '../redux/redux-utils.js'; | ||||
import { StaffContext } from '../staff/staff-context.js'; | import { StaffContext } from '../staff/staff-context.js'; | ||||
import { isTaskCancelledError } from '../utils/error-handling.js'; | import { isTaskCancelledError } from '../utils/error-handling.js'; | ||||
import { useStaffCanSee } from '../utils/staff-utils.js'; | import { useStaffCanSee } from '../utils/staff-utils.js'; | ||||
function SQLiteDataHandler(): React.Node { | function SQLiteDataHandler(): React.Node { | ||||
const storeLoaded = useSelector(state => state.storeLoaded); | const storeLoaded = useSelector(state => state.storeLoaded); | ||||
const dispatch = useDispatch(); | const dispatch = useDispatch(); | ||||
const rehydrateConcluded = useSelector( | const rehydrateConcluded = useSelector( | ||||
state => !!(state._persist && state._persist.rehydrated), | state => !!(state._persist && state._persist.rehydrated), | ||||
); | ); | ||||
const cookie = useSelector(state => state.cookie); | const cookie = useSelector(state => state.cookie); | ||||
const urlPrefix = useSelector(state => state.urlPrefix); | const urlPrefix = useSelector(state => state.urlPrefix); | ||||
const staffCanSee = useStaffCanSee(); | const staffCanSee = useStaffCanSee(); | ||||
const { staffUserHasBeenLoggedIn } = React.useContext(StaffContext); | const { staffUserHasBeenLoggedIn } = React.useContext(StaffContext); | ||||
const loggedIn = useSelector(isLoggedIn); | const loggedIn = useSelector(isLoggedIn); | ||||
const currentLoggedInUserID = useSelector(state => | const currentLoggedInUserID = useSelector(state => | ||||
state.currentUserInfo?.anonymous ? undefined : state.currentUserInfo?.id, | state.currentUserInfo?.anonymous ? undefined : state.currentUserInfo?.id, | ||||
); | ); | ||||
const mediaCacheContext = React.useContext(MediaCacheContext); | |||||
const callFetchNewCookieFromNativeCredentials = React.useCallback( | const callFetchNewCookieFromNativeCredentials = React.useCallback( | ||||
async (source: LogInActionSource) => { | async (source: LogInActionSource) => { | ||||
try { | try { | ||||
await fetchNewCookieFromNativeCredentials( | await fetchNewCookieFromNativeCredentials( | ||||
dispatch, | dispatch, | ||||
cookie, | cookie, | ||||
urlPrefix, | urlPrefix, | ||||
Show All 17 Lines | function SQLiteDataHandler(): React.Node { | ||||
); | ); | ||||
const callClearSensitiveData = React.useCallback( | const callClearSensitiveData = React.useCallback( | ||||
async (triggeredBy: string) => { | async (triggeredBy: string) => { | ||||
if (staffCanSee || staffUserHasBeenLoggedIn) { | if (staffCanSee || staffUserHasBeenLoggedIn) { | ||||
Alert.alert('Starting SQLite database deletion process'); | Alert.alert('Starting SQLite database deletion process'); | ||||
} | } | ||||
await commCoreModule.clearSensitiveData(); | await commCoreModule.clearSensitiveData(); | ||||
await filesystemMediaCache.clearCache(); | |||||
if (staffCanSee || staffUserHasBeenLoggedIn) { | if (staffCanSee || staffUserHasBeenLoggedIn) { | ||||
Alert.alert( | Alert.alert( | ||||
'SQLite database successfully deleted', | 'SQLite database successfully deleted', | ||||
`SQLite database deletion was triggered by ${triggeredBy}`, | `SQLite database deletion was triggered by ${triggeredBy}`, | ||||
); | ); | ||||
} | } | ||||
}, | }, | ||||
[staffCanSee, staffUserHasBeenLoggedIn], | [staffCanSee, staffUserHasBeenLoggedIn], | ||||
▲ Show 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | React.useEffect(() => { | ||||
if (storeLoaded) { | if (storeLoaded) { | ||||
return; | return; | ||||
} | } | ||||
if (!loggedIn) { | if (!loggedIn) { | ||||
dispatch({ type: setStoreLoadedActionType }); | dispatch({ type: setStoreLoadedActionType }); | ||||
return; | return; | ||||
} | } | ||||
(async () => { | (async () => { | ||||
await sensitiveDataHandled; | await Promise.all([ | ||||
sensitiveDataHandled, | |||||
mediaCacheContext?.evictCache(), | |||||
]); | |||||
try { | try { | ||||
const { threads, messages, drafts } = | const { threads, messages, drafts } = | ||||
await commCoreModule.getClientDBStore(); | await commCoreModule.getClientDBStore(); | ||||
const threadInfosFromDB = | const threadInfosFromDB = | ||||
convertClientDBThreadInfosToRawThreadInfos(threads); | convertClientDBThreadInfosToRawThreadInfos(threads); | ||||
dispatch({ | dispatch({ | ||||
type: setClientDBStoreActionType, | type: setClientDBStoreActionType, | ||||
payload: { | payload: { | ||||
Show All 29 Lines | React.useEffect(() => { | ||||
dispatch, | dispatch, | ||||
rehydrateConcluded, | rehydrateConcluded, | ||||
staffCanSee, | staffCanSee, | ||||
storeLoaded, | storeLoaded, | ||||
urlPrefix, | urlPrefix, | ||||
staffUserHasBeenLoggedIn, | staffUserHasBeenLoggedIn, | ||||
callFetchNewCookieFromNativeCredentials, | callFetchNewCookieFromNativeCredentials, | ||||
callClearSensitiveData, | callClearSensitiveData, | ||||
mediaCacheContext, | |||||
]); | ]); | ||||
return null; | return null; | ||||
} | } | ||||
export { SQLiteDataHandler }; | export { SQLiteDataHandler }; |