test / src /components /search-modal.tsx
aiioe's picture
Automated build & deploy
e454383 verified
Raw History Blame Contribute Delete
4.3 kB
"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<Chat[]>([])
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 (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="sm:max-w-[500px] p-0">
<DialogHeader className="px-6 pt-6 pb-4">
<DialogTitle className="text-lg font-semibold">Search Chats</DialogTitle>
</DialogHeader>
<div className="px-6 pb-4">
<div className="relative">
<Search className="absolute left-3 top-1/2 transform -translate-y-1/2 h-4 w-4 text-muted-foreground" />
<Input
placeholder="Search your conversations..."
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
className="pl-10 h-11 bg-muted/50 border-border/50 rounded-lg focus:bg-background"
autoFocus
/>
</div>
</div>
<ScrollArea className="max-h-[400px] px-6 pb-6">
{filteredChats.length > 0 ? (
<div className="space-y-1">
{filteredChats.map((chat) => (
<Button
key={chat.id}
variant="ghost"
className="w-full justify-start h-auto p-3 hover:bg-accent/50 rounded-lg"
onClick={() => handleChatSelect(chat.id)}
>
<div className="flex items-start gap-3 w-full">
<div className="w-8 h-8 bg-gradient-to-br from-primary/20 to-primary/10 rounded-lg flex items-center justify-center flex-shrink-0 mt-0.5">
<MessageSquare className="h-4 w-4 text-primary" />
</div>
<div className="flex-1 text-left min-w-0">
<p className="font-medium text-sm truncate">{chat.title}</p>
<div className="flex items-center gap-1 mt-1">
<Clock className="h-3 w-3 text-muted-foreground" />
<span className="text-xs text-muted-foreground">{formatDate(chat.date)}</span>
</div>
</div>
</div>
</Button>
))}
</div>
) : (
<div className="text-center py-8">
<Search className="h-8 w-8 text-muted-foreground/50 mx-auto mb-3" />
<p className="text-sm text-muted-foreground">
{searchTerm ? "No chats found" : "Start typing to search your conversations"}
</p>
</div>
)}
</ScrollArea>
</DialogContent>
</Dialog>
)
}
export default SearchModal