7
7
*/
8
8
import { createClient } from '@supabase/supabase-js' ;
9
9
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" ;
11
11
import { Textarea } from "@/components/ui/textarea" ;
12
12
13
13
type BotReply = string | undefined ;
@@ -33,6 +33,27 @@ export default function Chats({ author }: ChatProp) {
33
33
const [ cooldown , setCooldown ] = useState ( false ) ;
34
34
const lastMessageTime = useRef ( Date . now ( ) ) ;
35
35
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
+
36
57
const sendMessage = async ( ) => {
37
58
if ( ! inputMessage . trim ( ) ) return ;
38
59
@@ -52,27 +73,6 @@ export default function Chats({ author }: ChatProp) {
52
73
setMessages ( ( prevMessages ) => [ ...prevMessages , userMessage ] ) ;
53
74
setInputMessage ( "" ) ;
54
75
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
-
76
76
await saveMessageToSupabase ( userMessage ) ;
77
77
78
78
try {
@@ -139,6 +139,28 @@ export default function Chats({ author }: ChatProp) {
139
139
}
140
140
} ;
141
141
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
+
142
164
return (
143
165
< div className = "flex min-h-[100dvh] bg-[#1a1a1a] text-white" >
144
166
< div className = "hidden md:flex flex-col items-center justify-center w-1/3 bg-[#6b3fa0] p-8" >
0 commit comments