import { useCallback, useEffect, useRef, useState } from "react"; import { Activity, ActivityType } from "../network/models.ts"; import { network } from "../network/network.ts"; import showToast from "../components/toast.ts"; import { useTranslation } from "react-i18next"; import { MdArrowRight } from "react-icons/md"; import { useNavigate } from "react-router"; import Loading from "../components/loading.tsx"; import { ImageGrid } from "../components/image.tsx"; import { CommentContent } from "../components/comment_tile.tsx"; export default function ActivitiesPage() { const [activities, setActivities] = useState([]); const pageRef = useRef(0); const maxPageRef = useRef(1); const isLoadingRef = useRef(false); const fetchNextPage = useCallback(async () => { if (isLoadingRef.current || pageRef.current >= maxPageRef.current) return; isLoadingRef.current = true; const response = await network.getActivities(pageRef.current + 1); if (response.success) { setActivities((prev) => [...prev, ...response.data!]); pageRef.current += 1; maxPageRef.current = response.totalPages!; } else { showToast({ type: "error", message: response.message || "Failed to load activities", }); } isLoadingRef.current = false; }, []); useEffect(() => { fetchNextPage(); }, [fetchNextPage]); useEffect(() => { const handleScroll = () => { if ( window.innerHeight + window.scrollY >= document.documentElement.scrollHeight - 100 && !isLoadingRef.current && pageRef.current < maxPageRef.current ) { fetchNextPage(); } }; window.addEventListener("scroll", handleScroll); return () => window.removeEventListener("scroll", handleScroll); }, [fetchNextPage]); return (
{activities.map((activity) => ( ))} {pageRef.current < maxPageRef.current && }
); } function ActivityCard({ activity }: { activity: Activity }) { const { t } = useTranslation(); const messages = [ "Unknown activity", t("Published a resource"), t("Updated a resource"), t("Posted a comment"), ]; const navigate = useNavigate(); let content = <>; if ( activity.type === ActivityType.ResourcePublished || activity.type === ActivityType.ResourceUpdated ) { content = (
{activity.resource?.title}
{activity.resource?.image && (
{activity.resource.title}
)}
); } else if (activity.type === ActivityType.NewComment) { content = (
); } return (
{ if ( activity.type === ActivityType.ResourcePublished || activity.type === ActivityType.ResourceUpdated ) { navigate(`/resources/${activity.resource?.id}`); } else if (activity.type === ActivityType.NewComment) { navigate(`/comments/${activity.comment?.id}`); } }} >
{"avatar"}
{activity.user?.username} {messages[activity.type]}
{content}
); }