+'only running' filter
Browse files- pages/index.js +80 -57
pages/index.js
CHANGED
@@ -13,8 +13,15 @@ export default function Home() {
|
|
13 |
const [searchResults, setSearchResults] = useState([]);
|
14 |
const [isLoading, setIsLoading] = useState(false);
|
15 |
const [sortBy, setSortBy] = useState('relevance');
|
|
|
16 |
|
17 |
useEffect(() => {
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
async function fetchSpaceInfo(results) {
|
19 |
setIsLoading(true);
|
20 |
const spaceData = await Promise.all(
|
@@ -30,84 +37,100 @@ export default function Home() {
|
|
30 |
block: 'start',
|
31 |
});
|
32 |
}
|
33 |
-
|
34 |
-
if (searchResults.length > 0) {
|
35 |
-
fetchSpaceInfo(searchResults);
|
36 |
-
} else {
|
37 |
-
setSpaceInfo(null);
|
38 |
-
}
|
39 |
}, [searchResults]);
|
40 |
|
41 |
useEffect(() => {
|
42 |
if (spaceInfo) {
|
43 |
-
setSortedSpaceInfo(sortResults(spaceInfo, sortBy));
|
44 |
}
|
45 |
-
}, [spaceInfo, sortBy]);
|
46 |
-
|
47 |
-
async function onSearch(query) {
|
48 |
-
setIsLoading(true); // Show loading animation when searching
|
49 |
-
setSortBy('relevance'); // Reset sorting to Relevance on new search
|
50 |
-
setSearchResults(query ? await predict(query, 12) : []);
|
51 |
-
}
|
52 |
|
53 |
useEffect(() => {
|
54 |
document.querySelector('.search-bar')?.focus();
|
55 |
}, []);
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
function sortResults(results, sortBy) {
|
58 |
return sortBy === 'likes' ? [...results].sort((a, b) => b.likes - a.likes) : results;
|
59 |
}
|
60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
return (
|
62 |
<main className={`flex min-h-screen flex-col items-center p-8 md:px-24 pt-20 bg-gray-950 ${inter.className} justify-between`}>
|
63 |
<h1 className="text-4xl md:text-6xl font-bold text-center mb-12 text-white">π€ Hugging Face Spaces</h1>
|
64 |
<SearchBar onSearch={onSearch} />
|
65 |
-
{
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
{!isLoading && (
|
73 |
<div className="flex justify-center mt-4 items-baseline">
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
<span className="text-white mr-2">Sort by:</span>
|
75 |
-
{
|
76 |
-
<button
|
77 |
-
key={option}
|
78 |
-
className={`px-4 py-1 rounded-full mr-2 ${sortBy === option ? 'bg-white text-gray-900' : 'bg-gray-900 text-white'}`}
|
79 |
-
onClick={() => setSortBy(option)}
|
80 |
-
>
|
81 |
-
{option.charAt(0).toUpperCase() + option.slice(1)}
|
82 |
-
</button>
|
83 |
-
))}
|
84 |
</div>
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
<Card
|
92 |
-
key={index}
|
93 |
-
{...space}
|
94 |
-
space_id={space.space_id}
|
95 |
-
author={space.author}
|
96 |
-
title={space.title}
|
97 |
-
emoji={space.emoji}
|
98 |
-
lastModified={space.lastModified}
|
99 |
-
colorFrom={space.colorFrom}
|
100 |
-
colorTo={space.colorTo}
|
101 |
-
sdk={space.sdk}
|
102 |
-
runtimeStage={space.runtime_stage}
|
103 |
-
currentHardware={space.current_hardware}
|
104 |
-
/>
|
105 |
-
)
|
106 |
-
)
|
107 |
-
}
|
108 |
-
</div>
|
109 |
-
</>
|
110 |
-
))}
|
111 |
<footer className="text-center text-gray-500 text-sm mt-8 bottom-0 w-full p-4">
|
112 |
Created by Anzor Qunash
|
113 |
<br />
|
|
|
13 |
const [searchResults, setSearchResults] = useState([]);
|
14 |
const [isLoading, setIsLoading] = useState(false);
|
15 |
const [sortBy, setSortBy] = useState('relevance');
|
16 |
+
const [onlyRunning, setOnlyRunning] = useState(false);
|
17 |
|
18 |
useEffect(() => {
|
19 |
+
if (searchResults.length > 0) {
|
20 |
+
fetchSpaceInfo(searchResults);
|
21 |
+
} else {
|
22 |
+
setSpaceInfo(null);
|
23 |
+
}
|
24 |
+
|
25 |
async function fetchSpaceInfo(results) {
|
26 |
setIsLoading(true);
|
27 |
const spaceData = await Promise.all(
|
|
|
37 |
block: 'start',
|
38 |
});
|
39 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
}, [searchResults]);
|
41 |
|
42 |
useEffect(() => {
|
43 |
if (spaceInfo) {
|
44 |
+
setSortedSpaceInfo(filterResults(sortResults(spaceInfo, sortBy)));
|
45 |
}
|
46 |
+
}, [spaceInfo, sortBy, onlyRunning]);
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
useEffect(() => {
|
49 |
document.querySelector('.search-bar')?.focus();
|
50 |
}, []);
|
51 |
|
52 |
+
async function onSearch(query) {
|
53 |
+
setIsLoading(true);
|
54 |
+
setSortBy('relevance');
|
55 |
+
setSearchResults(query ? await predict(query, 24) : []);
|
56 |
+
}
|
57 |
+
|
58 |
function sortResults(results, sortBy) {
|
59 |
return sortBy === 'likes' ? [...results].sort((a, b) => b.likes - a.likes) : results;
|
60 |
}
|
61 |
|
62 |
+
function filterResults(results) {
|
63 |
+
return onlyRunning ? results.filter((space) => space.runtime_stage === 'RUNNING') : results;
|
64 |
+
}
|
65 |
+
|
66 |
+
function toggleOnlyRunning() {
|
67 |
+
setOnlyRunning(!onlyRunning);
|
68 |
+
}
|
69 |
+
|
70 |
+
const renderSortButtons = () => (
|
71 |
+
<>
|
72 |
+
{['relevance', 'likes'].map((option) => (
|
73 |
+
<div>
|
74 |
+
<button
|
75 |
+
key={option}
|
76 |
+
className={`px-4 py-1 rounded-full mr-2 ${sortBy === option ? 'bg-white text-gray-900' : 'bg-gray-900 text-white'}`}
|
77 |
+
onClick={() => setSortBy(option)}
|
78 |
+
>
|
79 |
+
{option.charAt(0).toUpperCase() + option.slice(1)}
|
80 |
+
</button>
|
81 |
+
</div>
|
82 |
+
))}
|
83 |
+
</>
|
84 |
+
);
|
85 |
+
|
86 |
+
const renderCards = () =>
|
87 |
+
sortedSpaceInfo.map(
|
88 |
+
(space, index) =>
|
89 |
+
space && (
|
90 |
+
<Card
|
91 |
+
key={index}
|
92 |
+
{...space}
|
93 |
+
space_id={space.space_id}
|
94 |
+
author={space.author}
|
95 |
+
title={space.title}
|
96 |
+
emoji={space.emoji}
|
97 |
+
lastModified={space.lastModified}
|
98 |
+
colorFrom={space.colorFrom}
|
99 |
+
colorTo={space.colorTo}
|
100 |
+
sdk={space.sdk}
|
101 |
+
runtimeStage={space.runtime_stage}
|
102 |
+
currentHardware={space.current_hardware}
|
103 |
+
/>
|
104 |
+
)
|
105 |
+
);
|
106 |
+
|
107 |
return (
|
108 |
<main className={`flex min-h-screen flex-col items-center p-8 md:px-24 pt-20 bg-gray-950 ${inter.className} justify-between`}>
|
109 |
<h1 className="text-4xl md:text-6xl font-bold text-center mb-12 text-white">π€ Hugging Face Spaces</h1>
|
110 |
<SearchBar onSearch={onSearch} />
|
111 |
+
{isLoading ? (
|
112 |
+
<div className="col-span-full flex justify-center items-center">
|
113 |
+
<div className="animate-spin rounded-full h-10 w-10 border-t-2 border-b-2 border-white"></div>
|
114 |
+
</div>
|
115 |
+
) : (
|
116 |
+
sortedSpaceInfo && (
|
117 |
+
<>
|
|
|
118 |
<div className="flex justify-center mt-4 items-baseline">
|
119 |
+
<button
|
120 |
+
className={`px-4 mx-4 py-1 rounded-full ${onlyRunning ? 'bg-white text-gray-900' : 'bg-gray-900 text-white'}`}
|
121 |
+
onClick={toggleOnlyRunning}
|
122 |
+
>
|
123 |
+
Only running
|
124 |
+
</button>
|
125 |
<span className="text-white mr-2">Sort by:</span>
|
126 |
+
{renderSortButtons()}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
</div>
|
128 |
+
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-5 w-full mt-8">
|
129 |
+
{renderCards()}
|
130 |
+
</div>
|
131 |
+
</>
|
132 |
+
)
|
133 |
+
)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
<footer className="text-center text-gray-500 text-sm mt-8 bottom-0 w-full p-4">
|
135 |
Created by Anzor Qunash
|
136 |
<br />
|