import { useEffect, useState } from 'preact/hooks'; import { httpGet } from '../../lib/http'; import { ActivityCounters } from './ActivityCounters'; import { ResourceProgress } from './ResourceProgress'; import { pageProgressMessage } from '../../stores/page'; import { EmptyActivity } from './EmptyActivity'; export type ActivityResponse = { done: { today: number; total: number; }; learning: { today: number; total: number; roadmaps: { title: string; id: string; learning: number; done: number; total: number; skipped: number; updatedAt: string; }[]; bestPractices: { title: string; id: string; learning: number; done: number; skipped: number; total: number; updatedAt: string; }[]; }; streak: { count: number; firstVisitAt: Date | null; lastVisitAt: Date | null; }; activity: { type: 'done' | 'learning' | 'pending' | 'skipped'; createdAt: Date; metadata: { resourceId?: string; resourceType?: 'roadmap' | 'best-practice'; topicId?: string; topicLabel?: string; resourceTitle?: string; }; }[]; }; export function ActivityPage() { const [activity, setActivity] = useState(); const [isLoading, setIsLoading] = useState(true); async function loadActivity() { const { error, response } = await httpGet( `${import.meta.env.PUBLIC_API_URL}/v1-get-user-stats` ); if (!response || error) { console.error('Error loading activity'); console.error(error); return; } setActivity(response); } useEffect(() => { loadActivity().finally(() => { pageProgressMessage.set(''); setIsLoading(false); }); }, []); const learningRoadmaps = activity?.learning.roadmaps || []; const learningBestPractices = activity?.learning.bestPractices || []; if (isLoading) { return null; } return ( <>
{learningRoadmaps.length === 0 && learningBestPractices.length === 0 && } {(learningRoadmaps.length > 0 || learningBestPractices.length > 0) && ( <>

Continue Following

{learningRoadmaps .sort((a, b) => { const updatedAtA = new Date(a.updatedAt); const updatedAtB = new Date(b.updatedAt); return updatedAtB.getTime() - updatedAtA.getTime(); }) .map((roadmap) => ( { pageProgressMessage.set('Updating activity'); loadActivity().finally(() => { pageProgressMessage.set(''); }); }} /> ))} {learningBestPractices .sort((a, b) => { const updatedAtA = new Date(a.updatedAt); const updatedAtB = new Date(b.updatedAt); return updatedAtB.getTime() - updatedAtA.getTime(); }) .map((bestPractice) => ( { pageProgressMessage.set('Updating activity'); loadActivity().finally(() => { pageProgressMessage.set(''); }); }} /> ))}
)}
); }