From 43a6db07d2d7c2abddeafb2c63be8bc5de49ec5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=83=A6=E7=83=A6?= Date: Sun, 6 Oct 2024 14:39:52 +0800 Subject: [PATCH] fix:resolve #26 --- .../components/OnChainBookInteractor.tsx | 33 +++++++++++++++++-- packages/nextjs/pages/index.tsx | 13 +++++--- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/packages/nextjs/components/OnChainBookInteractor.tsx b/packages/nextjs/components/OnChainBookInteractor.tsx index 2562128..df1f1d4 100644 --- a/packages/nextjs/components/OnChainBookInteractor.tsx +++ b/packages/nextjs/components/OnChainBookInteractor.tsx @@ -1,6 +1,9 @@ -import { useScaffoldContractRead } from "~~/hooks/scaffold-eth"; +import { useContractReads } from "wagmi"; +import { useDeployedContractInfo, useScaffoldContractRead } from "~~/hooks/scaffold-eth"; +import { getTargetNetwork } from "~~/utils/scaffold-eth"; +import { AbiFunctionReturnType, ContractAbi } from "~~/utils/scaffold-eth/contract"; -export const CommentReader = ({ commentId }: { commentId: number }) => { +export const useCommentReader = ({ commentId }: { commentId: number }) => { const commentIdBigInt = BigInt(commentId); const { data, isLoading, error } = useScaffoldContractRead({ contractName: "OnChainBook", @@ -14,3 +17,29 @@ export const CommentReader = ({ commentId }: { commentId: number }) => { error, }; }; + +export const useCommentsReader = (commentCount: bigint | undefined) => { + const { data: deployedContract } = useDeployedContractInfo("OnChainBook"); + const contractReadsParams = []; + for (let i = 0; i < (commentCount || 0); i++) { + const args = [BigInt(i)]; + contractReadsParams.push({ + chainId: getTargetNetwork().id, + contractName: "OnChainBook", + functionName: "comments", + address: deployedContract?.address, + abi: deployedContract?.abi, + args, + }); + } + return useContractReads({ contracts: contractReadsParams, watch: true, enabled: !!commentCount }) as unknown as Omit< + ReturnType, + "data" | "refetch" + > & { + data: AbiFunctionReturnType | undefined; + refetch: (options?: { + throwOnError: boolean; + cancelRefetch: boolean; + }) => Promise>; + }; +}; diff --git a/packages/nextjs/pages/index.tsx b/packages/nextjs/pages/index.tsx index b8e535b..e1b3c69 100644 --- a/packages/nextjs/pages/index.tsx +++ b/packages/nextjs/pages/index.tsx @@ -11,7 +11,7 @@ import TopicCard from "~~/components/TopicCard"; import { Address } from "~~/components/scaffold-eth"; import { useScaffoldContractRead, useScaffoldContractWrite } from "~~/hooks/scaffold-eth"; import { useCategoryContext } from "~~/provider/categoryProvider"; -import { CommentReader } from "~~/components/OnChainBookInteractor"; +import { useCommentReader, useCommentsReader } from "~~/components/OnChainBookInteractor"; interface ETHSpaceProps { markdownContentEn: string; markdownContentCn: string; @@ -53,11 +53,12 @@ const ETHSpace: NextPage = ({ const [newNoteWord, setNewNoteWord] = useState(""); const [newNoteContent, setNewNoteContent] = useState(""); const { address } = useAccount(); - const { data: commentCount } = useScaffoldContractRead({ contractName: "OnChainBook", functionName: "commentCount", }); + const commentReader = useCommentReader({ commentId: 0 }); + const commentsReader = useCommentsReader(commentCount); const { writeAsync: addCommentOnChain, isLoading: isAddingCommentOnChain } = useScaffoldContractWrite({ contractName: "OnChainBook", @@ -86,8 +87,8 @@ const ETHSpace: NextPage = ({ const fetchOnChainNotes = async () => { if (!commentCount) return []; console.log("commentCount", commentCount); - console.log("comment", CommentReader({ commentId: 0 })); - + // console.log("comment", useCommentReader({ commentId: 0 })); + // console.log("comments", useCommentsReader(commentCount)); // const onChainNotes = []; // for (let i = 0; i < commentCount; i++) { // const { data: comment } = await useScaffoldContractRead({ @@ -102,7 +103,9 @@ const ETHSpace: NextPage = ({ useEffect(() => { fetchNotes(); - fetchOnChainNotes(); + // fetchOnChainNotes(); + console.log("comment", commentReader); + console.log("comments", commentsReader); }, [pageIndex, category, language]); // Add language to the dependency array useEffect(() => {