|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import { useQuery } from '@tanstack/react-query'; |
| 3 | +import { Navbar } from '../components/Navbar'; |
| 4 | +import { CollapsibleTagFilter } from '../components/CollapsibleTagFilter'; |
| 5 | +import { getCertificatesPaginated, getCertificateIssuers } from '../lib/api'; |
| 6 | +import { useDebounce } from '../lib/hooks'; |
| 7 | +import { useDocumentMeta } from '../lib/useDocumentMeta'; |
| 8 | +import { Award, Search, ChevronLeft, ChevronRight } from 'lucide-react'; |
| 9 | +import { motion } from 'framer-motion'; |
| 10 | +import ReactMarkdown from 'react-markdown'; |
| 11 | + |
| 12 | +const ITEMS_PER_PAGE = 9; |
| 13 | + |
| 14 | +export default function CertificatesPage() { |
| 15 | + const [searchQuery, setSearchQuery] = useState(''); |
| 16 | + const [selectedIssuer, setSelectedIssuer] = useState<string | null>(null); |
| 17 | + const [currentPage, setCurrentPage] = useState(1); |
| 18 | + |
| 19 | + const debouncedSearchQuery = useDebounce(searchQuery, 300); |
| 20 | + |
| 21 | + useDocumentMeta({ |
| 22 | + title: 'Certificates', |
| 23 | + description: 'Browse all professional certifications and achievements.', |
| 24 | + }); |
| 25 | + |
| 26 | + const { data: allIssuers, isLoading: isLoadingIssuers } = useQuery({ |
| 27 | + queryKey: ['certificateIssuers'], |
| 28 | + queryFn: getCertificateIssuers, |
| 29 | + }); |
| 30 | + |
| 31 | + const { data: certData, isLoading: isLoadingCerts, isError } = useQuery({ |
| 32 | + queryKey: ['certificatesPaginated', currentPage, selectedIssuer, debouncedSearchQuery], |
| 33 | + queryFn: () => getCertificatesPaginated({ |
| 34 | + page: currentPage, |
| 35 | + limit: ITEMS_PER_PAGE, |
| 36 | + query: debouncedSearchQuery, |
| 37 | + issuer: selectedIssuer, |
| 38 | + }), |
| 39 | + placeholderData: (previousData) => previousData, |
| 40 | + }); |
| 41 | + |
| 42 | + const certificates = certData?.data ?? []; |
| 43 | + const totalCerts = certData?.count ?? 0; |
| 44 | + const totalPages = Math.ceil(totalCerts / ITEMS_PER_PAGE); |
| 45 | + |
| 46 | + const handlePageChange = (newPage: number) => { |
| 47 | + setCurrentPage(newPage); |
| 48 | + window.scrollTo({ top: 0, behavior: 'smooth' }); |
| 49 | + }; |
| 50 | + |
| 51 | + const handleIssuerFilter = (issuer: string | null) => { |
| 52 | + setSelectedIssuer(issuer); |
| 53 | + setCurrentPage(1); |
| 54 | + }; |
| 55 | + |
| 56 | + return ( |
| 57 | + <div className="min-h-screen bg-slate-50 dark:bg-slate-950 text-slate-900 dark:text-white transition-colors duration-300"> |
| 58 | + <Navbar /> |
| 59 | + <div className="pt-32 pb-20 max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 relative z-10"> |
| 60 | + {/* Header */} |
| 61 | + <div className="text-center mb-12"> |
| 62 | + <h1 className="text-4xl md:text-5xl font-bold mb-4">Certificates</h1> |
| 63 | + <p className="text-slate-600 dark:text-slate-400 max-w-2xl mx-auto"> |
| 64 | + Professional certifications, achievements, and credentials. |
| 65 | + </p> |
| 66 | + </div> |
| 67 | + |
| 68 | + {/* Search & Filters */} |
| 69 | + <div className="max-w-4xl mx-auto mb-12 space-y-6"> |
| 70 | + <div className="relative"> |
| 71 | + <Search className="absolute left-4 top-1/2 -translate-y-1/2 h-5 w-5 text-slate-500" /> |
| 72 | + <input |
| 73 | + type="text" |
| 74 | + placeholder="Search certificates..." |
| 75 | + value={searchQuery} |
| 76 | + onChange={(e) => { setSearchQuery(e.target.value); setCurrentPage(1); }} |
| 77 | + className="w-full bg-white dark:bg-slate-900/80 border border-slate-200 dark:border-white/10 rounded-full py-3 pl-12 pr-6 text-slate-900 dark:text-white focus:ring-2 focus:ring-indigo-500 outline-none backdrop-blur-sm shadow-sm" |
| 78 | + /> |
| 79 | + </div> |
| 80 | + |
| 81 | + <div className="flex justify-center"> |
| 82 | + <CollapsibleTagFilter |
| 83 | + tags={allIssuers ?? []} |
| 84 | + selectedTag={selectedIssuer} |
| 85 | + onTagClick={handleIssuerFilter} |
| 86 | + maxVisible={6} |
| 87 | + isLoading={isLoadingIssuers} |
| 88 | + /> |
| 89 | + </div> |
| 90 | + </div> |
| 91 | + |
| 92 | + {/* Certificate Grid */} |
| 93 | + <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 mb-12"> |
| 94 | + {isLoadingCerts && Array.from({ length: ITEMS_PER_PAGE }).map((_, i) => ( |
| 95 | + <div key={i} className="rounded-xl bg-slate-200 dark:bg-slate-800/50 aspect-[4/3] animate-pulse" /> |
| 96 | + ))} |
| 97 | + {isError && <div className="col-span-full py-20 text-center text-red-500">Error loading certificates.</div>} |
| 98 | + {certificates.map((cert, idx) => ( |
| 99 | + <motion.div |
| 100 | + key={cert.id} |
| 101 | + initial={{ opacity: 0, y: 20 }} |
| 102 | + whileInView={{ opacity: 1, y: 0 }} |
| 103 | + transition={{ delay: idx * 0.05 }} |
| 104 | + className="bg-white border border-slate-200 dark:bg-slate-900/50 dark:border-white/5 rounded-xl overflow-hidden hover:border-indigo-500/30 transition-all shadow-sm dark:shadow-none group flex flex-col" |
| 105 | + > |
| 106 | + {cert.file_url ? ( |
| 107 | + <div className="h-48 bg-slate-100 dark:bg-slate-800 overflow-hidden relative"> |
| 108 | + <img src={cert.file_url} alt={cert.title} className="w-full h-full object-cover group-hover:scale-105 transition-transform duration-500" /> |
| 109 | + </div> |
| 110 | + ) : ( |
| 111 | + <div className="h-48 bg-slate-100 dark:bg-slate-800 flex items-center justify-center text-slate-400"> |
| 112 | + <Award className="h-12 w-12" /> |
| 113 | + </div> |
| 114 | + )} |
| 115 | + <div className="p-6 flex-1 flex flex-col"> |
| 116 | + <h3 className="text-lg font-bold mb-2 text-slate-900 dark:text-white">{cert.title}</h3> |
| 117 | + <p className="text-sm text-indigo-600 dark:text-indigo-400 mb-2">{cert.issued_by}</p> |
| 118 | + <p className="text-xs text-slate-500 mb-4">{cert.issued_date} {cert.expiry_date ? ` - ${cert.expiry_date}` : ''}</p> |
| 119 | + <div className="text-slate-600 dark:text-slate-400 text-sm leading-relaxed mb-4 flex-1 prose prose-sm dark:prose-invert line-clamp-3"> |
| 120 | + <ReactMarkdown>{cert.description || ''}</ReactMarkdown> |
| 121 | + </div> |
| 122 | + {cert.credential_url && ( |
| 123 | + <a href={cert.credential_url} target="_blank" rel="noopener noreferrer" className="text-sm font-medium text-indigo-600 dark:text-indigo-400 hover:underline mt-auto"> |
| 124 | + View Credential → |
| 125 | + </a> |
| 126 | + )} |
| 127 | + </div> |
| 128 | + </motion.div> |
| 129 | + ))} |
| 130 | + {certificates.length === 0 && !isLoadingCerts && ( |
| 131 | + <div className="col-span-full py-20 text-center text-slate-500"> |
| 132 | + No certificates found matching your criteria. |
| 133 | + </div> |
| 134 | + )} |
| 135 | + </div> |
| 136 | + |
| 137 | + {/* Pagination */} |
| 138 | + {totalPages > 1 && ( |
| 139 | + <div className="flex justify-center gap-2"> |
| 140 | + <button |
| 141 | + onClick={() => handlePageChange(Math.max(1, currentPage - 1))} |
| 142 | + disabled={currentPage === 1} |
| 143 | + className="p-2 rounded-lg border border-slate-200 dark:border-white/10 text-slate-600 dark:text-slate-400 hover:bg-slate-100 dark:hover:bg-white/5 disabled:opacity-50 disabled:cursor-not-allowed" |
| 144 | + > |
| 145 | + <ChevronLeft className="h-5 w-5" /> |
| 146 | + </button> |
| 147 | + {Array.from({ length: totalPages }).map((_, i) => ( |
| 148 | + <button |
| 149 | + key={i} |
| 150 | + onClick={() => handlePageChange(i + 1)} |
| 151 | + className={`w-10 h-10 rounded-lg font-medium transition-colors ${ |
| 152 | + currentPage === i + 1 |
| 153 | + ? 'bg-indigo-600 text-white' |
| 154 | + : 'border border-slate-200 dark:border-white/10 text-slate-600 dark:text-slate-400 hover:bg-slate-100 dark:hover:bg-white/5' |
| 155 | + }`} |
| 156 | + > |
| 157 | + {i + 1} |
| 158 | + </button> |
| 159 | + ))} |
| 160 | + <button |
| 161 | + onClick={() => handlePageChange(Math.min(totalPages, currentPage + 1))} |
| 162 | + disabled={currentPage === totalPages} |
| 163 | + className="p-2 rounded-lg border border-slate-200 dark:border-white/10 text-slate-600 dark:text-slate-400 hover:bg-slate-100 dark:hover:bg-white/5 disabled:opacity-50 disabled:cursor-not-allowed" |
| 164 | + > |
| 165 | + <ChevronRight className="h-5 w-5" /> |
| 166 | + </button> |
| 167 | + </div> |
| 168 | + )} |
| 169 | + </div> |
| 170 | + </div> |
| 171 | + ); |
| 172 | +} |
0 commit comments