aboutsummaryrefslogtreecommitdiff
path: root/apps/web/components/views/chat/index.tsx
blob: 2b7befa13366b501a615f0392091cbfd45be9f5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"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 (
		<div className="flex flex-col h-full overflow-y-hidden border-l bg-background">
			<div className="border-b px-4 py-3 flex justify-between items-center">
				<h3 className="text-lg font-semibold line-clamp-1 text-ellipsis overflow-hidden">
					{getCurrentChat()?.title ?? "New Chat"}
				</h3>
				<div className="flex items-center gap-2">
					<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
						<DialogTrigger asChild>
							<Button
								variant="outline"
								size="icon"
								onClick={() => analytics.chatHistoryViewed()}
							>
								<HistoryIcon className="size-4 text-muted-foreground" />
							</Button>
						</DialogTrigger>
						<DialogContent className="sm:max-w-lg">
							<DialogHeader className="pb-4 border-b rounded-t-lg">
								<DialogTitle className="">Conversations</DialogTitle>
								<DialogDescription>
									Project{" "}
									<span className="font-mono font-medium">
										{selectedProject}
									</span>
								</DialogDescription>
							</DialogHeader>

							<ScrollArea className="max-h-96">
								<div className="flex flex-col gap-1">
									{sorted.map((c) => {
										const isActive = c.id === currentChatId;
										return (
											<div
												key={c.id}
												role="button"
												tabIndex={0}
												onClick={() => {
													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}
											>
												<div className="min-w-0">
													<div className="flex items-center gap-2">
														<span
															className={cn(
																"text-sm font-medium truncate",
																isActive ? "text-foreground" : undefined,
															)}
														>
															{c.title || "Untitled Chat"}
														</span>
													</div>
													<div className="text-xs text-muted-foreground">
														Last updated {formatRelativeTime(c.lastUpdated)}
													</div>
												</div>
												<Button
													type="button"
													variant="ghost"
													size="icon"
													onClick={(e) => {
														e.stopPropagation();
														analytics.chatDeleted();
													}}
													aria-label="Delete conversation"
												>
													<Trash2 className="size-4 text-muted-foreground" />
												</Button>
											</div>
										);
									})}
									{sorted.length === 0 && (
										<div className="text-xs text-muted-foreground px-3 py-2">
											No conversations yet
										</div>
									)}
								</div>
							</ScrollArea>
							<Button
								variant="outline"
								size="lg"
								className="w-full border-dashed"
								onClick={handleNewChat}
							>
								<Plus className="size-4 mr-1" /> New Conversation
							</Button>
						</DialogContent>
					</Dialog>
					<Button variant="outline" size="icon" onClick={handleNewChat}>
						<Plus className="size-4 text-muted-foreground" />
					</Button>
					<Button
						variant="outline"
						size="icon"
						onClick={() => setIsOpen(false)}
					>
						<X className="size-4 text-muted-foreground" />
					</Button>
				</div>
			</div>
			<ChatMessages />
		</div>
	);
}