1- import React , { useEffect , useCallback } from 'react' ;
1+ import React , { useEffect , useCallback , useState } from 'react' ;
22import { motion , AnimatePresence } from 'framer-motion' ;
33import { X , Loader2 } from 'lucide-react' ;
44import { toast } from 'sonner' ;
@@ -13,11 +13,21 @@ const PROACTIVE_MESSAGES = [
1313] ;
1414
1515const AIChatbot = ( ) => {
16- const [ isOpen , setIsOpen ] = React . useState ( false ) ;
17- const [ isLoading , setIsLoading ] = React . useState ( true ) ;
18- const [ showProactive , setShowProactive ] = React . useState ( false ) ;
19- const [ proactiveMessage , setProactiveMessage ] = React . useState ( '' ) ;
20- const [ hasInteracted , setHasInteracted ] = React . useState ( false ) ;
16+ const [ isOpen , setIsOpen ] = useState ( false ) ;
17+ const [ isLoading , setIsLoading ] = useState ( true ) ;
18+ const [ showProactive , setShowProactive ] = useState ( false ) ;
19+ const [ proactiveMessage , setProactiveMessage ] = useState ( '' ) ;
20+ const [ hasInteracted , setHasInteracted ] = useState ( false ) ;
21+ // Nuevo estado para cargar el iframe solo tras el primer render de la DApp
22+ const [ shouldLoadIframe , setShouldLoadIframe ] = useState ( false ) ;
23+
24+ // EFICIENCIA: Cargamos el iframe 2 segundos después para no bloquear el hilo principal de la DApp
25+ useEffect ( ( ) => {
26+ const timer = setTimeout ( ( ) => {
27+ setShouldLoadIframe ( true ) ;
28+ } , 2000 ) ;
29+ return ( ) => clearTimeout ( timer ) ;
30+ } , [ ] ) ;
2131
2232 // Proactive engagement - show after 45 seconds of inactivity
2333 useEffect ( ( ) => {
@@ -51,6 +61,7 @@ const AIChatbot = () => {
5161 setIsLoading ( true ) ;
5262 setShowProactive ( false ) ;
5363 setHasInteracted ( true ) ;
64+ setShouldLoadIframe ( true ) ; // Aseguramos carga si el usuario hace clic rápido
5465 toast . info ( 'AI Assistant Loading' , {
5566 description : 'Opening AI Tor Assistant...'
5667 } ) ;
@@ -160,7 +171,6 @@ const AIChatbot = () => {
160171 </ h3 >
161172 < p className = "text-xs text-alien-green font-exo" > AI Tor</ p >
162173 </ div >
163- { /* Status indicator with typing animation when loading */ }
164174 < div className = "flex items-center gap-1.5 ml-2" >
165175 { isLoading ? (
166176 < div className = "flex items-center gap-1" >
@@ -185,7 +195,7 @@ const AIChatbot = () => {
185195 </ button >
186196 </ div >
187197
188- { /* Loading State */ }
198+ { /* Loading State Overlay */ }
189199 < AnimatePresence >
190200 { isLoading && (
191201 < motion . div
@@ -205,31 +215,25 @@ const AIChatbot = () => {
205215 </ div >
206216 < Loader2 className = "absolute -bottom-1 -right-1 h-8 w-8 text-alien-gold animate-spin" />
207217 </ div >
208- < p className = "text-alien-gold font-nasalization text-sm mt-4" > Loading AI Tor...</ p >
209- { /* Typing indicator animation */ }
210- < div className = "flex gap-1.5 mt-3" >
211- < span className = "typing-dot-lg" />
212- < span className = "typing-dot-lg" style = { { animationDelay : '0.15s' } } />
213- < span className = "typing-dot-lg" style = { { animationDelay : '0.3s' } } />
214- </ div >
218+ < p className = "text-alien-gold font-nasalization text-sm mt-4 uppercase tracking-tighter" > Establishing Link...</ p >
215219 </ motion . div >
216220 ) }
217221 </ AnimatePresence >
218222
219- { /* iframe Content */ }
220- < iframe
221- src = "https://aitor.lovable.app/"
222- className = "w-full h-[calc(100%-3.5rem)] sm:h-[calc(100%-4rem)] border-none bg-alien-space-dark "
223- title = "AI Tor Assistant "
224- allow = "microphone; camera "
225- onLoad = { handleIframeLoad }
226- onError = { ( ) => {
227- setIsLoading ( false ) ;
228- toast . error ( 'AI Assistant Error' , {
229- description : 'Failed to load AI assistant. Please try again.'
230- } ) ;
231- } }
232- />
223+ { /* iframe Content con carga diferida */ }
224+ { shouldLoadIframe && (
225+ < iframe
226+ src = "https://aitor.lovable.app/ "
227+ className = "w-full h-[calc(100%-3.5rem)] sm:h-[calc(100%-4rem)] border-none bg-alien-space-dark "
228+ title = "AI Tor Assistant "
229+ allow = "microphone; camera"
230+ onLoad = { handleIframeLoad }
231+ onError = { ( ) => {
232+ setIsLoading ( false ) ;
233+ toast . error ( ' AI Assistant Error' ) ;
234+ } }
235+ />
236+ ) }
233237 </ motion . div >
234238 ) }
235239 </ AnimatePresence >
0 commit comments