feat: simplify materials library and use real metadata
All checks were successful
CI / Validate frontend and API (push) Successful in 34s
CI / Build and publish Docker images (push) Successful in 21s

This commit is contained in:
Cauê Faleiros
2026-09-08 15:22:45 -03:00
parent 54f598cd99
commit 10c3d03d9c
6 changed files with 113 additions and 942 deletions

View File

@@ -395,13 +395,23 @@ const CourseModal: React.FC<{
alert("Informe o nome do arquivo para download.");
return;
}
if (!newAttUrl.trim()) {
alert('Informe a URL do material.');
return;
}
try {
new URL(newAttUrl.trim());
} catch {
alert('Informe uma URL válida para o material.');
return;
}
const att: Attachment = {
id: `att-${Date.now()}`,
name: newAttName.trim(),
type: newAttType,
url: newAttUrl.trim() || '#',
size: newAttSize.trim() || '1.5 MB',
url: newAttUrl.trim(),
size: newAttSize.trim() || undefined,
description: newAttDesc.trim()
};
@@ -679,12 +689,21 @@ const CourseModal: React.FC<{
</select>
</div>
<div className="flex gap-2">
<input
type="url"
value={newAttUrl}
onChange={(e) => setNewAttUrl(e.target.value)}
placeholder="URL do material (https://...)"
className="min-w-0 flex-1 bg-white/5 border border-white/10 rounded-lg px-2.5 py-1.5 text-xs text-white focus:outline-none"
/>
</div>
<div className="flex gap-2">
<input
type="text"
value={newAttSize}
onChange={(e) => setNewAttSize(e.target.value)}
placeholder="Ex.: 2,4 MB"
placeholder="Tamanho (opcional)"
className="w-28 bg-white/5 border border-white/10 rounded-lg px-2.5 py-1.5 text-xs text-white focus:outline-none"
/>
<input

File diff suppressed because it is too large Load Diff

View File

@@ -49,6 +49,8 @@ export const courseSelect = (publicOnly = false, includeUnreadyMedia = false) =>
'name', a.name,
'kind', a.kind,
'url', a.url,
'sizeBytes', a.size_bytes,
'createdAt', a.created_at,
'description', a.description,
'accessLevel', a.access_level
) order by a.created_at)
@@ -81,6 +83,8 @@ export const courseSelect = (publicOnly = false, includeUnreadyMedia = false) =>
'name', a.name,
'kind', a.kind,
'url', a.url,
'sizeBytes', a.size_bytes,
'createdAt', a.created_at,
'description', a.description,
'accessLevel', a.access_level
) order by a.created_at)

View File

@@ -20,6 +20,7 @@ const assetSchema = z.object({
name: z.string().trim().min(1).max(255),
kind: z.enum(['document', 'spreadsheet', 'archive', 'image', 'link']),
url: z.string().url(),
sizeBytes: z.number().int().nonnegative().nullable().optional(),
description: z.string().max(2000).default(''),
accessLevel: z.enum(['public', 'enrolled']).default('enrolled'),
});
@@ -85,9 +86,9 @@ async function uniqueSlug(client: PoolClient, requestedSlug: string | undefined,
async function insertAssets(client: PoolClient, parent: { courseId?: string; lessonId?: string }, assets: CourseInput['assets']) {
for (const asset of assets) {
await client.query(
`insert into assets (course_id, lesson_id, name, kind, url, description, access_level)
values ($1, $2, $3, $4, $5, $6, $7)`,
[parent.courseId ?? null, parent.lessonId ?? null, asset.name, asset.kind, asset.url, asset.description, asset.accessLevel],
`insert into assets (course_id, lesson_id, name, kind, url, size_bytes, description, access_level)
values ($1, $2, $3, $4, $5, $6, $7, $8)`,
[parent.courseId ?? null, parent.lessonId ?? null, asset.name, asset.kind, asset.url, asset.sizeBytes ?? null, asset.description, asset.accessLevel],
);
}
}

View File

@@ -8,6 +8,8 @@ type ApiAsset = {
name: string;
kind: 'document' | 'spreadsheet' | 'archive' | 'image' | 'link';
url: string;
sizeBytes: number | null;
createdAt: string;
description: string;
};
@@ -88,9 +90,19 @@ const toAttachment = (asset: ApiAsset): Attachment => ({
name: asset.name,
type: toAttachmentType(asset.kind),
url: asset.url,
size: asset.sizeBytes === null ? undefined : formatBytes(asset.sizeBytes),
sizeBytes: asset.sizeBytes ?? undefined,
createdAt: asset.createdAt,
description: asset.description,
});
const formatBytes = (bytes: number) => {
if (bytes < 1024) return `${bytes} B`;
const units = ['KB', 'MB', 'GB'];
const index = Math.min(Math.floor(Math.log(bytes) / Math.log(1024)) - 1, units.length - 1);
return `${new Intl.NumberFormat('pt-BR', { maximumFractionDigits: 1 }).format(bytes / 1024 ** (index + 1))} ${units[index]}`;
};
const toDuration = (seconds: number | null) => {
if (!seconds || seconds < 1) return '—';
const totalSeconds = Math.round(seconds);
@@ -166,10 +178,19 @@ const toAssetPayload = (attachment: Attachment) => ({
name: attachment.name,
kind: toAssetKind(attachment.type),
url: attachment.url,
sizeBytes: attachment.sizeBytes ?? parseSizeBytes(attachment.size),
description: attachment.description || '',
accessLevel: 'enrolled' as const,
});
const parseSizeBytes = (value?: string) => {
if (!value) return null;
const match = value.trim().replace(',', '.').match(/^(\d+(?:\.\d+)?)\s*(B|KB|MB|GB)$/i);
if (!match) return null;
const units: Record<string, number> = { B: 1, KB: 1024, MB: 1024 ** 2, GB: 1024 ** 3 };
return Math.round(Number(match[1]) * units[match[2].toUpperCase()]);
};
const toCoursePayload = (course: Course) => ({
title: course.title,
description: course.description,

View File

@@ -8,6 +8,8 @@ export interface Attachment {
type: AttachmentType;
url: string;
size?: string; // e.g. "4.2 MB"
sizeBytes?: number;
createdAt?: string;
description?: string;
lessonId?: string; // Associated lesson or global course asset
downloadCount?: number;