Files
compor-academy/components/catalog/Artwork.tsx
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

141 lines
6.2 KiB
TypeScript

import React, { useState } from 'react';
import { Link } from 'react-router-dom';
import { Course } from '../../types';
import { PublicLearningPath } from '../../services/db';
import { CourseCoverImage } from '../CourseCoverImage';
import { useCatalog } from '../../context/CatalogContext';
import { useAuth } from '../../context/AuthContext';
import { courseMeta, hasRealCover, plural, totalDuration } from '../../lib/format';
import { paths } from '../../lib/routes';
type Framing = { zoom?: number; x?: number; y?: number };
/** A real cover (Bunny) replaces the colour block in the same format. */
export const ArtImage: React.FC<{ source: string; framing?: Framing; onMissing?: () => void }> = ({ source, framing, onMissing }) => (
<CourseCoverImage
source={source}
alt=""
loading="lazy"
draggable={false}
className="art-img"
onFinalError={onMissing}
style={{ transform: framing?.zoom && framing.zoom !== 1 ? `scale(${framing.zoom})` : undefined, objectPosition: `${framing?.x ?? 50}% ${framing?.y ?? 50}%` }}
/>
);
export const courseFraming = (course: Course): Framing => ({ zoom: course.coverImageZoom, x: course.coverImagePositionX, y: course.coverImagePositionY });
/** Hook that tells whether a course shows its uploaded image, falling back to colour if it fails to load. */
export const useCoverImage = (source?: string | null) => {
const [broken, setBroken] = useState(false);
const show = hasRealCover(source) && !broken;
return { show, onMissing: () => setBroken(true) };
};
export const Progress: React.FC<{ value: number; inside?: boolean }> = ({ value, inside }) => {
const pct = Math.max(0, Math.min(100, Math.round(value)));
return (
<span className={inside ? 'inprog' : 'prog'} role="progressbar" aria-label={`${pct}% concluído`} aria-valuenow={pct} aria-valuemin={0} aria-valuemax={100} style={inside ? undefined : { display: 'block' }}>
<i style={{ width: `${pct}%` }} />
</span>
);
};
interface CoverProps {
course: Course;
variant?: 'big' | 'mini';
progress?: number;
className?: string;
}
/** Course artwork: 16:10 block in the area colour with the title, or the uploaded image. */
export const Cover: React.FC<CoverProps> = ({ course, variant, progress, className = '' }) => {
const { toneOf } = useCatalog();
const image = useCoverImage(course.thumbnail);
const showBar = progress !== undefined && progress > 0;
return (
<div className={`cover tr-${toneOf(course.category)}${variant ? ` ${variant}` : ''}${showBar ? ' hp' : ''}${image.show ? ' has-img' : ''} ${className}`}>
{image.show && <ArtImage source={course.thumbnail} framing={courseFraming(course)} onMissing={image.onMissing} />}
{!image.show && <span className="t">{course.title}</span>}
{showBar && <Progress value={progress} inside />}
</div>
);
};
/** Small colour/image chip used next to titles in lists. */
export const Thumb: React.FC<{ course: Course; className?: string }> = ({ course, className = 'thumb' }) => {
const { toneOf } = useCatalog();
const image = useCoverImage(course.thumbnail);
return (
<span className={`${className} tr-${toneOf(course.category)}`} aria-hidden="true">
{image.show && <ArtImage source={course.thumbnail} framing={courseFraming(course)} onMissing={image.onMissing} />}
</span>
);
};
export const CourseCard: React.FC<{ course: Course }> = ({ course }) => {
const { user } = useAuth();
const image = useCoverImage(course.thumbnail);
const progress = user ? course.progress || 0 : 0;
return (
<Link className="card" to={paths.course(course.id)}>
<Cover course={course} progress={progress} />
<div className="m">
{image.show ? (
<><b>{course.title}</b><span className="num one">{[course.instructor, courseMeta(course)].filter(Boolean).join(' · ')}</span></>
) : (
<><b>{course.instructor || course.category}</b><span className="num one">{[course.category, courseMeta(course)].join(' · ')}</span></>
)}
</div>
</Link>
);
};
/** "Continue assistindo": opens the next unfinished lesson directly. */
export const ContinueCard: React.FC<{ course: Course }> = ({ course }) => (
<Link className="card" to={paths.continueCourse(course.id)}>
<Cover course={course} progress={course.progress || 0} />
<div className="m">
<b>{course.title}</b>
<span className="num one">{`${Math.round(course.progress || 0)}% concluído · ${course.lessons.length} aulas`}</span>
</div>
</Link>
);
export const pathCourses = (path: PublicLearningPath, courseById: (id: string) => Course | undefined) => path.courses
.slice()
.sort((a, b) => a.position - b.position)
.map((item) => courseById(item.id))
.filter((course): course is Course => Boolean(course));
export const pathProgress = (courses: Course[]) => {
const lessons = courses.reduce((sum, course) => sum + course.lessons.length, 0);
if (!lessons) return 0;
const seen = courses.reduce((sum, course) => sum + ((course.progress || 0) / 100) * course.lessons.length, 0);
return (seen / lessons) * 100;
};
/** Trilha: 2:3 poster with two cards stacked behind it. */
export const TrailPoster: React.FC<{ path: PublicLearningPath }> = ({ path }) => {
const { pathTone, courseById } = useCatalog();
const { user } = useAuth();
const image = useCoverImage(path.coverImageUrl);
const tone = pathTone(path);
const courses = pathCourses(path, courseById);
const duration = totalDuration(courses);
const progress = user ? pathProgress(courses) : 0;
return (
<Link className={`poster-card tr-${tone}`} to={paths.trail(path.id)}>
<div className={`poster tr-${tone}${image.show ? ' has-img' : ''}`}>
{image.show && path.coverImageUrl && <ArtImage source={path.coverImageUrl} framing={{ zoom: path.coverImageZoom, x: path.coverImagePositionX, y: path.coverImagePositionY }} onMissing={image.onMissing} />}
<div>
{!image.show && <span className="t">{path.title}</span>}
<span className="n num">{[plural(path.courses.length, 'curso', 'cursos'), duration].filter(Boolean).join(' · ')}</span>
</div>
{progress > 0 && <Progress value={progress} inside />}
</div>
{image.show && <div className="card"><div className="m"><b>{path.title}</b></div></div>}
</Link>
);
};