|
| 1 | +'use client'; |
| 2 | + |
1 | 3 | import NewConfigCard from './NewConfig'; |
2 | 4 | import prisma from '@/lib/db'; |
3 | 5 | import ConfigCard from './ConfigCard'; |
| 6 | +import { Input } from '@/components/ui/input'; |
| 7 | +import SearchCategoryMenu from './SearchCategoryMenu'; |
| 8 | +import SkeletonCard from './SkeletonCard'; |
| 9 | +import { Suspense, useState } from 'react'; |
| 10 | +import { faXmarkCircle } from '@fortawesome/free-solid-svg-icons'; |
| 11 | +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; |
4 | 12 |
|
5 | | -export default async function ConfigsGrid() { |
6 | | - const configs = await prisma.config.findMany({ |
7 | | - select: { |
8 | | - id: true, |
9 | | - title: true, |
10 | | - description: true, |
11 | | - categories: true, |
12 | | - config: true, |
13 | | - user: true, |
14 | | - }, |
15 | | - orderBy: { |
16 | | - createdAt: 'desc', |
| 13 | +export default function ConfigsGrid({ configs }: { configs: any }) { |
| 14 | + const [category, setCategory] = useState('All'); |
| 15 | + const [filter, setFilter] = useState(''); |
| 16 | + |
| 17 | + const updateCategory = (value: string) => { |
| 18 | + setCategory(value); |
| 19 | + }; |
| 20 | + |
| 21 | + const updateFilter = (e: any) => { |
| 22 | + setFilter(e.target.value); |
| 23 | + }; |
| 24 | + |
| 25 | + const filteredConfigs = configs.filter( |
| 26 | + ({ title, description, categories, user }: any) => { |
| 27 | + const matchesCategory = |
| 28 | + category === 'All' || |
| 29 | + categories |
| 30 | + .map((cate: string) => cate.toLowerCase()) |
| 31 | + .includes(category.toLowerCase()); |
| 32 | + const matchesSearchQuery = |
| 33 | + title.toLowerCase().includes(filter.toLowerCase()) || |
| 34 | + description.toLowerCase().includes(filter.toLowerCase()); |
| 35 | + categories |
| 36 | + .map((cat: string) => cat.toLowerCase()) |
| 37 | + .includes(filter.toLowerCase()); |
| 38 | + return matchesCategory && matchesSearchQuery; |
17 | 39 | }, |
18 | | - }); |
| 40 | + ); |
19 | 41 |
|
20 | 42 | return ( |
21 | | - <> |
22 | | - <NewConfigCard /> |
| 43 | + <div> |
| 44 | + <div className="flex mb-4 gap-4"> |
| 45 | + <SearchCategoryMenu value={category} onChange={updateCategory} /> |
| 46 | + <Input |
| 47 | + type="text" |
| 48 | + placeholder="Filter Configs" |
| 49 | + value={filter} |
| 50 | + onChange={updateFilter} |
| 51 | + /> |
| 52 | + </div> |
| 53 | + <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 auto-rows-fr"> |
| 54 | + <Suspense fallback={<Fallback />}> |
| 55 | + {filteredConfigs.length === 0 ? ( |
| 56 | + <div className="flex flex-col col-span-full text-center py-10 gap-2"> |
| 57 | + <FontAwesomeIcon |
| 58 | + icon={faXmarkCircle} |
| 59 | + className="size-8 text-black left-0 right-0 mx-auto" |
| 60 | + /> |
| 61 | + <h2 className="text-2xl font-semibold text-gray-600 font-sans tracking-tight"> |
| 62 | + No scripts found |
| 63 | + </h2> |
| 64 | + <div className="flex justify-center"> |
| 65 | + <p className="text-gray-500 text-center max-w-4xl"> |
| 66 | + Try adjusting your search or filters to find what you're |
| 67 | + looking for. |
| 68 | + </p> |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + ) : ( |
| 72 | + <> |
| 73 | + <NewConfigCard /> |
| 74 | + {filteredConfigs.map((config: any) => ( |
| 75 | + <ConfigCard key={config.id} config={config} /> |
| 76 | + ))} |
| 77 | + </> |
| 78 | + )} |
| 79 | + </Suspense> |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + ); |
| 83 | +} |
23 | 84 |
|
24 | | - {configs.map((config) => ( |
25 | | - <ConfigCard key={config.id} config={config} /> |
26 | | - ))} |
| 85 | +function Fallback() { |
| 86 | + return ( |
| 87 | + <> |
| 88 | + {Array(6) |
| 89 | + .fill(null) |
| 90 | + .map((_, index: number) => ( |
| 91 | + <SkeletonCard key={index} /> |
| 92 | + ))} |
27 | 93 | </> |
28 | 94 | ); |
29 | 95 | } |
0 commit comments