-
Notifications
You must be signed in to change notification settings - Fork 88
/
chat-support.tsx
161 lines (152 loc) · 4.91 KB
/
chat-support.tsx
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
"use client";
import React, { useEffect, useRef, useState } from "react";
import { Button } from "@/components/ui/button";
import { Bot, Send } from "lucide-react";
import {
ChatBubble,
ChatBubbleAvatar,
ChatBubbleMessage,
} from "@/components/ui/chat/chat-bubble";
import { ChatInput } from "@/components/ui/chat/chat-input";
import {
ExpandableChat,
ExpandableChatHeader,
ExpandableChatBody,
ExpandableChatFooter,
} from "@/components/ui/chat/expandable-chat";
import { ChatMessageList } from "@/components/ui/chat/chat-message-list";
import { AnimatePresence, motion } from "framer-motion";
interface Message {
id: string;
content: string;
sender: "user" | "ai";
timestamp: string;
}
const initialChatSupportMessages: Message[] = [
{
id: "1",
content: "Hello! How can I help you today?",
sender: "ai",
timestamp: new Date().toLocaleTimeString(),
},
];
export default function ChatSupport() {
const [messages, setMessages] = useState<Message[]>(
initialChatSupportMessages,
);
const [inputMessage, setInputMessage] = useState("");
const messagesContainerRef = useRef<HTMLDivElement>(null);
useEffect(() => {
if (messagesContainerRef.current) {
messagesContainerRef.current.scrollTop =
messagesContainerRef.current.scrollHeight;
}
}, [messages]);
const handleSendMessage = () => {
if (inputMessage.trim()) {
const newMessage: Message = {
id: Date.now().toString(),
content: inputMessage,
sender: "user",
timestamp: new Date().toLocaleTimeString(),
};
setMessages([...messages, newMessage]);
setInputMessage("");
}
};
const onKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
handleSendMessage();
}
};
return (
<ExpandableChat
icon={<Bot className="h-6 w-6" />}
size="lg"
position="bottom-right"
>
<ExpandableChatHeader className="flex-col text-center justify-center">
<h1 className="text-xl font-semibold">Chat with our AI ✨</h1>
<p>Ask any question for our AI to answer</p>
<div className="flex gap-2 items-center pt-2">
<Button variant="secondary">New Chat</Button>
<Button variant="secondary">See FAQ</Button>
</div>
</ExpandableChatHeader>
<ExpandableChatBody>
<ChatMessageList
ref={messagesContainerRef}
className="dark:bg-muted/40"
>
<AnimatePresence>
{messages.map((message, index) => {
return (
<motion.div
key={index}
layout
initial={{ opacity: 0, scale: 1, y: 10, x: 0 }}
animate={{ opacity: 1, scale: 1, y: 0, x: 0 }}
exit={{ opacity: 0, scale: 1, y: 1, x: 0 }}
transition={{
opacity: { duration: 0.1 },
layout: {
type: "spring",
bounce: 0.3,
duration: index * 0.05 0.2,
},
}}
style={{ originX: 0.5, originY: 0.5 }}
className="flex flex-col"
>
<ChatBubble
key={message.id}
variant={message.sender === "user" ? "sent" : "received"}
>
<ChatBubbleAvatar
src={
message.sender === "user"
? "https://avatars.githubusercontent.com/u/114422072?s=400&u=8a176a310ca29c1578a70b1c33bdeea42bf000b4&v=4"
: ""
}
fallback={message.sender === "user" ? "US" : "🤖"}
/>
<ChatBubbleMessage
variant={message.sender === "user" ? "sent" : "received"}
>
{message.content}
</ChatBubbleMessage>
</ChatBubble>
</motion.div>
);
})}
</AnimatePresence>
</ChatMessageList>
</ExpandableChatBody>
<ExpandableChatFooter>
<form
onSubmit={(e) => {
e.preventDefault();
handleSendMessage();
}}
className="flex relative gap-2"
>
<ChatInput
onKeyDown={onKeyDown}
value={inputMessage}
onChange={(e) => setInputMessage(e.target.value)}
placeholder="Type a message..."
/>
<Button
disabled={!inputMessage.trim()}
type="submit"
size="icon"
className="absolute right-2 top-1/2 transform -translate-y-1/2 shrink-0"
>
<Send className="size-4" />
</Button>
</form>
</ExpandableChatFooter>
</ExpandableChat>
);
}