@pocketping/widget 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -6
- package/dist/index.js +2 -2
- package/dist/index.mjs +2 -2
- package/dist/pocketping.min.global.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,11 +6,22 @@ Embeddable chat widget for PocketPing. Drop-in customer support chat that connec
|
|
|
6
6
|
|
|
7
7
|
### Via CDN (Recommended)
|
|
8
8
|
|
|
9
|
+
**One-line install (SaaS users):**
|
|
10
|
+
|
|
11
|
+
```html
|
|
12
|
+
<script src="https://cdn.pocketping.io/widget.js" data-project-id="proj_xxxxxxxxxxxxx"></script>
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
**With custom options:**
|
|
16
|
+
|
|
9
17
|
```html
|
|
10
18
|
<script src="https://cdn.pocketping.io/widget.js"></script>
|
|
11
19
|
<script>
|
|
12
20
|
PocketPing.init({
|
|
13
|
-
|
|
21
|
+
projectId: 'proj_xxxxxxxxxxxxx', // SaaS users
|
|
22
|
+
// OR
|
|
23
|
+
endpoint: 'https://yoursite.com/pocketping', // Self-hosted
|
|
24
|
+
operatorName: 'Support Team',
|
|
14
25
|
});
|
|
15
26
|
</script>
|
|
16
27
|
```
|
|
@@ -22,10 +33,12 @@ npm install @pocketping/widget
|
|
|
22
33
|
```
|
|
23
34
|
|
|
24
35
|
```javascript
|
|
25
|
-
import
|
|
36
|
+
import PocketPing from '@pocketping/widget';
|
|
26
37
|
|
|
27
|
-
init({
|
|
28
|
-
|
|
38
|
+
PocketPing.init({
|
|
39
|
+
projectId: 'proj_xxxxxxxxxxxxx', // SaaS users
|
|
40
|
+
// OR
|
|
41
|
+
endpoint: 'https://yoursite.com/pocketping', // Self-hosted
|
|
29
42
|
});
|
|
30
43
|
```
|
|
31
44
|
|
|
@@ -33,11 +46,12 @@ init({
|
|
|
33
46
|
|
|
34
47
|
## Configuration Options
|
|
35
48
|
|
|
36
|
-
### Required
|
|
49
|
+
### Required (one of)
|
|
37
50
|
|
|
38
51
|
| Option | Type | Description |
|
|
39
52
|
|--------|------|-------------|
|
|
40
|
-
| `
|
|
53
|
+
| `projectId` | `string` | Your project ID from [app.pocketping.io](https://app.pocketping.io) (SaaS) |
|
|
54
|
+
| `endpoint` | `string` | Your backend endpoint (self-hosted, e.g., `"https://yoursite.com/pocketping"`) |
|
|
41
55
|
|
|
42
56
|
---
|
|
43
57
|
|
package/dist/index.js
CHANGED
|
@@ -637,7 +637,7 @@ function StatusIcon({ status }) {
|
|
|
637
637
|
}
|
|
638
638
|
|
|
639
639
|
// src/version.ts
|
|
640
|
-
var VERSION = "0.3.
|
|
640
|
+
var VERSION = "0.3.1";
|
|
641
641
|
|
|
642
642
|
// src/client.ts
|
|
643
643
|
var PocketPingClient = class {
|
|
@@ -1322,7 +1322,7 @@ function on(eventName, handler) {
|
|
|
1322
1322
|
if (typeof document !== "undefined") {
|
|
1323
1323
|
const script = document.currentScript;
|
|
1324
1324
|
if (script) {
|
|
1325
|
-
const projectId = script.dataset.
|
|
1325
|
+
const projectId = script.dataset.projectId;
|
|
1326
1326
|
const endpoint = script.dataset.endpoint;
|
|
1327
1327
|
if (projectId || endpoint) {
|
|
1328
1328
|
init({
|
package/dist/index.mjs
CHANGED
|
@@ -600,7 +600,7 @@ function StatusIcon({ status }) {
|
|
|
600
600
|
}
|
|
601
601
|
|
|
602
602
|
// src/version.ts
|
|
603
|
-
var VERSION = "0.3.
|
|
603
|
+
var VERSION = "0.3.1";
|
|
604
604
|
|
|
605
605
|
// src/client.ts
|
|
606
606
|
var PocketPingClient = class {
|
|
@@ -1285,7 +1285,7 @@ function on(eventName, handler) {
|
|
|
1285
1285
|
if (typeof document !== "undefined") {
|
|
1286
1286
|
const script = document.currentScript;
|
|
1287
1287
|
if (script) {
|
|
1288
|
-
const projectId = script.dataset.
|
|
1288
|
+
const projectId = script.dataset.projectId;
|
|
1289
1289
|
const endpoint = script.dataset.endpoint;
|
|
1290
1290
|
if (projectId || endpoint) {
|
|
1291
1291
|
init({
|
|
@@ -326,4 +326,4 @@
|
|
|
326
326
|
.pp-footer a:hover {
|
|
327
327
|
text-decoration: underline;
|
|
328
328
|
}
|
|
329
|
-
`}var yt=0;function d(t,e,n,s,o,i){e||(e={});var a,c,p=e;if("ref"in p)for(c in p={},e)c=="ref"?a=e[c]:p[c]=e[c];var l={type:t,props:p,key:n,ref:a,__k:null,__:null,__b:0,__e:null,__c:null,constructor:void 0,__v:--yt,__i:-1,__u:0,__source:o,__self:i};if(typeof t=="function"&&(a=t.defaultProps))for(c in a)p[c]===void 0&&(p[c]=a[c]);return g.vnode&&g.vnode(l),l}function ze({client:t,config:e}){let[n,s]=V(!1),[o,i]=V([]),[a,c]=V(""),[p,l]=V(!1),[f,r]=V(!1),[u,_]=V(!1),x=_e(null),S=_e(null);N(()=>{let m=t.on("openChange",s),P=t.on("message",()=>{i([...t.getMessages()])}),C=t.on("typing",$=>{l($.isTyping)}),D=t.on("presence",$=>{r($.online)}),I=t.on("connect",()=>{_(!0),i(t.getMessages()),r(t.getSession()?.operatorOnline??!1)});return t.isConnected()&&(_(!0),i(t.getMessages()),r(t.getSession()?.operatorOnline??!1)),()=>{m(),P(),C(),D(),I()}},[t]),N(()=>{x.current?.scrollIntoView({behavior:"smooth"})},[o]),N(()=>{n&&S.current?.focus()},[n]);let b=Ue(()=>{if(!n||!u)return;let m=o.filter(P=>P.sender!=="visitor"&&P.status!=="read");if(m.length>0){let P=m.map(C=>C.id);t.sendReadStatus(P,"read")}},[n,u,o,t]);if(N(()=>{if(!n||!u)return;let m=setTimeout(()=>{b()},1e3);return()=>clearTimeout(m)},[n,u,o,b]),N(()=>{let m=()=>{document.visibilityState==="visible"&&n&&b()};return document.addEventListener("visibilitychange",m),()=>document.removeEventListener("visibilitychange",m)},[n,b]),N(()=>{let m=t.on("read",()=>{i([...t.getMessages()])});return()=>m()},[t]),!bt(e))return null;let h=async m=>{if(m.preventDefault(),!a.trim())return;let P=a;c("");try{await t.sendMessage(P)}catch(C){console.error("[PocketPing] Failed to send message:",C)}},M=m=>{let P=m.target;c(P.value),t.sendTyping(!0)},T=e.position??"bottom-right",H=xt(e.theme??"auto"),R=e.primaryColor??"#6366f1";return d(E,{children:[d("style",{children:Fe(R,H)}),d("button",{class:`pp-toggle pp-${T}`,onClick:()=>t.toggleOpen(),"aria-label":n?"Close chat":"Open chat",children:[n?d(je,{}):d(wt,{}),!n&&f&&d("span",{class:"pp-online-dot"})]}),n&&d("div",{class:`pp-window pp-${T} pp-theme-${H}`,children:[d("div",{class:"pp-header",children:[d("div",{class:"pp-header-info",children:[e.operatorAvatar&&d("img",{src:e.operatorAvatar,alt:"",class:"pp-avatar"}),d("div",{children:[d("div",{class:"pp-header-title",children:e.operatorName??"Support"}),d("div",{class:"pp-header-status",children:f?d(E,{children:[d("span",{class:"pp-status-dot pp-online"})," Online"]}):d(E,{children:[d("span",{class:"pp-status-dot"})," Away"]})})]})]}),d("button",{class:"pp-close-btn",onClick:()=>t.setOpen(!1),"aria-label":"Close chat",children:d(je,{})})]}),d("div",{class:"pp-messages",children:[e.welcomeMessage&&o.length===0&&d("div",{class:"pp-welcome",children:e.welcomeMessage}),o.map(m=>d("div",{class:`pp-message pp-message-${m.sender}`,children:[d("div",{class:"pp-message-content",children:m.content}),d("div",{class:"pp-message-time",children:[kt(m.timestamp),m.sender==="ai"&&d("span",{class:"pp-ai-badge",children:"AI"}),m.sender==="visitor"&&d("span",{class:`pp-status pp-status-${m.status??"sent"}`,children:d(St,{status:m.status})})]})]},m.id)),p&&d("div",{class:"pp-message pp-message-operator pp-typing",children:[d("span",{}),d("span",{}),d("span",{})]}),d("div",{ref:x})]}),d("form",{class:"pp-input-form",onSubmit:h,children:[d("input",{ref:S,type:"text",class:"pp-input",placeholder:e.placeholder??"Type a message...",value:a,onInput:M,disabled:!u}),d("button",{type:"submit",class:"pp-send-btn",disabled:!a.trim()||!u,"aria-label":"Send message",children:d(Pt,{})})]}),d("div",{class:"pp-footer",children:["Powered by ",d("a",{href:"https://github.com/pocketping/pocketping",target:"_blank",rel:"noopener",children:"PocketPing"})]})]})]})}function bt(t){let e=window.location.pathname;return t.hideOnPages?.some(n=>new RegExp(n).test(e))?!1:t.showOnPages?.length?t.showOnPages.some(n=>new RegExp(n).test(e)):!0}function xt(t){return t==="auto"?window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light":t}function kt(t){return new Date(t).toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"})}function wt(){return d("svg",{viewBox:"0 0 24 24",fill:"none",stroke:"currentColor","stroke-width":"2",children:d("path",{d:"M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"})})}function je(){return d("svg",{viewBox:"0 0 24 24",fill:"none",stroke:"currentColor","stroke-width":"2",children:[d("line",{x1:"18",y1:"6",x2:"6",y2:"18"}),d("line",{x1:"6",y1:"6",x2:"18",y2:"18"})]})}function Pt(){return d("svg",{viewBox:"0 0 24 24",fill:"none",stroke:"currentColor","stroke-width":"2",children:[d("line",{x1:"22",y1:"2",x2:"11",y2:"13"}),d("polygon",{points:"22 2 15 22 11 13 2 9 22 2"})]})}function St({status:t}){return!t||t==="sending"||t==="sent"?d("svg",{viewBox:"0 0 16 16",fill:"none",stroke:"currentColor","stroke-width":"2",class:"pp-check",children:d("polyline",{points:"3 8 7 12 13 4"})}):t==="delivered"?d("svg",{viewBox:"0 0 20 16",fill:"none",stroke:"currentColor","stroke-width":"2",class:"pp-check-double",children:[d("polyline",{points:"1 8 5 12 11 4"}),d("polyline",{points:"7 8 11 12 17 4"})]}):t==="read"?d("svg",{viewBox:"0 0 20 16",fill:"none",stroke:"currentColor","stroke-width":"2",class:"pp-check-double pp-check-read",children:[d("polyline",{points:"1 8 5 12 11 4"}),d("polyline",{points:"7 8 11 12 17 4"})]}):null}var K="0.3.0";var Q=class{constructor(e){this.session=null;this.ws=null;this.isOpen=!1;this.listeners=new Map;this.customEventHandlers=new Map;this.reconnectAttempts=0;this.maxReconnectAttempts=5;this.reconnectTimeout=null;this.config=e}async connect(){let e=this.getOrCreateVisitorId(),n=this.getStoredSessionId(),s=this.getStoredIdentity(),o=await this.fetch("/connect",{method:"POST",body:JSON.stringify({visitorId:e,sessionId:n,metadata:{url:window.location.href,referrer:document.referrer||void 0,pageTitle:document.title||void 0,userAgent:navigator.userAgent,timezone:Intl.DateTimeFormat().resolvedOptions().timeZone,language:navigator.language,screenResolution:`${window.screen.width}x${window.screen.height}`},identity:s||void 0})});return this.session={sessionId:o.sessionId,visitorId:o.visitorId,operatorOnline:o.operatorOnline??!1,messages:o.messages??[],identity:o.identity||s||void 0},this.storeSessionId(o.sessionId),this.connectWebSocket(),this.emit("connect",this.session),this.config.onConnect?.(o.sessionId),this.session}disconnect(){this.ws?.close(),this.ws=null,this.session=null,this.reconnectTimeout&&clearTimeout(this.reconnectTimeout)}async sendMessage(e){if(!this.session)throw new Error("Not connected");let n=`temp-${this.generateId()}`,s={id:n,sessionId:this.session.sessionId,content:e,sender:"visitor",timestamp:new Date().toISOString(),status:"sending"};this.session.messages.push(s),this.emit("message",s);try{let o=await this.fetch("/message",{method:"POST",body:JSON.stringify({sessionId:this.session.sessionId,content:e,sender:"visitor"})}),i=this.session.messages.findIndex(c=>c.id===n);i>=0&&(this.session.messages[i].id=o.messageId,this.session.messages[i].timestamp=o.timestamp,this.session.messages[i].status="sent",this.emit("message",this.session.messages[i]));let a=this.session.messages[i]||{id:o.messageId,sessionId:this.session.sessionId,content:e,sender:"visitor",timestamp:o.timestamp,status:"sent"};return this.config.onMessage?.(a),a}catch(o){let i=this.session.messages.findIndex(a=>a.id===n);throw i>=0&&(this.session.messages.splice(i,1),this.emit("message",s)),o}}async fetchMessages(e){if(!this.session)throw new Error("Not connected");let n=new URLSearchParams({sessionId:this.session.sessionId});return e&&n.set("after",e),(await this.fetch(`/messages?${n}`,{method:"GET"})).messages}async sendTyping(e=!0){this.session&&await this.fetch("/typing",{method:"POST",body:JSON.stringify({sessionId:this.session.sessionId,sender:"visitor",isTyping:e})})}async sendReadStatus(e,n){if(!(!this.session||e.length===0))try{await this.fetch("/read",{method:"POST",body:JSON.stringify({sessionId:this.session.sessionId,messageIds:e,status:n})});for(let s of this.session.messages)e.includes(s.id)&&(s.status=n,n==="delivered"?s.deliveredAt=new Date().toISOString():n==="read"&&(s.readAt=new Date().toISOString()));this.emit("readStatusSent",{messageIds:e,status:n})}catch(s){console.error("[PocketPing] Failed to send read status:",s)}}async getPresence(){return this.fetch("/presence",{method:"GET"})}async identify(e){if(!e?.id)throw new Error("[PocketPing] identity.id is required");if(this.storeIdentity(e),this.session)try{await this.fetch("/identify",{method:"POST",body:JSON.stringify({sessionId:this.session.sessionId,identity:e})}),this.session.identity=e,this.emit("identify",e)}catch(n){throw console.error("[PocketPing] Failed to identify:",n),n}}async reset(e){this.clearIdentity(),this.session&&(this.session.identity=void 0),e?.newSession&&(localStorage.removeItem("pocketping_session_id"),localStorage.removeItem("pocketping_visitor_id"),this.disconnect(),await this.connect()),this.emit("reset",null)}getIdentity(){return this.session?.identity||this.getStoredIdentity()}getSession(){return this.session}getMessages(){return this.session?.messages??[]}isConnected(){return this.session!==null}isWidgetOpen(){return this.isOpen}setOpen(e){this.isOpen=e,this.emit("openChange",e),e?this.config.onOpen?.():this.config.onClose?.()}toggleOpen(){this.setOpen(!this.isOpen)}on(e,n){return this.listeners.has(e)||this.listeners.set(e,new Set),this.listeners.get(e).add(n),()=>{this.listeners.get(e)?.delete(n)}}emit(e,n){this.listeners.get(e)?.forEach(s=>s(n))}trigger(e,n){if(!this.ws||this.ws.readyState!==WebSocket.OPEN){console.warn("[PocketPing] Cannot trigger event: WebSocket not connected");return}let s={name:e,data:n,timestamp:new Date().toISOString()};this.ws.send(JSON.stringify({type:"event",data:s})),this.emit(`event:${e}`,s)}onEvent(e,n){return this.customEventHandlers.has(e)||this.customEventHandlers.set(e,new Set),this.customEventHandlers.get(e).add(n),()=>{this.customEventHandlers.get(e)?.delete(n)}}offEvent(e,n){this.customEventHandlers.get(e)?.delete(n)}emitCustomEvent(e){let n=this.customEventHandlers.get(e.name);n&&n.forEach(s=>s(e.data,e)),this.emit("event",e),this.emit(`event:${e.name}`,e)}connectWebSocket(){if(!this.session)return;let e=this.config.endpoint.replace(/^http/,"ws").replace(/\/$/,"")+`/stream?sessionId=${this.session.sessionId}`;try{this.ws=new WebSocket(e),this.ws.onopen=()=>{this.reconnectAttempts=0,this.emit("wsConnected",null)},this.ws.onmessage=n=>{try{let s=JSON.parse(n.data);this.handleWebSocketEvent(s)}catch(s){console.error("[PocketPing] Failed to parse WS message:",s)}},this.ws.onclose=()=>{this.emit("wsDisconnected",null),this.scheduleReconnect()},this.ws.onerror=n=>{console.error("[PocketPing] WebSocket error:",n)}}catch{console.warn("[PocketPing] WebSocket unavailable, using polling"),this.startPolling()}}handleWebSocketEvent(e){switch(e.type){case"message":let n=e.data;if(this.session){let c=this.session.messages.findIndex(p=>p.id===n.id);if(c<0&&n.sender==="visitor"&&(c=this.session.messages.findIndex(p=>p.id.startsWith("temp-")&&p.content===n.content&&p.sender==="visitor"),c>=0&&(this.session.messages[c].id=n.id)),c<0&&n.sender!=="visitor"){let p=new Date(n.timestamp).getTime();c=this.session.messages.findIndex(l=>l.sender===n.sender&&l.content===n.content&&Math.abs(new Date(l.timestamp).getTime()-p)<2e3)}if(c>=0){let p=this.session.messages[c];n.status&&n.status!==p.status&&(p.status=n.status,n.deliveredAt&&(p.deliveredAt=n.deliveredAt),n.readAt&&(p.readAt=n.readAt),this.emit("read",{messageIds:[n.id],status:n.status}))}else this.session.messages.push(n),this.emit("message",n),this.config.onMessage?.(n)}n.sender!=="visitor"&&this.emit("typing",{isTyping:!1});break;case"typing":let s=e.data;s.sender!=="visitor"&&this.emit("typing",{isTyping:s.isTyping});break;case"presence":this.session&&(this.session.operatorOnline=e.data.online),this.emit("presence",e.data);break;case"ai_takeover":this.emit("aiTakeover",e.data);break;case"read":let o=e.data;if(this.session)for(let c of this.session.messages)o.messageIds.includes(c.id)&&(c.status=o.status,o.deliveredAt&&(c.deliveredAt=o.deliveredAt),o.readAt&&(c.readAt=o.readAt));this.emit("read",o);break;case"event":let i=e.data;this.emitCustomEvent(i);break;case"version_warning":let a=e.data;this.handleVersionWarning(a);break}}handleVersionWarning(e){let n="[PocketPing]",s=e.upgradeUrl?` Upgrade: ${e.upgradeUrl}`:" Update your widget to the latest version.";switch(e.severity){case"error":console.error(`${n} \u{1F6A8} VERSION ERROR: ${e.message}${s}`),console.error(`${n} Current: ${e.currentVersion}, Required: ${e.minVersion||"unknown"}`);break;case"warning":console.warn(`${n} \u26A0\uFE0F VERSION WARNING: ${e.message}${s}`),console.warn(`${n} Current: ${e.currentVersion}, Latest: ${e.latestVersion||"unknown"}`);break;case"info":console.info(`${n} \u2139\uFE0F ${e.message}`);break}this.emit("versionWarning",e),this.config.onVersionWarning?.(e),e.canContinue||(console.error(`${n} Widget is incompatible with backend. Please update immediately.`),this.disconnect())}scheduleReconnect(){if(this.reconnectAttempts>=this.maxReconnectAttempts){console.warn("[PocketPing] Max reconnect attempts reached, switching to polling"),this.startPolling();return}let e=Math.min(1e3*Math.pow(2,this.reconnectAttempts),3e4);this.reconnectAttempts++,this.reconnectTimeout=setTimeout(()=>{this.connectWebSocket()},e)}startPolling(){let e=async()=>{if(this.session){try{let n=this.session.messages[this.session.messages.length-1]?.id,s=await this.fetchMessages(n);for(let o of s)this.session.messages.find(i=>i.id===o.id)||(this.session.messages.push(o),this.emit("message",o),this.config.onMessage?.(o))}catch(n){console.error("[PocketPing] Polling error:",n)}this.session&&setTimeout(e,3e3)}};e()}async fetch(e,n){let s=this.config.endpoint.replace(/\/$/,"")+e,o=await fetch(s,{...n,headers:{"Content-Type":"application/json","X-PocketPing-Version":K,...n.headers}});if(this.checkVersionHeaders(o),!o.ok){let i=await o.text();throw new Error(`PocketPing API error: ${o.status} ${i}`)}return o.json()}checkVersionHeaders(e){let n=e.headers.get("X-PocketPing-Version-Status"),s=e.headers.get("X-PocketPing-Min-Version"),o=e.headers.get("X-PocketPing-Latest-Version"),i=e.headers.get("X-PocketPing-Version-Message");if(!n||n==="ok")return;let a="info",c=!0;n==="deprecated"?a="warning":n==="unsupported"?(a="error",c=!1):n==="outdated"&&(a="info");let p={severity:a,message:i||`Widget version ${K} is ${n}`,currentVersion:K,minVersion:s||void 0,latestVersion:o||void 0,canContinue:c,upgradeUrl:"https://docs.pocketping.io/widget/installation"};this.handleVersionWarning(p)}getOrCreateVisitorId(){let e="pocketping_visitor_id",n=localStorage.getItem(e);return n||(n=this.generateId(),localStorage.setItem(e,n)),n}getStoredSessionId(){return localStorage.getItem("pocketping_session_id")}storeSessionId(e){localStorage.setItem("pocketping_session_id",e)}getStoredIdentity(){try{let e=localStorage.getItem("pocketping_user_identity");return e?JSON.parse(e):null}catch{return null}}storeIdentity(e){localStorage.setItem("pocketping_user_identity",JSON.stringify(e))}clearIdentity(){localStorage.removeItem("pocketping_user_identity")}generateId(){return`${Date.now().toString(36)}-${Math.random().toString(36).slice(2,11)}`}};var y=null,A=null,It="https://app.pocketping.io/api/widget";function ue(t){if(y)return console.warn("[PocketPing] Already initialized"),y;let e=t.endpoint;if(!e&&t.projectId&&(e=`${It}/${t.projectId}`),!e)throw new Error("[PocketPing] endpoint or projectId is required");let n={...t,endpoint:e};return y=new Q(n),A=document.createElement("div"),A.id="pocketping-container",document.body.appendChild(A),ce(ie(ze,{client:y,config:t}),A),y.connect().catch(s=>{console.error("[PocketPing] Failed to connect:",s)}),y}function Be(){A&&(ce(null,A),A.remove(),A=null),y&&(y.disconnect(),y=null)}function qe(){y?.setOpen(!0)}function Je(){y?.setOpen(!1)}function Xe(){y?.toggleOpen()}function Ge(t){if(!y)throw new Error("[PocketPing] Not initialized");return y.sendMessage(t)}function Ze(t,e){if(!y){console.warn("[PocketPing] Not initialized, cannot trigger event");return}y.trigger(t,e)}function Ke(t,e){return y?y.onEvent(t,e):(console.warn("[PocketPing] Not initialized, cannot subscribe to event"),()=>{})}function Qe(t,e){y?.offEvent(t,e)}async function Ye(t){if(!y)throw new Error("[PocketPing] Not initialized");return y.identify(t)}async function et(t){if(!y){console.warn("[PocketPing] Not initialized");return}return y.reset(t)}function tt(){return y?.getIdentity()||null}function nt(t,e){return y?y.on(t,e):(console.warn("[PocketPing] Not initialized, cannot subscribe to event"),()=>{})}if(typeof document<"u"){let t=document.currentScript;if(t){let e=t.dataset.key,n=t.dataset.endpoint;(e||n)&&ue({projectId:e,endpoint:n,theme:t.dataset.theme||"auto",position:t.dataset.position||"bottom-right"})}}var Ct={init:ue,destroy:Be,open:qe,close:Je,toggle:Xe,sendMessage:Ge,trigger:Ze,onEvent:Ke,offEvent:Qe,on:nt,identify:Ye,reset:et,getIdentity:tt};return ct(Et);})();
|
|
329
|
+
`}var yt=0;function d(t,e,n,s,o,i){e||(e={});var a,c,p=e;if("ref"in p)for(c in p={},e)c=="ref"?a=e[c]:p[c]=e[c];var l={type:t,props:p,key:n,ref:a,__k:null,__:null,__b:0,__e:null,__c:null,constructor:void 0,__v:--yt,__i:-1,__u:0,__source:o,__self:i};if(typeof t=="function"&&(a=t.defaultProps))for(c in a)p[c]===void 0&&(p[c]=a[c]);return g.vnode&&g.vnode(l),l}function ze({client:t,config:e}){let[n,s]=V(!1),[o,i]=V([]),[a,c]=V(""),[p,l]=V(!1),[f,r]=V(!1),[u,_]=V(!1),x=_e(null),S=_e(null);N(()=>{let m=t.on("openChange",s),P=t.on("message",()=>{i([...t.getMessages()])}),C=t.on("typing",$=>{l($.isTyping)}),D=t.on("presence",$=>{r($.online)}),I=t.on("connect",()=>{_(!0),i(t.getMessages()),r(t.getSession()?.operatorOnline??!1)});return t.isConnected()&&(_(!0),i(t.getMessages()),r(t.getSession()?.operatorOnline??!1)),()=>{m(),P(),C(),D(),I()}},[t]),N(()=>{x.current?.scrollIntoView({behavior:"smooth"})},[o]),N(()=>{n&&S.current?.focus()},[n]);let b=Ue(()=>{if(!n||!u)return;let m=o.filter(P=>P.sender!=="visitor"&&P.status!=="read");if(m.length>0){let P=m.map(C=>C.id);t.sendReadStatus(P,"read")}},[n,u,o,t]);if(N(()=>{if(!n||!u)return;let m=setTimeout(()=>{b()},1e3);return()=>clearTimeout(m)},[n,u,o,b]),N(()=>{let m=()=>{document.visibilityState==="visible"&&n&&b()};return document.addEventListener("visibilitychange",m),()=>document.removeEventListener("visibilitychange",m)},[n,b]),N(()=>{let m=t.on("read",()=>{i([...t.getMessages()])});return()=>m()},[t]),!bt(e))return null;let h=async m=>{if(m.preventDefault(),!a.trim())return;let P=a;c("");try{await t.sendMessage(P)}catch(C){console.error("[PocketPing] Failed to send message:",C)}},M=m=>{let P=m.target;c(P.value),t.sendTyping(!0)},T=e.position??"bottom-right",H=xt(e.theme??"auto"),R=e.primaryColor??"#6366f1";return d(E,{children:[d("style",{children:Fe(R,H)}),d("button",{class:`pp-toggle pp-${T}`,onClick:()=>t.toggleOpen(),"aria-label":n?"Close chat":"Open chat",children:[n?d(je,{}):d(wt,{}),!n&&f&&d("span",{class:"pp-online-dot"})]}),n&&d("div",{class:`pp-window pp-${T} pp-theme-${H}`,children:[d("div",{class:"pp-header",children:[d("div",{class:"pp-header-info",children:[e.operatorAvatar&&d("img",{src:e.operatorAvatar,alt:"",class:"pp-avatar"}),d("div",{children:[d("div",{class:"pp-header-title",children:e.operatorName??"Support"}),d("div",{class:"pp-header-status",children:f?d(E,{children:[d("span",{class:"pp-status-dot pp-online"})," Online"]}):d(E,{children:[d("span",{class:"pp-status-dot"})," Away"]})})]})]}),d("button",{class:"pp-close-btn",onClick:()=>t.setOpen(!1),"aria-label":"Close chat",children:d(je,{})})]}),d("div",{class:"pp-messages",children:[e.welcomeMessage&&o.length===0&&d("div",{class:"pp-welcome",children:e.welcomeMessage}),o.map(m=>d("div",{class:`pp-message pp-message-${m.sender}`,children:[d("div",{class:"pp-message-content",children:m.content}),d("div",{class:"pp-message-time",children:[kt(m.timestamp),m.sender==="ai"&&d("span",{class:"pp-ai-badge",children:"AI"}),m.sender==="visitor"&&d("span",{class:`pp-status pp-status-${m.status??"sent"}`,children:d(St,{status:m.status})})]})]},m.id)),p&&d("div",{class:"pp-message pp-message-operator pp-typing",children:[d("span",{}),d("span",{}),d("span",{})]}),d("div",{ref:x})]}),d("form",{class:"pp-input-form",onSubmit:h,children:[d("input",{ref:S,type:"text",class:"pp-input",placeholder:e.placeholder??"Type a message...",value:a,onInput:M,disabled:!u}),d("button",{type:"submit",class:"pp-send-btn",disabled:!a.trim()||!u,"aria-label":"Send message",children:d(Pt,{})})]}),d("div",{class:"pp-footer",children:["Powered by ",d("a",{href:"https://github.com/pocketping/pocketping",target:"_blank",rel:"noopener",children:"PocketPing"})]})]})]})}function bt(t){let e=window.location.pathname;return t.hideOnPages?.some(n=>new RegExp(n).test(e))?!1:t.showOnPages?.length?t.showOnPages.some(n=>new RegExp(n).test(e)):!0}function xt(t){return t==="auto"?window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light":t}function kt(t){return new Date(t).toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"})}function wt(){return d("svg",{viewBox:"0 0 24 24",fill:"none",stroke:"currentColor","stroke-width":"2",children:d("path",{d:"M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"})})}function je(){return d("svg",{viewBox:"0 0 24 24",fill:"none",stroke:"currentColor","stroke-width":"2",children:[d("line",{x1:"18",y1:"6",x2:"6",y2:"18"}),d("line",{x1:"6",y1:"6",x2:"18",y2:"18"})]})}function Pt(){return d("svg",{viewBox:"0 0 24 24",fill:"none",stroke:"currentColor","stroke-width":"2",children:[d("line",{x1:"22",y1:"2",x2:"11",y2:"13"}),d("polygon",{points:"22 2 15 22 11 13 2 9 22 2"})]})}function St({status:t}){return!t||t==="sending"||t==="sent"?d("svg",{viewBox:"0 0 16 16",fill:"none",stroke:"currentColor","stroke-width":"2",class:"pp-check",children:d("polyline",{points:"3 8 7 12 13 4"})}):t==="delivered"?d("svg",{viewBox:"0 0 20 16",fill:"none",stroke:"currentColor","stroke-width":"2",class:"pp-check-double",children:[d("polyline",{points:"1 8 5 12 11 4"}),d("polyline",{points:"7 8 11 12 17 4"})]}):t==="read"?d("svg",{viewBox:"0 0 20 16",fill:"none",stroke:"currentColor","stroke-width":"2",class:"pp-check-double pp-check-read",children:[d("polyline",{points:"1 8 5 12 11 4"}),d("polyline",{points:"7 8 11 12 17 4"})]}):null}var K="0.3.1";var Q=class{constructor(e){this.session=null;this.ws=null;this.isOpen=!1;this.listeners=new Map;this.customEventHandlers=new Map;this.reconnectAttempts=0;this.maxReconnectAttempts=5;this.reconnectTimeout=null;this.config=e}async connect(){let e=this.getOrCreateVisitorId(),n=this.getStoredSessionId(),s=this.getStoredIdentity(),o=await this.fetch("/connect",{method:"POST",body:JSON.stringify({visitorId:e,sessionId:n,metadata:{url:window.location.href,referrer:document.referrer||void 0,pageTitle:document.title||void 0,userAgent:navigator.userAgent,timezone:Intl.DateTimeFormat().resolvedOptions().timeZone,language:navigator.language,screenResolution:`${window.screen.width}x${window.screen.height}`},identity:s||void 0})});return this.session={sessionId:o.sessionId,visitorId:o.visitorId,operatorOnline:o.operatorOnline??!1,messages:o.messages??[],identity:o.identity||s||void 0},this.storeSessionId(o.sessionId),this.connectWebSocket(),this.emit("connect",this.session),this.config.onConnect?.(o.sessionId),this.session}disconnect(){this.ws?.close(),this.ws=null,this.session=null,this.reconnectTimeout&&clearTimeout(this.reconnectTimeout)}async sendMessage(e){if(!this.session)throw new Error("Not connected");let n=`temp-${this.generateId()}`,s={id:n,sessionId:this.session.sessionId,content:e,sender:"visitor",timestamp:new Date().toISOString(),status:"sending"};this.session.messages.push(s),this.emit("message",s);try{let o=await this.fetch("/message",{method:"POST",body:JSON.stringify({sessionId:this.session.sessionId,content:e,sender:"visitor"})}),i=this.session.messages.findIndex(c=>c.id===n);i>=0&&(this.session.messages[i].id=o.messageId,this.session.messages[i].timestamp=o.timestamp,this.session.messages[i].status="sent",this.emit("message",this.session.messages[i]));let a=this.session.messages[i]||{id:o.messageId,sessionId:this.session.sessionId,content:e,sender:"visitor",timestamp:o.timestamp,status:"sent"};return this.config.onMessage?.(a),a}catch(o){let i=this.session.messages.findIndex(a=>a.id===n);throw i>=0&&(this.session.messages.splice(i,1),this.emit("message",s)),o}}async fetchMessages(e){if(!this.session)throw new Error("Not connected");let n=new URLSearchParams({sessionId:this.session.sessionId});return e&&n.set("after",e),(await this.fetch(`/messages?${n}`,{method:"GET"})).messages}async sendTyping(e=!0){this.session&&await this.fetch("/typing",{method:"POST",body:JSON.stringify({sessionId:this.session.sessionId,sender:"visitor",isTyping:e})})}async sendReadStatus(e,n){if(!(!this.session||e.length===0))try{await this.fetch("/read",{method:"POST",body:JSON.stringify({sessionId:this.session.sessionId,messageIds:e,status:n})});for(let s of this.session.messages)e.includes(s.id)&&(s.status=n,n==="delivered"?s.deliveredAt=new Date().toISOString():n==="read"&&(s.readAt=new Date().toISOString()));this.emit("readStatusSent",{messageIds:e,status:n})}catch(s){console.error("[PocketPing] Failed to send read status:",s)}}async getPresence(){return this.fetch("/presence",{method:"GET"})}async identify(e){if(!e?.id)throw new Error("[PocketPing] identity.id is required");if(this.storeIdentity(e),this.session)try{await this.fetch("/identify",{method:"POST",body:JSON.stringify({sessionId:this.session.sessionId,identity:e})}),this.session.identity=e,this.emit("identify",e)}catch(n){throw console.error("[PocketPing] Failed to identify:",n),n}}async reset(e){this.clearIdentity(),this.session&&(this.session.identity=void 0),e?.newSession&&(localStorage.removeItem("pocketping_session_id"),localStorage.removeItem("pocketping_visitor_id"),this.disconnect(),await this.connect()),this.emit("reset",null)}getIdentity(){return this.session?.identity||this.getStoredIdentity()}getSession(){return this.session}getMessages(){return this.session?.messages??[]}isConnected(){return this.session!==null}isWidgetOpen(){return this.isOpen}setOpen(e){this.isOpen=e,this.emit("openChange",e),e?this.config.onOpen?.():this.config.onClose?.()}toggleOpen(){this.setOpen(!this.isOpen)}on(e,n){return this.listeners.has(e)||this.listeners.set(e,new Set),this.listeners.get(e).add(n),()=>{this.listeners.get(e)?.delete(n)}}emit(e,n){this.listeners.get(e)?.forEach(s=>s(n))}trigger(e,n){if(!this.ws||this.ws.readyState!==WebSocket.OPEN){console.warn("[PocketPing] Cannot trigger event: WebSocket not connected");return}let s={name:e,data:n,timestamp:new Date().toISOString()};this.ws.send(JSON.stringify({type:"event",data:s})),this.emit(`event:${e}`,s)}onEvent(e,n){return this.customEventHandlers.has(e)||this.customEventHandlers.set(e,new Set),this.customEventHandlers.get(e).add(n),()=>{this.customEventHandlers.get(e)?.delete(n)}}offEvent(e,n){this.customEventHandlers.get(e)?.delete(n)}emitCustomEvent(e){let n=this.customEventHandlers.get(e.name);n&&n.forEach(s=>s(e.data,e)),this.emit("event",e),this.emit(`event:${e.name}`,e)}connectWebSocket(){if(!this.session)return;let e=this.config.endpoint.replace(/^http/,"ws").replace(/\/$/,"")+`/stream?sessionId=${this.session.sessionId}`;try{this.ws=new WebSocket(e),this.ws.onopen=()=>{this.reconnectAttempts=0,this.emit("wsConnected",null)},this.ws.onmessage=n=>{try{let s=JSON.parse(n.data);this.handleWebSocketEvent(s)}catch(s){console.error("[PocketPing] Failed to parse WS message:",s)}},this.ws.onclose=()=>{this.emit("wsDisconnected",null),this.scheduleReconnect()},this.ws.onerror=n=>{console.error("[PocketPing] WebSocket error:",n)}}catch{console.warn("[PocketPing] WebSocket unavailable, using polling"),this.startPolling()}}handleWebSocketEvent(e){switch(e.type){case"message":let n=e.data;if(this.session){let c=this.session.messages.findIndex(p=>p.id===n.id);if(c<0&&n.sender==="visitor"&&(c=this.session.messages.findIndex(p=>p.id.startsWith("temp-")&&p.content===n.content&&p.sender==="visitor"),c>=0&&(this.session.messages[c].id=n.id)),c<0&&n.sender!=="visitor"){let p=new Date(n.timestamp).getTime();c=this.session.messages.findIndex(l=>l.sender===n.sender&&l.content===n.content&&Math.abs(new Date(l.timestamp).getTime()-p)<2e3)}if(c>=0){let p=this.session.messages[c];n.status&&n.status!==p.status&&(p.status=n.status,n.deliveredAt&&(p.deliveredAt=n.deliveredAt),n.readAt&&(p.readAt=n.readAt),this.emit("read",{messageIds:[n.id],status:n.status}))}else this.session.messages.push(n),this.emit("message",n),this.config.onMessage?.(n)}n.sender!=="visitor"&&this.emit("typing",{isTyping:!1});break;case"typing":let s=e.data;s.sender!=="visitor"&&this.emit("typing",{isTyping:s.isTyping});break;case"presence":this.session&&(this.session.operatorOnline=e.data.online),this.emit("presence",e.data);break;case"ai_takeover":this.emit("aiTakeover",e.data);break;case"read":let o=e.data;if(this.session)for(let c of this.session.messages)o.messageIds.includes(c.id)&&(c.status=o.status,o.deliveredAt&&(c.deliveredAt=o.deliveredAt),o.readAt&&(c.readAt=o.readAt));this.emit("read",o);break;case"event":let i=e.data;this.emitCustomEvent(i);break;case"version_warning":let a=e.data;this.handleVersionWarning(a);break}}handleVersionWarning(e){let n="[PocketPing]",s=e.upgradeUrl?` Upgrade: ${e.upgradeUrl}`:" Update your widget to the latest version.";switch(e.severity){case"error":console.error(`${n} \u{1F6A8} VERSION ERROR: ${e.message}${s}`),console.error(`${n} Current: ${e.currentVersion}, Required: ${e.minVersion||"unknown"}`);break;case"warning":console.warn(`${n} \u26A0\uFE0F VERSION WARNING: ${e.message}${s}`),console.warn(`${n} Current: ${e.currentVersion}, Latest: ${e.latestVersion||"unknown"}`);break;case"info":console.info(`${n} \u2139\uFE0F ${e.message}`);break}this.emit("versionWarning",e),this.config.onVersionWarning?.(e),e.canContinue||(console.error(`${n} Widget is incompatible with backend. Please update immediately.`),this.disconnect())}scheduleReconnect(){if(this.reconnectAttempts>=this.maxReconnectAttempts){console.warn("[PocketPing] Max reconnect attempts reached, switching to polling"),this.startPolling();return}let e=Math.min(1e3*Math.pow(2,this.reconnectAttempts),3e4);this.reconnectAttempts++,this.reconnectTimeout=setTimeout(()=>{this.connectWebSocket()},e)}startPolling(){let e=async()=>{if(this.session){try{let n=this.session.messages[this.session.messages.length-1]?.id,s=await this.fetchMessages(n);for(let o of s)this.session.messages.find(i=>i.id===o.id)||(this.session.messages.push(o),this.emit("message",o),this.config.onMessage?.(o))}catch(n){console.error("[PocketPing] Polling error:",n)}this.session&&setTimeout(e,3e3)}};e()}async fetch(e,n){let s=this.config.endpoint.replace(/\/$/,"")+e,o=await fetch(s,{...n,headers:{"Content-Type":"application/json","X-PocketPing-Version":K,...n.headers}});if(this.checkVersionHeaders(o),!o.ok){let i=await o.text();throw new Error(`PocketPing API error: ${o.status} ${i}`)}return o.json()}checkVersionHeaders(e){let n=e.headers.get("X-PocketPing-Version-Status"),s=e.headers.get("X-PocketPing-Min-Version"),o=e.headers.get("X-PocketPing-Latest-Version"),i=e.headers.get("X-PocketPing-Version-Message");if(!n||n==="ok")return;let a="info",c=!0;n==="deprecated"?a="warning":n==="unsupported"?(a="error",c=!1):n==="outdated"&&(a="info");let p={severity:a,message:i||`Widget version ${K} is ${n}`,currentVersion:K,minVersion:s||void 0,latestVersion:o||void 0,canContinue:c,upgradeUrl:"https://docs.pocketping.io/widget/installation"};this.handleVersionWarning(p)}getOrCreateVisitorId(){let e="pocketping_visitor_id",n=localStorage.getItem(e);return n||(n=this.generateId(),localStorage.setItem(e,n)),n}getStoredSessionId(){return localStorage.getItem("pocketping_session_id")}storeSessionId(e){localStorage.setItem("pocketping_session_id",e)}getStoredIdentity(){try{let e=localStorage.getItem("pocketping_user_identity");return e?JSON.parse(e):null}catch{return null}}storeIdentity(e){localStorage.setItem("pocketping_user_identity",JSON.stringify(e))}clearIdentity(){localStorage.removeItem("pocketping_user_identity")}generateId(){return`${Date.now().toString(36)}-${Math.random().toString(36).slice(2,11)}`}};var y=null,A=null,It="https://app.pocketping.io/api/widget";function ue(t){if(y)return console.warn("[PocketPing] Already initialized"),y;let e=t.endpoint;if(!e&&t.projectId&&(e=`${It}/${t.projectId}`),!e)throw new Error("[PocketPing] endpoint or projectId is required");let n={...t,endpoint:e};return y=new Q(n),A=document.createElement("div"),A.id="pocketping-container",document.body.appendChild(A),ce(ie(ze,{client:y,config:t}),A),y.connect().catch(s=>{console.error("[PocketPing] Failed to connect:",s)}),y}function Be(){A&&(ce(null,A),A.remove(),A=null),y&&(y.disconnect(),y=null)}function qe(){y?.setOpen(!0)}function Je(){y?.setOpen(!1)}function Xe(){y?.toggleOpen()}function Ge(t){if(!y)throw new Error("[PocketPing] Not initialized");return y.sendMessage(t)}function Ze(t,e){if(!y){console.warn("[PocketPing] Not initialized, cannot trigger event");return}y.trigger(t,e)}function Ke(t,e){return y?y.onEvent(t,e):(console.warn("[PocketPing] Not initialized, cannot subscribe to event"),()=>{})}function Qe(t,e){y?.offEvent(t,e)}async function Ye(t){if(!y)throw new Error("[PocketPing] Not initialized");return y.identify(t)}async function et(t){if(!y){console.warn("[PocketPing] Not initialized");return}return y.reset(t)}function tt(){return y?.getIdentity()||null}function nt(t,e){return y?y.on(t,e):(console.warn("[PocketPing] Not initialized, cannot subscribe to event"),()=>{})}if(typeof document<"u"){let t=document.currentScript;if(t){let e=t.dataset.projectId,n=t.dataset.endpoint;(e||n)&&ue({projectId:e,endpoint:n,theme:t.dataset.theme||"auto",position:t.dataset.position||"bottom-right"})}}var Ct={init:ue,destroy:Be,open:qe,close:Je,toggle:Xe,sendMessage:Ge,trigger:Ze,onEvent:Ke,offEvent:Qe,on:nt,identify:Ye,reset:et,getIdentity:tt};return ct(Et);})();
|