feat(home): add spotlight search functionality

This commit is contained in:
shafin-r
2026-02-14 03:24:22 +06:00
parent 7f82e640e0
commit 96eb2c13b0
4 changed files with 282 additions and 61 deletions

View File

@ -0,0 +1,232 @@
import { motion, AnimatePresence } from "framer-motion";
import { useEffect, useMemo } from "react";
import { Search, X } from "lucide-react";
import type { PracticeSheet } from "../types/sheet";
import { useNavigate } from "react-router-dom";
import type { SearchItem } from "../types/search";
import { formatGroupTitle } from "../lib/utils";
interface Props {
sheets: PracticeSheet[];
onClose: () => void;
searchQuery: string;
setSearchQuery: (value: string) => void;
}
const navigationItems: SearchItem[] = [
{
type: "route",
title: "Hard Test Modules",
description: "Access advanced SAT modules",
route: "/student/hard-test-modules",
group: "Pages",
},
{
type: "route",
title: "Targeted Practice",
description: "Focus on what matters",
route: "/student/practice/targeted-practice",
group: "Pages",
},
{
type: "route",
title: "Drills",
description: "Train speed and accuracy",
route: "/student/practice/drills",
group: "Pages",
},
{
type: "route",
title: "Leaderboard",
description: "View student rankings",
route: "/student/rewards",
group: "Pages",
},
{
type: "route",
title: "Practice",
description: "See how you can practice",
route: "/student/practice",
group: "Pages",
},
{
type: "route",
title: "Lessons",
description: "Watch detailed lessons on SAT techniques",
route: "/student/lessons",
group: "Pages",
},
{
type: "route",
title: "Profile",
description: "View your profile",
route: "/student/profile",
group: "Pages",
},
];
const highlightText = (text: string, query: string) => {
if (!query.trim()) return text;
const escapedQuery = query.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
const regex = new RegExp(`(${escapedQuery})`, "gi");
const parts = text.split(regex);
return parts.map((part, index) => {
const isMatch = part.toLowerCase() === query.toLowerCase();
return isMatch ? (
<motion.span
key={index}
initial={{ opacity: 0, scale: 0.95 }}
animate={{ opacity: 1, scale: 1 }}
transition={{ duration: 0.2, delay: index * 0.05 }}
className="bg-purple-200 text-purple-900 px-1 rounded-md"
>
{part}
</motion.span>
) : (
part
);
});
};
export const SearchOverlay = ({
sheets,
onClose,
searchQuery,
setSearchQuery,
}: Props) => {
const navigate = useNavigate();
const searchItems = useMemo<SearchItem[]>(() => {
const sheetItems = sheets.map((sheet) => ({
type: "sheet",
id: sheet.id,
title: sheet.title,
description: sheet.description,
route: `/student/practice/${sheet.id}`,
group: formatGroupTitle(sheet.user_status), // 👈 reuse your grouping
}));
return [...navigationItems, ...sheetItems];
}, [sheets]);
// Close on ESC
useEffect(() => {
const handleKey = (e: KeyboardEvent) => {
if (e.key === "Escape") onClose();
};
window.addEventListener("keydown", handleKey);
return () => window.removeEventListener("keydown", handleKey);
}, [onClose]);
const groupedResults = useMemo(() => {
if (!searchQuery.trim()) return {};
const q = searchQuery.toLowerCase();
const filtered = searchItems.filter((item) => {
const title = item.title?.toLowerCase() || "";
const description = item.description?.toLowerCase() || "";
return title.includes(q) || description.includes(q);
});
return filtered.reduce<Record<string, SearchItem[]>>((acc, item) => {
if (!acc[item.group]) {
acc[item.group] = [];
}
acc[item.group].push(item);
return acc;
}, {});
}, [searchQuery, searchItems]);
return (
<AnimatePresence>
<motion.div
className="fixed inset-0 z-50 bg-black/40 backdrop-blur-sm flex flex-col items-center pt-24 px-4"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
onClick={onClose}
>
{/* Search Box */}
<motion.div
initial={{ y: -40, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
exit={{ y: -40, opacity: 0 }}
transition={{ type: "spring", stiffness: 300 }}
onClick={(e) => e.stopPropagation()}
className="w-full max-w-2xl bg-white rounded-3xl shadow-2xl p-6"
>
<div className="flex items-center gap-3">
<Search size={20} />
<input
autoFocus
value={searchQuery}
onChange={(e) => setSearchQuery(e.target.value)}
placeholder="Search..."
className="flex-1 outline-none font-satoshi text-lg"
/>
<button onClick={onClose}>
<X size={20} />
</button>
</div>
{/* Results */}
<div className="mt-6 max-h-96 overflow-y-auto space-y-6">
{/* {!searchQuery && (
<p className="font-satoshi text-gray-500">
Start typing to search...
</p>
)} */}
{searchQuery.length === 0 ? (
<p className="text-gray-400 font-satoshi">
Start typing to search...
</p>
) : Object.keys(groupedResults).length === 0 ? (
<p className="text-gray-400 font-satoshi">No results found.</p>
) : (
Object.entries(groupedResults).map(([group, items]) => (
<div key={group}>
<p className="text-xs uppercase tracking-wider text-gray-400 font-satoshi mb-3">
{group}
</p>
<div className="space-y-2">
{items.map((item, index) => (
<div
key={index}
onClick={() => {
onClose();
navigate(item.route!);
}}
className="p-4 rounded-2xl hover:bg-gray-100 cursor-pointer transition"
>
<p className="font-satoshi-medium">
{highlightText(item.title, searchQuery)}
</p>
{item.description && (
<p className="text-sm text-gray-500">
{highlightText(item.description, searchQuery)}
</p>
)}
<p className="text-xs text-purple-500 mt-1">
{item.type === "route" ? "" : "Practice Sheet"}
</p>
</div>
))}
</div>
</div>
))
)}
</div>
</motion.div>
</motion.div>
</AnimatePresence>
);
};