You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
memos/web/src/components/MemoContent/EmbeddedContent/EmbeddedMemo.tsx

40 lines
1.2 KiB
TypeScript

import { useContext, useEffect } from "react";
import useLoading from "@/hooks/useLoading";
import { useMemoStore } from "@/store/v1";
import MemoContent from "..";
import { RendererContext } from "../types";
import Error from "./Error";
interface Props {
memoId: number;
params: string;
}
const EmbeddedMemo = ({ memoId }: Props) => {
const context = useContext(RendererContext);
const loadingState = useLoading();
const memoStore = useMemoStore();
const memo = memoStore.getMemoById(memoId);
const resourceName = `memos/${memoId}`;
useEffect(() => {
memoStore.getOrFetchMemoById(memoId).finally(() => loadingState.setFinish());
}, [memoId]);
if (loadingState.isLoading) {
return null;
}
if (!memo) {
return <Error message={`Memo not found: ${memoId}`} />;
}
if (memoId === context.memoId || context.embeddedMemos.has(resourceName)) {
return <Error message={`Nested Rendering Error: ![[${resourceName}]]`} />;
}
// Add the memo to the set of embedded memos. This is used to prevent infinite loops when a memo embeds itself.
context.embeddedMemos.add(resourceName);
return <MemoContent nodes={memo.nodes} memoId={memoId} embeddedMemos={context.embeddedMemos} />;
};
export default EmbeddedMemo;