"use client"; import { formatDate } from "@/utils"; import * as ScrollArea from "@radix-ui/react-scroll-area"; import { useEffect, useState } from "react"; import { Article } from "@/lib/prisma"; import { Link, useFetcher } from "@remix-run/react"; const Feed = ({ className, variant = "col", icon, title, initialData, }: { className?: string; variant?: "row" | "col"; title: string; icon: keyof typeof ICON_DICT; initialData: Article[]; }) => { const filters = ["latest", "day", "week", "month"] as const; const [content, setContent] = useState>(initialData); 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 (!initialLoad) { // Fetch data for subsequent pages fetcher.load(`/api/feed?filter=${selectedFilter}&page=${currentPage}`); } }, [initialLoad, selectedFilter, currentPage]); useEffect(() => { if (fetcher.data) { setContent((prevContent) => [...prevContent, ...fetcher.data.data]); } }, [initialLoad, fetcher.data]); const handleFilterChange = (filter: (typeof filters)[number]) => { setIsInitialLoad(false); setContent([]); setSelectedFilter(filter); setCurrentPage(0); }; const handleLoadMore = () => { setIsInitialLoad(false); setCurrentPage((prevPage) => prevPage + 1); }; let activeContent = content; if (!initialLoad) { if (fetcher.state === "loading") { activeContent = [ { createdAt: new Date(), updatedAt: new Date(), title: "Loading", id: 0, cid: "", url: "", site: "", }, ]; } } return (
{activeContent.map((item, index) => { return (
{formatDate(item.createdAt)}

{item.title}

); })} {(fetcher.data as any)?.next ? ( ) : null}
); }; export default Feed; const PaperIcon = ({ className }: { className?: string }) => { return ( ); }; const TopArrowLodashIcon = ({ className }: { className?: string }) => { return ( ); }; const TrendUpIcon = ({ className }: { className?: string }) => { return ( ); }; const ICON_DICT = { "paper-icon": PaperIcon, "trend-up-icon": TrendUpIcon, "top-arrow-icon": TopArrowLodashIcon, } as const;