feat: simplify materials library and use real metadata
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user