import { ConnectError } from "@connectrpc/connect"; import { ArrowUpLeftFromCircleIcon, MessageCircleIcon } from "lucide-react"; import { observer } from "mobx-react-lite"; import { useEffect, useState } from "react"; import { toast } from "react-hot-toast"; import { Link, useLocation, useParams } from "react-router-dom"; import { MemoDetailSidebar, MemoDetailSidebarDrawer } from "@/components/MemoDetailSidebar"; import MemoEditor from "@/components/MemoEditor"; import MemoView from "@/components/MemoView"; import MobileHeader from "@/components/MobileHeader"; import { Button } from "@/components/ui/button"; import useCurrentUser from "@/hooks/useCurrentUser"; import useNavigateTo from "@/hooks/useNavigateTo"; import useResponsiveWidth from "@/hooks/useResponsiveWidth"; import { cn } from "@/lib/utils"; import { memoStore } from "@/store"; import { memoNamePrefix } from "@/store/common"; import { Memo, MemoRelation_Type } from "@/types/proto/api/v1/memo_service_pb"; import { useTranslate } from "@/utils/i18n"; const MemoDetail = observer(() => { const t = useTranslate(); const { md } = useResponsiveWidth(); const params = useParams(); const navigateTo = useNavigateTo(); const { state: locationState } = useLocation(); const currentUser = useCurrentUser(); const uid = params.uid; const memoName = `${memoNamePrefix}${uid}`; const memo = memoStore.getMemoByName(memoName); const [parentMemo, setParentMemo] = useState(undefined); const [showCommentEditor, setShowCommentEditor] = useState(false); const commentRelations = memo?.relations.filter((relation) => relation.relatedMemo?.name === memo.name && relation.type === MemoRelation_Type.COMMENT) || []; const comments = commentRelations.map((relation) => memoStore.getMemoByName(relation.memo!.name)).filter((memo) => memo) as any as Memo[]; const showCreateCommentButton = currentUser && !showCommentEditor; // Prepare memo. useEffect(() => { if (memoName) { memoStore.getOrFetchMemoByName(memoName).catch((error: ConnectError) => { toast.error(error.message); navigateTo("/403"); }); } else { navigateTo("/404"); } }, [memoName]); // Prepare memo comments. useEffect(() => { if (!memo) { return; } (async () => { if (memo.parent) { memoStore.getOrFetchMemoByName(memo.parent).then((memo: Memo) => { setParentMemo(memo); }); } else { setParentMemo(undefined); } await Promise.all(commentRelations.map((relation) => memoStore.getOrFetchMemoByName(relation.memo!.name))); })(); }, [memo]); if (!memo) { return null; } const handleShowCommentEditor = () => { setShowCommentEditor(true); }; const handleCommentCreated = async (memoCommentName: string) => { await memoStore.getOrFetchMemoByName(memoCommentName); await memoStore.getOrFetchMemoByName(memo.name, { skipCache: true }); setShowCommentEditor(false); }; return (
{!md && ( )}
{parentMemo && (
{parentMemo.content}
)}

{t("memo.comment.self")}

{comments.length === 0 ? ( showCreateCommentButton && (
) ) : ( <>
{t("memo.comment.self")} ({comments.length})
{showCreateCommentButton && ( )}
{comments.map((comment) => ( ))} )}
{showCommentEditor && (
setShowCommentEditor(false)} />
)}
{md && (
)}
); }); export default MemoDetail;