From 32ff3e1e80018b06fdb9aeaf0f87c35e732145f4 Mon Sep 17 00:00:00 2001 From: hmacr Date: Thu, 5 Dec 2024 19:20:08 +0530 Subject: [PATCH] set sidebarContext only in state --- app/actions/definitions/comments.tsx | 6 ++-- app/components/EventListItem.tsx | 4 ++- .../Document/components/CommentSortMenu.tsx | 6 ++-- .../Document/components/CommentThread.tsx | 6 ++-- app/scenes/Document/components/Document.tsx | 29 +++++++++++++++---- .../Document/components/DocumentMeta.tsx | 9 +++--- app/scenes/Document/components/Editor.tsx | 19 ++++++------ app/scenes/Document/components/Header.tsx | 7 ++++- app/scenes/Document/components/History.tsx | 4 ++- app/scenes/Document/components/Insights.tsx | 4 ++- 10 files changed, 65 insertions(+), 29 deletions(-) diff --git a/app/actions/definitions/comments.tsx b/app/actions/definitions/comments.tsx index 7cd7b204eb..f377374517 100644 --- a/app/actions/definitions/comments.tsx +++ b/app/actions/definitions/comments.tsx @@ -53,10 +53,11 @@ export const resolveCommentFactory = ({ perform: async ({ t }) => { await comment.resolve(); + const locationState = history.location.state as Record; history.replace({ ...history.location, state: { - ...(history.location.state as Record), + sidebarContext: locationState["sidebarContext"], commentId: undefined, }, }); @@ -84,10 +85,11 @@ export const unresolveCommentFactory = ({ perform: async () => { await comment.unresolve(); + const locationState = history.location.state as Record; history.replace({ ...history.location, state: { - ...(history.location.state as Record), + sidebarContext: locationState["sidebarContext"], commentId: undefined, }, }); diff --git a/app/components/EventListItem.tsx b/app/components/EventListItem.tsx index ce4f73f634..2f6c8b405e 100644 --- a/app/components/EventListItem.tsx +++ b/app/components/EventListItem.tsx @@ -24,6 +24,7 @@ import RevisionMenu from "~/menus/RevisionMenu"; import { hover } from "~/styles"; import Logger from "~/utils/Logger"; import { documentHistoryPath } from "~/utils/routeHelpers"; +import { useLocationState } from "./Sidebar/hooks/useLocationState"; type Props = { document: Document; @@ -35,6 +36,7 @@ const EventListItem = ({ event, latest, document, ...rest }: Props) => { const { t } = useTranslation(); const { revisions } = useStores(); const location = useLocation(); + const sidebarContext = useLocationState(); const opts = { userName: event.actor.name, }; @@ -67,7 +69,7 @@ const EventListItem = ({ event, latest, document, ...rest }: Props) => { to = { pathname: documentHistoryPath(document, event.modelId || "latest"), state: { - ...(location.state as Record), + sidebarContext, retainScrollPosition: true, }, }; diff --git a/app/scenes/Document/components/CommentSortMenu.tsx b/app/scenes/Document/components/CommentSortMenu.tsx index 56e0f38848..bf88eeef57 100644 --- a/app/scenes/Document/components/CommentSortMenu.tsx +++ b/app/scenes/Document/components/CommentSortMenu.tsx @@ -6,6 +6,7 @@ import styled from "styled-components"; import { s } from "@shared/styles"; import { UserPreference } from "@shared/types"; import InputSelect from "~/components/InputSelect"; +import { useLocationState } from "~/components/Sidebar/hooks/useLocationState"; import useCurrentUser from "~/hooks/useCurrentUser"; import useQuery from "~/hooks/useQuery"; import { CommentSortType } from "~/types"; @@ -13,6 +14,7 @@ import { CommentSortType } from "~/types"; const CommentSortMenu = () => { const { t } = useTranslation(); const location = useLocation(); + const sidebarContext = useLocationState(); const history = useHistory(); const user = useCurrentUser(); const params = useQuery(); @@ -39,7 +41,7 @@ const CommentSortMenu = () => { resolved: "", }), pathname: location.pathname, - state: location.state, + state: { sidebarContext }, }); }; @@ -50,7 +52,7 @@ const CommentSortMenu = () => { resolved: undefined, }), pathname: location.pathname, - state: location.state, + state: { sidebarContext }, }); }; diff --git a/app/scenes/Document/components/CommentThread.tsx b/app/scenes/Document/components/CommentThread.tsx index 55d31e6ce0..33697448ff 100644 --- a/app/scenes/Document/components/CommentThread.tsx +++ b/app/scenes/Document/components/CommentThread.tsx @@ -15,6 +15,7 @@ import { useDocumentContext } from "~/components/DocumentContext"; import Facepile from "~/components/Facepile"; import Fade from "~/components/Fade"; import { ResizingHeightContainer } from "~/components/ResizingHeightContainer"; +import { useLocationState } from "~/components/Sidebar/hooks/useLocationState"; import useOnClickOutside from "~/hooks/useOnClickOutside"; import usePersistedState from "~/hooks/usePersistedState"; import usePolicy from "~/hooks/usePolicy"; @@ -61,6 +62,7 @@ function CommentThread({ const { t } = useTranslation(); const history = useHistory(); const location = useLocation(); + const sidebarContext = useLocationState(); const [autoFocus, setAutoFocus] = React.useState(thread.isNew); const can = usePolicy(document); @@ -102,8 +104,8 @@ function CommentThread({ search: location.search, pathname: location.pathname, state: { - ...(location.state as Record), commentId: undefined, + sidebarContext, }, }); } @@ -119,8 +121,8 @@ function CommentThread({ search: thread.isResolved ? "resolved=" : "", pathname: location.pathname.replace(/\/history$/, ""), state: { - ...(location.state as Record), commentId: thread.id, + sidebarContext, }, }); }; diff --git a/app/scenes/Document/components/Document.tsx b/app/scenes/Document/components/Document.tsx index 3cc1838aa0..81c8bac958 100644 --- a/app/scenes/Document/components/Document.tsx +++ b/app/scenes/Document/components/Document.tsx @@ -40,6 +40,7 @@ import LoadingIndicator from "~/components/LoadingIndicator"; import PageTitle from "~/components/PageTitle"; import PlaceholderDocument from "~/components/PlaceholderDocument"; import RegisterKeyDown from "~/components/RegisterKeyDown"; +import { SidebarContextType } from "~/components/Sidebar/components/SidebarContext"; import withStores from "~/components/withStores"; import type { Editor as TEditor } from "~/editor"; import { SearchResult } from "~/editor/components/LinkEditor"; @@ -77,6 +78,7 @@ type LocationState = { title?: string; restore?: boolean; revisionId?: string; + sidebarContext?: SidebarContextType; }; type Props = WithTranslation & @@ -246,7 +248,10 @@ class DocumentScene extends React.Component { const { document, abilities } = this.props; if (abilities.update) { - this.props.history.push(documentEditPath(document)); + this.props.history.push({ + pathname: documentEditPath(document), + state: { sidebarContext: this.props.location.state?.sidebarContext }, + }); } } else if (this.editor.current?.isBlurred) { ev.preventDefault(); @@ -265,9 +270,15 @@ class DocumentScene extends React.Component { const { document, location } = this.props; if (location.pathname.endsWith("history")) { - this.props.history.push(document.url); + this.props.history.push({ + pathname: document.url, + state: { sidebarContext: this.props.location.state?.sidebarContext }, + }); } else { - this.props.history.push(documentHistoryPath(document)); + this.props.history.push({ + pathname: documentHistoryPath(document), + state: { sidebarContext: this.props.location.state?.sidebarContext }, + }); } }; @@ -333,10 +344,16 @@ class DocumentScene extends React.Component { this.isEditorDirty = false; if (options.done) { - this.props.history.push(savedDocument.url); + this.props.history.push({ + pathname: savedDocument.url, + state: { sidebarContext: this.props.location.state?.sidebarContext }, + }); this.props.ui.setActiveDocument(savedDocument); } else if (document.isNew) { - this.props.history.push(documentEditPath(savedDocument)); + this.props.history.push({ + pathname: documentEditPath(savedDocument), + state: { sidebarContext: this.props.location.state?.sidebarContext }, + }); this.props.ui.setActiveDocument(savedDocument); } } catch (err) { @@ -392,7 +409,7 @@ class DocumentScene extends React.Component { if (!this.props.readOnly) { this.props.history.push({ pathname: this.props.document.url, - state: this.props.location.state, + state: { sidebarContext: this.props.location.state?.sidebarContext }, }); } }; diff --git a/app/scenes/Document/components/DocumentMeta.tsx b/app/scenes/Document/components/DocumentMeta.tsx index 98c7f8013a..851394ce8b 100644 --- a/app/scenes/Document/components/DocumentMeta.tsx +++ b/app/scenes/Document/components/DocumentMeta.tsx @@ -3,13 +3,14 @@ import { observer, useObserver } from "mobx-react"; import { CommentIcon } from "outline-icons"; import * as React from "react"; import { useTranslation } from "react-i18next"; -import { Link, useLocation, useRouteMatch } from "react-router-dom"; +import { Link, useRouteMatch } from "react-router-dom"; import styled from "styled-components"; import { TeamPreference } from "@shared/types"; import Document from "~/models/Document"; import Revision from "~/models/Revision"; import DocumentMeta from "~/components/DocumentMeta"; import Fade from "~/components/Fade"; +import { useLocationState } from "~/components/Sidebar/hooks/useLocationState"; import useCurrentTeam from "~/hooks/useCurrentTeam"; import usePolicy from "~/hooks/usePolicy"; import useStores from "~/hooks/useStores"; @@ -27,7 +28,7 @@ function TitleDocumentMeta({ to, document, revision, ...rest }: Props) { const { views, comments, ui } = useStores(); const { t } = useTranslation(); const match = useRouteMatch(); - const location = useLocation(); + const sidebarContext = useLocationState(); const team = useCurrentTeam(); const documentViews = useObserver(() => views.inDocument(document.id)); const totalViewers = documentViews.length; @@ -48,7 +49,7 @@ function TitleDocumentMeta({ to, document, revision, ...rest }: Props) { ui.toggleComments()} > @@ -71,7 +72,7 @@ function TitleDocumentMeta({ to, document, revision, ...rest }: Props) { match.url === insightsPath ? documentPath(document) : insightsPath, - state: location.state, + state: { sidebarContext }, }} > {t("Viewed by")}{" "} diff --git a/app/scenes/Document/components/Editor.tsx b/app/scenes/Document/components/Editor.tsx index 92221d1ddc..db3293516f 100644 --- a/app/scenes/Document/components/Editor.tsx +++ b/app/scenes/Document/components/Editor.tsx @@ -3,7 +3,7 @@ import { observer } from "mobx-react"; import * as React from "react"; import { useTranslation } from "react-i18next"; import { mergeRefs } from "react-merge-refs"; -import { useHistory, useLocation, useRouteMatch } from "react-router-dom"; +import { useHistory, useRouteMatch } from "react-router-dom"; import { richExtensions, withComments } from "@shared/editor/nodes"; import { TeamPreference } from "@shared/types"; import { colorPalette } from "@shared/utils/collections"; @@ -13,6 +13,7 @@ import { RefHandle } from "~/components/ContentEditable"; import { useDocumentContext } from "~/components/DocumentContext"; import Editor, { Props as EditorProps } from "~/components/Editor"; import Flex from "~/components/Flex"; +import { useLocationState } from "~/components/Sidebar/hooks/useLocationState"; import BlockMenuExtension from "~/editor/extensions/BlockMenu"; import ClipboardTextSerializer from "~/editor/extensions/ClipboardTextSerializer"; import EmojiMenuExtension from "~/editor/extensions/EmojiMenu"; @@ -82,7 +83,7 @@ function DocumentEditor(props: Props, ref: React.RefObject) { const user = useCurrentUser({ rejectOnEmpty: false }); const team = useCurrentTeam({ rejectOnEmpty: false }); const history = useHistory(); - const location = useLocation(); + const sidebarContext = useLocationState(); const params = useQuery(); const { document, @@ -115,14 +116,14 @@ function DocumentEditor(props: Props, ref: React.RefObject) { search: focusedComment.isResolved ? "resolved=" : "", pathname: location.pathname, state: { - ...(location.state as Record), commentId: focusedComment.id, + sidebarContext, }, }); } ui.set({ commentsExpanded: true }); } - }, [focusedComment, ui, document.id, history, location, params]); + }, [focusedComment, ui, document.id, history, params, sidebarContext]); // Save document when blurring title, but delay so that if clicking on a // button this is allowed to execute first. @@ -147,10 +148,10 @@ function DocumentEditor(props: Props, ref: React.RefObject) { (commentId: string) => { history.replace({ pathname: window.location.pathname.replace(/\/history$/, ""), - state: { ...(location.state as Record), commentId }, + state: { commentId, sidebarContext }, }); }, - [history, location] + [history, sidebarContext] ); // Create a Comment model in local store when a comment mark is created, this @@ -175,10 +176,10 @@ function DocumentEditor(props: Props, ref: React.RefObject) { history.replace({ pathname: window.location.pathname.replace(/\/history$/, ""), - state: { ...(location.state as Record), commentId }, + state: { commentId, sidebarContext }, }); }, - [comments, user?.id, props.id, history, location] + [comments, user?.id, props.id, history, sidebarContext] ); // Soft delete the Comment model when associated mark is totally removed. @@ -247,7 +248,7 @@ function DocumentEditor(props: Props, ref: React.RefObject) { match.path === matchDocumentHistory ? documentPath(document) : documentHistoryPath(document), - state: location.state, + state: { sidebarContext }, }} rtl={ titleRef.current?.getComputedDirection() === "rtl" ? true : false diff --git a/app/scenes/Document/components/Header.tsx b/app/scenes/Document/components/Header.tsx index 39319b2e08..e3c15977c7 100644 --- a/app/scenes/Document/components/Header.tsx +++ b/app/scenes/Document/components/Header.tsx @@ -24,6 +24,7 @@ import { useDocumentContext } from "~/components/DocumentContext"; import Flex from "~/components/Flex"; import Header from "~/components/Header"; import Icon from "~/components/Icon"; +import { useLocationState } from "~/components/Sidebar/hooks/useLocationState"; import Star from "~/components/Star"; import Tooltip from "~/components/Tooltip"; import { publishDocument } from "~/actions/definitions/documents"; @@ -91,6 +92,7 @@ function DocumentHeader({ const isEditingFocus = useEditingFocus(); const { editor } = useDocumentContext(); const { hasHeadings } = useDocumentContext(); + const sidebarContext = useLocationState(); // We cache this value for as long as the component is mounted so that if you // apply a template there is still the option to replace it until the user @@ -154,7 +156,10 @@ function DocumentHeader({