import { getNotesByAuthor } from "@libs/storage"; import { CancelIcon } from "@shared/icons"; import { NoteBase } from "@shared/notes/base"; import { NoteQuoteRepost } from "@shared/notes/quoteRepost"; import { NoteSkeleton } from "@shared/notes/skeleton"; import { useActiveAccount } from "@stores/accounts"; import { useVirtualizer } from "@tanstack/react-virtual"; import { useEffect, useMemo, useRef } from "react"; import useSWRInfinite from "swr/infinite"; const ITEM_PER_PAGE = 10; const TIME = Math.floor(Date.now() / 1000); const fetcher = async ([pubkey, offset]) => getNotesByAuthor(pubkey, TIME, ITEM_PER_PAGE, offset); export function FeedBlock({ params }: { params: any }) { const removeBlock = useActiveAccount((state: any) => state.removeBlock); const close = () => { removeBlock(params.id, true); }; const getKey = (pageIndex, previousPageData) => { if (previousPageData && !previousPageData.data) return null; if (pageIndex === 0) return [params.content, 0]; return [params.content, previousPageData.nextCursor]; }; const { data, isLoading, size, setSize } = useSWRInfinite(getKey, fetcher); const notes = useMemo( () => (data ? data.flatMap((d) => d.data) : []), [data], ); const parentRef = useRef(); const rowVirtualizer = useVirtualizer({ count: notes.length, getScrollElement: () => parentRef.current, estimateSize: () => 400, overscan: 2, }); const itemsVirtualizer = rowVirtualizer.getVirtualItems(); useEffect(() => { const [lastItem] = [...rowVirtualizer.getVirtualItems()].reverse(); if (!lastItem) { return; } if (lastItem.index >= notes.length - 1) { setSize(size + 1); } }, [notes.length, rowVirtualizer.getVirtualItems()]); return (

{params.title}

{!data || isLoading ? (
) : (
{rowVirtualizer.getVirtualItems().map((virtualRow) => { const note = notes[virtualRow.index]; if (note) { if (note.kind === 1) { return (
); } else { return (
); } } })}
)}
); }