11import React , { FC , useEffect , useState , useRef } from "react" ;
22import { useLocation , useNavigate } from "react-router" ;
3- import { FaArrowLeft , FaPlus , FaTimes , FaFilter , FaMagic , FaEdit , FaCheck , FaExclamationCircle , FaThumbsUp } from "react-icons/fa" ;
3+ import { FaArrowLeft } from "react-icons/fa" ;
44import { MultiRangeSlider } from "../components/MultiRangeSlider" ;
55import * as d3 from "d3" ; // Make sure to install @types /d3 and d3
66
77// import { getErrorMessage } from "../lib/errors";
88import { useNotifications } from "../lib/notifications" ;
9- import { FrequencySelector } from "../components/FrequencySelector" ;
109import { TopicRefiner } from "../components/TopicRefiner" ;
1110
12- // Mock data - replace with actual API calls in your implementation
13- const mockTopicData = [
14- { name : "data-visualization" , count : 342 } ,
15- { name : "graph-theory" , count : 289 } ,
16- { name : "network-analysis" , count : 256 } ,
17- { name : "scientific-computing" , count : 198 } ,
18- { name : "python" , count : 187 } ,
19- { name : "javascript" , count : 165 } ,
20- { name : "d3" , count : 142 } ,
21- { name : "typescript" , count : 128 } ,
22- { name : "react" , count : 112 } ,
23- { name : "machine-learning" , count : 98 } ,
24- { name : "data-science" , count : 87 } ,
25- { name : "visualization" , count : 76 } ,
26- { name : "neo4j" , count : 65 } ,
27- { name : "graphql" , count : 54 } ,
28- { name : "sigma-js" , count : 43 } ,
29- ] ;
30-
3111// Topic Histogram Component
3212interface TopicHistogramProps {
3313 data : Array < { name : string ; count : number } > ;
@@ -145,7 +125,6 @@ const TopicHistogram: FC = () => {
145125 // Get both search term and user topic from location state
146126 const searchTerm = ( location . state as { searchTerm ?: string } | undefined ) ?. searchTerm || "" ;
147127 const userTopic = ( location . state as { userTopic ?: string } | undefined ) ?. userTopic || "" ;
148- console . log ( "userTopic" , userTopic ) ;
149128
150129 // Add state for storing the user topic
151130 const [ originalTopic , setOriginalTopic ] = useState ( userTopic ) ;
@@ -161,7 +140,7 @@ const TopicHistogram: FC = () => {
161140
162141 // State for frequency range
163142 const [ frequencyRange , setFrequencyRange ] = useState ( { min : 0 , max : 100 } ) ;
164- const maxCount = Math . max ( ...extractedTopics . map ( item => item . count ) , 1 ) ;
143+ const maxCount = Math . max ( ...extractedTopics . map ( item => item . count || 0 ) , 1 ) ;
165144
166145 // Update frequency range when maxCount changes
167146 useEffect ( ( ) => {
@@ -199,17 +178,50 @@ const TopicHistogram: FC = () => {
199178 }
200179 } , [ searchTerm , userTopic , navigate , notify ] ) ;
201180
202- // Effect to simulate topic extraction when search term changes
181+ // Effect to handle topic extraction when search term changes
203182 useEffect ( ( ) => {
204- if ( searchTerm ) {
205- setIsLoading ( true ) ;
206- // Simulate API call to extract topics
207- setTimeout ( ( ) => {
208- setExtractedTopics ( mockTopicData ) ;
183+ // Add a check for extractedTopics length to prevent refetching
184+ if ( ! userTopic || extractedTopics . length > 0 ) return ;
185+
186+ setIsLoading ( true ) ;
187+
188+ fetch ( 'http://127.0.0.1:5002/process-topics' , {
189+ method : 'POST' ,
190+ headers : {
191+ 'Content-Type' : 'application/json' ,
192+ } ,
193+ body : JSON . stringify ( {
194+ topic : userTopic ,
195+ searchTerm : searchTerm
196+ } )
197+ } )
198+ . then ( response => {
199+ if ( ! response . ok ) {
200+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
201+ }
202+ return response . json ( ) ;
203+ } )
204+ . then ( data => {
205+ if ( data . success && data . data . length === 0 ) {
206+ notify ( {
207+ message : "No topics found for this search. Try a different topic." ,
208+ type : "warning"
209+ } ) ;
210+ }
211+ setExtractedTopics ( data . data || [ ] ) ;
212+ } )
213+ . catch ( error => {
214+ console . error ( 'Fetch error:' , error ) ;
215+ notify ( {
216+ message : "Failed to fetch topics. Please try again." ,
217+ type : "error"
218+ } ) ;
219+ setExtractedTopics ( [ ] ) ;
220+ } )
221+ . finally ( ( ) => {
209222 setIsLoading ( false ) ;
210- } , 1000 ) ;
211- }
212- } , [ searchTerm ] ) ;
223+ } ) ;
224+ } , [ userTopic ] ) ;
213225
214226 // Effect to update selected topics based on frequency range
215227 useEffect ( ( ) => {
@@ -266,8 +278,6 @@ const TopicHistogram: FC = () => {
266278
267279 // Function to handle form submission for the final step
268280 const handleSubmit = ( ) => {
269- console . log ( "Final topics submitted:" , finalTopics ) ;
270- // Navigate to graph view with the selected topics
271281 navigate ( '/graph' , { state : { topics : finalTopics } } ) ;
272282 } ;
273283
0 commit comments