"use client" import { cn } from "@lib/utils" import { Button } from "@ui/components/button" import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "@ui/components/dialog" import { ScrollArea } from "@ui/components/scroll-area" import { formatDistanceToNow } from "date-fns" import { HistoryIcon, Plus, Trash2, X } from "lucide-react" import { useMemo, useState } from "react" import { analytics } from "@/lib/analytics" import { useChatOpen, usePersistentChat, useProject } from "@/stores" import { ChatMessages } from "./chat-messages" import { generateId } from "@lib/generate-id" export function ChatRewrite() { const { setIsOpen } = useChatOpen() const { selectedProject } = useProject() const { conversations, currentChatId, setCurrentChatId, getCurrentChat } = usePersistentChat() const [isDialogOpen, setIsDialogOpen] = useState(false) const sorted = useMemo(() => { return [...conversations].sort((a, b) => a.lastUpdated < b.lastUpdated ? 1 : -1, ) }, [conversations]) function handleNewChat() { analytics.newChatStarted() const newId = generateId() setCurrentChatId(newId) setIsDialogOpen(false) } function formatRelativeTime(isoString: string): string { return formatDistanceToNow(new Date(isoString), { addSuffix: true }) } return (