import { useInfiniteQuery } from '@tanstack/react-query'; import { useVirtualizer } from '@tanstack/react-virtual'; import { useEffect, useRef } from 'react'; import { Link } from 'react-router-dom'; import { useNewsfeed } from '@app/space/hooks/useNewsfeed'; import { getNotes } from '@libs/storage'; import { Note } from '@shared/notes/note'; import { NoteSkeleton } from '@shared/notes/skeleton'; import { TitleBar } from '@shared/titleBar'; import { useNote } from '@stores/note'; const ITEM_PER_PAGE = 10; export function FollowingBlock() { // subscribe for live update useNewsfeed(); // notify user that new note is arrive const [hasNewNote, toggleHasNewNote] = useNote((state) => [ state.hasNewNote, state.toggleHasNewNote, ]); const { status, data, fetchNextPage, hasNextPage, isFetchingNextPage, refetch } = 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: any }) => 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 refreshFirstPage = () => { // refetch refetch({ refetchPage: (_, index: number) => index === 0 }); // scroll to top rowVirtualizer.scrollToIndex(1); // stop notify toggleHasNewNote(false); }; const renderItem = (index: string | number) => { const note = notes[index]; if (!note) return; return (
You not have any posts to see yet
Follow more people to have more fun.