@@ -11,7 +11,7 @@ import CloseIcon from "@mui/icons-material/Close";
1111import AutoFixHighIcon from "@mui/icons-material/AutoFixHigh" ;
1212import ContentCopyIcon from "@mui/icons-material/ContentCopy" ;
1313import { motion } from "framer-motion" ;
14- import axios from "axios" ;
14+ // import axios from "axios";
1515import Microphone from "./Microphone" ;
1616
1717interface SidebarProps {
@@ -27,7 +27,7 @@ const Sidebar: React.FC<SidebarProps> = ({ open, onClose, text, textArea }) => {
2727 const [ inputText , setInputText ] = useState ( text ) ;
2828 const [ transcript , setTranscript ] = useState < string | null > ( null ) ;
2929 const [ isFinal , setIsFinal ] = useState ( false ) ;
30- const [ inputFinal , setInputFinal ] = useState ( "" ) ;
30+ // const [inputFinal, setInputFinal] = useState("");
3131 console . log ( transcript , isFinal ) ;
3232
3333 useEffect ( ( ) => {
@@ -36,33 +36,29 @@ const Sidebar: React.FC<SidebarProps> = ({ open, onClose, text, textArea }) => {
3636 } , [ text ] ) ;
3737
3838 const fetchRephrasedPrompts = async ( ) => {
39-
40- console . log ( "Chrome runtime" , chrome . runtime ) ;
4139 setLoading ( true ) ;
42- if ( transcript && transcript . length > 0 ) {
43- console . log ( "First coniditon" , transcript ) ;
44- setInputFinal ( transcript ) ;
45- } else {
46- console . log ( "Second coniditon" , inputText ) ;
47- setInputFinal ( inputText ) ;
48- }
49- try {
50- const response = await axios . post (
51- "https://cl-func-ai-app.azurewebsites.net/api/AnalyzePrompt?code=m1O0Wffba6Ga_FufbEzGl8xppUHflNClvx2uFoEdn9N1AzFu_NFfvw==" ,
52- {
53- prompt : inputFinal . length > 0 ? inputFinal : inputText ,
54- }
55- ) ;
56- setSuggestions ( [
57- response . data . rephrasedPrompt1 ,
58- response . data . rephrasedPrompt2 ,
59- response . data . rephrasedPrompt3 ,
60- ] ) ;
61- } catch ( error ) {
62- console . error ( "Error fetching suggestions:" , error ) ;
40+ const finalInput = transcript && transcript . length > 0 ? transcript : inputText ;
41+
42+ if ( ! finalInput . trim ( ) ) {
43+ setLoading ( false ) ;
44+ return ;
6345 }
64- setLoading ( false ) ;
46+
47+ // Communicate with the content script
48+ window . postMessage ( { type : "FETCH_PROMPTS" , prompt : finalInput } , "*" ) ;
49+
50+ // Listen for the response
51+ const handleResponse = ( event : MessageEvent ) => {
52+ if ( event . data . type === "PROMPT_RESPONSE" ) {
53+ setSuggestions ( event . data . data . suggestions || [ ] ) ;
54+ setLoading ( false ) ;
55+ window . removeEventListener ( "message" , handleResponse ) ; // Clean up event listener
56+ }
57+ } ;
58+
59+ window . addEventListener ( "message" , handleResponse ) ;
6560 } ;
61+
6662
6763 const handleUsePrompt = ( prompt : string ) => {
6864 if ( ! textArea ) return ;
0 commit comments