Files
outline/app/scenes/Document/components/Comments.tsx
Tom Moor 08df14618c Various commenting improvements (#4938)
* fix: New threads attached to previous as replies

* fix: Cannot use floating toolbar properly in comments

* perf: Avoid re-writing history on click in editor

* fix: Comment on text selection

* fix: 'Copy link' on comments uses wrong hostname

* Show comment buttons on input focus rather than non-empty input
Increase maximum sidebar size

* Allow opening comments from document menu

* fix: Clicking comment menu should not focus thread
2023-02-26 11:19:12 -08:00

91 lines
2.8 KiB
TypeScript

import { AnimatePresence } from "framer-motion";
import { observer } from "mobx-react";
import * as React from "react";
import { useTranslation } from "react-i18next";
import { useRouteMatch } from "react-router-dom";
import styled from "styled-components";
import Empty from "~/components/Empty";
import Flex from "~/components/Flex";
import useCurrentUser from "~/hooks/useCurrentUser";
import useFocusedComment from "~/hooks/useFocusedComment";
import useStores from "~/hooks/useStores";
import CommentForm from "./CommentForm";
import CommentThread from "./CommentThread";
import Sidebar from "./SidebarLayout";
function Comments() {
const { ui, comments, documents } = useStores();
const { t } = useTranslation();
const user = useCurrentUser();
const match = useRouteMatch<{ documentSlug: string }>();
const document = documents.getByUrl(match.params.documentSlug);
const focusedComment = useFocusedComment();
if (!document) {
return null;
}
const threads = comments
.threadsInDocument(document.id)
.filter((thread) => !thread.isNew || thread.createdById === user.id);
const hasComments = threads.length > 0;
return (
<Sidebar title={t("Comments")} onClose={ui.collapseComments}>
<Wrapper $hasComments={hasComments}>
{hasComments ? (
threads.map((thread) => (
<CommentThread
key={thread.id}
comment={thread}
document={document}
recessed={!!focusedComment && focusedComment.id !== thread.id}
focused={focusedComment?.id === thread.id}
/>
))
) : (
<NoComments align="center" justify="center" auto>
<Empty>{t("No comments yet")}</Empty>
</NoComments>
)}
<AnimatePresence initial={false}>
{!focusedComment && (
<NewCommentForm
documentId={document.id}
placeholder={`${t("Add a comment")}`}
autoFocus={false}
dir={document.dir}
animatePresence
standalone
/>
)}
</AnimatePresence>
</Wrapper>
</Sidebar>
);
}
const NoComments = styled(Flex)`
padding-bottom: 65px;
height: 100%;
`;
const Wrapper = styled.div<{ $hasComments: boolean }>`
padding-bottom: ${(props) => (props.$hasComments ? "50vh" : "0")};
height: ${(props) => (props.$hasComments ? "auto" : "100%")};
`;
const NewCommentForm = styled(CommentForm)<{ dir?: "ltr" | "rtl" }>`
background: ${(props) => props.theme.background};
position: absolute;
padding: 12px;
padding-right: ${(props) => (props.dir !== "rtl" ? "18px" : "12px")};
padding-left: ${(props) => (props.dir === "rtl" ? "18px" : "12px")};
left: 0;
right: 0;
bottom: 0;
`;
export default observer(Comments);