Files
compor-academy/lib/downloads.ts
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

15 lines
1000 B
TypeScript

import { Attachment } from '../types';
export type DownloadEntry = { attachment: Attachment; courseId: string; courseTitle: string; downloadedAt: string };
const keyFor = (userId: string) => `ca-downloads:${userId}`;
export const readDownloads = (userId?: string): DownloadEntry[] => {
if (!userId) return [];
try { const value = JSON.parse(localStorage.getItem(keyFor(userId)) || '[]'); return Array.isArray(value) ? value : []; }
catch { return []; }
};
export const recordDownload = (userId: string, attachment: Attachment, courseId: string, courseTitle: string) => {
const entry: DownloadEntry = { attachment, courseId, courseTitle, downloadedAt: new Date().toISOString() };
const next = [entry, ...readDownloads(userId).filter((item) => item.attachment.id !== attachment.id)].slice(0, 100);
try { localStorage.setItem(keyFor(userId), JSON.stringify(next)); window.dispatchEvent(new Event('ca-downloads')); } catch { /* A completed download does not depend on local history. */ }
};