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.
87 lines
4.2 KiB
TypeScript
87 lines
4.2 KiB
TypeScript
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 <PdfMockIcon className="ph" />;
|
|
case 'spreadsheet': return <XlsMockIcon className="ph" />;
|
|
case 'zip': return <ZipMockIcon className="ph" />;
|
|
case 'doc': return <DocMockIcon className="ph" />;
|
|
case 'image': return <ImageIcon className="ph" />;
|
|
case 'link': return <LinkSimpleIcon className="ph" />;
|
|
case 'video': return <VideoCameraIcon className="ph" />;
|
|
default: return <FileIcon className="ph" />;
|
|
}
|
|
};
|
|
|
|
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<string | null>(null);
|
|
const [doneId, setDoneId] = useState<string | null>(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<typeof useMaterialAction>; label?: boolean; className?: string }> = ({ attachment, action, label = true, className = 'btn sec' }) => {
|
|
const { user } = useAuth();
|
|
if (!user) return label ? <Link className={className} to={paths.signIn}>Entrar para baixar</Link> : null;
|
|
const isLink = attachment.type === 'link';
|
|
const busy = action.busyId === attachment.id;
|
|
const done = action.doneId === attachment.id;
|
|
const icon = busy ? <CircleNotchIcon className="ph spin" /> : done ? <CheckIcon className="ph" /> : isLink ? <ArrowSquareOutIcon className="ph" /> : <DownloadMockIcon className="ph" />;
|
|
const text = isLink ? 'Abrir' : done ? 'Baixado' : 'Baixar';
|
|
return (
|
|
<button type="button" className={className} onClick={() => void action.open(attachment)} disabled={busy} aria-label={`${text} ${attachment.name}`}>
|
|
{icon}{label && ` ${text}`}
|
|
</button>
|
|
);
|
|
};
|
|
|
|
export const MaterialRow: React.FC<{ attachment: Attachment; detail?: string; action: ReturnType<typeof useMaterialAction>; compact?: boolean }> = ({ attachment, detail, action, compact }) => (
|
|
<div className="file" style={compact ? { paddingBlock: 8 } : undefined}>
|
|
<span className="ic">{attachmentIcon(attachment.type)}</span>
|
|
<span>{attachment.name}<small className="num">{[detail, attachmentLabel(attachment.type), attachment.size].filter(Boolean).join(' · ')}</small></span>
|
|
{compact ? <MaterialButton attachment={attachment} action={action} label={false} className="iconbtn" /> : <MaterialButton attachment={attachment} action={action} />}
|
|
</div>
|
|
);
|