Skip to content

Commit e10b5b5

Browse files
committed
fix:(Fixed on Supabase integration for chatbot AI)
1 parent 763612a commit e10b5b5

File tree

1 file changed

+44
-22
lines changed

1 file changed

+44
-22
lines changed

components/ui/chats.tsx

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88
import { createClient } from '@supabase/supabase-js';
99
import { Button } from "@/components/ui/button";
10-
import React, { useState, JSX, SVGProps, useRef } from "react";
10+
import React, { useState, useEffect, JSX, SVGProps, useRef } from "react";
1111
import { Textarea } from "@/components/ui/textarea";
1212

1313
type BotReply = string | undefined;
@@ -33,6 +33,27 @@ export default function Chats({ author }: ChatProp) {
3333
const [cooldown, setCooldown] = useState(false);
3434
const lastMessageTime = useRef(Date.now());
3535

36+
async function saveMessageToSupabase(message: {
37+
role: string;
38+
content: string;
39+
}) {
40+
try {
41+
const { data, error } = await supabase.from("bot_chat_messages").insert([
42+
{
43+
role: message.role,
44+
content: message.content,
45+
author,
46+
timestamp: new Date().toISOString(),
47+
},
48+
]);
49+
50+
if (error) throw error;
51+
console.log("Message saved to Supabase:", data);
52+
} catch (error) {
53+
console.error("Error saving message to Supabase:", error);
54+
}
55+
}
56+
3657
const sendMessage = async () => {
3758
if (!inputMessage.trim()) return;
3859

@@ -52,27 +73,6 @@ export default function Chats({ author }: ChatProp) {
5273
setMessages((prevMessages) => [...prevMessages, userMessage]);
5374
setInputMessage("");
5475

55-
async function saveMessageToSupabase(message: {
56-
role: string;
57-
content: string;
58-
}) {
59-
try {
60-
const { data, error } = await supabase.from("bot_chat_messages").insert([
61-
{
62-
role: message.role,
63-
content: message.content,
64-
author,
65-
timestamp: new Date().toISOString(),
66-
},
67-
]);
68-
69-
if (error) throw error;
70-
console.log("Message saved to Supabase:", data);
71-
} catch (error) {
72-
console.error("Error saving message to Supabase:", error);
73-
}
74-
}
75-
7676
await saveMessageToSupabase(userMessage);
7777

7878
try {
@@ -139,6 +139,28 @@ export default function Chats({ author }: ChatProp) {
139139
}
140140
};
141141

142+
useEffect(() => {
143+
async function loadMessagesFromSupabase() {
144+
try {
145+
const { data, error } = await supabase
146+
.from("bot_user_messages")
147+
.select("*")
148+
.eq("author", author)
149+
.order("timestamp", { ascending: true });
150+
151+
if (error) throw error;
152+
153+
if (data) {
154+
setMessages(data.map(({ role, content }) => ({ role, content })));
155+
}
156+
} catch (error) {
157+
console.error("Error loading messages from Supabase:", error);
158+
}
159+
}
160+
161+
loadMessagesFromSupabase();
162+
}, [author]);
163+
142164
return (
143165
<div className="flex min-h-[100dvh] bg-[#1a1a1a] text-white">
144166
<div className="hidden md:flex flex-col items-center justify-center w-1/3 bg-[#6b3fa0] p-8">

0 commit comments

Comments
 (0)