Files
compor-academy/pages/DownloadsPage.tsx
Cauê Faleiros 21085e887d
All checks were successful
CI / Validate frontend and API (push) Successful in 2m22s
CI / Build and publish Docker images (push) Successful in 1m6s
feat: finalize academy redesign and production readiness
Align admin, community, search, library, learning and course management with the mockup. Add saved courses, download history, public community feed, instructor directory, backend search, course views, admin content tools and community moderation. Harden API and Nginx for production.
2026-11-05 15:06:53 -03:00

52 lines
3.9 KiB
TypeScript

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<DownloadHistoryEntry[]>([]);
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 <main className="m-page m-gut enter">
<header className="m-large"><h1>Meus downloads</h1></header>
{loading ? <p className="m-empty">Carregando…</p> : entries.length ? <section className="m-group"><div className="m-list">{entries.map(({ attachment, courseTitle, downloadedAt }) => <div className="m-row" key={attachment.id}><span className="m-ic">{attachmentIcon(attachment.type)}</span><span className="m-rt"><b>{attachment.name}</b><small>{courseTitle} · baixado {downloadedLabel(downloadedAt)}</small></span><button className="m-icb" type="button" onClick={() => void action.open(attachment)} aria-label={`Baixar ${attachment.name} novamente`} disabled={action.busyId === attachment.id}><DownloadMockIcon className="ph" /></button></div>)}</div></section> : <p className="m-empty">Seus downloads aparecerão aqui.</p>}
</main>;
return <main className="wrap page enter">
<header className="ptitle"><div><h1>Meus downloads</h1><p>Arquivos que você já baixou das aulas.</p></div><Link className="btn sec" to={paths.library}>Ver biblioteca</Link></header>
{loading ? <div className="loading"><CircleNotchIcon className="ph spin" /></div> : entries.length ? <div className="list">{entries.map(({ attachment, courseTitle, lessonNumber, downloadedAt }) => <div className="file" key={attachment.id} style={{ padding: '14px 4px' }}><span className="ic">{attachmentIcon(attachment.type)}</span><span>{attachment.name}<small className="num">{[courseTitle, lessonNumber ? `aula ${lessonNumber}` : '', attachmentLabel(attachment.type), attachment.size, `baixado ${downloadedLabel(downloadedAt)}`].filter(Boolean).join(' · ')}</small></span><button type="button" className="btn sec" onClick={() => void action.open(attachment)} disabled={action.busyId === attachment.id}><DownloadMockIcon className="ph" /> {attachment.type === 'link' ? 'Abrir de novo' : 'Baixar de novo'}</button></div>)}</div> : <div className="empty"><DownloadMockIcon className="ph" style={{ fontSize: 24, marginBottom: 8 }} /><p>Seus downloads aparecerão aqui.</p><Link className="link" to={paths.library}>Explorar a biblioteca</Link></div>}
</main>;
};