diff --git a/native/data/sqlite-context-provider.js b/native/data/sqlite-context-provider.js index b331edbdd..80b613a63 100644 --- a/native/data/sqlite-context-provider.js +++ b/native/data/sqlite-context-provider.js @@ -1,76 +1,104 @@ // @flow import * as React from 'react'; +import { Alert } from 'react-native'; +import ExitApp from 'react-native-exit-app'; import { useDispatch } from 'react-redux'; import { setMessageStoreMessages } from 'lib/actions/message-actions.js'; import { setThreadStoreActionType } from 'lib/actions/thread-actions'; +import { isStaff } from 'lib/shared/user-utils'; import { loginActionSources } from 'lib/types/account-types'; import { fetchNewCookieFromNativeCredentials } from 'lib/utils/action-utils'; +import { getMessageForException } from 'lib/utils/errors'; import { convertClientDBThreadInfosToRawThreadInfos } from 'lib/utils/thread-ops-utils'; import { commCoreModule } from '../native-modules'; import { useSelector } from '../redux/redux-utils'; import { SQLiteContext } from './sqlite-context'; type Props = { +children: React.Node, }; function SQLiteContextProvider(props: Props): React.Node { const [storeLoaded, setStoreLoaded] = React.useState(false); const dispatch = useDispatch(); const rehydrateConcluded = useSelector( state => !!(state._persist && state._persist.rehydrated), ); const cookie = useSelector(state => state.cookie); const urlPrefix = useSelector(state => state.urlPrefix); + const viewerID = useSelector( + state => state.currentUserInfo && state.currentUserInfo.id, + ); React.useEffect(() => { if (storeLoaded || !rehydrateConcluded) { return; } (async () => { try { const threads = await commCoreModule.getAllThreads(); const threadInfosFromDB = convertClientDBThreadInfosToRawThreadInfos( threads, ); dispatch({ type: setThreadStoreActionType, payload: { threadInfos: threadInfosFromDB }, }); const messages = await commCoreModule.getAllMessages(); dispatch({ type: setMessageStoreMessages, payload: messages, }); - } catch { - await fetchNewCookieFromNativeCredentials( - dispatch, - cookie, - urlPrefix, - loginActionSources.sqliteLoadFailure, - ); - } finally { setStoreLoaded(true); + } catch (setStoreException) { + if (__DEV__ || (viewerID && isStaff(viewerID))) { + Alert.alert( + `Error setting threadStore or messageStore: ${ + getMessageForException(setStoreException) ?? + '{no exception message}' + }`, + ); + } + try { + await fetchNewCookieFromNativeCredentials( + dispatch, + cookie, + urlPrefix, + loginActionSources.sqliteLoadFailure, + ); + setStoreLoaded(true); + } catch (fetchCookieException) { + if (__DEV__ || (viewerID && isStaff(viewerID))) { + Alert.alert( + `Error fetching new cookie from native credentials: ${ + getMessageForException(fetchCookieException) ?? + '{no exception message}' + }. Please kill the app.`, + ); + } else { + ExitApp.exitApp(); + } + } } })(); - }, [storeLoaded, urlPrefix, rehydrateConcluded, cookie, dispatch]); + }, [storeLoaded, urlPrefix, rehydrateConcluded, cookie, dispatch, viewerID]); const contextValue = React.useMemo( () => ({ storeLoaded, }), [storeLoaded], ); return ( {props.children} ); } export { SQLiteContextProvider };