import { useInfiniteQuery } from '@tanstack/react-query'; import { useVirtualizer } from '@tanstack/react-virtual'; import { useCallback, useEffect, useRef } from 'react'; import { Link } from 'react-router-dom'; import { useNewsfeed } from '@app/space/hooks/useNewsfeed'; import { getNotes } 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 { LumeEvent } from '@utils/types'; const ITEM_PER_PAGE = 10; export function NetworkBlock() { // subscribe for live update useNewsfeed(); const { status, data, fetchNextPage, hasNextPage, isFetchingNextPage } = useInfiniteQuery({ queryKey: ['newsfeed-circle'], queryFn: async ({ pageParam = 0 }) => { return await getNotes(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(); const totalSize = rowVirtualizer.getTotalSize(); useEffect(() => { const [lastItem] = [...itemsVirtualizer].reverse(); if (!lastItem) { return; } if (lastItem.index >= notes.length - 1 && hasNextPage && !isFetchingNextPage) { fetchNextPage(); } }, [notes.length, fetchNextPage, itemsVirtualizer]); const renderItem = useCallback( (index: string | number) => { const note: LumeEvent = notes[index]; if (!note) return; switch (note.kind) { case 1: { let root: string; let reply: string; if (note.tags?.[0]?.[0] === 'e' && !note.tags?.[0]?.[3]) { root = note.tags[0][1]; } else { root = note.tags.find((el) => el[3] === 'root')?.[1]; 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 ? (

You not have any posts to see yet
Follow more people to have more fun.

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