Spaces:
Running
Running
add search functionality
#3
by
awaketurbulence
- opened
- components/spaces/index.tsx +47 -9
components/spaces/index.tsx
CHANGED
@@ -1,13 +1,11 @@
|
|
1 |
"use client";
|
2 |
-
import { useEffect, useState } from "react";
|
3 |
-
import { useLocalStorage, useUpdateEffect } from "react-use";
|
4 |
import { useRouter } from "next/navigation";
|
5 |
import { useTheme } from "next-themes";
|
6 |
-
import { MdDarkMode, MdLightMode } from "react-icons/md";
|
7 |
|
8 |
import { SpaceProps } from "@/utils/type";
|
9 |
import { SpaceIcon } from "@/components/space_icon";
|
10 |
-
|
11 |
import { Space } from "@/components/spaces/space";
|
12 |
import { Sort } from "@/components/sort";
|
13 |
|
@@ -18,10 +16,11 @@ export const Spaces: React.FC<{ spaces: SpaceProps[] }> = ({ spaces }) => {
|
|
18 |
const [selectedGpu, setSelectedGpu] = useState<string | undefined>(undefined);
|
19 |
const router = useRouter();
|
20 |
const [sort, setSort] = useState<"likes" | "createdAt">("likes");
|
|
|
21 |
|
22 |
-
|
23 |
router.push("/?sort=" + sort);
|
24 |
-
}, [sort]);
|
25 |
|
26 |
const handleSelectGpu = (gpu: string) => {
|
27 |
if (selectedGpu === gpu) {
|
@@ -31,6 +30,17 @@ export const Spaces: React.FC<{ spaces: SpaceProps[] }> = ({ spaces }) => {
|
|
31 |
setSelectedGpu(gpu);
|
32 |
};
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
return (
|
35 |
<section className="flex h-screen overflow-auto">
|
36 |
<div className="w-full container px-6 py-10 lg:py-20 mx-auto space-y-10 lg:space-y-14">
|
@@ -51,7 +61,7 @@ export const Spaces: React.FC<{ spaces: SpaceProps[] }> = ({ spaces }) => {
|
|
51 |
</button>
|
52 |
<div className="font-regular text-xs text-center max-w-max rounded-full border-gray-200 bg-gray-50 border text-gray-700 dark:text-slate-400 dark:bg-slate-900 dark:border-slate-800 px-3 py-2 transition-all duration-300">
|
53 |
<SpaceIcon className="inline-block w-4 h-4 mr-2 drop-shadow-lg" />
|
54 |
-
Browse {
|
55 |
</div>
|
56 |
</div>
|
57 |
<h1 className="font-extrabold text-3xl text-black dark:text-slate-100">
|
@@ -61,10 +71,38 @@ export const Spaces: React.FC<{ spaces: SpaceProps[] }> = ({ spaces }) => {
|
|
61 |
Discover spaces with zero GPU usage on 🤗 Hugging Face Spaces.
|
62 |
</p>
|
63 |
</div>
|
64 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
</header>
|
66 |
<div className="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-4 pb-10">
|
67 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
<Space
|
69 |
key={space.id}
|
70 |
space={space}
|
|
|
1 |
"use client";
|
2 |
+
import { useEffect, useMemo, useState } from "react";
|
|
|
3 |
import { useRouter } from "next/navigation";
|
4 |
import { useTheme } from "next-themes";
|
5 |
+
import { MdDarkMode, MdLightMode, MdSearch } from "react-icons/md";
|
6 |
|
7 |
import { SpaceProps } from "@/utils/type";
|
8 |
import { SpaceIcon } from "@/components/space_icon";
|
|
|
9 |
import { Space } from "@/components/spaces/space";
|
10 |
import { Sort } from "@/components/sort";
|
11 |
|
|
|
16 |
const [selectedGpu, setSelectedGpu] = useState<string | undefined>(undefined);
|
17 |
const router = useRouter();
|
18 |
const [sort, setSort] = useState<"likes" | "createdAt">("likes");
|
19 |
+
const [searchQuery, setSearchQuery] = useState<string>("");
|
20 |
|
21 |
+
useEffect(() => {
|
22 |
router.push("/?sort=" + sort);
|
23 |
+
}, [sort, router]);
|
24 |
|
25 |
const handleSelectGpu = (gpu: string) => {
|
26 |
if (selectedGpu === gpu) {
|
|
|
30 |
setSelectedGpu(gpu);
|
31 |
};
|
32 |
|
33 |
+
const filteredSpaces = useMemo(() => {
|
34 |
+
if (!searchQuery) return spaces;
|
35 |
+
return spaces.filter(
|
36 |
+
(space) =>
|
37 |
+
space.title?.toLowerCase().includes(searchQuery.trim().toLowerCase()) ||
|
38 |
+
space.authorData?.name
|
39 |
+
?.toLowerCase()
|
40 |
+
.includes(searchQuery.trim().toLowerCase())
|
41 |
+
);
|
42 |
+
}, [spaces, searchQuery]);
|
43 |
+
|
44 |
return (
|
45 |
<section className="flex h-screen overflow-auto">
|
46 |
<div className="w-full container px-6 py-10 lg:py-20 mx-auto space-y-10 lg:space-y-14">
|
|
|
61 |
</button>
|
62 |
<div className="font-regular text-xs text-center max-w-max rounded-full border-gray-200 bg-gray-50 border text-gray-700 dark:text-slate-400 dark:bg-slate-900 dark:border-slate-800 px-3 py-2 transition-all duration-300">
|
63 |
<SpaceIcon className="inline-block w-4 h-4 mr-2 drop-shadow-lg" />
|
64 |
+
Browse {filteredSpaces.length} spaces
|
65 |
</div>
|
66 |
</div>
|
67 |
<h1 className="font-extrabold text-3xl text-black dark:text-slate-100">
|
|
|
71 |
Discover spaces with zero GPU usage on 🤗 Hugging Face Spaces.
|
72 |
</p>
|
73 |
</div>
|
74 |
+
<div className="flex items-center justify-end gap-2">
|
75 |
+
<div className="relative">
|
76 |
+
<MdSearch className="absolute left-2.5 top-2.5 w-5 h-5 text-gray-600 dark:text-slate-400 z-[1]" />
|
77 |
+
<input
|
78 |
+
type="text"
|
79 |
+
placeholder="Search spaces..."
|
80 |
+
className="border-[3px] text-gray-600 dark:text-slate-300 text-sm rounded-full outline-none border-gray-50 drop-shadow-sm bg-white dark:bg-slate-900 dark:border-slate-800 ring-[1px] ring-gray-200 dark:ring-slate-700 px-2.5 pl-8 py-1.5"
|
81 |
+
value={searchQuery}
|
82 |
+
onChange={(e) => setSearchQuery(e.target.value)}
|
83 |
+
/>
|
84 |
+
</div>
|
85 |
+
<Sort value={sort} onChange={setSort} />
|
86 |
+
</div>
|
87 |
</header>
|
88 |
<div className="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-4 pb-10">
|
89 |
+
{filteredSpaces.length === 0 && (
|
90 |
+
<div className="col-span-full flex items-center flex-col justify-center gap-2">
|
91 |
+
<p className="text-neutral-500 font-regular text-base dark:text-slate-300">
|
92 |
+
No spaces found.
|
93 |
+
</p>
|
94 |
+
<button
|
95 |
+
className="text-sm text-gray-500 underline dark:text-slate-500"
|
96 |
+
onClick={() => {
|
97 |
+
setSearchQuery("");
|
98 |
+
setSelectedGpu(undefined);
|
99 |
+
}}
|
100 |
+
>
|
101 |
+
Reset filters
|
102 |
+
</button>
|
103 |
+
</div>
|
104 |
+
)}
|
105 |
+
{filteredSpaces?.map((space: SpaceProps) => (
|
106 |
<Space
|
107 |
key={space.id}
|
108 |
space={space}
|