11"use client"
22
33import type React from "react"
4- import { useState , useEffect } from "react"
4+ import { useState , useEffect , useRef } from "react"
5+ import { useRouter } from "next/navigation"
56import Link from "next/link"
67import { ArrowLeft , Globe , MessageSquare , Send , ThumbsDown , ThumbsUp , Menu , Link as LinkIcon } from "lucide-react"
78import { Button } from "@/components/ui/button"
@@ -18,6 +19,8 @@ import BiasMeter from "@/components/bias-meter"
1819 */
1920export default function AnalyzePage ( ) {
2021 const [ analysisData , setAnalysisData ] = useState < any > ( null )
22+ const router = useRouter ( )
23+ const isRedirecting = useRef ( false ) ;
2124 const [ activeTab , setActiveTab ] = useState ( "summary" )
2225 const [ message , setMessage ] = useState ( "" )
2326 const [ isLoading , setIsLoading ] = useState ( true )
@@ -38,6 +41,41 @@ export default function AnalyzePage() {
3841 return ( ) => clearTimeout ( timer )
3942 } , [ ] )
4043
44+
45+ useEffect ( ( ) => {
46+
47+ if ( isRedirecting . current ) {
48+ return ;
49+ }
50+
51+ const timer = setTimeout ( ( ) => setIsLoading ( false ) , 1500 ) ;
52+ const storedData = sessionStorage . getItem ( "analysisResult" ) ;
53+
54+ if ( storedData ) {
55+ const parsedData = JSON . parse ( storedData ) ;
56+ const requiredFields = [ 'cleaned_text' , 'facts' , 'sentiment' , 'perspective' , 'score' ] ;
57+ const isDataValid = requiredFields . every ( field => parsedData [ field ] !== undefined && parsedData [ field ] !== null ) ;
58+
59+ if ( isDataValid ) {
60+ setAnalysisData ( parsedData ) ;
61+ } else {
62+ console . warn ( "Incomplete analysis data. Redirecting..." ) ;
63+
64+
65+ isRedirecting . current = true ;
66+ router . push ( "/analyze" ) ;
67+ }
68+ } else {
69+ console . warn ( "No analysis result found. Redirecting..." ) ;
70+
71+ isRedirecting . current = true ;
72+ router . push ( "/analyze" ) ;
73+ }
74+
75+ return ( ) => clearTimeout ( timer ) ;
76+ } , [ router ] ) ;
77+
78+
4179 const handleSendMessage = ( e : React . FormEvent ) => {
4280 e . preventDefault ( )
4381 if ( ! message . trim ( ) ) return
@@ -57,7 +95,7 @@ export default function AnalyzePage() {
5795 )
5896 }
5997
60- const { cleaned_text, facts, sentiment, perspective, score } = analysisData
98+ const { cleaned_text, facts= [ ] , sentiment, perspective, score } = analysisData
6199
62100 return (
63101 < div className = "flex flex-col min-h-screen" >
@@ -92,36 +130,60 @@ export default function AnalyzePage() {
92130 </ TabsContent >
93131
94132 < TabsContent value = "perspectives" >
95- < div className = "space-y-4" >
96- < h2 className = "text-xl font-semibold" > Counter-Perspective</ h2 >
97- < p className = "italic" > "{ perspective . perspective } "</ p >
98- < h3 className = "font-medium" > Reasoning:</ h3 >
99- < p > { perspective . reasoning } </ p >
100- </ div >
101- </ TabsContent >
133+ { perspective ? (
134+ < div className = "space-y-4" >
135+ < h2 className = "text-xl font-semibold" > Counter-Perspective</ h2 >
136+ < p className = "italic" > "{ perspective . perspective } "</ p >
137+ < h3 className = "font-medium" > Reasoning:</ h3 >
138+ < p > { perspective . reasoning } </ p >
139+ </ div >
140+ ) : (
141+ < div className = "text-muted-foreground p-4" >
142+ No counter-perspective was generated for this content.
143+ </ div >
144+ ) }
145+ </ TabsContent >
102146
103147 < TabsContent value = "facts" >
104- < div className = "space-y-4" >
105- { facts . map ( ( fact : any , idx : number ) => (
106- < Card key = { idx } className = "border" >
107- < CardHeader >
108- < div className = "flex justify-between items-center" >
109- < CardTitle > { fact . original_claim } </ CardTitle >
110- < Badge variant = { fact . verdict === 'True' ? 'success' : fact . verdict === 'False' ? 'destructive' : 'warning' } >
111- { fact . verdict }
112- </ Badge >
113- </ div >
114- </ CardHeader >
115- < CardContent >
116- < p className = "mb-2" > { fact . explanation } </ p >
117- < Link href = { fact . source_link } target = "_blank" className = "flex items-center text-sm hover:underline" >
118- < LinkIcon className = "mr-1 h-4 w-4" /> Source
119- </ Link >
120- </ CardContent >
121- </ Card >
122- ) ) }
123- </ div >
124- </ TabsContent >
148+ < div className = "space-y-4" >
149+ { facts . length > 0 ? (
150+ facts . map ( ( fact : any , idx : number ) => (
151+ < Card key = { idx } className = "border" >
152+ < CardHeader >
153+ < div className = "flex justify-between items-center" >
154+ < CardTitle > { fact . original_claim } </ CardTitle >
155+ < Badge
156+ variant = {
157+ fact . verdict === 'True'
158+ ? 'success'
159+ : fact . verdict === 'False'
160+ ? 'destructive'
161+ : 'warning'
162+ }
163+ >
164+ { fact . verdict }
165+ </ Badge >
166+ </ div >
167+ </ CardHeader >
168+ < CardContent >
169+ < p className = "mb-2" > { fact . explanation } </ p >
170+ < Link
171+ href = { fact . source_link }
172+ target = "_blank"
173+ className = "flex items-center text-sm hover:underline"
174+ >
175+ < LinkIcon className = "mr-1 h-4 w-4" /> Source
176+ </ Link >
177+ </ CardContent >
178+ </ Card >
179+ ) )
180+ ) : (
181+ < div className = "text-muted-foreground p-4" >
182+ No specific claims were identified for fact-checking in this content.
183+ </ div >
184+ ) }
185+ </ div >
186+ </ TabsContent >
125187 </ Tabs >
126188 </ div >
127189
0 commit comments