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, CourseCard, Cover, courseFraming, useCoverImage } from '../components/catalog/Artwork';
import { MaterialRow, useMaterialAction } from '../components/course/Materials';
import { CourseReviews } from '../components/course/CourseReviews';
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 (
{image.show && }
{first && Aula 1 · {first.title}{lessonDuration(first)}
}
);
};
export const CoursePage: React.FC = () => {
const { courseId } = useParams<{ courseId: string }>();
const { course, isLoading, missing } = useCourse(courseId);
const { user } = useAuth();
const { paths: trails, courses: catalogCourses } = useCatalog();
const progress = useLessonProgress(course);
const action = useMaterialAction();
const saved = useSavedCourses();
const isMobile = useIsMobile();
const [mobileTab, setMobileTab] = useState<'aulas' | 'materiais' | 'sobre' | 'avaliacoes'>('aulas');
if (isLoading) return
;
if (missing || !course) return Este curso não está disponível.
;
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;
const related = catalogCourses
.filter((item) => item.id !== course.id && (item.category === course.category || (course.instructorId && item.instructorId === course.instructorId)))
.sort((a, b) => Number(b.category === course.category) - Number(a.category === course.category))
.slice(0, 4);
if (isMobile) return
{user && progress.doneCount > 0 &&
{progress.doneCount} de {course.lessons.length} aulas
}
{([['aulas', 'Aulas'], ['materiais', 'Materiais'], ['sobre', 'Sobre'], ['avaliacoes', 'Avaliações']] as const).map(([key, label]) => )}
{mobileTab === 'aulas' &&
{course.lessons.map((lesson, index) => {progress.completed.has(lesson.id) ? : index + 1}{lesson.title}{lessonDuration(lesson)})}
}
{mobileTab === 'materiais' &&
{materials.map((item) =>
{attachmentIcon(item.attachment.type)}{item.attachment.name}{item.detail} · {item.attachment.size}
)}
{!materials.length && Este curso não tem materiais para baixar.
}}
{mobileTab === 'sobre' && <>
{course.description}
{course.instructorId &&
}{trail &&
}>}
{mobileTab === 'avaliacoes' &&
}
{related.length > 0 &&
Cursos relacionados
{related.map((item) => {item.title}{item.instructor})}
}
{user && }{user && started ? `Continuar aula ${progress.nextIndex + 1}` : user ? 'Assistir' : 'Criar conta para assistir'}
;
return (
Explorar
{course.category}
{course.title}
{course.title}
{course.description &&
{course.description}
}
{[course.instructor, courseMeta(course)].filter(Boolean).join(' · ')}
{user ? (
next &&
{started ? `Continuar aula ${progress.nextIndex + 1}` : 'Assistir'}
) : (
<>
Criar conta para assistir
Entrar
>
)}
{user &&
}
{course.lessons.length > 0 ? : }
Aulas
{courseMeta(course)}
{course.lessons.length === 0 ? As aulas deste curso ainda estão sendo preparadas.
: (
{course.lessons.map((lesson, index) => (
-
{progress.completed.has(lesson.id) ? : index + 1}
{lesson.title}
{lessonDuration(lesson)}
))}
)}
{related.length > 0 &&
{related.map((item) => )}
}
);
};
/** 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
;
if (missing || !course) return ;
if (!user) return ;
if (!progress.next) return ;
return ;
};