diff --git a/native/data/sqlite-context-provider.js b/native/data/sqlite-context-provider.js --- a/native/data/sqlite-context-provider.js +++ b/native/data/sqlite-context-provider.js @@ -5,9 +5,7 @@ import ExitApp from 'react-native-exit-app'; import { useDispatch } from 'react-redux'; -import { setDraftStoreDrafts } from 'lib/actions/draft-actions'; -import { setMessageStoreMessages } from 'lib/actions/message-actions.js'; -import { setThreadStoreActionType } from 'lib/actions/thread-actions'; +import { setClientDBStoreActionType } from 'lib/actions/client-db-store-actions'; import { isLoggedIn } from 'lib/selectors/user-selectors'; import { logInActionSources } from 'lib/types/account-types'; import { fetchNewCookieFromNativeCredentials } from 'lib/utils/action-utils'; @@ -19,14 +17,13 @@ import { StaffContext } from '../staff/staff-context'; import { isTaskCancelledError } from '../utils/error-handling'; import { useStaffCanSee } from '../utils/staff-utils'; -import { SQLiteContext } from './sqlite-context'; type Props = { +children: React.Node, }; function SQLiteContextProvider(props: Props): React.Node { - const [storeLoaded, setStoreLoaded] = React.useState(false); + const storeLoaded = useSelector(state => state.storeLoaded); const dispatch = useDispatch(); const rehydrateConcluded = useSelector( @@ -88,7 +85,15 @@ return; } if (!loggedIn) { - setStoreLoaded(true); + dispatch({ + type: setClientDBStoreActionType, + payload: { + drafts: [], + messages: [], + threadStore: {}, + currentUserID: currentLoggedInUserID, + }, + }); return; } (async () => { @@ -102,22 +107,27 @@ const threadInfosFromDB = convertClientDBThreadInfosToRawThreadInfos( threads, ); + console.log(drafts); dispatch({ - type: setThreadStoreActionType, - payload: { threadInfos: threadInfosFromDB }, + type: setClientDBStoreActionType, + payload: { + drafts: drafts, + messages: messages, + threadStore: { threadInfos: threadInfosFromDB }, + currentUserID: currentLoggedInUserID, + }, }); - dispatch({ - type: setMessageStoreMessages, - payload: messages, - }); - dispatch({ - type: setDraftStoreDrafts, - payload: drafts, - }); - setStoreLoaded(true); } catch (setStoreException) { if (isTaskCancelledError(setStoreException)) { - setStoreLoaded(true); + dispatch({ + type: setClientDBStoreActionType, + payload: { + drafts: {}, + messages: {}, + threadStore: {}, + currentUserID: currentLoggedInUserID, + }, + }); return; } if (staffCanSee) { @@ -135,7 +145,15 @@ urlPrefix, logInActionSources.sqliteLoadFailure, ); - setStoreLoaded(true); + dispatch({ + type: setClientDBStoreActionType, + payload: { + drafts: {}, + messages: {}, + threadStore: {}, + currentUserID: currentLoggedInUserID, + }, + }); } catch (fetchCookieException) { if (staffCanSee) { Alert.alert( @@ -151,6 +169,7 @@ } })(); }, [ + currentLoggedInUserID, handleSensitiveData, loggedIn, cookie, @@ -161,18 +180,7 @@ urlPrefix, ]); - const contextValue = React.useMemo( - () => ({ - storeLoaded, - }), - [storeLoaded], - ); - - return ( - - {props.children} - - ); + return props.children; } export { SQLiteContextProvider }; diff --git a/native/data/sqlite-context.js b/native/data/sqlite-context.js deleted file mode 100644 --- a/native/data/sqlite-context.js +++ /dev/null @@ -1,13 +0,0 @@ -// @flow - -import * as React from 'react'; - -export type SQLiteContextType = { - +storeLoaded: boolean, -}; - -const SQLiteContext: React.Context = React.createContext( - null, -); - -export { SQLiteContext }; diff --git a/native/navigation/app-navigator.react.js b/native/navigation/app-navigator.react.js --- a/native/navigation/app-navigator.react.js +++ b/native/navigation/app-navigator.react.js @@ -2,13 +2,13 @@ import * as SplashScreen from 'expo-splash-screen'; import * as React from 'react'; +import { useSelector } from 'react-redux'; import { PersistGate } from 'redux-persist/integration/react'; import MultimediaMessageTooltipModal from '../chat/multimedia-message-tooltip-modal.react'; import RobotextMessageTooltipModal from '../chat/robotext-message-tooltip-modal.react'; import ThreadSettingsMemberTooltipModal from '../chat/settings/thread-settings-member-tooltip-modal.react'; import TextMessageTooltipModal from '../chat/text-message-tooltip-modal.react'; -import { type SQLiteContextType, SQLiteContext } from '../data/sqlite-context'; import KeyboardStateContainer from '../keyboard/keyboard-state-container.react'; import CameraModal from '../media/camera-modal.react'; import ImageModal from '../media/image-modal.react'; @@ -60,10 +60,7 @@ const { navigation } = props; const rootContext = React.useContext(RootContext); - const localDatabaseContext: ?SQLiteContextType = React.useContext( - SQLiteContext, - ); - const storeLoadedFromLocalDatabase = localDatabaseContext?.storeLoaded; + const storeLoadedFromLocalDatabase = useSelector(state => state.storeLoaded); const setNavStateInitialized = rootContext && rootContext.setNavStateInitialized; React.useEffect(() => { diff --git a/native/selectors/app-state-selectors.js b/native/selectors/app-state-selectors.js --- a/native/selectors/app-state-selectors.js +++ b/native/selectors/app-state-selectors.js @@ -1,15 +1,12 @@ // @flow -import * as React from 'react'; import { useSelector } from 'react-redux'; -import { SQLiteContext } from '../data/sqlite-context'; - function usePersistedStateLoaded(): boolean { const rehydrateConcluded = useSelector(state => !!state._persist?.rehydrated); - const localDatabaseContext = React.useContext(SQLiteContext); + const storeLoaded = useSelector(state => state.storeLoaded); - return rehydrateConcluded && !!localDatabaseContext?.storeLoaded; + return rehydrateConcluded && storeLoaded; } export { usePersistedStateLoaded };