@@ -5,13 +5,7 @@ import { useState, useEffect, useRef } from "react";
55import { useRouter } from "next/navigation" ;
66import Link from "next/link" ;
77import {
8- ArrowLeft ,
9- Globe ,
10- MessageSquare ,
118 Send ,
12- ThumbsDown ,
13- ThumbsUp ,
14- Menu ,
159 Link as LinkIcon ,
1610} from "lucide-react" ;
1711import { Button } from "@/components/ui/button" ;
@@ -23,12 +17,11 @@ import {
2317 CardHeader ,
2418 CardTitle ,
2519} from "@/components/ui/card" ;
26- import { Avatar , AvatarFallback , AvatarImage } from "@/components/ui/avatar" ;
2720import { Input } from "@/components/ui/input" ;
2821import { Badge } from "@/components/ui/badge" ;
29- import { Separator } from "@/components/ui/separator" ;
3022import BiasMeter from "@/components/bias-meter" ;
3123
24+
3225/**
3326 * Renders the article analysis page with summary, perspectives, fact checks, bias meter, AI chat, and sources.
3427 */
@@ -40,7 +33,6 @@ export default function AnalyzePage() {
4033 const [ activeTab , setActiveTab ] = useState ( "summary" ) ;
4134 const [ message , setMessage ] = useState ( "" ) ;
4235 const [ isLoading , setIsLoading ] = useState ( true ) ;
43- const [ isMobileMenuOpen , setIsMobileMenuOpen ] = useState ( false ) ;
4436 const [ messages , setMessages ] = useState < { role : string ; content : string } [ ] > (
4537 [
4638 {
@@ -52,54 +44,39 @@ export default function AnalyzePage() {
5244 ) ;
5345
5446 useEffect ( ( ) => {
55- const timer = setTimeout ( ( ) => setIsLoading ( false ) , 1500 ) ;
56- const storedData = sessionStorage . getItem ( "analysisResult" ) ;
5747 const storedBiasScore = sessionStorage . getItem ( "BiasScore" ) ;
48+ const storedData = sessionStorage . getItem ( "analysisResult" ) ;
49+ if ( storedBiasScore && storedData ) {
50+ setIsLoading ( false ) ;
51+ }
52+
5853 if ( storedBiasScore ) setBiasScore ( JSON . parse ( storedBiasScore ) . bias_score ) ;
5954 else console . warn ( "No bias score found." ) ;
6055
6156 if ( storedData ) setAnalysisData ( JSON . parse ( storedData ) ) ;
6257 else console . warn ( "No analysis result found" ) ;
63- return ( ) => clearTimeout ( timer ) ;
58+
6459 } , [ ] ) ;
6560
6661 useEffect ( ( ) => {
6762 if ( isRedirecting . current ) {
6863 return ;
6964 }
7065
71- const timer = setTimeout ( ( ) => setIsLoading ( false ) , 1500 ) ;
66+
7267 const storedData = sessionStorage . getItem ( "analysisResult" ) ;
68+ const storedBiasScore = sessionStorage . getItem ( "BiasScore" ) ;
7369
74- if ( storedData ) {
75- const parsedData = JSON . parse ( storedData ) ;
76- const requiredFields = [
77- "cleaned_text" ,
78- "facts" ,
79- "sentiment" ,
80- "perspective" ,
81- "score" ,
82- ] ;
83- const isDataValid = requiredFields . every (
84- ( field ) => parsedData [ field ] !== undefined && parsedData [ field ] !== null
85- ) ;
86-
87- if ( isDataValid ) {
88- setAnalysisData ( parsedData ) ;
89- } else {
90- console . warn ( "Incomplete analysis data. Redirecting..." ) ;
91-
92- isRedirecting . current = true ;
93- router . push ( "/analyze" ) ;
94- }
95- } else {
96- console . warn ( "No analysis result found. Redirecting..." ) ;
97-
98- isRedirecting . current = true ;
99- router . push ( "/analyze" ) ;
70+ if ( storedBiasScore && storedData ) {
71+ // inside here TS knows storedBiasScore and storedData are strings
72+ setBiasScore ( JSON . parse ( storedBiasScore ) . bias_score ) ;
73+ setAnalysisData ( JSON . parse ( storedData ) ) ;
74+ setIsLoading ( false ) ;
75+ } else {
76+ console . warn ( "No bias or data found. Redirecting..." ) ;
77+ router . push ( "/analyze" ) ;
10078 }
10179
102- return ( ) => clearTimeout ( timer ) ;
10380 } , [ router ] ) ;
10481
10582 const handleSendMessage = ( e : React . FormEvent ) => {
@@ -120,7 +97,7 @@ export default function AnalyzePage() {
12097 } , 1000 ) ;
12198 } ;
12299
123- if ( isLoading || ! analysisData ) {
100+ if ( isLoading || ! analysisData || ! biasScore ) {
124101 return (
125102 < div className = "flex items-center justify-center h-screen" >
126103 < div className = "text-muted-foreground" > Analyzing content...</ div >
@@ -156,7 +133,7 @@ export default function AnalyzePage() {
156133 </ Badge >
157134 </ div >
158135 < div className = "bg-card rounded-lg border p-4 mb-8" >
159- < BiasMeter score = { score } />
136+ < BiasMeter score = { biasScore } />
160137 < p className = "text-sm mt-2" > Bias Score: { biasScore } </ p >
161138 </ div >
162139
0 commit comments