| // src/components/admin/AdminGlobalSearch.tsx | |
| import { useState } from "react"; | |
| import { Search, User, FileText, MessageSquare, Users } from "lucide-react"; | |
| import { Link } from "react-router-dom"; | |
| import { motion, AnimatePresence } from "framer-motion"; | |
| import api from "@/lib/api"; | |
| export function AdminGlobalSearch() { | |
| const [query, setQuery] = useState(""); | |
| const [results, setResults] = useState<any[]>([]); | |
| const [loading, setLoading] = useState(false); | |
| const handleSearch = async (e: React.ChangeEvent<HTMLInputElement>) => { | |
| const value = e.target.value; | |
| setQuery(value); | |
| if (value.length < 2) { | |
| setResults([]); | |
| return; | |
| } | |
| setLoading(true); | |
| try { | |
| const res = await api.get(`/admin/search?q=${encodeURIComponent(value)}`); | |
| setResults(res.data || []); | |
| } catch (error) { | |
| console.error("Search failed", error); | |
| } finally { | |
| setLoading(false); | |
| } | |
| }; | |
| const getIcon = (type: string) => { | |
| switch (type) { | |
| case "user": | |
| return <User className="w-4 h-4 text-blue-500" />; | |
| case "article": | |
| return <FileText className="w-4 h-4 text-emerald-500" />; | |
| case "comment": | |
| return <MessageSquare className="w-4 h-4 text-indigo-500" />; | |
| default: | |
| return <Users className="w-4 h-4 text-amber-500" />; | |
| } | |
| }; | |
| return ( | |
| <div className="relative"> | |
| <div className="relative"> | |
| <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" /> | |
| <input | |
| type="text" | |
| placeholder="Search users, articles, comments..." | |
| value={query} | |
| onChange={handleSearch} | |
| className="w-full pl-9 pr-3 py-2 bg-white/50 dark:bg-gray-900/50 backdrop-blur-md border border-gray-200 dark:border-gray-800 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary text-sm transition" | |
| /> | |
| </div> | |
| <AnimatePresence> | |
| {query.length > 1 && ( | |
| <motion.div | |
| initial={{ opacity: 0, y: -10 }} | |
| animate={{ opacity: 1, y: 0 }} | |
| exit={{ opacity: 0, y: -10 }} | |
| className="absolute top-full left-0 right-0 mt-2 bg-white/90 dark:bg-gray-900/90 backdrop-blur-lg rounded-xl shadow-xl border border-gray-200 dark:border-gray-800 z-50 max-h-80 overflow-y-auto" | |
| > | |
| {loading ? ( | |
| <div className="p-4 text-center text-gray-500 text-sm">Searching...</div> | |
| ) : results.length === 0 ? ( | |
| <div className="p-4 text-center text-gray-500 text-sm">No results found</div> | |
| ) : ( | |
| results.map((result) => ( | |
| <Link | |
| key={`${result.type}-${result.id}`} | |
| to={result.url || "#"} | |
| className="flex items-center gap-3 p-3 hover:bg-gray-100/50 dark:hover:bg-gray-800/50 transition border-b border-gray-100 dark:border-gray-800 last:border-0" | |
| > | |
| <div className="p-1.5 rounded-lg bg-gray-100 dark:bg-gray-800 shrink-0"> | |
| {getIcon(result.type)} | |
| </div> | |
| <div className="flex-1 min-w-0"> | |
| <p className="text-sm font-medium truncate">{result.title}</p> | |
| <p className="text-xs text-gray-500 truncate">{result.description || result.subtitle || ""}</p> | |
| </div> | |
| </Link> | |
| )) | |
| )} | |
| </motion.div> | |
| )} | |
| </AnimatePresence> | |
| </div> | |
| ); | |
| } | |
Xet Storage Details
- Size:
- 3.51 kB
- Xet hash:
- 48d26b449f0364a6352856da501041c7db511271f13d18dcf2f86f46c2ea06cc
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.