"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"; 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 = crypto.randomUUID(); setCurrentChatId(newId); setIsDialogOpen(false); } function formatRelativeTime(isoString: string): string { return formatDistanceToNow(new Date(isoString), { addSuffix: true }); } return (

{getCurrentChat()?.title ?? "New Chat"}

Conversations Project{" "} {selectedProject}
{sorted.map((c) => { const isActive = c.id === currentChatId; return (
{ setCurrentChatId(c.id); setIsDialogOpen(false); }} className={cn( "flex items-center justify-between rounded-md px-3 py-2 outline-none", "transition-colors", isActive ? "bg-primary/10" : "hover:bg-muted", )} aria-current={isActive ? "true" : undefined} >
{c.title || "Untitled Chat"}
Last updated {formatRelativeTime(c.lastUpdated)}
); })} {sorted.length === 0 && (
No conversations yet
)}
); }