diff --git a/app/components/Feed.tsx b/app/components/Feed.tsx index 6093607..cd4157b 100644 --- a/app/components/Feed.tsx +++ b/app/components/Feed.tsx @@ -24,39 +24,52 @@ const Feed = ({ const [selectedFilter, setSelectedFilter] = useState<(typeof filters)[number]>("latest"); const [currentPage, setCurrentPage] = useState(0); + const [initialLoad, setIsInitialLoad] = useState(true); const fetcher = useFetcher(); const Icon = ICON_DICT[icon]; useEffect(() => { - if (currentPage !== 0) { + if (!initialLoad) { // Fetch data for subsequent pages fetcher.load(`/api/feed?filter=${selectedFilter}&page=${currentPage}`); } - }, [selectedFilter, currentPage]); + }, [initialLoad, selectedFilter, currentPage]); useEffect(() => { - if (fetcher.data && currentPage !== 0) { + if (fetcher.data) { setContent((prevContent) => [...prevContent, ...fetcher.data.data]); } - }, [fetcher.data, currentPage]); + }, [initialLoad, fetcher.data]); const handleFilterChange = (filter: (typeof filters)[number]) => { + setIsInitialLoad(false); + setContent([]); setSelectedFilter(filter); setCurrentPage(0); }; const handleLoadMore = () => { + setIsInitialLoad(false); setCurrentPage((prevPage) => prevPage + 1); }; - if (currentPage) { - if (fetcher.state === "loading") { - return
Loading...
; - } + let activeContent = content; - if (!fetcher.data) { - return
Failed to load
; + if (!initialLoad) { + if (fetcher.state === "loading") { + activeContent = [ + { + createdAt: new Date(), + updatedAt: new Date(), + title: "Loading", + id: 0, + slug: "loading", + cid: "", + url: "", + siteKey: "", + }, + ]; } } @@ -90,7 +103,7 @@ const Feed = ({ >
- {content.map((item, index) => { + {activeContent.map((item, index) => { return (