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 }) => ( ); 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 ( ); }; 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 = ({ course, variant, progress, className = '' }) => { const { toneOf } = useCatalog(); const image = useCoverImage(course.thumbnail); const showBar = progress !== undefined && progress > 0; return (
{image.show && } {!image.show && {course.title}} {showBar && }
); }; /** 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 ( ); }; export const CourseCard: React.FC<{ course: Course }> = ({ course }) => { const { user } = useAuth(); const image = useCoverImage(course.thumbnail); const progress = user ? course.progress || 0 : 0; return (
{image.show ? ( <>{course.title}{[course.instructor, courseMeta(course)].filter(Boolean).join(' · ')} ) : ( <>{course.instructor || course.category}{[course.category, courseMeta(course)].join(' · ')} )}
); }; /** "Continue assistindo": opens the next unfinished lesson directly. */ export const ContinueCard: React.FC<{ course: Course }> = ({ course }) => (
{course.title} {`${Math.round(course.progress || 0)}% concluído · ${course.lessons.length} aulas`}
); 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 (
{image.show && path.coverImageUrl && }
{!image.show && {path.title}} {[plural(path.courses.length, 'curso', 'cursos'), duration].filter(Boolean).join(' · ')}
{progress > 0 && }
{image.show &&
{path.title}
} ); };