@@ -19,7 +19,7 @@ import { Badge } from "@/components/ui/badge";
1919import BiasMeter from "@/components/bias-meter" ;
2020import axios from "axios" ;
2121
22- const backend_url = process . env . NEXT_PUBLIC_API_URL ;
22+ const backendUrl = ( process . env . NEXT_PUBLIC_API_URL ?? "http://localhost:8000" ) . trim ( ) ;
2323
2424/**
2525 * Renders the article analysis page with summary, perspectives, fact checks, bias meter, AI chat, and sources.
@@ -32,6 +32,7 @@ export default function AnalyzePage() {
3232 const [ activeTab , setActiveTab ] = useState ( "summary" ) ;
3333 const [ message , setMessage ] = useState ( "" ) ;
3434 const [ isLoading , setIsLoading ] = useState ( true ) ;
35+ const [ isChatLoading , setIsChatLoading ] = useState ( false ) ;
3536 const [ messages , setMessages ] = useState < { role : string ; content : string } [ ] > (
3637 [
3738 {
@@ -80,20 +81,42 @@ export default function AnalyzePage() {
8081
8182 async function handleSendMessage ( e : React . FormEvent ) {
8283 e . preventDefault ( ) ;
83- if ( ! message . trim ( ) ) return ;
84- const newMessages = [ ...messages , { role : "user" , content : message } ] ;
84+ const userMessage = message . trim ( ) ;
85+ if ( ! userMessage || isChatLoading ) return ;
86+
87+ const newMessages = [ ...messages , { role : "user" , content : userMessage } ] ;
8588 setMessages ( newMessages ) ;
8689 setMessage ( "" ) ;
90+ setIsChatLoading ( true ) ;
8791
88- const res = await axios . post ( `${ backend_url } /api/chat` , {
89- message : message ,
90- } ) ;
91- const data = res . data ;
92-
93- console . log ( data ) ;
92+ try {
93+ const articleContext = analysisData ?. cleaned_text ?. trim ( ) ?? "" ;
94+ const res = await axios . post (
95+ `${ backendUrl } /api/chat` ,
96+ {
97+ message : userMessage ,
98+ article_context : articleContext ,
99+ } ,
100+ { timeout : 45000 }
101+ ) ;
102+ const answer = res . data ?. answer ?? "I could not generate an answer right now." ;
94103
95- // 🔹 Step 2: Append LLM’s response
96- setMessages ( [ ...newMessages , { role : "assistant" , content : data . answer } ] ) ;
104+ setMessages ( [ ...newMessages , { role : "assistant" , content : answer } ] ) ;
105+ } catch ( error : any ) {
106+ const fallback =
107+ error ?. response ?. data ?. detail ||
108+ error ?. message ||
109+ "Chat request failed. Please try again." ;
110+ setMessages ( [
111+ ...newMessages ,
112+ {
113+ role : "assistant" ,
114+ content : `Sorry, I couldn't fetch a reply: ${ fallback } ` ,
115+ } ,
116+ ] ) ;
117+ } finally {
118+ setIsChatLoading ( false ) ;
119+ }
97120 }
98121 if ( isLoading ) {
99122 return (
@@ -187,10 +210,10 @@ export default function AnalyzePage() {
187210 < Badge
188211 variant = {
189212 fact . verdict === "True"
190- ? "success "
213+ ? "secondary "
191214 : fact . verdict === "False"
192215 ? "destructive"
193- : "warning "
216+ : "outline "
194217 }
195218 >
196219 { fact . verdict }
@@ -255,7 +278,7 @@ export default function AnalyzePage() {
255278 value = { message }
256279 onChange = { ( e ) => setMessage ( e . target . value ) }
257280 />
258- < Button type = "submit" disabled = { ! message . trim ( ) } >
281+ < Button type = "submit" disabled = { ! message . trim ( ) || isChatLoading } >
259282 < Send />
260283 </ Button >
261284 </ form >
0 commit comments