import { useCallback, useEffect, useState } from 'react'; import { useToast } from '../../hooks/use-toast'; import { httpGet } from '../../lib/http'; import { getRelativeTimeString } from '../../lib/date'; import { Eye, Loader2, RefreshCcw } from 'lucide-react'; import { AIRoadmapAlert } from '../GenerateRoadmap/AIRoadmapAlert.tsx'; export interface AIRoadmapDocument { _id?: string; term: string; title: string; data: string; viewCount: number; createdAt: Date; updatedAt: Date; } type ExploreRoadmapsResponse = { data: AIRoadmapDocument[]; totalCount: number; totalPages: number; currPage: number; perPage: number; }; export function ExploreAIRoadmap() { const toast = useToast(); const [isLoading, setIsLoading] = useState(true); const [isLoadingMore, setIsLoadingMore] = useState(false); const [roadmaps, setRoadmaps] = useState([]); const [currPage, setCurrPage] = useState(1); const [totalPages, setTotalPages] = useState(1); const loadAIRoadmaps = useCallback( async (currPage: number) => { const { response, error } = await httpGet( `${import.meta.env.PUBLIC_API_URL}/v1-list-ai-roadmaps`, { currPage, }, ); if (error || !response) { toast.error(error?.message || 'Something went wrong'); return; } const newRoadmaps = [...roadmaps, ...response.data]; if ( JSON.stringify(roadmaps) === JSON.stringify(response.data) || JSON.stringify(roadmaps) === JSON.stringify(newRoadmaps) ) { return; } setRoadmaps(newRoadmaps); setCurrPage(response.currPage); setTotalPages(response.totalPages); }, [currPage, roadmaps], ); useEffect(() => { loadAIRoadmaps(currPage).finally(() => { setIsLoading(false); }); }, []); const hasMorePages = currPage < totalPages; return (
{isLoading ? (
    {new Array(21).fill(0).map((_, index) => (
  • ))}
) : (
{roadmaps?.length === 0 ? (
No roadmaps found
) : ( <> {hasMorePages && (
)} )}
)}
); }