|
| 1 | +// Zendesk Web Widget (Classic) — predates Messenger. Different snippet, different |
| 2 | +// API surface (zE('webWidget', '...')). Kept separate from ahize/zendesk so |
| 3 | +// types don't pretend cross-compatibility. |
| 4 | + |
| 5 | +import { waitForDefer } from "../_defer.ts"; |
| 6 | +import { createIdentityStore } from "../_identity.ts"; |
| 7 | +import { createLifecycle, hashConfig } from "../_lifecycle.ts"; |
| 8 | +import { injectScript, isBrowser, removeScript } from "../_loader.ts"; |
| 9 | +import { createQueue } from "../_queue.ts"; |
| 10 | +import type { |
| 11 | + EventMetadata, |
| 12 | + Identity, |
| 13 | + IdentityListener, |
| 14 | + IdentityState, |
| 15 | + LoadOptions, |
| 16 | +} from "../_types.ts"; |
| 17 | + |
| 18 | +type ZendeskFn = (api: string, command: string, ...args: unknown[]) => void; |
| 19 | + |
| 20 | +interface ZendeskWindow { |
| 21 | + zE?: ZendeskFn; |
| 22 | + zESettings?: Record<string, unknown>; |
| 23 | +} |
| 24 | + |
| 25 | +function w(): ZendeskWindow { |
| 26 | + return globalThis as unknown as ZendeskWindow; |
| 27 | +} |
| 28 | + |
| 29 | +const queue = createQueue<ZendeskFn>(); |
| 30 | +const store = createIdentityStore(); |
| 31 | +const lifecycle = createLifecycle(); |
| 32 | + |
| 33 | +export interface ZendeskClassicLoadOptions extends LoadOptions { |
| 34 | + key: string; |
| 35 | +} |
| 36 | + |
| 37 | +export async function load(options: ZendeskClassicLoadOptions): Promise<void> { |
| 38 | + if (!isBrowser()) return; |
| 39 | + if (options.consent === false) return; |
| 40 | + const h = hashConfig({ key: options.key }); |
| 41 | + if (lifecycle.state() === "ready" && lifecycle.configHash() === h) return; |
| 42 | + if (lifecycle.configHash() && lifecycle.configHash() !== h) await destroy(); |
| 43 | + |
| 44 | + lifecycle.transition("loading"); |
| 45 | + lifecycle.setConfigHash(h); |
| 46 | + await waitForDefer(options.defer ?? "immediate"); |
| 47 | + |
| 48 | + try { |
| 49 | + await injectScript({ |
| 50 | + id: "ze-snippet", |
| 51 | + src: `https://static.zdassets.com/ekr/snippet.js?key=${options.key}`, |
| 52 | + nonce: options.nonce, |
| 53 | + partytown: options.partytown, |
| 54 | + }); |
| 55 | + } catch (error) { |
| 56 | + lifecycle.transition("idle"); |
| 57 | + lifecycle.clearConfigHash(); |
| 58 | + throw error; |
| 59 | + } |
| 60 | + |
| 61 | + const fn = w().zE; |
| 62 | + if (typeof fn === "function") queue.ready(fn); |
| 63 | + lifecycle.transition("ready"); |
| 64 | +} |
| 65 | + |
| 66 | +export function ready(): Promise<void> { |
| 67 | + if (!isBrowser()) return Promise.resolve(); |
| 68 | + return queue.enqueue(() => {}); |
| 69 | +} |
| 70 | + |
| 71 | +export function identify(identity: Identity): Promise<void> { |
| 72 | + if (!isBrowser()) return Promise.resolve(); |
| 73 | + store.identify(identity); |
| 74 | + return queue.enqueue((zE) => { |
| 75 | + zE("webWidget", "prefill", { |
| 76 | + name: { value: identity.name, readOnly: false }, |
| 77 | + email: { value: identity.email, readOnly: false }, |
| 78 | + phone: { value: identity.phone, readOnly: false }, |
| 79 | + }); |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +export function track<T extends EventMetadata = EventMetadata>( |
| 84 | + event: string, |
| 85 | + metadata?: T, |
| 86 | +): Promise<void> { |
| 87 | + if (!isBrowser()) return Promise.resolve(); |
| 88 | + return queue.enqueue((zE) => { |
| 89 | + zE("webWidget", "updatePath", { url: location.href, title: event }); |
| 90 | + if (metadata) { |
| 91 | + zE("webWidget", "updateSettings", { |
| 92 | + webWidget: { contactForm: { tags: Object.keys(metadata) } }, |
| 93 | + }); |
| 94 | + } |
| 95 | + }); |
| 96 | +} |
| 97 | + |
| 98 | +export function pageView(_info?: { path?: string; locale?: string }): Promise<void> { |
| 99 | + if (!isBrowser()) return Promise.resolve(); |
| 100 | + return queue.enqueue((zE) => zE("webWidget", "updatePath")); |
| 101 | +} |
| 102 | + |
| 103 | +export function show(): Promise<void> { |
| 104 | + if (!isBrowser()) return Promise.resolve(); |
| 105 | + return queue.enqueue((zE) => { |
| 106 | + zE("webWidget", "show"); |
| 107 | + zE("webWidget", "open"); |
| 108 | + }); |
| 109 | +} |
| 110 | + |
| 111 | +export function hide(): Promise<void> { |
| 112 | + if (!isBrowser()) return Promise.resolve(); |
| 113 | + return queue.enqueue((zE) => zE("webWidget", "hide")); |
| 114 | +} |
| 115 | + |
| 116 | +export function shutdown(): Promise<void> { |
| 117 | + if (!isBrowser()) return Promise.resolve(); |
| 118 | + return queue |
| 119 | + .enqueue((zE) => zE("webWidget", "logout")) |
| 120 | + .then(() => { |
| 121 | + store.reset(); |
| 122 | + lifecycle.transition("shutdown"); |
| 123 | + }); |
| 124 | +} |
| 125 | + |
| 126 | +export async function destroy(): Promise<void> { |
| 127 | + if (!isBrowser()) return; |
| 128 | + await shutdown().catch(() => undefined); |
| 129 | + removeScript("ze-snippet"); |
| 130 | + const g = w(); |
| 131 | + Reflect.deleteProperty(g, "zE"); |
| 132 | + Reflect.deleteProperty(g, "zESettings"); |
| 133 | + queue.reset(); |
| 134 | + store.reset(); |
| 135 | + lifecycle.clearConfigHash(); |
| 136 | + lifecycle.transition("idle"); |
| 137 | +} |
| 138 | + |
| 139 | +export function getIdentity(): IdentityState { |
| 140 | + return store.get(); |
| 141 | +} |
| 142 | + |
| 143 | +export function onIdentityChange(listener: IdentityListener): () => void { |
| 144 | + return store.onChange(listener); |
| 145 | +} |
| 146 | + |
| 147 | +export function isReady(): boolean { |
| 148 | + return lifecycle.state() === "ready"; |
| 149 | +} |
| 150 | + |
| 151 | +export function state(): "idle" | "loading" | "ready" | "shutdown" { |
| 152 | + return lifecycle.state(); |
| 153 | +} |
0 commit comments