mirror of
https://github.com/outline/outline.git
synced 2026-11-28 00:09:58 +03:00
refactor
This commit is contained in:
3 files changed
+34
-65
No files matched your search
@@ -49,7 +49,6 @@ import Contents from "./Contents";
|
||||
import Editor from "./Editor";
|
||||
import Header from "./Header";
|
||||
import Notices from "./Notices";
|
||||
import PublicReferences from "./PublicReferences";
|
||||
import References from "./References";
|
||||
import RevisionViewer from "./RevisionViewer";
|
||||
|
||||
@@ -608,7 +607,7 @@ class DocumentScene extends React.Component<Props> {
|
||||
>
|
||||
{shareId ? (
|
||||
<ReferencesWrapper>
|
||||
<PublicReferences documentId={document.id} />
|
||||
<References document={document} />
|
||||
</ReferencesWrapper>
|
||||
) : !revision ? (
|
||||
<ReferencesWrapper>
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
import { observer } from "mobx-react";
|
||||
import { useMemo } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import type { NavigationNode } from "@shared/types";
|
||||
import Subheading from "~/components/Subheading";
|
||||
import ReferenceListItem from "./ReferenceListItem";
|
||||
import useShare from "@shared/hooks/useShare";
|
||||
|
||||
type Props = {
|
||||
documentId: string;
|
||||
};
|
||||
|
||||
function PublicReferences(props: Props) {
|
||||
const { t } = useTranslation();
|
||||
const { sharedTree } = useShare();
|
||||
const { documentId } = props;
|
||||
|
||||
// The sharedTree is the entire document tree starting at the shared document
|
||||
// we must filter down the tree to only the part with the document we're
|
||||
// currently viewing
|
||||
const children = useMemo(() => {
|
||||
let result: NavigationNode[];
|
||||
|
||||
function findChildren(node?: NavigationNode) {
|
||||
if (!node) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (node.id === documentId) {
|
||||
result = node.children;
|
||||
} else {
|
||||
node.children.forEach((node) => {
|
||||
if (result) {
|
||||
return;
|
||||
}
|
||||
|
||||
findChildren(node);
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return findChildren(sharedTree) || [];
|
||||
}, [documentId, sharedTree]);
|
||||
|
||||
if (!children.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Subheading>{t("Documents")}</Subheading>
|
||||
{children.map((node) => (
|
||||
<ReferenceListItem key={node.id} document={node} />
|
||||
))}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export default observer(PublicReferences);
|
||||
@@ -1,5 +1,5 @@
|
||||
import { observer } from "mobx-react";
|
||||
import { useEffect, useRef, Fragment } from "react";
|
||||
import { useEffect, useRef, Fragment, useMemo } from "react";
|
||||
import { Trans } from "react-i18next";
|
||||
import { useLocation } from "react-router-dom";
|
||||
import styled from "styled-components";
|
||||
@@ -12,6 +12,8 @@ import useCurrentUser from "~/hooks/useCurrentUser";
|
||||
import { useLocationSidebarContext } from "~/hooks/useLocationSidebarContext";
|
||||
import useStores from "~/hooks/useStores";
|
||||
import ReferenceListItem from "./ReferenceListItem";
|
||||
import useShare from "@shared/hooks/useShare";
|
||||
import type { NavigationNode } from "@shared/types";
|
||||
|
||||
type Props = {
|
||||
document: Document;
|
||||
@@ -22,13 +24,42 @@ function References({ document }: Props) {
|
||||
const user = useCurrentUser();
|
||||
const location = useLocation();
|
||||
const locationSidebarContext = useLocationSidebarContext();
|
||||
const { sharedTree } = useShare();
|
||||
|
||||
useEffect(() => {
|
||||
void documents.fetchBacklinks(document.id);
|
||||
}, [documents, document.id]);
|
||||
|
||||
// The sharedTree is the entire document tree starting at the shared document
|
||||
// we must filter down the tree to only the part with the document we're
|
||||
// currently viewing
|
||||
const children = useMemo(() => {
|
||||
let result: NavigationNode[];
|
||||
|
||||
function findChildren(node?: NavigationNode) {
|
||||
if (!node) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (node.id === document.id) {
|
||||
result = node.children;
|
||||
} else {
|
||||
node.children.forEach((node) => {
|
||||
if (result) {
|
||||
return;
|
||||
}
|
||||
|
||||
findChildren(node);
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
return sharedTree ? findChildren(sharedTree) || [] : document.children;
|
||||
}, [document.id, sharedTree]);
|
||||
|
||||
const backlinks = document.backlinks;
|
||||
const children = document.children;
|
||||
const showBacklinks = !!backlinks.length;
|
||||
const showChildDocuments = !!children.length;
|
||||
const shouldFade = useRef(!showBacklinks && !showChildDocuments);
|
||||
|
||||
Reference in new issue
Block a user