useBeacon.js (2064B)
1 import { useEffect } from '@wordpress/element' 2 3 export default function useBeacon(show) { 4 const showBeacon = () => { 5 const container = document.getElementById('beacon-container') 6 if (container) { 7 container.style.position = 'relative' 8 container.style.zIndex = Number.MAX_SAFE_INTEGER 9 container.style.display = 'block' 10 } 11 } 12 13 const hideBeacon = () => { 14 const container = document.getElementById('beacon-container') 15 if (container) { 16 container.style.display = 'none' 17 window.Beacon('close') 18 } 19 } 20 21 useEffect(() => { 22 if (!show) { 23 return 24 } 25 26 if (window.Beacon) { 27 showBeacon() 28 return () => hideBeacon() 29 } 30 31 // Code direct from HS 32 (function ( 33 e, t, n, 34 ) { 35 function a() { 36 const e = t.getElementsByTagName('script')[0], 37 n = t.createElement('script') 38 ;(n.async = !0), (n.src = 'https://beacon-v2.helpscout.net'), e.parentNode?.insertBefore(n, e) 39 } 40 if ( 41 ((e.Beacon = n = function ( 42 t, n, a, 43 ) { 44 e.Beacon.readyQueue.push({ 45 method: t, options: n, data: a, 46 }) 47 }), 48 (n.readyQueue = []), 49 'complete' === t.readyState) 50 ) 51 return a() 52 e.attachEvent 53 ? e.attachEvent('onload', a) 54 : e.addEventListener( 55 'load', a, !1, 56 ) 57 })( 58 window, document, window.Beacon || function () {}, 59 ) 60 61 window.Beacon('init', '2b8c11c0-5afc-4cb9-bee0-a5cb76b2fc91') 62 window.Beacon( 63 'on', 'ready', showBeacon, 64 ) 65 return () => { 66 window.Beacon( 67 'off', 'ready', showBeacon, 68 ) 69 hideBeacon() 70 } 71 }, [show]) 72 73 }