From 9f14a63268a81b2a79fd2a9a9a665f4247f8d654 Mon Sep 17 00:00:00 2001 From: plebeius Date: Wed, 11 Feb 2026 16:07:01 +0800 Subject: [PATCH] feat: populate quotedCids when publishing replies with quote references Parse reply content for >>{postNumber} references, resolve post numbers to CIDs via usePostNumberStore, and include resolved CIDs in publishCommentOptions when publishing. Extracted quote handling into reply-quote-utils.ts for testability and clarity. --- src/hooks/use-publish-reply.ts | 11 +++++++++-- src/lib/utils/reply-quote-utils.ts | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 src/lib/utils/reply-quote-utils.ts diff --git a/src/hooks/use-publish-reply.ts b/src/hooks/use-publish-reply.ts index 1715ed83..229421a1 100644 --- a/src/hooks/use-publish-reply.ts +++ b/src/hooks/use-publish-reply.ts @@ -1,6 +1,8 @@ -import { useCallback } from 'react'; +import { useCallback, useMemo } from 'react'; import { Comment, useAccount, usePublishComment } from '@plebbit/plebbit-react-hooks'; import usePublishReplyStore from '../stores/use-publish-reply-store'; +import usePostNumberStore from '../stores/use-post-number-store'; +import { getQuotedCidsFromContent, mergeQuotedCids } from '../lib/utils/reply-quote-utils'; const usePublishReply = ({ cid, subplebbitAddress, postCid }: { cid: string; subplebbitAddress: string; postCid?: string }) => { const parentCid = cid; @@ -54,7 +56,12 @@ const usePublishReply = ({ cid, subplebbitAddress, postCid }: { cid: string; sub const resetPublishReplyOptions = useCallback(() => resetPublishReplyStore(parentCid), [parentCid, resetPublishReplyStore]); - const { index, publishComment } = usePublishComment(publishCommentOptions); + const numberToCid = usePostNumberStore((state) => state.numberToCid); + const quotedCids = useMemo(() => getQuotedCidsFromContent(content, numberToCid), [content, numberToCid]); + + const publishOptions = useMemo(() => mergeQuotedCids(publishCommentOptions, quotedCids), [publishCommentOptions, quotedCids]); + + const { index, publishComment } = usePublishComment(publishOptions); return { setPublishReplyOptions, diff --git a/src/lib/utils/reply-quote-utils.ts b/src/lib/utils/reply-quote-utils.ts new file mode 100644 index 00000000..b00fa991 --- /dev/null +++ b/src/lib/utils/reply-quote-utils.ts @@ -0,0 +1,23 @@ +import { QUOTE_NUMBER_REGEX } from './url-utils'; + +export const getQuotedCidsFromContent = (content: string | undefined, numberToCid: Record) => { + if (!content) return undefined; + const cids = new Set(); + for (const match of content.matchAll(new RegExp(QUOTE_NUMBER_REGEX.source, 'g'))) { + const num = parseInt(match[1], 10); + const cid = numberToCid[num]; + if (cid) cids.add(cid); + } + return cids.size > 0 ? [...cids] : undefined; +}; + +export const mergeQuotedCids = (publishCommentOptions: Record | undefined, quotedCids: string[] | undefined) => { + if (!publishCommentOptions || !quotedCids?.length) return publishCommentOptions; + const currentQuotedCids = publishCommentOptions.quotedCids ?? []; + const mergedQuotedCids = [...new Set([...currentQuotedCids, ...quotedCids])]; + + return { + ...publishCommentOptions, + quotedCids: mergedQuotedCids, + }; +};