import { ClientError } from "nice-grpc-web"; import { useEffect, useState } from "react"; import { toast } from "react-hot-toast"; import { Link, useParams } from "react-router-dom"; import Icon from "@/components/Icon"; import MemoEditor from "@/components/MemoEditor"; import MemoView from "@/components/MemoView"; import MobileHeader from "@/components/MobileHeader"; import useCurrentUser from "@/hooks/useCurrentUser"; import useNavigateTo from "@/hooks/useNavigateTo"; import { extractMemoIdFromName, useMemoStore } from "@/store/v1"; import { MemoRelation_Type } from "@/types/proto/api/v2/memo_relation_service"; import { Memo } from "@/types/proto/api/v2/memo_service"; import { useTranslate } from "@/utils/i18n"; const MemoDetail = () => { const t = useTranslate(); const params = useParams(); const navigateTo = useNavigateTo(); const currentUser = useCurrentUser(); const memoStore = useMemoStore(); const memoName = params.memoName; const memo = memoStore.getMemoByName(memoName || ""); const [parentMemo, setParentMemo] = useState(undefined); const commentRelations = memo?.relations.filter( (relation) => relation.relatedMemoId === extractMemoIdFromName(memo.name) && relation.type === MemoRelation_Type.COMMENT, ) || []; const comments = commentRelations.map((relation) => memoStore.getMemoById(relation.memoId)).filter((memo) => memo) as any as Memo[]; // Prepare memo. useEffect(() => { if (memoName) { memoStore.searchMemos(`resource_name == "${memoName}"`).catch((error: ClientError) => { toast.error(error.details); navigateTo("/403"); }); } else { navigateTo("/404"); } }, [memoName]); // Prepare memo comments. useEffect(() => { if (!memo) { return; } (async () => { if (memo.parentId) { memoStore.getOrFetchMemoById(memo.parentId).then((memo: Memo) => { setParentMemo(memo); }); } else { setParentMemo(undefined); } await Promise.all(commentRelations.map((relation) => memoStore.getOrFetchMemoById(relation.memoId))); })(); }, [memo]); if (!memo) { return null; } const handleCommentCreated = async (commentId: number) => { await memoStore.getOrFetchMemoById(commentId); await memoStore.getOrFetchMemoById(extractMemoIdFromName(memo.name), { skipCache: true }); }; return (
{parentMemo && (
{parentMemo.content}
)}

Comments

{comments.length === 0 ? (

{t("memo.comment.no-comment")}

) : ( <>
{t("memo.comment.self")} ({comments.length})
{comments.map((comment) => ( ))} )} {/* Only show comment editor when user login */} {currentUser && ( )}
); }; export default MemoDetail;