import { useState } from 'preact/hooks'; import { httpPost } from '../../lib/http'; import { getRelativeTimeString } from '../../lib/date'; import { useToast } from '../../hooks/use-toast'; type ResourceProgressType = { resourceType: 'roadmap' | 'best-practice'; resourceId: string; title: string; updatedAt: string; totalCount: number; doneCount: number; learningCount: number; skippedCount: number; onCleared?: () => void; showClearButton?: boolean; }; export function ResourceProgress(props: ResourceProgressType) { const { showClearButton = true } = props; const toast = useToast(); const [isClearing, setIsClearing] = useState(false); const [isConfirming, setIsConfirming] = useState(false); const { updatedAt, resourceType, resourceId, title, totalCount, learningCount, doneCount, skippedCount, onCleared, } = props; async function clearProgress() { setIsClearing(true); const { error, response } = await httpPost( `${import.meta.env.PUBLIC_API_URL}/v1-clear-resource-progress`, { resourceId, resourceType, } ); if (error || !response) { toast.error('Error clearing progress. Please try again.'); console.error(error); setIsClearing(false); return; } localStorage.removeItem(`${resourceType}-${resourceId}-favorite`); localStorage.removeItem(`${resourceType}-${resourceId}-progress`); setIsClearing(false); setIsConfirming(false); if (onCleared) { onCleared(); } } const url = resourceType === 'roadmap' ? `/${resourceId}` : `/best-practices/${resourceId}`; const totalMarked = doneCount + skippedCount; const progressPercentage = Math.round((totalMarked / totalCount) * 100); return (
{doneCount > 0 && ( <> {doneCount} done • > )} {learningCount > 0 && ( <> {learningCount} in progress • > )} {skippedCount > 0 && ( <> {skippedCount} skipped • > )} {totalCount} total {showClearButton && ( <> {!isConfirming && ( )} {isConfirming && ( Are you sure?{' '} {' '} )} > )}