Repository navigation
Expand file tree
/
Copy pathChatWindow.tsx
More file actions
232 lines (213 loc) · 6.42 KB
/
Copy pathChatWindow.tsx
File metadata and controls
232 lines (213 loc) · 6.42 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import type { ReactNode } from "react";
import React, { useEffect, useRef, useState } from "react";
import {
FaBrain,
FaListAlt,
FaPlayCircle,
FaSave,
FaStar,
FaCopy,
} from "react-icons/fa";
import autoAnimate from "@formkit/auto-animate";
import PopIn from "./motions/popin";
import Expand from "./motions/expand";
import * as htmlToImage from "html-to-image";
interface ChatWindowProps {
children?: ReactNode;
className?: string;
messages: Message[];
}
const messageListId = "chat-window-message-list";
const ChatWindow = ({ messages, children, className }: ChatWindowProps) => {
const [hasUserScrolled, setHasUserScrolled] = useState(false);
const scrollRef = useRef<HTMLDivElement>(null);
const handleScroll = (event: React.UIEvent<HTMLDivElement>) => {
const { scrollTop, scrollHeight, clientHeight } = event.currentTarget;
// Use has scrolled if we have scrolled up at all from the bottom
if (scrollTop < scrollHeight - clientHeight - 10) {
setHasUserScrolled(true);
} else {
setHasUserScrolled(false);
}
};
useEffect(() => {
// Scroll to bottom on re-renders
if (scrollRef && scrollRef.current) {
if (!hasUserScrolled) {
scrollRef.current.scrollTop = scrollRef.current.scrollHeight;
}
}
});
useEffect(() => {
scrollRef.current && autoAnimate(scrollRef.current);
}, [messages]);
return (
<div
className={
"border-translucent flex w-full flex-col rounded-3xl border-2 border-white/20 bg-zinc-900 text-white shadow-2xl drop-shadow-lg " +
(className ?? "")
}
>
<MacWindowHeader />
<div
className="mb-2 mr-2 h-[11em] overflow-y-auto overflow-x-hidden sm-h:h-[16em] md-h:h-[21em] lg-h:h-[30em] "
ref={scrollRef}
onScroll={handleScroll}
id={messageListId}
>
{messages.map((message, index) => (
<ChatMessage key={`${index}-${message.type}`} message={message} />
))}
{children}
{messages.length === 0 ? (
<Expand delay={0.8} type="spring">
<ChatMessage
message={{
type: "system",
value:
"> Create an agent by adding a name / goal, and hitting deploy!",
}}
/>
<ChatMessage
message={{
type: "system",
value:
"📢 Please first provide your own OpenAI API key via the settings tab!",
}}
/>
</Expand>
) : (
""
)}
</div>
</div>
);
};
const MacWindowHeader = () => {
const saveElementAsImage = (elementId: string) => {
const element = document.getElementById(elementId);
if (!element) {
return;
}
htmlToImage
.toJpeg(element, {
height: element.scrollHeight,
style: {
overflowY: "visible",
maxHeight: "none",
border: "none",
},
})
.then((dataUrl) => {
const link = document.createElement("a");
link.href = dataUrl;
link.download = "element-image.png";
link.click();
})
.catch(console.error);
};
return (
<div className="flex items-center gap-1 rounded-t-3xl p-3">
<PopIn delay={0.4}>
<div className="h-3 w-3 rounded-full bg-red-500" />
</PopIn>
<PopIn delay={0.5}>
<div className="h-3 w-3 rounded-full bg-yellow-500" />
</PopIn>
<PopIn delay={0.6} className="flex-grow">
<div className="h-3 w-3 rounded-full bg-green-500" />
</PopIn>
<div
className="mr-1 flex cursor-pointer items-center gap-2 rounded-full border-2 border-white/30 p-1 px-2 hover:bg-white/10"
onClick={(): void => saveElementAsImage(messageListId)}
>
<FaSave size={12} />
<p className="font-mono">Save</p>
</div>
</div>
);
};
const ChatMessage = ({ message }: { message: Message }) => {
const [showCopy, setShowCopy] = useState(false);
const [copied, setCopied] = useState(false);
const handleCopyClick = () => {
void navigator.clipboard.writeText(message.value);
setCopied(true);
};
useEffect(() => {
let timeoutId: NodeJS.Timeout;
if (copied) {
timeoutId = setTimeout(() => {
setCopied(false);
}, 2000);
}
return () => {
clearTimeout(timeoutId);
};
}, [copied]);
return (
<div
className="mx-2 my-1 rounded-lg border-[2px] border-white/10 bg-white/20 p-1 font-mono text-sm hover:border-[#1E88E5]/40 sm:mx-4 sm:p-3 sm:text-base"
onMouseEnter={() => setShowCopy(true)}
onMouseLeave={() => setShowCopy(false)}
onClick={handleCopyClick}
>
<div className="mr-2 inline-block h-[0.9em]">
{getMessageIcon(message)}
</div>
<span className="mr-2 font-bold">{getMessagePrefix(message)}</span>
{message.type == "thinking" && (
<span className="italic text-zinc-400">
(Restart if this takes more than 30 seconds)
</span>
)}
<span>{message.value}</span>
<div className="relative">
{copied ? (
<span className="absolute bottom-0 right-0 rounded-full border-2 border-white/30 bg-zinc-800 p-1 px-2 text-gray-300">
Copied!
</span>
) : (
<span
className={`absolute bottom-0 right-0 rounded-full border-2 border-white/30 bg-zinc-800 p-1 px-2 ${
showCopy ? "visible" : "hidden"
}`}
>
<FaCopy className="text-white-300 cursor-pointer" />
</span>
)}
</div>
</div>
);
};
const getMessageIcon = (message: Message) => {
switch (message.type) {
case "goal":
return <FaStar className="text-yellow-400" />;
case "task":
return <FaListAlt className="text-gray-300" />;
case "thinking":
return <FaBrain className="mt-[0.1em] text-pink-400" />;
case "action":
return <FaPlayCircle className="text-green-500" />;
}
};
const getMessagePrefix = (message: Message) => {
switch (message.type) {
case "goal":
return "Embarking on a new goal:";
case "task":
return "Added task:";
case "thinking":
return "Thinking...";
case "action":
return message.info ? message.info : "Executing:";
}
};
export interface Message {
type: "goal" | "thinking" | "task" | "action" | "system";
info?: string;
value: string;
}
export default ChatWindow;
export { ChatMessage };