handle app load

This commit is contained in:
hmacr committed 2024-11-29 15:13:26 +05:30
1 parent a334224675
commit 36db19b87d
1 file changed
+14 -1
+14 -1
View File
@@ -1,6 +1,7 @@
import * as React from "react";
import { StaticContext } from "react-router";
import { StaticContext, useHistory } from "react-router";
import { RouteComponentProps } from "react-router-dom";
import { SidebarContextType } from "~/components/Sidebar/components/SidebarContext";
import { useLastVisitedPath } from "~/hooks/useLastVisitedPath";
import useStores from "~/hooks/useStores";
import DataLoader from "./components/DataLoader";
@@ -16,12 +17,14 @@ type LocationState = {
title?: string;
restore?: boolean;
revisionId?: string;
sidebarContext?: SidebarContextType;
};
type Props = RouteComponentProps<Params, StaticContext, LocationState>;
export default function DocumentScene(props: Props) {
const { ui } = useStores();
const history = useHistory();
const { documentSlug, revisionId } = props.match.params;
const currentPath = props.location.pathname;
const [, setLastVisitedPath] = useLastVisitedPath();
@@ -32,6 +35,16 @@ export default function DocumentScene(props: Props) {
React.useEffect(() => () => ui.clearActiveDocument(), [ui]);
React.useEffect(() => {
// When opening a document directly on app load, sidebarContext will not be set.
if (!props.location.state?.sidebarContext) {
history.replace({
...props.location,
state: { ...props.location.state, sidebarContext: "collections" }, // optimistic preference of "collections"
});
}
}, [props.location, history]);
// the urlId portion of the url does not include the slugified title
// we only want to force a re-mount of the document component when the
// document changes, not when the title does so only this portion is used