@@ -11,12 +11,21 @@ import { io, Socket } from "socket.io-client";
11
11
import { useAuth } from "@/app/auth/auth-context" ;
12
12
import LoadingScreen from "@/components/common/loading-screen" ;
13
13
import { sendAiMessage } from "@/lib/api/openai/send-ai-message" ;
14
+ import { getChatHistory } from "@/lib/api/collab-service/get-chat-history" ;
14
15
15
16
interface Message {
16
17
id : string ;
17
18
userId : string ;
18
19
text : string ;
19
20
timestamp : Date ;
21
+ messageIndex ?: number ;
22
+ }
23
+
24
+ interface ChatHistoryMessage {
25
+ messageIndex : number ;
26
+ userId : string ;
27
+ text : string ;
28
+ timestamp : string ;
20
29
}
21
30
22
31
export default function Chat ( { roomId } : { roomId : string } ) {
@@ -28,10 +37,45 @@ export default function Chat({ roomId }: { roomId: string }) {
28
37
const [ partnerMessages , setPartnerMessages ] = useState < Message [ ] > ( [ ] ) ;
29
38
const [ aiMessages , setAiMessages ] = useState < Message [ ] > ( [ ] ) ;
30
39
const [ isConnected , setIsConnected ] = useState ( false ) ;
40
+ const [ isLoading , setIsLoading ] = useState ( true ) ;
31
41
const lastMessageRef = useRef < HTMLDivElement | null > ( null ) ;
32
42
33
43
useEffect ( ( ) => {
34
- if ( ! auth ?. user ?. id ) return ; // Avoid connecting if user is not authenticated
44
+ const fetchChatHistory = async ( ) => {
45
+ try {
46
+ const response = await getChatHistory ( roomId ) ;
47
+
48
+ if ( response . ok ) {
49
+ const history : ChatHistoryMessage [ ] = await response . json ( ) ;
50
+ const formattedHistory = history . map ( ( msg ) => ( {
51
+ id : msg . messageIndex . toString ( ) ,
52
+ userId : msg . userId ,
53
+ text : msg . text ,
54
+ timestamp : new Date ( msg . timestamp ) ,
55
+ messageIndex : msg . messageIndex ,
56
+ } ) ) ;
57
+
58
+ formattedHistory . sort (
59
+ ( a : Message , b : Message ) =>
60
+ ( a . messageIndex ?? 0 ) - ( b . messageIndex ?? 0 )
61
+ ) ;
62
+
63
+ setPartnerMessages ( formattedHistory ) ;
64
+ }
65
+ } catch ( error ) {
66
+ console . error ( "Error fetching chat history:" , error ) ;
67
+ } finally {
68
+ setIsLoading ( false ) ;
69
+ }
70
+ } ;
71
+
72
+ if ( roomId ) {
73
+ fetchChatHistory ( ) ;
74
+ }
75
+ } , [ roomId ] ) ;
76
+
77
+ useEffect ( ( ) => {
78
+ if ( ! auth ?. user ?. id ) return ;
35
79
36
80
const socketInstance = io (
37
81
process . env . NEXT_PUBLIC_COLLAB_SERVICE_URL || "http://localhost:3002" ,
@@ -52,7 +96,24 @@ export default function Chat({ roomId }: { roomId: string }) {
52
96
} ) ;
53
97
54
98
socketInstance . on ( "chatMessage" , ( message : Message ) => {
55
- setPartnerMessages ( ( prev ) => [ ...prev , message ] ) ;
99
+ setPartnerMessages ( ( prev ) => {
100
+ const exists = prev . some (
101
+ ( msg ) => msg . messageIndex === message . messageIndex
102
+ ) ;
103
+ if ( exists ) return prev ;
104
+
105
+ const newMessage = {
106
+ ...message ,
107
+ id : message . messageIndex ?. toString ( ) || crypto . randomUUID ( ) ,
108
+ timestamp : new Date ( message . timestamp ) ,
109
+ } ;
110
+
111
+ const newMessages = [ ...prev , newMessage ] . sort (
112
+ ( a , b ) => ( a . messageIndex ?? 0 ) - ( b . messageIndex ?? 0 )
113
+ ) ;
114
+
115
+ return newMessages ;
116
+ } ) ;
56
117
} ) ;
57
118
58
119
setSocket ( socketInstance ) ;
@@ -77,20 +138,19 @@ export default function Chat({ roomId }: { roomId: string }) {
77
138
const sendMessage = async ( ) => {
78
139
if ( ! newMessage . trim ( ) || ! socket || ! isConnected || ! own_user_id ) return ;
79
140
80
- const message = {
81
- id : crypto . randomUUID ( ) ,
82
- userId : own_user_id ,
83
- text : newMessage ,
84
- timestamp : new Date ( ) ,
85
- } ;
86
-
87
141
if ( chatTarget === "partner" ) {
88
142
socket . emit ( "sendMessage" , {
89
143
roomId,
90
144
userId : own_user_id ,
91
145
text : newMessage ,
92
146
} ) ;
93
147
} else {
148
+ const message : Message = {
149
+ id : crypto . randomUUID ( ) ,
150
+ userId : own_user_id ,
151
+ text : newMessage ,
152
+ timestamp : new Date ( ) ,
153
+ } ;
94
154
setAiMessages ( ( prev ) => [ ...prev , message ] ) ;
95
155
const response = await sendAiMessage ( newMessage ) ;
96
156
const data = await response . json ( ) ;
@@ -110,7 +170,10 @@ export default function Chat({ roomId }: { roomId: string }) {
110
170
} ;
111
171
112
172
const formatTimestamp = ( date : Date ) => {
113
- return new Date ( date ) . toLocaleTimeString ( [ ] , {
173
+ if ( ! ( date instanceof Date ) || isNaN ( date . getTime ( ) ) ) {
174
+ return "Invalid Date" ;
175
+ }
176
+ return date . toLocaleTimeString ( [ ] , {
114
177
hour : "2-digit" ,
115
178
minute : "2-digit" ,
116
179
} ) ;
@@ -134,7 +197,7 @@ export default function Chat({ roomId }: { roomId: string }) {
134
197
</ div >
135
198
) ;
136
199
137
- if ( ! own_user_id ) {
200
+ if ( ! own_user_id || isLoading ) {
138
201
return < LoadingScreen /> ;
139
202
}
140
203
0 commit comments