import React, { useEffect, useState } from 'react'; import { Link, useLocation } from 'react-router-dom'; import { CaretRightIcon, PlusIcon } from '@phosphor-icons/react'; import { Comment, Course } from '../types'; import { getCommunityComments } from '../services/db'; import { useCatalog } from '../context/CatalogContext'; import { useAuth } from '../context/AuthContext'; import { Avatar } from '../components/ui/Avatar'; import { ChatMockIcon, PlusMockIcon, TeacherMockIcon } from '../components/ui/MockupIcons'; import { paths } from '../lib/routes'; import { useIsMobile } from '../hooks/useMediaQuery'; type Thread = { comment: Comment; course: Course }; const lessonNumberFor = (comment: Comment, course: Course) => { if (!comment.lessonId) return 0; return course.lessons.findIndex((lesson) => lesson.id === comment.lessonId) + 1; }; export const CommunityPage: React.FC = () => { const { courses, professors } = useCatalog(); const { user } = useAuth(); const location = useLocation(); const isMobile = useIsMobile(); const [threads, setThreads] = useState([]); const [view, setView] = useState<'discussoes' | 'professores'>(() => new URLSearchParams(location.search).get('tab') === 'professores' ? 'professores' : 'discussoes'); const [filter, setFilter] = useState<'todas' | 'abertas' | 'professor'>('todas'); useEffect(() => { const courseById = new Map(courses.map((course) => [course.id, course])); let cancelled = false; getCommunityComments(100).then((comments) => { if (cancelled) return; setThreads(comments.flatMap((comment) => { const course = courseById.get(comment.courseId); return course ? [{ comment, course }] : []; })); }).catch(() => { if (!cancelled) setThreads([]); }); return () => { cancelled = true; }; }, [courses]); useEffect(() => { if (new URLSearchParams(location.search).get('tab') === 'professores') setView('professores'); }, [location.search]); const visible = threads.filter(({ comment }) => filter === 'todas' || (filter === 'abertas' ? !comment.reply : Boolean(comment.reply))); if (isMobile) return

Comunidade

{user && view === 'discussoes' && }
{view === 'professores' ?
{professors.map((professor) => {professor.name}{professor.headline ? `${professor.headline} · ` : ''}{professor.courses.length} {professor.courses.length === 1 ? 'curso' : 'cursos'})}
: <>
{[['todas', 'Todas'], ['abertas', 'Sem resposta'], ['professor', 'Com professor']].map(([key, label]) => )}
{visible.map(({ comment, course }) => {comment.text}{comment.user} · {course.title} · {comment.date}{comment.reply && })}
{!visible.length &&

Nenhuma discussão por aqui ainda.

}
}
; return

Discussões

Perguntas e resultados dos alunos, organizados por aula.

{user ? Nova pergunta : Entrar para participar}
{([['todas', 'Todas'], ['abertas', 'Sem resposta'], ['professor', 'Respondidas por professor']] as const).map(([key, label]) => )}
{visible.map(({ comment, course }) => { const lessonNumber = lessonNumberFor(comment, course); return {comment.text}{[comment.user, `${course.title}${lessonNumber ? `, aula ${lessonNumber}` : ''}`, comment.date].join(' · ')} {comment.reply && } {comment.reply ? 1 : 0} ; })}
{!visible.length &&

Nenhuma discussão por aqui ainda.

}
; };