UNPKG

analytica-frontend-lib

Version:

Repositório público dos componentes utilizados nas plataformas da Analytica Ensino

73 lines (70 loc) 2.92 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/hooks/useZendesk.ts var _react = require('react'); var SCRIPT_ID = "ze-snippet"; var INITIAL_RETRY_MS = 2e3; var MAX_RETRY_MS = 3e4; var POLL_MS = 500; var getZE = () => globalThis.zE; var useZendesk = ({ zendeskKey, enabled = true }) => { const [ready, setReady] = _react.useState.call(void 0, false); _react.useEffect.call(void 0, () => { if (!enabled || !zendeskKey) return; let cancelled = false; let attempt = 0; let retryTimer; let pollTimer; function confirmReady() { const zE = getZE(); if (!zE) return false; zE("messenger:set", "locale", "pt-BR"); if (!cancelled) setReady(true); return true; } function scheduleRetry() { if (cancelled) return; const delay = Math.min(INITIAL_RETRY_MS * 2 ** attempt, MAX_RETRY_MS); attempt += 1; retryTimer = setTimeout(inject, delay); } function handleScriptError() { if (cancelled) return; _optionalChain([document, 'access', _ => _.getElementById, 'call', _2 => _2(SCRIPT_ID), 'optionalAccess', _3 => _3.remove, 'call', _4 => _4()]); scheduleRetry(); } function inject() { if (cancelled || confirmReady()) return; const existing = document.getElementById( SCRIPT_ID ); if (existing) { existing.addEventListener("error", handleScriptError, { once: true }); return; } const script = document.createElement("script"); script.id = SCRIPT_ID; script.src = `https://static.zdassets.com/ekr/snippet.js?key=${zendeskKey}`; script.async = true; script.addEventListener("error", handleScriptError, { once: true }); document.body.appendChild(script); } inject(); pollTimer = setInterval(() => { if (cancelled) return; if (confirmReady() && pollTimer) clearInterval(pollTimer); }, POLL_MS); return () => { cancelled = true; if (retryTimer) clearTimeout(retryTimer); if (pollTimer) clearInterval(pollTimer); }; }, [zendeskKey, enabled]); const openChat = _react.useCallback.call(void 0, () => { _optionalChain([getZE, 'call', _5 => _5(), 'optionalCall', _6 => _6("messenger", "open")]); }, []); return { ready, openChat }; }; exports.useZendesk = useZendesk; //# sourceMappingURL=chunk-WIFUWQSW.js.map