"use client" import { useState, useEffect } from "react" import { Search, MessageSquare, Clock } from "lucide-react" import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog" import { Input } from "@/components/ui/input" import { ScrollArea } from "@/components/ui/scroll-area" import { Button } from "@/components/ui/button" import { getAllChats } from "@/lib/storage" import { useChatStore } from "@/stores/chatStore" import type { Chat } from "@/lib/types" interface SearchModalProps { open: boolean onOpenChange: (open: boolean) => void } const SearchModal = ({ open, onOpenChange }: SearchModalProps) => { const [searchTerm, setSearchTerm] = useState("") const [chats, setChats] = useState([]) const { loadChat } = useChatStore() useEffect(() => { const loadChats = async () => { try { const loadedChats = await getAllChats() setChats(loadedChats.reverse()) } catch (error) { console.error("Failed to load chats:", error) } } if (open) { loadChats() } }, [open]) const filteredChats = chats.filter((chat) => chat.title.toLowerCase().includes(searchTerm.toLowerCase())) const handleChatSelect = (chatId: string) => { loadChat(chatId) onOpenChange(false) setSearchTerm("") } const formatDate = (dateString: string) => { const date = new Date(dateString) const now = new Date() const diffTime = Math.abs(now.getTime() - date.getTime()) const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)) if (diffDays === 1) return "Today" if (diffDays === 2) return "Yesterday" if (diffDays <= 7) return `${diffDays - 1} days ago` return date.toLocaleDateString() } return ( Search Chats
setSearchTerm(e.target.value)} className="pl-10 h-11 bg-muted/50 border-border/50 rounded-lg focus:bg-background" autoFocus />
{filteredChats.length > 0 ? (
{filteredChats.map((chat) => ( ))}
) : (

{searchTerm ? "No chats found" : "Start typing to search your conversations"}

)}
) } export default SearchModal