import { Inter } from 'next/font/google'; import SearchBar from '@/components/searchBar'; import Card from '@/components/card'; import { predict } from '@/pages/api/api_hf'; import { get_space_info } from '@/pages/api/hf_space'; import { useState, useEffect } from 'react'; const inter = Inter({ subsets: ['latin'] }); export default function Home() { const [spaceInfo, setSpaceInfo] = useState(null); const [sortedSpaceInfo, setSortedSpaceInfo] = useState(null); const [searchResults, setSearchResults] = useState([]); const [isLoading, setIsLoading] = useState(false); const [sortBy, setSortBy] = useState('relevance'); useEffect(() => { async function fetchSpaceInfo(results) { setIsLoading(true); const spaceData = await Promise.all( results.map(async ([id, description]) => { const space = await get_space_info(id); return space ? { ...space, description } : null; }) ); setSpaceInfo(spaceData); setIsLoading(false); document.querySelector('.search-bar').scrollIntoView({ behavior: 'smooth', block: 'start', }); } if (searchResults.length > 0) { fetchSpaceInfo(searchResults); } else { setSpaceInfo(null); } }, [searchResults]); useEffect(() => { if (spaceInfo) { setSortedSpaceInfo(sortResults(spaceInfo, sortBy)); } }, [spaceInfo, sortBy]); async function onSearch(query) { setIsLoading(true); // Show loading animation when searching setSortBy('relevance'); // Reset sorting to Relevance on new search setSearchResults(query ? await predict(query, 12) : []); } useEffect(() => { document.querySelector('.search-bar')?.focus(); }, []); function sortResults(results, sortBy) { return sortBy === 'likes' ? [...results].sort((a, b) => b.likes - a.likes) : results; } return (

🤗 Hugging Face Spaces

{ isLoading ? (
) : ( sortedSpaceInfo && ( <> {!isLoading && (
Sort by: {['relevance', 'likes'].map((option) => ( ))}
)}
{ sortedSpaceInfo.map( (space, index) => space && ( ) ) }
))}
); }