Skip to content

Commit 5d52131

Browse files
Move document redirect logic one component up (outline#11917)
* fix: move redirect logic one component up * fix: use <Redirect>
1 parent 025f422 commit 5d52131

File tree

2 files changed

+30
-29
lines changed

2 files changed

+30
-29
lines changed

app/scenes/Document/components/DataLoader.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { observer } from "mobx-react";
22
import * as React from "react";
33
import type { RouteComponentProps, StaticContext } from "react-router";
4-
import { useLocation } from "react-router";
4+
import { Redirect, useLocation } from "react-router";
55
import { TeamPreference } from "@shared/types";
66
import { ProsemirrorHelper } from "@shared/utils/ProsemirrorHelper";
77
import { RevisionHelper } from "@shared/utils/RevisionHelper";
@@ -27,7 +27,11 @@ import {
2727
PaymentRequiredError,
2828
} from "~/utils/errors";
2929
import history from "~/utils/history";
30-
import { matchDocumentEdit, settingsPath } from "~/utils/routeHelpers";
30+
import {
31+
matchDocumentEdit,
32+
settingsPath,
33+
updateDocumentPath,
34+
} from "~/utils/routeHelpers";
3135
import useDocumentSidebar from "../hooks/useDocumentSidebar";
3236
import Loading from "./Loading";
3337
import MarkAsViewed from "./MarkAsViewed";
@@ -240,6 +244,21 @@ function DataLoader({ match, children }: Props) {
240244
);
241245
}
242246

247+
// Redirect to the canonical URL if the document slug has changed, e.g.
248+
// after a rename, so the browser address bar stays in sync.
249+
const canonicalUrl = updateDocumentPath(match.url, document);
250+
if (location.pathname !== canonicalUrl) {
251+
return (
252+
<Redirect
253+
to={{
254+
pathname: canonicalUrl,
255+
state: location.state,
256+
hash: location.hash,
257+
}}
258+
/>
259+
);
260+
}
261+
243262
const canEdit = can.update && !document.isArchived && !revisionId;
244263
const readOnly = !isEditing || !canEdit;
245264

app/scenes/Document/components/Document.tsx

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as React from "react";
1010
import type { WithTranslation } from "react-i18next";
1111
import { withTranslation } from "react-i18next";
1212
import type { RouteComponentProps, StaticContext } from "react-router";
13-
import { Prompt, withRouter, Redirect } from "react-router";
13+
import { Prompt, withRouter } from "react-router";
1414
import { toast } from "sonner";
1515
import styled from "styled-components";
1616
import breakpoint from "styled-components-breakpoint";
@@ -40,11 +40,7 @@ import type { Editor as TEditor } from "~/editor";
4040
import type { Properties } from "~/types";
4141
import { client } from "~/utils/ApiClient";
4242
import { emojiToUrl } from "~/utils/emoji";
43-
import {
44-
documentHistoryPath,
45-
documentEditPath,
46-
updateDocumentPath,
47-
} from "~/utils/routeHelpers";
43+
import { documentHistoryPath, documentEditPath } from "~/utils/routeHelpers";
4844
import Container from "./Container";
4945
import Contents from "./Contents";
5046
import Editor from "./Editor";
@@ -476,10 +472,6 @@ class DocumentScene extends React.Component<Props> {
476472
const multiplayerEditor =
477473
!document.isArchived && !document.isDeleted && !revision && !isShare;
478474

479-
const canonicalUrl = shareId
480-
? this.props.match.url
481-
: updateDocumentPath(this.props.match.url, document);
482-
483475
const hasEmojiInTitle = determineIconType(document.icon) === IconType.Emoji;
484476
const title = hasEmojiInTitle
485477
? document.titleWithDefault.replace(document.icon!, "")
@@ -492,15 +484,6 @@ class DocumentScene extends React.Component<Props> {
492484

493485
return (
494486
<ErrorBoundary showTitle>
495-
{this.props.location.pathname !== canonicalUrl && (
496-
<Redirect
497-
to={{
498-
pathname: canonicalUrl,
499-
state: this.props.location.state,
500-
hash: this.props.location.hash,
501-
}}
502-
/>
503-
)}
504487
<RegisterKeyDown trigger="m" handler={this.onMove} />
505488
<RegisterKeyDown trigger="z" handler={this.onUndoRedo} />
506489
<RegisterKeyDown trigger="e" handler={this.goToEdit} />
@@ -670,10 +653,9 @@ const Main = styled.div<MainProps>`
670653
@media print {
671654
display: block;
672655
max-width: ${({ fullWidth }: MainProps) =>
673-
fullWidth
674-
? `100%`
675-
: `calc(${EditorStyleHelper.documentWidth} + ${EditorStyleHelper.documentGutter})`
676-
};
656+
fullWidth
657+
? `100%`
658+
: `calc(${EditorStyleHelper.documentWidth} + ${EditorStyleHelper.documentGutter})`};
677659
}
678660
`;
679661

@@ -722,10 +704,10 @@ const EditorContainer = styled.div<EditorContainerProps>`
722704
723705
// Decides the editor column position & span
724706
grid-column: ${({
725-
docFullWidth,
726-
showContents,
727-
tocPosition,
728-
}: EditorContainerProps) =>
707+
docFullWidth,
708+
showContents,
709+
tocPosition,
710+
}: EditorContainerProps) =>
729711
docFullWidth
730712
? showContents
731713
? tocPosition === TOCPosition.Left

0 commit comments

Comments
 (0)