Files
compor-academy/pages/CoursePage.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

162 lines
11 KiB
TypeScript

import React from 'react';
import { Link, Navigate, useParams } from 'react-router-dom';
import { BookmarkSimpleIcon, CaretRightIcon, CheckIcon, CircleNotchIcon, PlayIcon } from '@phosphor-icons/react';
import { Attachment, Course } from '../types';
import { useCourse, useLessonProgress } from '../hooks/useCourse';
import { useAuth } from '../context/AuthContext';
import { useCatalog } from '../context/CatalogContext';
import { ArtImage, Cover, courseFraming, useCoverImage } from '../components/catalog/Artwork';
import { MaterialRow, useMaterialAction } from '../components/course/Materials';
import { Avatar } from '../components/ui/Avatar';
import { courseMeta, lessonDuration, plural } from '../lib/format';
import { paths } from '../lib/routes';
import { useSavedCourses } from '../hooks/useSavedCourses';
import { useIsMobile } from '../hooks/useMediaQuery';
import { MaterialButton, attachmentIcon } from '../components/course/Materials';
import { useState } from 'react';
export type CourseMaterial = { attachment: Attachment; detail: string; lessonIndex?: number };
export const courseMaterials = (course: Course): CourseMaterial[] => [
...(course.attachments || []).map((attachment) => ({ attachment, detail: 'Material do curso' })),
...course.lessons.flatMap((lesson, index) => (lesson.attachments || []).map((attachment) => ({ attachment, detail: `Aula ${index + 1}`, lessonIndex: index }))),
];
export const signUpState = { message: 'Crie uma conta gratuita para assistir a esta aula.' };
const PlayerPreview: React.FC<{ course: Course; to: string }> = ({ course, to }) => {
const image = useCoverImage(course.thumbnail);
const first = course.lessons[0];
return (
<Link className="player" to={to} state={to === paths.signUp ? signUpState : undefined} aria-label={first ? `Assistir aula 1: ${first.title}` : 'Assistir'}>
{image.show && <ArtImage source={course.thumbnail} framing={courseFraming(course)} onMissing={image.onMissing} />}
<div className="pl"><span className="playbtn"><PlayIcon className="ph" weight="fill" /></span></div>
{first && <div className="ctl num"><span>Aula 1 · {first.title}</span><span style={{ flex: 1 }} /><span>{lessonDuration(first)}</span></div>}
</Link>
);
};
export const CoursePage: React.FC = () => {
const { courseId } = useParams<{ courseId: string }>();
const { course, isLoading, missing } = useCourse(courseId);
const { user } = useAuth();
const { paths: trails } = useCatalog();
const progress = useLessonProgress(course);
const action = useMaterialAction();
const saved = useSavedCourses();
const isMobile = useIsMobile();
const [mobileTab, setMobileTab] = useState<'aulas' | 'materiais' | 'sobre'>('aulas');
if (isLoading) return <main className="wrap page"><div className="loading"><CircleNotchIcon className="ph spin" /></div></main>;
if (missing || !course) return <main className="wrap page"><p className="empty">Este curso não está disponível.</p></main>;
const trail = trails.find((item) => item.courses.some((entry) => entry.id === course.id));
const materials = courseMaterials(course);
const next = progress.next;
const watchTo = (lessonId?: string) => (user && lessonId ? paths.lesson(course.id, lessonId) : paths.signUp);
const started = progress.doneCount > 0;
if (isMobile) return <main className="m-page has-cta enter">
<div className="m-gut">
<Cover course={course} progress={user ? Math.round(progress.doneCount / Math.max(course.lessons.length, 1) * 100) : undefined} className="m-cover" />
<header className="m-title"><h1>{course.title}</h1><p className="num">{[course.instructor, courseMeta(course)].filter(Boolean).join(' · ')}</p></header>
{user && progress.doneCount > 0 && <div className="m-progress"><span className="prog"><i style={{ width: `${Math.round(progress.doneCount / Math.max(course.lessons.length, 1) * 100)}%` }} /></span><span>{progress.doneCount} de {course.lessons.length} aulas</span></div>}
<div className="m-seg" role="tablist">{([['aulas', 'Aulas'], ['materiais', 'Materiais'], ['sobre', 'Sobre']] as const).map(([key, label]) => <button type="button" role="tab" key={key} aria-selected={mobileTab === key} onClick={() => setMobileTab(key)}>{label}</button>)}</div>
<div className="m-pane">
{mobileTab === 'aulas' && <section className="m-group"><div className="m-list">{course.lessons.map((lesson, index) => <Link className="m-row" key={lesson.id} to={watchTo(lesson.id)} state={user ? undefined : signUpState}><span className="m-n num">{progress.completed.has(lesson.id) ? <CheckIcon className="ph" /> : index + 1}</span><span className="m-rt"><b>{lesson.title}</b><small>{lessonDuration(lesson)}</small></span><CaretRightIcon className="m-chev" /></Link>)}</div></section>}
{mobileTab === 'materiais' && <section className="m-group"><div className="m-list">{materials.map((item) => <div className="m-row" key={item.attachment.id}><span className="m-ic">{attachmentIcon(item.attachment.type)}</span><span className="m-rt"><b>{item.attachment.name}</b><small>{item.detail} · {item.attachment.size}</small></span><MaterialButton attachment={item.attachment} action={action} label={false} className="m-icb" /></div>)}</div>{!materials.length && <p className="m-empty">Este curso não tem materiais para baixar.</p>}</section>}
{mobileTab === 'sobre' && <><p className="m-body">{course.description}</p>{course.instructorId && <section className="m-group"><h2>Professor</h2><div className="m-list"><Link className="m-row" to={paths.professor(course.instructorId)}><Avatar name={course.instructor || 'Professor'} size="sm" /><span className="m-rt"><b>{course.instructor}</b></span><CaretRightIcon className="m-chev" /></Link></div></section>}{trail && <section className="m-group"><h2>Trilha</h2><div className="m-list"><Link className="m-row" to={paths.trail(trail.id)}><span className="m-rt"><b>{trail.title}</b></span><CaretRightIcon className="m-chev" /></Link></div></section>}</>}
</div>
</div>
<div className="m-cta">{user && <button type="button" className="btn sec m-half save-icon" onClick={() => saved.toggle(course.id)} aria-label={saved.isSaved(course.id) ? 'Remover dos salvos' : 'Salvar curso'} aria-pressed={saved.isSaved(course.id)}><BookmarkSimpleIcon className="ph" weight={saved.isSaved(course.id) ? 'fill' : 'regular'} /></button>}<Link className="btn m-big" to={user && next ? paths.lesson(course.id, next.id) : paths.signUp} state={user ? undefined : signUpState}>{user && started ? `Continuar aula ${progress.nextIndex + 1}` : user ? 'Assistir' : 'Criar conta para assistir'}</Link></div>
</main>;
return (
<main className="wrap page enter">
<div className="crumb">
<Link to={paths.explore}>Explorar</Link><CaretRightIcon className="ph" weight="bold" />
<Link to={`${paths.search}?area=${encodeURIComponent(course.category)}`}>{course.category}</Link><CaretRightIcon className="ph" weight="bold" />
<span>{course.title}</span>
</div>
<section className="chead">
<div>
<h1>{course.title}</h1>
{course.description && <p className="d">{course.description}</p>}
<p className="meta num">{[course.instructor, courseMeta(course)].filter(Boolean).join(' · ')}</p>
<div className="acts">
{user ? (
next && <Link className="btn lg" to={paths.lesson(course.id, next.id)}><PlayIcon className="ph" weight="fill" /> {started ? `Continuar aula ${progress.nextIndex + 1}` : 'Assistir'}</Link>
) : (
<>
<Link className="btn lg" to={paths.signUp} state={signUpState}>Criar conta para assistir</Link>
<Link className="btn lg sec" to={paths.signIn}>Entrar</Link>
</>
)}
{user && <button className="btn lg sec" type="button" onClick={() => saved.toggle(course.id)} aria-pressed={saved.isSaved(course.id)}><BookmarkSimpleIcon className="ph" weight={saved.isSaved(course.id) ? 'fill' : 'regular'} /> {saved.isSaved(course.id) ? 'Salvo' : 'Salvar'}</button>}
</div>
</div>
{course.lessons.length > 0 ? <PlayerPreview course={course} to={watchTo(course.lessons[0]?.id)} /> : <Cover course={course} variant="big" />}
</section>
<div className="split">
<div>
<section className="shelf" style={{ marginTop: 0 }}>
<header><h2>Aulas</h2><span className="count num">{courseMeta(course)}</span></header>
{course.lessons.length === 0 ? <p className="empty">As aulas deste curso ainda estão sendo preparadas.</p> : (
<ol className="list">
{course.lessons.map((lesson, index) => (
<li key={lesson.id}>
<Link className="row" to={watchTo(lesson.id)} state={user ? undefined : signUpState}>
<span className="i num">{progress.completed.has(lesson.id) ? <span className="done" aria-label="Concluída"><CheckIcon className="ph" weight="bold" /></span> : index + 1}</span>
<span className="t">{lesson.title}</span>
<span className="r num">{lessonDuration(lesson)}</span>
</Link>
</li>
))}
</ol>
)}
</section>
</div>
<aside className="side">
{course.instructorId && (
<Link className="box person" to={paths.professor(course.instructorId)} style={{ display: 'flex' }}>
<Avatar name={course.instructor || 'Professor'} />
<span>{course.instructor}<small>Professor</small></span>
</Link>
)}
{trail && (
<Link className="box" to={paths.trail(trail.id)}>
<h3 style={{ marginBottom: 4 }}>Faz parte da trilha</h3>
<span className="link">{trail.title} <CaretRightIcon className="ph" weight="bold" /></span>
</Link>
)}
<div className="box">
<h3>Materiais</h3>
{materials.length ? materials.map((item) => <MaterialRow key={item.attachment.id} attachment={item.attachment} detail={item.detail} action={action} compact />) : <p className="small">Este curso não tem materiais para baixar.</p>}
</div>
{user && course.lessons.length > 0 && (
<div className="box">
<h3>Seu progresso</h3>
<span className="prog" style={{ display: 'block', margin: '0 0 10px' }}><i style={{ width: `${Math.round((progress.doneCount / course.lessons.length) * 100)}%` }} /></span>
<p className="small num">{progress.doneCount} de {plural(course.lessons.length, 'aula', 'aulas')}</p>
</div>
)}
</aside>
</div>
</main>
);
};
/** Opens the learner's next unfinished lesson. */
export const ContinueCourse: React.FC = () => {
const { courseId } = useParams<{ courseId: string }>();
const { course, isLoading, missing } = useCourse(courseId);
const { user, isLoading: authLoading } = useAuth();
const progress = useLessonProgress(course);
if (authLoading || isLoading || (course && !progress.loaded)) return <main className="wrap page"><div className="loading"><CircleNotchIcon className="ph spin" /></div></main>;
if (missing || !course) return <Navigate to={paths.explore} replace />;
if (!user) return <Navigate to={paths.signUp} state={signUpState} replace />;
if (!progress.next) return <Navigate to={paths.course(course.id)} replace />;
return <Navigate to={paths.lesson(course.id, progress.next.id)} replace />;
};