Changeset View
Changeset View
Standalone View
Standalone View
native/data/sqlite-data-handler.js
- This file was moved from native/data/sqlite-context-provider.js.
// @flow | // @flow | ||||
import * as React from 'react'; | import * as React from 'react'; | ||||
import { Alert } from 'react-native'; | import { Alert } from 'react-native'; | ||||
import ExitApp from 'react-native-exit-app'; | import ExitApp from 'react-native-exit-app'; | ||||
import { useDispatch } from 'react-redux'; | import { useDispatch } from 'react-redux'; | ||||
import { setDraftStoreDrafts } from 'lib/actions/draft-actions'; | import { setClientDBStoreActionType } from 'lib/actions/client-db-store-actions'; | ||||
import { setMessageStoreMessages } from 'lib/actions/message-actions.js'; | |||||
import { setThreadStoreActionType } from 'lib/actions/thread-actions'; | |||||
import { isLoggedIn } from 'lib/selectors/user-selectors'; | import { isLoggedIn } from 'lib/selectors/user-selectors'; | ||||
import { logInActionSources } from 'lib/types/account-types'; | import { logInActionSources } from 'lib/types/account-types'; | ||||
import { fetchNewCookieFromNativeCredentials } from 'lib/utils/action-utils'; | import { fetchNewCookieFromNativeCredentials } from 'lib/utils/action-utils'; | ||||
import { getMessageForException } from 'lib/utils/errors'; | import { getMessageForException } from 'lib/utils/errors'; | ||||
import { convertClientDBThreadInfosToRawThreadInfos } from 'lib/utils/thread-ops-utils'; | import { convertClientDBThreadInfosToRawThreadInfos } from 'lib/utils/thread-ops-utils'; | ||||
import { commCoreModule } from '../native-modules'; | import { commCoreModule } from '../native-modules'; | ||||
import { setStoreLoadedActionType } from '../redux/action-types'; | |||||
import { useSelector } from '../redux/redux-utils'; | import { useSelector } from '../redux/redux-utils'; | ||||
import { StaffContext } from '../staff/staff-context'; | import { StaffContext } from '../staff/staff-context'; | ||||
import { isTaskCancelledError } from '../utils/error-handling'; | import { isTaskCancelledError } from '../utils/error-handling'; | ||||
import { useStaffCanSee } from '../utils/staff-utils'; | import { useStaffCanSee } from '../utils/staff-utils'; | ||||
import { SQLiteContext } from './sqlite-context'; | |||||
type Props = { | function SQLiteDataHandler(): React.Node { | ||||
+children: React.Node, | const storeLoaded = useSelector(state => state.storeLoaded); | ||||
}; | |||||
function SQLiteContextProvider(props: Props): React.Node { | |||||
const [storeLoaded, setStoreLoaded] = React.useState<boolean>(false); | |||||
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(); | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | React.useEffect(() => { | ||||
if (!rehydrateConcluded) { | if (!rehydrateConcluded) { | ||||
return; | return; | ||||
} | } | ||||
const sensitiveDataHandled = handleSensitiveData(); | const sensitiveDataHandled = handleSensitiveData(); | ||||
if (storeLoaded) { | if (storeLoaded) { | ||||
return; | return; | ||||
} | } | ||||
if (!loggedIn) { | if (!loggedIn) { | ||||
setStoreLoaded(true); | dispatch({ type: setStoreLoadedActionType }); | ||||
return; | return; | ||||
} | } | ||||
(async () => { | (async () => { | ||||
await sensitiveDataHandled; | await sensitiveDataHandled; | ||||
try { | try { | ||||
const [threads, messages, drafts] = await Promise.all([ | const [threads, messages, drafts] = await Promise.all([ | ||||
commCoreModule.getAllThreads(), | commCoreModule.getAllThreads(), | ||||
commCoreModule.getAllMessages(), | commCoreModule.getAllMessages(), | ||||
commCoreModule.getAllDrafts(), | commCoreModule.getAllDrafts(), | ||||
]); | ]); | ||||
const threadInfosFromDB = convertClientDBThreadInfosToRawThreadInfos( | const threadInfosFromDB = convertClientDBThreadInfosToRawThreadInfos( | ||||
threads, | threads, | ||||
); | ); | ||||
dispatch({ | dispatch({ | ||||
type: setThreadStoreActionType, | type: setClientDBStoreActionType, | ||||
payload: { threadInfos: threadInfosFromDB }, | payload: { | ||||
}); | drafts, | ||||
dispatch({ | messages, | ||||
type: setMessageStoreMessages, | threadStore: { threadInfos: threadInfosFromDB }, | ||||
payload: messages, | currentUserID: currentLoggedInUserID, | ||||
}); | }, | ||||
dispatch({ | |||||
type: setDraftStoreDrafts, | |||||
payload: drafts, | |||||
}); | }); | ||||
setStoreLoaded(true); | |||||
} catch (setStoreException) { | } catch (setStoreException) { | ||||
if (isTaskCancelledError(setStoreException)) { | if (isTaskCancelledError(setStoreException)) { | ||||
setStoreLoaded(true); | dispatch({ type: setStoreLoadedActionType }); | ||||
return; | return; | ||||
} | } | ||||
if (staffCanSee) { | if (staffCanSee) { | ||||
Alert.alert( | Alert.alert( | ||||
`Error setting threadStore or messageStore: ${ | `Error setting threadStore or messageStore: ${ | ||||
getMessageForException(setStoreException) ?? | getMessageForException(setStoreException) ?? | ||||
'{no exception message}' | '{no exception message}' | ||||
}`, | }`, | ||||
); | ); | ||||
} | } | ||||
try { | try { | ||||
await fetchNewCookieFromNativeCredentials( | await fetchNewCookieFromNativeCredentials( | ||||
dispatch, | dispatch, | ||||
cookie, | cookie, | ||||
urlPrefix, | urlPrefix, | ||||
logInActionSources.sqliteLoadFailure, | logInActionSources.sqliteLoadFailure, | ||||
); | ); | ||||
setStoreLoaded(true); | dispatch({ type: setStoreLoadedActionType }); | ||||
} catch (fetchCookieException) { | } catch (fetchCookieException) { | ||||
if (staffCanSee) { | if (staffCanSee) { | ||||
Alert.alert( | Alert.alert( | ||||
`Error fetching new cookie from native credentials: ${ | `Error fetching new cookie from native credentials: ${ | ||||
getMessageForException(fetchCookieException) ?? | getMessageForException(fetchCookieException) ?? | ||||
'{no exception message}' | '{no exception message}' | ||||
}. Please kill the app.`, | }. Please kill the app.`, | ||||
); | ); | ||||
} else { | } else { | ||||
ExitApp.exitApp(); | ExitApp.exitApp(); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
})(); | })(); | ||||
}, [ | }, [ | ||||
currentLoggedInUserID, | |||||
handleSensitiveData, | handleSensitiveData, | ||||
loggedIn, | loggedIn, | ||||
cookie, | cookie, | ||||
dispatch, | dispatch, | ||||
rehydrateConcluded, | rehydrateConcluded, | ||||
staffCanSee, | staffCanSee, | ||||
storeLoaded, | storeLoaded, | ||||
urlPrefix, | urlPrefix, | ||||
]); | ]); | ||||
const contextValue = React.useMemo( | return null; | ||||
() => ({ | |||||
storeLoaded, | |||||
}), | |||||
[storeLoaded], | |||||
); | |||||
return ( | |||||
<SQLiteContext.Provider value={contextValue}> | |||||
{props.children} | |||||
</SQLiteContext.Provider> | |||||
); | |||||
} | } | ||||
export { SQLiteContextProvider }; | export { SQLiteDataHandler }; |