import { useQuery } from '@tanstack/react-query'; import { BookOpen, Loader2 } from 'lucide-react'; import { useEffect, useState } from 'react'; import { deleteUrlParam, getUrlParams, setUrlParams } from '../../lib/browser'; import { isLoggedIn } from '../../lib/jwt'; import { showLoginPopup } from '../../lib/popup'; import { listUserAIGuidesOptions, type ListUserAIGuidesQuery, } from '../../queries/ai-guide'; import { queryClient } from '../../stores/query-client'; import { AITutorTallMessage } from '../AITutor/AITutorTallMessage'; import { UpgradeAccountModal } from '../Billing/UpgradeAccountModal'; import { Pagination } from '../Pagination/Pagination'; import { AICourseSearch } from '../GenerateCourse/AICourseSearch'; import { AIGuideCard } from '../AIGuide/AIGuideCard'; export function UserGuidesList() { const [isInitialLoading, setIsInitialLoading] = useState(true); const [showUpgradePopup, setShowUpgradePopup] = useState(false); const [pageState, setPageState] = useState({ perPage: '21', currPage: '1', query: '', }); const { data: userAiGuides, isFetching: isUserAiGuidesLoading } = useQuery( listUserAIGuidesOptions(pageState), queryClient, ); useEffect(() => { setIsInitialLoading(false); }, [userAiGuides]); const guides = userAiGuides?.data ?? []; useEffect(() => { const queryParams = getUrlParams(); setPageState({ ...pageState, currPage: queryParams?.p || '1', query: queryParams?.q || '', }); }, []); useEffect(() => { if (pageState?.currPage !== '1' || pageState?.query !== '') { setUrlParams({ p: pageState?.currPage || '1', q: pageState?.query || '', }); } else { deleteUrlParam('p'); deleteUrlParam('q'); } }, [pageState]); const isUserAuthenticated = isLoggedIn(); const isAnyLoading = isUserAiGuidesLoading || isInitialLoading; return ( <> {showUpgradePopup && ( setShowUpgradePopup(false)} /> )} { setPageState({ ...pageState, query: value, currPage: '1', }); }} disabled={isAnyLoading} placeholder="Search guides..." /> {isAnyLoading && (

Loading your guides...

)} {!isAnyLoading && ( <>

{isUserAuthenticated ? `You have generated ${userAiGuides?.totalCount} guides so far.` : 'Sign up or login to generate your first guide. Takes 2s to do so.'}

{isUserAuthenticated && !isAnyLoading && guides.length > 0 && (
{guides.map((guide) => ( ))}
{ setPageState({ ...pageState, currPage: String(page) }); }} className="rounded-lg border border-gray-200 bg-white p-4" />
)} {!isAnyLoading && guides.length === 0 && ( { if (isUserAuthenticated) { window.location.href = '/ai'; } else { showLoginPopup(); } }} /> )} )} ); }