import { ConnectError } from "@connectrpc/connect"; import { ArrowUpLeftFromCircleIcon, MessageCircleIcon } from "lucide-react"; import { 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 { memoNamePrefix } from "@/helpers/resource-names"; import useCurrentUser from "@/hooks/useCurrentUser"; import useMediaQuery from "@/hooks/useMediaQuery"; import { useMemo, useMemoComments } from "@/hooks/useMemoQueries"; import useNavigateTo from "@/hooks/useNavigateTo"; import { cn } from "@/lib/utils"; import { useTranslate } from "@/utils/i18n"; const MemoDetail = () => { const t = useTranslate(); const md = useMediaQuery("md"); const params = useParams(); const navigateTo = useNavigateTo(); const { state: locationState } = useLocation(); const currentUser = useCurrentUser(); const uid = params.uid; const memoName = `${memoNamePrefix}${uid}`; const [showCommentEditor, setShowCommentEditor] = useState(false); // Fetch main memo with React Query const { data: memo, error, isLoading } = useMemo(memoName, { enabled: !!memoName }); // Handle errors if (error) { toast.error((error as ConnectError).message); navigateTo("/403"); } // Fetch parent memo if exists const { data: parentMemo } = useMemo(memo?.parent || "", { enabled: !!memo?.parent, }); // Fetch all comments for this memo in a single query const { data: commentsResponse } = useMemoComments(memoName, { enabled: !!memo, }); const comments = commentsResponse?.memos || []; const showCreateCommentButton = currentUser && !showCommentEditor; if (isLoading || !memo) { return null; } const handleShowCommentEditor = () => { setShowCommentEditor(true); }; const handleCommentCreated = async (_memoCommentName: string) => { // React Query will auto-refetch due to invalidation in the mutation 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;