import React, { useEffect, useState } from 'react'; import { Link, useSearchParams } from 'react-router-dom'; import { CourseCard, Thumb, TrailPoster } from '../components/catalog/Artwork'; import { Avatar } from '../components/ui/Avatar'; import { SearchMockIcon } from '../components/ui/MockupIcons'; import { useCatalog } from '../context/CatalogContext'; import { searchCatalog, SearchResults } from '../services/db'; import { useIsMobile } from '../hooks/useMediaQuery'; import { paths } from '../lib/routes'; const emptyResults: SearchResults = { courses: [], paths: [], professors: [] }; export const SearchPage: React.FC = () => { const { courseById, pathTone } = useCatalog(); const isMobile = useIsMobile(); const [params] = useSearchParams(); const [query, setQuery] = useState(params.get('q') || params.get('area') || ''); const [results, setResults] = useState(emptyResults); const [isLoading, setIsLoading] = useState(true); const [failed, setFailed] = useState(false); const normalized = query.trim().length > 0; useEffect(() => { let cancelled = false; setIsLoading(true); const timer = window.setTimeout(() => { searchCatalog(query) .then((response) => { if (cancelled) return; setResults(response); setFailed(false); }) .catch(() => { if (cancelled) return; setResults(emptyResults); setFailed(true); }) .finally(() => { if (!cancelled) setIsLoading(false); }); }, 250); return () => { cancelled = true; window.clearTimeout(timer); }; }, [query]); if (isMobile) return

Buscar

{isLoading ?

Carregando…

: failed ?

Não foi possível buscar agora. Tente novamente.

: <> {results.courses.length > 0 &&

Cursos

{results.courses.map((course) => { const listed = courseById(course.id); const shown = listed ? { ...course, progress: listed.progress } : course; return {course.title}{course.instructor} · {course.lessons.length} aulas; })}
} {normalized && results.paths.length > 0 &&

Trilhas

{results.paths.map((trail) =>
} {normalized && results.professors.length > 0 &&

Professores

{results.professors.map((professor) => {professor.name}{professor.headline && {professor.headline}})}
} {!results.courses.length && !results.paths.length && !results.professors.length &&

Nada encontrado para “{query}”.
Tente “Meta Ads” ou “CRM”.

} }
; return

Buscar

{isLoading ?

Carregando…

: failed ?

Não foi possível buscar agora. Tente novamente.

: <> {normalized && results.professors.length > 0 &&

Professores

{results.professors.map((professor) => {professor.name}{professor.headline && {professor.headline}}{professor.courseCount} {professor.courseCount === 1 ? 'curso' : 'cursos'})}
} {normalized && results.paths.length > 0 &&

Trilhas

{results.paths.map((trail) => )}
}

{normalized ? <>Cursos {results.courses.length} : 'Todos os cursos'}

{results.courses.length ?
{results.courses.map((course) => { const listed = courseById(course.id); return ; })}
:

Nenhum curso encontrado. Tente outro termo, como “Meta Ads” ou “CRM”.

}
}
; };