|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useTranslations } from 'next-intl'; |
| 4 | +import { Search, X } from 'lucide-react'; |
| 5 | + |
| 6 | +import AccordionList from '@/components/q&a/AccordionList'; |
| 7 | +import { Pagination } from '@/components/q&a/Pagination'; |
| 8 | +import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs'; |
| 9 | +import { categoryData } from '@/data/category'; |
| 10 | +import { useQaTabs } from '@/components/q&a/useQaTabs'; |
| 11 | + |
| 12 | +export default function TabsSection() { |
| 13 | + const t = useTranslations('qa'); |
| 14 | + const { |
| 15 | + active, |
| 16 | + currentPage, |
| 17 | + debouncedSearch, |
| 18 | + handleCategoryChange, |
| 19 | + handlePageChange, |
| 20 | + isLoading, |
| 21 | + items, |
| 22 | + localeKey, |
| 23 | + searchQuery, |
| 24 | + setSearchQuery, |
| 25 | + totalPages, |
| 26 | + clearSearch, |
| 27 | + } = useQaTabs(); |
| 28 | + |
| 29 | + return ( |
| 30 | + <div className="w-full"> |
| 31 | + <div className="relative mb-6"> |
| 32 | + <Search className="absolute left-3 top-1/2 -translate-y-1/2 h-5 w-5 text-gray-400" /> |
| 33 | + |
| 34 | + <input |
| 35 | + type="text" |
| 36 | + value={searchQuery} |
| 37 | + onChange={e => setSearchQuery(e.target.value)} |
| 38 | + placeholder={t('searchPlaceholder')} |
| 39 | + className="w-full pl-10 pr-10 py-3 border rounded-lg" |
| 40 | + /> |
| 41 | + |
| 42 | + {searchQuery && ( |
| 43 | + <button |
| 44 | + onClick={clearSearch} |
| 45 | + className="absolute right-3 top-1/2 -translate-y-1/2" |
| 46 | + > |
| 47 | + <X className="h-5 w-5" /> |
| 48 | + </button> |
| 49 | + )} |
| 50 | + </div> |
| 51 | + |
| 52 | + <Tabs value={active} onValueChange={handleCategoryChange}> |
| 53 | + <TabsList className="!bg-transparent !p-0 !h-auto !w-full grid grid-cols-3 sm:grid-cols-5 lg:grid-cols-10 mb-6 gap-2"> |
| 54 | + {categoryData.map(category => ( |
| 55 | + <TabsTrigger |
| 56 | + key={category.slug} |
| 57 | + value={category.slug} |
| 58 | + className="data-[state=active]:bg-blue-600 data-[state=active]:text-white bg-gray-100 dark:bg-gray-800 hover:bg-gray-200 dark:hover:bg-gray-700 rounded-lg px-3 py-2 text-sm font-medium transition-colors" |
| 59 | + > |
| 60 | + {category.translations[localeKey] ?? |
| 61 | + category.translations.en ?? |
| 62 | + category.slug} |
| 63 | + </TabsTrigger> |
| 64 | + ))} |
| 65 | + </TabsList> |
| 66 | + |
| 67 | + {categoryData.map(category => ( |
| 68 | + <TabsContent key={category.slug} value={category.slug}> |
| 69 | + {isLoading ? ( |
| 70 | + <div className="flex justify-center py-12"> |
| 71 | + <div className="animate-spin h-8 w-8 border-b-2" /> |
| 72 | + </div> |
| 73 | + ) : items.length ? ( |
| 74 | + <AccordionList items={items} /> |
| 75 | + ) : ( |
| 76 | + <p className="text-center py-12"> |
| 77 | + {debouncedSearch |
| 78 | + ? t('noResults', { query: debouncedSearch }) |
| 79 | + : t('noQuestions')} |
| 80 | + </p> |
| 81 | + )} |
| 82 | + </TabsContent> |
| 83 | + ))} |
| 84 | + </Tabs> |
| 85 | + |
| 86 | + {!isLoading && totalPages > 1 && ( |
| 87 | + <Pagination |
| 88 | + currentPage={currentPage} |
| 89 | + totalPages={totalPages} |
| 90 | + onPageChange={handlePageChange} |
| 91 | + /> |
| 92 | + )} |
| 93 | + </div> |
| 94 | + ); |
| 95 | +} |
0 commit comments