import { Button } from "@mui/joy"; import clsx from "clsx"; 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 { MemoDetailSidebar, MemoDetailSidebarDrawer } from "@/components/MemoDetailSidebar"; 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 useResponsiveWidth from "@/hooks/useResponsiveWidth"; import { useMemoStore, useWorkspaceSettingStore } from "@/store/v1"; import { MemoRelation_Type } from "@/types/proto/api/v1/memo_relation_service"; import { Memo } from "@/types/proto/api/v1/memo_service"; import { WorkspaceMemoRelatedSetting, WorkspaceSettingKey } from "@/types/proto/store/workspace_setting"; import { useTranslate } from "@/utils/i18n"; const MemoDetail = () => { const t = useTranslate(); const { md } = useResponsiveWidth(); const params = useParams(); const navigateTo = useNavigateTo(); const workspaceSettingStore = useWorkspaceSettingStore(); const currentUser = useCurrentUser(); const memoStore = useMemoStore(); const uid = params.uid; const memo = memoStore.getMemoByUid(uid || ""); const workspaceMemoRelatedSetting = WorkspaceMemoRelatedSetting.fromPartial( workspaceSettingStore.getWorkspaceSettingByKey(WorkspaceSettingKey.MEMO_RELATED)?.memoRelatedSetting || {}, ); const [parentMemo, setParentMemo] = useState(undefined); const [showCommentEditor, setShowCommentEditor] = useState(false); const commentRelations = memo?.relations.filter((relation) => relation.relatedMemo === memo.name && relation.type === MemoRelation_Type.COMMENT) || []; const comments = commentRelations.map((relation) => memoStore.getMemoByName(relation.memo)).filter((memo) => memo) as any as Memo[]; const showCreateCommentButton = workspaceMemoRelatedSetting.enableComment && currentUser; // Prepare memo. useEffect(() => { if (uid) { memoStore.fetchMemoByUid(uid).catch((error: ClientError) => { toast.error(error.details); navigateTo("/403"); }); } else { navigateTo("/404"); } }, [uid]); // 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))); })(); }, [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;