1
1
import { Box } from '@chakra-ui/react'
2
- import { useEffect , useState } from 'react'
3
2
4
3
import { Accessibility } from '@/atoms/Icons/Accessibility'
5
4
import { vars } from '@theme'
6
5
7
- declare global {
8
- interface Window {
9
- UserWay : {
10
- widgetOpen : ( ) => void
11
- }
12
- }
13
- }
14
-
15
- interface IUserWay {
16
- color ?: string
17
- }
18
-
19
- const waitForUserWay = async ( ) : Promise < void > => {
20
- return await new Promise ( ( resolve , reject ) => {
21
- const checkUserWay = ( ) : void => {
22
- if ( window . UserWay ) {
23
- resolve ( ) // Resuelve cuando UserWay está disponible
24
- } else {
25
- requestAnimationFrame ( checkUserWay ) // Sigue esperando
26
- }
27
- }
28
-
29
- try {
30
- checkUserWay ( ) // Inicia la verificación
31
- } catch ( error ) {
32
- reject ( new Error ( 'UserWay script failed to load' ) )
33
- }
34
- } )
35
- }
36
-
37
- export function UserWay ( { color = '#0189FF' } : IUserWay ) : JSX . Element {
38
- const [ isUserWayReady , setIsUserWayReady ] = useState ( false )
39
-
40
- useEffect ( ( ) => {
41
- // Cargar el script de UserWay
42
- if ( ! document . getElementById ( 'userway-script' ) ) {
43
- const script = document . createElement ( 'script' )
44
- script . id = 'userway-script'
45
- script . src = 'https://cdn.userway.org/widget.js'
46
- script . async = true
47
- script . setAttribute ( 'data-account' , '06mvMZzmO4' )
48
- script . setAttribute ( 'data-color' , color )
49
- script . setAttribute ( 'data-trigger' , 'customTrigger' )
50
- document . body . appendChild ( script )
51
- }
52
-
53
- // Esperar a que el script esté listo
54
- waitForUserWay ( )
55
- . then ( ( ) => {
56
- setIsUserWayReady ( true )
57
- return undefined
58
- } )
59
- . catch ( ( error ) => {
60
- console . error ( 'Failed to load UserWay:' , error )
61
- } )
62
- } , [ color ] )
63
-
64
- const handleButtonClick = ( ) : void => {
65
- if ( isUserWayReady && window ?. UserWay ?. widgetOpen ) {
66
- window . UserWay . widgetOpen ( ) // Abrir el menú
67
- } else {
68
- console . error ( 'UserWay script not loaded yet.' )
6
+ export function UserWay ( ) : JSX . Element {
7
+ const triggerWidget = ( ) : void => {
8
+ const buttonB = document . getElementById ( 'userWayTrigger' )
9
+ if ( buttonB ?. click ) {
10
+ buttonB . click ( ) // Hace clic en el botón del archivo index.html
69
11
}
70
12
}
71
13
@@ -78,13 +20,14 @@ export function UserWay({ color = '#0189FF' }: IUserWay): JSX.Element {
78
20
border = "1px solid transparent"
79
21
display = "flex"
80
22
height = "30px"
81
- id = "customTrigger "
23
+ id = "UserWayButton "
82
24
justifyContent = "center"
83
- onClick = { handleButtonClick }
25
+ padding = "0 8px"
84
26
width = "30px"
85
27
_hover = { {
86
28
background : 'rgba(96, 121, 142, 0.8)' ,
87
29
} }
30
+ onClick = { triggerWidget }
88
31
>
89
32
< Accessibility />
90
33
</ Box >
0 commit comments