Files
outline/app/scenes/Document/components/DocumentMeta.tsx
T
Tom Moor 30da7e7ae6 fix: Reduce layout shift on page load (#14843)
* fix: Reduce layout shift on page load

- Open the history sidebar on load without the slide-in animation
- Keep server task counts until the editor exists
- Measure the header before paint so actions do not switch to compact
- Do not mark a store loaded from a filtered list
- Show cached list items only once that list has been fetched
- Reserve space for the collection document count, search result count and document views

* fix: Clear the document sidebar before a replacement instance sets it

* test
2026-12-02 22:50:43 -05:00

150 lines
4.8 KiB
TypeScript

import type { LocationDescriptor } from "history";
import { observer } from "mobx-react";
import { CommentIcon } from "outline-icons";
import { useRef, Fragment, useCallback } from "react";
import { useTranslation } from "react-i18next";
import { useHistory } from "react-router-dom";
import styled from "styled-components";
import type Document from "~/models/Document";
import type Revision from "~/models/Revision";
import type Template from "~/models/Template";
import {
Insights,
openDocumentInsights,
} from "~/actions/definitions/documents";
import DocumentMeta from "~/components/DocumentMeta";
import {
MetaButton,
Separator,
headerMetaStyles,
} from "~/components/HeaderMeta";
import Fade from "~/components/Fade";
import { useSplitView } from "~/components/SplitView/context";
import useClickIntent from "~/hooks/useClickIntent";
import useCurrentTeam from "~/hooks/useCurrentTeam";
import { useLocationSidebarContext } from "~/hooks/useLocationSidebarContext";
import usePolicy from "~/hooks/usePolicy";
import useStores from "~/hooks/useStores";
import { documentPath } from "~/utils/routeHelpers";
import { DocumentComments, DocumentHistory } from "../hooks/useDocumentSidebar";
type Props = {
/* The document to display meta data for */
document: Document | Template;
revision?: Revision;
to?: LocationDescriptor;
rtl?: boolean;
};
function TitleDocumentMeta({ to, document, revision, rtl, ...rest }: Props) {
const { views, comments, ui } = useStores();
const { t } = useTranslation();
const history = useHistory();
const { pane } = useSplitView();
const sidebarContext = useLocationSidebarContext();
const team = useCurrentTeam();
const documentViews = views.inDocument(document.id);
const totalViewers = documentViews.length;
const onlyYou = totalViewers === 1 && documentViews[0].userId;
const viewsLoadedOnMount = useRef(totalViewers > 0);
const can = usePolicy(document);
const Wrapper = viewsLoadedOnMount.current ? Fragment : Fade;
const commentsCount = comments.unresolvedCommentsInDocumentCount(document.id);
const commentingEnabled = team.commentingEnabled;
const commentsOpen = ui.getRightSidebar(pane) === "comments";
const {
ref: commentsRef,
handleMouseEnter: handleCommentsMouseEnter,
handleMouseLeave: handleCommentsMouseLeave,
} = useClickIntent(DocumentComments.preload, {
predict: true,
});
const {
ref: insightsRef,
handleMouseEnter: handleInsightsMouseEnter,
handleMouseLeave: handleInsightsMouseLeave,
} = useClickIntent(Insights.preload, { predict: true });
const handleClickMeta = useCallback(() => {
if (to) {
history.replace(to);
}
}, [history, to]);
const handleClickComment = useCallback(() => {
history.push({
pathname: documentPath(document as Document),
state: { sidebarContext },
});
ui.setRightSidebar(commentsOpen ? null : "comments", pane);
}, [history, document, sidebarContext, ui, pane, commentsOpen]);
return (
<Meta
document={document as Document}
revision={revision}
onClick={to ? handleClickMeta : undefined}
onClickIntent={to ? DocumentHistory.preload : undefined}
ownerIds={"allOwnerIds" in document ? document.allOwnerIds : undefined}
$rtl={rtl}
{...rest}
>
{commentingEnabled && can.comment && (
<>
<Separator />
<MetaButton
ref={commentsRef}
onClick={handleClickComment}
onMouseEnter={handleCommentsMouseEnter}
onMouseLeave={handleCommentsMouseLeave}
aria-expanded={commentsOpen}
>
<CommentIcon size={18} />
{commentsCount
? t("{{ count }} comment", { count: commentsCount })
: t("Comment")}
</MetaButton>
</>
)}
{can.listViews && !(document as Document).isDraft ? (
totalViewers ? (
<Wrapper>
<Separator />
<MetaButton
ref={insightsRef}
action={openDocumentInsights}
onMouseEnter={handleInsightsMouseEnter}
onMouseLeave={handleInsightsMouseLeave}
>
{t("Viewed by")}{" "}
{onlyYou
? t("only you")
: `${totalViewers} ${
totalViewers === 1 ? t("person") : t("people")
}`}
</MetaButton>
</Wrapper>
) : (
// Reserve the line while views load, it stacks on mobile.
<ViewsPlaceholder aria-hidden>
{t("Viewed by")} {t("only you")}
</ViewsPlaceholder>
)
) : null}
</Meta>
);
}
const ViewsPlaceholder = styled.span`
visibility: hidden;
`;
export const Meta = styled(DocumentMeta)<{ $rtl?: boolean }>`
${headerMetaStyles}
`;
export default observer(TitleDocumentMeta);