diff --git a/app/lish/[did]/[publication]/[rkey]/DocumentPageRenderer.tsx b/app/lish/[did]/[publication]/[rkey]/DocumentPageRenderer.tsx index 0fcd6d74..03b4fe7d 100644 --- a/app/lish/[did]/[publication]/[rkey]/DocumentPageRenderer.tsx +++ b/app/lish/[did]/[publication]/[rkey]/DocumentPageRenderer.tsx @@ -2,6 +2,8 @@ import { AtpAgent } from "@atproto/api"; import { ids } from "lexicons/api/lexicons"; import { PubLeafletBlocksBskyPost, + PubLeafletBlocksOrderedList, + PubLeafletBlocksUnorderedList, PubLeafletPagesLinearDocument, PubLeafletPagesCanvas, } from "lexicons/api"; @@ -65,9 +67,7 @@ export async function DocumentPageRenderer({ let bskyPosts = pages.flatMap((p) => { let page = p as PubLeafletPagesLinearDocument.Main; - return page.blocks?.filter( - (b) => b.block.$type === ids.PubLeafletBlocksBskyPost, - ); + return extractBlocksByType(page.blocks || [], ids.PubLeafletBlocksBskyPost); }) || []; // Batch bsky posts into groups of 25 and fetch in parallel @@ -98,10 +98,7 @@ export async function DocumentPageRenderer({ // Extract poll blocks and fetch vote data let pollBlocks = pages.flatMap((p) => { let page = p as PubLeafletPagesLinearDocument.Main; - return ( - page.blocks?.filter((b) => b.block.$type === ids.PubLeafletBlocksPoll) || - [] - ); + return extractBlocksByType(page.blocks || [], ids.PubLeafletBlocksPoll); }); let pollData = await fetchPollData( pollBlocks.map((b) => (b.block as any).pollRef.uri), @@ -123,7 +120,10 @@ export async function DocumentPageRenderer({ return ( - + ); } + +function extractBlocksByType( + blocks: PubLeafletPagesLinearDocument.Block[], + type: string, +): { block: { $type: string; [key: string]: unknown } }[] { + let results: { block: { $type: string; [key: string]: unknown } }[] = []; + for (let b of blocks) { + if (b.block.$type === type) { + results.push(b as { block: { $type: string; [key: string]: unknown } }); + } + if ( + b.block.$type === ids.PubLeafletBlocksOrderedList || + b.block.$type === ids.PubLeafletBlocksUnorderedList + ) { + let list = b.block as + | PubLeafletBlocksOrderedList.Main + | PubLeafletBlocksUnorderedList.Main; + extractFromListItems(list.children, type, results); + } + } + return results; +} + +function extractFromListItems( + items: + | PubLeafletBlocksOrderedList.ListItem[] + | PubLeafletBlocksUnorderedList.ListItem[], + type: string, + results: { block: { $type: string; [key: string]: unknown } }[], +) { + for (let item of items) { + if ((item.content as { $type?: string })?.$type === type) { + results.push({ + block: item.content as { $type: string; [key: string]: unknown }, + }); + } + if (item.children) { + extractFromListItems(item.children, type, results); + } + let orderedChildren = ( + item as PubLeafletBlocksUnorderedList.ListItem + ).orderedListChildren; + if (orderedChildren) { + extractFromListItems(orderedChildren.children, type, results); + } + let unorderedChildren = ( + item as PubLeafletBlocksOrderedList.ListItem + ).unorderedListChildren; + if (unorderedChildren) { + extractFromListItems(unorderedChildren.children, type, results); + } + } +}