import React, { useEffect, useState } from 'react'; import { Link } from 'react-router-dom'; import { CircleNotchIcon } from '@phosphor-icons/react'; import { useAuth } from '../context/AuthContext'; import { attachmentIcon, attachmentLabel, useMaterialAction } from '../components/course/Materials'; import { DownloadHistoryEntry, getDownloadHistory } from '../services/db'; import { DownloadMockIcon } from '../components/ui/MockupIcons'; import { useIsMobile } from '../hooks/useMediaQuery'; import { paths } from '../lib/routes'; const months = ['jan', 'fev', 'mar', 'abr', 'mai', 'jun', 'jul', 'ago', 'set', 'out', 'nov', 'dez']; const downloadedLabel = (value: string) => { const date = new Date(value); if (Number.isNaN(date.getTime())) return ''; const today = new Date(); const day = new Date(date.getFullYear(), date.getMonth(), date.getDate()); const current = new Date(today.getFullYear(), today.getMonth(), today.getDate()); const days = Math.round((current.getTime() - day.getTime()) / 86_400_000); if (days <= 0) return 'hoje'; if (days === 1) return 'ontem'; return `${date.getDate()} ${months[date.getMonth()]}`; }; export const DownloadsPage: React.FC = () => { const { user } = useAuth(); const action = useMaterialAction(); const isMobile = useIsMobile(); const [entries, setEntries] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { let cancelled = false; if (!user) { setEntries([]); setLoading(false); return; } setLoading(true); getDownloadHistory() .then((history) => { if (!cancelled) setEntries(history); }) .catch(() => { if (!cancelled) setEntries([]); }) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; }; }, [user?.id]); if (isMobile) return

Meus downloads

{loading ?

Carregando…

: entries.length ?
{entries.map(({ attachment, courseTitle, downloadedAt }) =>
{attachmentIcon(attachment.type)}{attachment.name}{courseTitle} · baixado {downloadedLabel(downloadedAt)}
)}
:

Seus downloads aparecerão aqui.

}
; return

Meus downloads

Arquivos que você já baixou das aulas.

Ver biblioteca
{loading ?
: entries.length ?
{entries.map(({ attachment, courseTitle, lessonNumber, downloadedAt }) =>
{attachmentIcon(attachment.type)}{attachment.name}{[courseTitle, lessonNumber ? `aula ${lessonNumber}` : '', attachmentLabel(attachment.type), attachment.size, `baixado ${downloadedLabel(downloadedAt)}`].filter(Boolean).join(' · ')}
)}
:

Seus downloads aparecerão aqui.

Explorar a biblioteca
}
; };