import React, { useEffect, useState } from 'react'; import { Link } from 'react-router-dom'; import { CircleNotchIcon } from '@phosphor-icons/react'; import { Course } from '../types'; import { LearningStats, getLearningStats, getMyLearningCourses } from '../services/db'; import { CourseCard, Cover, Thumb } from '../components/catalog/Artwork'; import { PlayMockIcon } from '../components/ui/MockupIcons'; import { Select } from '../components/ui/Select'; import { useSavedCourses } from '../hooks/useSavedCourses'; import { useCatalog } from '../context/CatalogContext'; import { useIsMobile } from '../hooks/useMediaQuery'; import { readPreference, writePreference } from '../lib/storage'; import { paths } from '../lib/routes'; export const MyLearning: React.FC<{ onPlay: (course: Course) => void }> = () => { const [courses, setCourses] = useState([]); const [stats, setStats] = useState(null); const [loading, setLoading] = useState(true); const [tab, setTab] = useState<'andamento' | 'concluidos' | 'salvos'>('andamento'); const [weeklyGoal, setWeeklyGoal] = useState(() => { const stored = Number(readPreference('ca-weekly-goal')); return [3, 5, 7, 10].includes(stored) ? stored : 3; }); const saved = useSavedCourses(); const isMobile = useIsMobile(); const { courses: catalogCourses } = useCatalog(); useEffect(() => { getMyLearningCourses().then(setCourses).catch(() => setCourses([])).finally(() => setLoading(false)); getLearningStats().then(setStats).catch(() => setStats(null)); }, []); const updateGoal = (value: string) => { const next = Number(value); if (![3, 5, 7, 10].includes(next)) return; setWeeklyGoal(next); writePreference('ca-weekly-goal', String(next)); }; const formatWatchTime = (seconds: number) => { const hours = Math.floor(seconds / 3600); const minutes = Math.floor((seconds % 3600) / 60); return hours ? `${hours}h ${minutes}min` : `${minutes}min`; }; const weeklyCompleted = stats?.weeklyCompleted ?? 0; const goalPct = Math.min(100, Math.round((weeklyCompleted / weeklyGoal) * 100)); const goalOptions = [3, 5, 7, 10].map((value) => ({ value: String(value), label: `${value} aulas` })); const going = courses.filter((course) => (course.progress || 0) < 100); const done = courses.filter((course) => (course.progress || 0) >= 100); const savedCourses = catalogCourses.filter((course) => saved.isSaved(course.id)); const first = going[0]; const completedCount = first ? Math.round(((first.progress || 0) / 100) * first.lessons.length) : 0; const nextLesson = first?.lessons[Math.min(completedCount, Math.max(first.lessons.length - 1, 0))]; if (loading) return
; if (isMobile) { const chosen = tab === 'andamento' ? going : tab === 'concluidos' ? done : savedCourses; return

Minha aprendizagem

{stats?.streakDays ?? 0}dias seguidos
{Math.floor((stats?.watchedSeconds ?? 0) / 60)}min assistidos
{stats?.completedLessons ?? 0}aulas concluídas
{weeklyCompleted}/{weeklyGoal}meta semanal

Meta da semana

{weeklyCompleted} de {weeklyGoal} aulas{goalPct}% concluído

Em andamento

{going.map((course) => )}

Concluídos

{done.map((course) => )}

Salvos

{savedCourses.map((course) => )}
; };