import React, { useState } from 'react'; import { Link } from 'react-router-dom'; import { ArrowSquareOutIcon, CheckIcon, CircleNotchIcon, FileIcon, ImageIcon, LinkSimpleIcon, VideoCameraIcon, } from '@phosphor-icons/react'; import { DocMockIcon, DownloadMockIcon, PdfMockIcon, XlsMockIcon, ZipMockIcon } from '../ui/MockupIcons'; import { Attachment, AttachmentType } from '../../types'; import { downloadCourseAsset } from '../../services/api'; import { recordDownloadHistory } from '../../services/db'; import { useToast } from '../../context/ToastContext'; import { useAuth } from '../../context/AuthContext'; import { paths } from '../../lib/routes'; export const attachmentIcon = (type: AttachmentType) => { switch (type) { case 'pdf': return ; case 'spreadsheet': return ; case 'zip': return ; case 'doc': return ; case 'image': return ; case 'link': return ; case 'video': return ; default: return ; } }; export const attachmentLabel = (type: AttachmentType) => ({ pdf: 'PDF', spreadsheet: 'Planilha', zip: 'ZIP', doc: 'Documento', image: 'Imagem', link: 'Link', video: 'Vídeo', }[type] || 'Arquivo'); /** Downloads through the Academy (or opens a link) with the same feedback as before. */ export const useMaterialAction = () => { const { showDownloadToast, showToast } = useToast(); const { user } = useAuth(); const [busyId, setBusyId] = useState(null); const [doneId, setDoneId] = useState(null); const open = async (attachment: Attachment) => { if (attachment.type === 'link') { showToast(`Abrindo “${attachment.name}”.`, 'info'); window.open(attachment.url, '_blank', 'noopener,noreferrer'); if (user) void recordDownloadHistory(attachment.id).catch(() => undefined); return false; } setBusyId(attachment.id); try { await downloadCourseAsset(attachment.id, attachment.name); showDownloadToast(attachment.name, attachment.size); if (user) void recordDownloadHistory(attachment.id).catch(() => undefined); setDoneId(attachment.id); window.setTimeout(() => setDoneId((current) => (current === attachment.id ? null : current)), 3000); return true; } catch (error) { showToast(error instanceof Error ? error.message : 'Não foi possível baixar este material.', 'error'); return false; } finally { setBusyId(null); } }; return { open, busyId, doneId }; }; export const MaterialButton: React.FC<{ attachment: Attachment; action: ReturnType; label?: boolean; className?: string }> = ({ attachment, action, label = true, className = 'btn sec' }) => { const { user } = useAuth(); if (!user) return label ? Entrar para baixar : null; const isLink = attachment.type === 'link'; const busy = action.busyId === attachment.id; const done = action.doneId === attachment.id; const icon = busy ? : done ? : isLink ? : ; const text = isLink ? 'Abrir' : done ? 'Baixado' : 'Baixar'; return ( ); }; export const MaterialRow: React.FC<{ attachment: Attachment; detail?: string; action: ReturnType; compact?: boolean }> = ({ attachment, detail, action, compact }) => (
{attachmentIcon(attachment.type)} {attachment.name}{[detail, attachmentLabel(attachment.type), attachment.size].filter(Boolean).join(' · ')} {compact ? : }
);