diff --git a/app/scenes/Document/components/DataLoader.tsx b/app/scenes/Document/components/DataLoader.tsx index 8d5c1bfecf..09e9af503e 100644 --- a/app/scenes/Document/components/DataLoader.tsx +++ b/app/scenes/Document/components/DataLoader.tsx @@ -1,7 +1,7 @@ import { observer } from "mobx-react"; import * as React from "react"; import type { RouteComponentProps, StaticContext } from "react-router"; -import { useLocation } from "react-router"; +import { Redirect, useLocation } from "react-router"; import { TeamPreference } from "@shared/types"; import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper"; import { RevisionHelper } from "@shared/utils/RevisionHelper"; @@ -27,7 +27,11 @@ import { PaymentRequiredError, } from "~/utils/errors"; import history from "~/utils/history"; -import { matchDocumentEdit, settingsPath } from "~/utils/routeHelpers"; +import { + matchDocumentEdit, + settingsPath, + updateDocumentPath, +} from "~/utils/routeHelpers"; import useDocumentSidebar from "../hooks/useDocumentSidebar"; import Loading from "./Loading"; import MarkAsViewed from "./MarkAsViewed"; @@ -240,6 +244,21 @@ function DataLoader({ match, children }: Props) { ); } + // Redirect to the canonical URL if the document slug has changed, e.g. + // after a rename, so the browser address bar stays in sync. + const canonicalUrl = updateDocumentPath(match.url, document); + if (location.pathname !== canonicalUrl) { + return ( + + ); + } + const canEdit = can.update && !document.isArchived && !revisionId; const readOnly = !isEditing || !canEdit; diff --git a/app/scenes/Document/components/Document.tsx b/app/scenes/Document/components/Document.tsx index 73295927ba..b7273777bc 100644 --- a/app/scenes/Document/components/Document.tsx +++ b/app/scenes/Document/components/Document.tsx @@ -10,7 +10,7 @@ import * as React from "react"; import type { WithTranslation } from "react-i18next"; import { withTranslation } from "react-i18next"; import type { RouteComponentProps, StaticContext } from "react-router"; -import { Prompt, withRouter, Redirect } from "react-router"; +import { Prompt, withRouter } from "react-router"; import { toast } from "sonner"; import styled from "styled-components"; import breakpoint from "styled-components-breakpoint"; @@ -40,11 +40,7 @@ import type { Editor as TEditor } from "~/editor"; import type { Properties } from "~/types"; import { client } from "~/utils/ApiClient"; import { emojiToUrl } from "~/utils/emoji"; -import { - documentHistoryPath, - documentEditPath, - updateDocumentPath, -} from "~/utils/routeHelpers"; +import { documentHistoryPath, documentEditPath } from "~/utils/routeHelpers"; import Container from "./Container"; import Contents from "./Contents"; import Editor from "./Editor"; @@ -476,10 +472,6 @@ class DocumentScene extends React.Component { const multiplayerEditor = !document.isArchived && !document.isDeleted && !revision && !isShare; - const canonicalUrl = shareId - ? this.props.match.url - : updateDocumentPath(this.props.match.url, document); - const hasEmojiInTitle = determineIconType(document.icon) === IconType.Emoji; const title = hasEmojiInTitle ? document.titleWithDefault.replace(document.icon!, "") @@ -492,15 +484,6 @@ class DocumentScene extends React.Component { return ( - {this.props.location.pathname !== canonicalUrl && ( - - )} @@ -670,10 +653,9 @@ const Main = styled.div` @media print { display: block; max-width: ${({ fullWidth }: MainProps) => - fullWidth - ? `100%` - : `calc(${EditorStyleHelper.documentWidth} + ${EditorStyleHelper.documentGutter})` - }; + fullWidth + ? `100%` + : `calc(${EditorStyleHelper.documentWidth} + ${EditorStyleHelper.documentGutter})`}; } `; @@ -722,10 +704,10 @@ const EditorContainer = styled.div` // Decides the editor column position & span grid-column: ${({ - docFullWidth, - showContents, - tocPosition, -}: EditorContainerProps) => + docFullWidth, + showContents, + tocPosition, + }: EditorContainerProps) => docFullWidth ? showContents ? tocPosition === TOCPosition.Left