import { useInfiniteQuery } from '@tanstack/react-query'; import { useVirtualizer } from '@tanstack/react-virtual'; import { useCallback, useEffect, useRef } from 'react'; import { getNotesByAuthors } from '@libs/storage'; import { NoteKind_1, NoteKind_1063, NoteThread, Repost } from '@shared/notes'; import { NoteKindUnsupport } from '@shared/notes/kinds/unsupport'; import { NoteSkeleton } from '@shared/notes/skeleton'; import { TitleBar } from '@shared/titleBar'; import { Block, LumeEvent } from '@utils/types'; const ITEM_PER_PAGE = 10; export function FeedBlock({ params }: { params: Block }) { const { status, data, fetchNextPage, hasNextPage, isFetchingNextPage } = useInfiniteQuery({ queryKey: ['newsfeed', params.content], queryFn: async ({ pageParam = 0 }) => { return await getNotesByAuthors(params.content, ITEM_PER_PAGE, pageParam); }, getNextPageParam: (lastPage) => lastPage.nextCursor, }); const notes = data ? data.pages.flatMap((d: { data: LumeEvent[] }) => d.data) : []; const parentRef = useRef(); const rowVirtualizer = useVirtualizer({ count: hasNextPage ? notes.length + 1 : notes.length, getScrollElement: () => parentRef.current, estimateSize: () => 500, overscan: 2, }); const itemsVirtualizer = rowVirtualizer.getVirtualItems(); useEffect(() => { const [lastItem] = [...rowVirtualizer.getVirtualItems()].reverse(); if (!lastItem) { return; } if (lastItem.index >= notes.length - 1 && hasNextPage && !isFetchingNextPage) { fetchNextPage(); } }, [notes.length, fetchNextPage, rowVirtualizer.getVirtualItems()]); const renderItem = useCallback( (index: string | number) => { const note: LumeEvent = notes[index]; if (!note) return; switch (note.kind) { case 1: { const root = note.tags.find((el) => el[3] === 'root')?.[1]; const reply = note.tags.find((el) => el[3] === 'reply')?.[1]; if (root || reply) { return (
); } else { return (
); } } case 6: return (
); case 1063: return (
); default: return (
); } }, [notes] ); return (
{status === 'loading' ? (
) : itemsVirtualizer.length === 0 ? (

Not found any posts from last 48 hours

) : (
{itemsVirtualizer.map((virtualRow) => renderItem(virtualRow.index))}
)} {isFetchingNextPage && (
)}
); }