@@ -10,7 +10,6 @@ import {
1010 Minus ,
1111 Check
1212} from "lucide-react" ;
13- import { API_ENDPOINTS } from '../lib/config' ;
1413import { FaArrowLeft } from "react-icons/fa" ;
1514
1615interface AIModel {
@@ -33,24 +32,26 @@ interface TopicRefinerProps {
3332 llmSuggestions : string [ ] ;
3433 setLlmSuggestions : ( suggestions : string [ ] ) => void ;
3534 selectedTopics : string [ ] ;
35+ setSelectedTopics : ( topics : string [ ] ) => void ;
3636 newTopic : string ;
3737 setNewTopic : ( topic : string ) => void ;
38- addNewTopic : ( ) => void ;
3938 prevStep : ( ) => void ;
4039 handleSubmit : ( ) => void ;
4140 searchTerm : string ;
41+ onRequestSuggestions : ( model : string , prompt : string , apiKey : string , topics : string [ ] ) => Promise < void > ;
4242}
4343
4444export const TopicRefiner : FC < Omit < TopicRefinerProps , 'isLlmProcessing' > > = ( {
4545 llmSuggestions = [ ] ,
4646 setLlmSuggestions,
4747 selectedTopics = [ ] ,
48+ setSelectedTopics,
4849 newTopic = "" ,
4950 setNewTopic,
50- addNewTopic,
5151 prevStep,
5252 handleSubmit,
53- searchTerm
53+ searchTerm,
54+ onRequestSuggestions
5455} ) => {
5556 const [ showPromptModal , setShowPromptModal ] = useState ( false ) ;
5657 const [ showWelcomeModal , setShowWelcomeModal ] = useState ( true ) ;
@@ -91,29 +92,30 @@ export const TopicRefiner: FC<Omit<TopicRefinerProps, 'isLlmProcessing'>> = ({
9192 }
9293
9394 try {
94- const response = await fetch ( API_ENDPOINTS . AI_PROCESS , {
95- method : 'POST' ,
96- headers : { 'Content-Type' : 'application/json' } ,
97- body : JSON . stringify ( {
98- selectedModel,
99- customPrompt,
100- apiKey,
101- searchTerm,
102- selectedTopics
103- } )
104- } ) ;
105- const data = await response . json ( ) ;
106- if ( data . success && data . result ) {
107- setLlmSuggestions ( data . result ) ;
108- } else {
109- alert ( 'Failed to get AI suggestions.' ) ;
110- }
95+ await onRequestSuggestions ( selectedModel , customPrompt , apiKey , selectedTopics ) ;
11196 setShowPromptModal ( false ) ;
11297 } catch {
11398 alert ( 'Failed to get AI suggestions. Please try again.' ) ;
11499 }
115100 } ;
116101
102+ const handleAddNewTopic = ( ) => {
103+ if ( ! newTopic . trim ( ) ) return ; // Don't add empty topics
104+
105+ // Add to selected topics if not already present
106+ if ( ! selectedTopics . includes ( newTopic . trim ( ) ) ) {
107+ setSelectedTopics ( [ ...selectedTopics , newTopic . trim ( ) ] ) ;
108+ }
109+
110+ // Add to finalized topics if not already present
111+ if ( ! finalizedTopics . includes ( newTopic . trim ( ) ) ) {
112+ setFinalizedTopics ( prev => [ ...prev , newTopic . trim ( ) ] ) ;
113+ }
114+
115+ // Clear the input
116+ setNewTopic ( "" ) ;
117+ } ;
118+
117119 return (
118120 < main className = "container-fluid py-4" style = { { height : '100vh' , overflowY : 'auto' } } >
119121 { /* Navigation/Header Row */ }
@@ -255,12 +257,16 @@ export const TopicRefiner: FC<Omit<TopicRefinerProps, 'isLlmProcessing'>> = ({
255257 placeholder = "Add a custom topic"
256258 value = { newTopic }
257259 onChange = { ( e ) => setNewTopic ( e . target . value ) }
258- onKeyDown = { ( e ) => e . key === "Enter" && addNewTopic ( ) }
260+ onKeyDown = { ( e ) => {
261+ if ( e . key === "Enter" && newTopic . trim ( ) ) {
262+ handleAddNewTopic ( ) ;
263+ }
264+ } }
259265 />
260266 < button
261267 className = "btn btn-primary"
262- onClick = { addNewTopic }
263- disabled = { ! newTopic }
268+ onClick = { handleAddNewTopic }
269+ disabled = { ! newTopic . trim ( ) }
264270 >
265271 < Plus size = { 16 } />
266272 </ button >
0 commit comments