Skip to content

Commit

Permalink
Wave8/Last visited path (#21)
Browse files Browse the repository at this point in the history
* Add last path logic

* Fix problem with home root screen by removing it

* Add default value for lastVisitedPath

---------

Co-authored-by: Wojciech Boman <wojciechboman@gmail.com>
  • Loading branch information
mateuuszzzzz and WojtekBoman authored Jan 17, 2024
1 parent 66402e4 commit 1f2226e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/Expensify.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ const propTypes = {
/** Whether we should display the notification alerting the user that focus mode has been auto-enabled */
focusModeNotification: PropTypes.bool,

/** Last visited path in the app */
lastVisitedPath: PropTypes.string,

...withLocalizePropTypes,
};

Expand All @@ -92,6 +95,7 @@ const defaultProps = {
screenShareRequest: null,
isCheckingPublicRoom: true,
focusModeNotification: false,
lastVisitedPath: undefined,
};

const SplashScreenHiddenContext = React.createContext({});
Expand All @@ -102,6 +106,7 @@ function Expensify(props) {
const [isOnyxMigrated, setIsOnyxMigrated] = useState(false);
const [isSplashHidden, setIsSplashHidden] = useState(false);
const [hasAttemptedToOpenPublicRoom, setAttemptedToOpenPublicRoom] = useState(false);
const [initialUrl, setInitialUrl] = useState(null);

useEffect(() => {
if (props.isCheckingPublicRoom) {
Expand Down Expand Up @@ -182,6 +187,7 @@ function Expensify(props) {

// If the app is opened from a deep link, get the reportID (if exists) from the deep link and navigate to the chat report
Linking.getInitialURL().then((url) => {
setInitialUrl(url);
Report.openReportFromDeepLink(url, isAuthenticated);
});

Expand Down Expand Up @@ -237,6 +243,8 @@ function Expensify(props) {
<NavigationRoot
onReady={setNavigationReady}
authenticated={isAuthenticated}
lastVisitedPath={props.lastVisitedPath}
initialUrl={initialUrl}
/>
</SplashScreenHiddenContext.Provider>
)}
Expand Down Expand Up @@ -272,6 +280,9 @@ export default compose(
key: ONYXKEYS.FOCUS_MODE_NOTIFICATION,
initWithStoredValues: false,
},
lastVisitedPath: {
key: ONYXKEYS.LAST_VISITED_PATH,
},
}),
)(Expensify);

Expand Down
4 changes: 4 additions & 0 deletions src/ONYXKEYS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ const ONYXKEYS = {
// Max width supported for HTML <canvas> element
MAX_CANVAS_WIDTH: 'maxCanvasWidth',

// Stores last visited path
LAST_VISITED_PATH: 'lastVisitedPath',

/** Collection Keys */
COLLECTION: {
DOWNLOAD: 'download_',
Expand Down Expand Up @@ -432,6 +435,7 @@ type OnyxValues = {
[ONYXKEYS.MAX_CANVAS_AREA]: number;
[ONYXKEYS.MAX_CANVAS_HEIGHT]: number;
[ONYXKEYS.MAX_CANVAS_WIDTH]: number;
[ONYXKEYS.LAST_VISITED_PATH]: string | undefined;

// Collections
[ONYXKEYS.COLLECTION.DOWNLOAD]: OnyxTypes.Download;
Expand Down
2 changes: 1 addition & 1 deletion src/ROUTES.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function getUrlWithBackToParam<TUrl extends string>(url: TUrl, backTo?: string):
}

const ROUTES = {
HOME: '',
HOME: 'home',

ALL_SETTINGS: 'all-settings',

Expand Down
33 changes: 32 additions & 1 deletion src/libs/Navigation/NavigationRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@ import useFlipper from '@hooks/useFlipper';
import useTheme from '@hooks/useTheme';
import useWindowDimensions from '@hooks/useWindowDimensions';
import Log from '@libs/Log';
import {getPathFromURL} from '@libs/Url';
import {updateLastVisitedPath} from '@userActions/App';
import type {Route} from '@src/ROUTES';
import AppNavigator from './AppNavigator';
import getStateFromPath from './getStateFromPath';
import linkingConfig from './linkingConfig';
import Navigation, {navigationRef} from './Navigation';

type NavigationRootProps = {
/** Whether the current user is logged in with an authToken */
authenticated: boolean;

/** Stores path of last visited page */
lastVisitedPath: Route;

/** Initial url */
initialUrl: string | null;

/** Fired when react-navigation is ready */
onReady: () => void;
};
Expand All @@ -27,6 +37,7 @@ function parseAndLogRoute(state: NavigationState) {
}

const currentPath = getPathFromState(state, linkingConfig.config);
updateLastVisitedPath(currentPath);

// Don't log the route transitions from OldDot because they contain authTokens
if (currentPath.includes('/transition')) {
Expand All @@ -38,14 +49,33 @@ function parseAndLogRoute(state: NavigationState) {
Navigation.setIsNavigationReady();
}

function NavigationRoot({authenticated, onReady}: NavigationRootProps) {
function NavigationRoot({authenticated, lastVisitedPath, initialUrl, onReady}: NavigationRootProps) {
useFlipper(navigationRef);
const firstRenderRef = useRef(true);
const theme = useTheme();

const currentReportIDValue = useCurrentReportID();
const {isSmallScreenWidth} = useWindowDimensions();

const initialState = useMemo(
() => {
if (!lastVisitedPath) {
return undefined;
}

const path = initialUrl ? getPathFromURL(initialUrl) : null;

// For non-nullable paths we don't want to set initial state
if (path) {
return;
}

return getStateFromPath(lastVisitedPath);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[],
);

// https://reactnavigation.org/docs/themes
const navigationTheme = useMemo(
() => ({
Expand Down Expand Up @@ -86,6 +116,7 @@ function NavigationRoot({authenticated, onReady}: NavigationRootProps) {

return (
<NavigationContainer
initialState={initialState}
onStateChange={handleStateChange}
onReady={onReady}
theme={navigationTheme}
Expand Down
5 changes: 5 additions & 0 deletions src/libs/actions/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,10 @@ function handleRestrictedEvent(eventName: string) {
API.write('HandleRestrictedEvent', parameters);
}

function updateLastVisitedPath(path: string) {
Onyx.set(ONYXKEYS.LAST_VISITED_PATH, path);
}

export {
setLocale,
setLocaleAndNavigate,
Expand All @@ -544,4 +548,5 @@ export {
finalReconnectAppAfterActivatingReliableUpdates,
savePolicyDraftByNewWorkspace,
createWorkspaceWithPolicyDraftAndNavigateToIt,
updateLastVisitedPath,
};

0 comments on commit 1f2226e

Please sign in to comment.