@tbisoftware/phone 2.0.9 → 2.0.10
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/dist/index.cjs +1 -1
- package/dist/index.js +2 -2
- package/dist/react/Phone.d.ts.map +1 -1
- package/dist/react/index.cjs +1 -1
- package/dist/react/index.js +2 -2
- package/dist/{usePhoneManager-CjRcDBFA.js → usePhoneManager-CDKZ09Gg.js} +33 -34
- package/dist/{usePhoneManager-CfG7kttP.cjs → usePhoneManager-_ZmUf7wJ.cjs} +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("./usePhoneManager-
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("./usePhoneManager-_ZmUf7wJ.cjs"),o=require("./index-1EBiLrV_.cjs");exports.Phone=e.Phone;exports.PhoneProvider=e.PhoneProvider;exports.default=e.Phone;exports.usePhone=e.usePhone;exports.usePhoneManager=e.usePhoneManager;exports.cn=o.cn;exports.defaultLabels=o.defaultLabels;exports.formatDuration=o.formatDuration;
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { P as e } from "./usePhoneManager-
|
|
2
|
-
import { a as s, u as t, b as f } from "./usePhoneManager-
|
|
1
|
+
import { P as e } from "./usePhoneManager-CDKZ09Gg.js";
|
|
2
|
+
import { a as s, u as t, b as f } from "./usePhoneManager-CDKZ09Gg.js";
|
|
3
3
|
import { c as u, d as P, f as m } from "./index-8z_LWKp3.js";
|
|
4
4
|
export {
|
|
5
5
|
e as Phone,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Phone.d.ts","sourceRoot":"","sources":["../../src/react/Phone.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAoB,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"Phone.d.ts","sourceRoot":"","sources":["../../src/react/Phone.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,UAAU,EAAoB,MAAM,UAAU,CAAC;AAoZ7D,MAAM,CAAC,OAAO,UAAU,KAAK,CAAC,EAAE,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,EAAE,EAAE,UAAU,2CAW9G"}
|
package/dist/react/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("../usePhoneManager-
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("../usePhoneManager-_ZmUf7wJ.cjs");exports.Phone=e.Phone;exports.PhoneProvider=e.PhoneProvider;exports.default=e.Phone;exports.usePhone=e.usePhone;exports.usePhoneManager=e.usePhoneManager;
|
package/dist/react/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { P as o } from "../usePhoneManager-
|
|
2
|
-
import { a as s, u as P, b as n } from "../usePhoneManager-
|
|
1
|
+
import { P as o } from "../usePhoneManager-CDKZ09Gg.js";
|
|
2
|
+
import { a as s, u as P, b as n } from "../usePhoneManager-CDKZ09Gg.js";
|
|
3
3
|
export {
|
|
4
4
|
o as Phone,
|
|
5
5
|
s as PhoneProvider,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { jsx as t, jsxs as s, Fragment as se } from "react/jsx-runtime";
|
|
2
|
-
import { useState as v, useRef as $, useEffect as
|
|
2
|
+
import { useState as v, useRef as $, useEffect as M, useCallback as U, createContext as oe, useContext as ae } from "react";
|
|
3
3
|
import { J as G, d as le, c as K, f as J, P as ce } from "./index-8z_LWKp3.js";
|
|
4
4
|
let _ = null, V = null;
|
|
5
5
|
function ie(e) {
|
|
@@ -94,14 +94,14 @@ function ge({
|
|
|
94
94
|
onStatusChange: d,
|
|
95
95
|
onIncomingCall: y
|
|
96
96
|
}) {
|
|
97
|
-
const [h, o] = v(""), [a, n] = v("disconnected"), [g, z] = v(null), [N, b] = v(0), [
|
|
98
|
-
|
|
97
|
+
const [h, o] = v(""), [a, n] = v("disconnected"), [g, z] = v(null), [N, b] = v(0), [j, L] = v([]), [T, C] = v(!1), [c, I] = v("disconnected"), [m, k] = v(!1), [E, D] = v(null), S = $(null), l = $(null), H = $(null);
|
|
98
|
+
M(() => {
|
|
99
99
|
l.current = g;
|
|
100
100
|
}, [g]);
|
|
101
101
|
const Z = U(() => {
|
|
102
102
|
m || (k(!0), I("connecting"));
|
|
103
103
|
}, [m]);
|
|
104
|
-
|
|
104
|
+
M(() => {
|
|
105
105
|
if (!m) return;
|
|
106
106
|
const r = de(e);
|
|
107
107
|
H.current = r;
|
|
@@ -128,35 +128,35 @@ function ge({
|
|
|
128
128
|
w.terminate({ status_code: 486, reason_phrase: "Busy Here" });
|
|
129
129
|
return;
|
|
130
130
|
}
|
|
131
|
-
|
|
132
|
-
console.error("Incoming call failed:", O?.cause), n("failed"), P(p, 0, "missed"),
|
|
131
|
+
D({ session: w, callerNumber: p, callerName: B }), o(p), n("ringing"), y?.(p, B), w.on("failed", (O) => {
|
|
132
|
+
console.error("Incoming call failed:", O?.cause), n("failed"), P(p, 0, "missed"), D(null), S.current = null, setTimeout(() => n("disconnected"), 3e3);
|
|
133
133
|
}), w.on("ended", () => {
|
|
134
134
|
n("ended");
|
|
135
135
|
const O = l.current, q = O ? Math.floor((Date.now() - O) / 1e3) : 0;
|
|
136
|
-
P(p, q, "completed"), u?.(p, q, "completed"),
|
|
136
|
+
P(p, q, "completed"), u?.(p, q, "completed"), D(null), S.current = null, setTimeout(() => {
|
|
137
137
|
n("disconnected"), z(null);
|
|
138
138
|
}, 2e3);
|
|
139
139
|
}), w.on("confirmed", () => {
|
|
140
|
-
n("confirmed"), z(Date.now()),
|
|
140
|
+
n("confirmed"), z(Date.now()), D(null);
|
|
141
141
|
});
|
|
142
142
|
}
|
|
143
143
|
};
|
|
144
144
|
return me(r, R), ue(r), () => {
|
|
145
145
|
he(r, R);
|
|
146
146
|
};
|
|
147
|
-
}, [m, e.websocketUrl, e.sipUri, e.password, e.registrarServer, e.displayName, e.authorizationUser]),
|
|
147
|
+
}, [m, e.websocketUrl, e.sipUri, e.password, e.registrarServer, e.displayName, e.authorizationUser]), M(() => {
|
|
148
148
|
d?.(a);
|
|
149
|
-
}, [a, d]),
|
|
149
|
+
}, [a, d]), M(() => {
|
|
150
150
|
const r = localStorage.getItem("tbi-phone-call-history");
|
|
151
151
|
if (r)
|
|
152
152
|
try {
|
|
153
|
-
|
|
153
|
+
L(JSON.parse(r));
|
|
154
154
|
} catch (x) {
|
|
155
155
|
console.error("Error loading call history", x);
|
|
156
156
|
}
|
|
157
|
-
}, []),
|
|
158
|
-
|
|
159
|
-
}, [
|
|
157
|
+
}, []), M(() => {
|
|
158
|
+
j.length > 0 && localStorage.setItem("tbi-phone-call-history", JSON.stringify(j));
|
|
159
|
+
}, [j]), M(() => {
|
|
160
160
|
if (a === "confirmed" && g) {
|
|
161
161
|
const r = setInterval(() => {
|
|
162
162
|
b(Math.floor((Date.now() - g) / 1e3));
|
|
@@ -164,7 +164,7 @@ function ge({
|
|
|
164
164
|
return () => clearInterval(r);
|
|
165
165
|
} else
|
|
166
166
|
b(0);
|
|
167
|
-
}, [a, g]),
|
|
167
|
+
}, [a, g]), M(() => {
|
|
168
168
|
const r = (x) => {
|
|
169
169
|
const R = x.detail.number;
|
|
170
170
|
a === "disconnected" && W(R);
|
|
@@ -181,9 +181,9 @@ function ge({
|
|
|
181
181
|
duration: x,
|
|
182
182
|
status: R
|
|
183
183
|
};
|
|
184
|
-
|
|
184
|
+
L((p) => [w, ...p].slice(0, 50));
|
|
185
185
|
}, []), ee = U(() => {
|
|
186
|
-
S.current && (S.current.terminate(), S.current = null),
|
|
186
|
+
S.current && (S.current.terminate(), S.current = null), D(null);
|
|
187
187
|
}, []), te = U(() => {
|
|
188
188
|
if (!E) return;
|
|
189
189
|
const { session: r, callerNumber: x } = E, R = {
|
|
@@ -192,7 +192,7 @@ function ge({
|
|
|
192
192
|
try {
|
|
193
193
|
r.answer(R), S.current = r, i?.(x);
|
|
194
194
|
} catch (w) {
|
|
195
|
-
console.error("Failed to answer call:", w), n("failed"), P(x, 0, "missed"),
|
|
195
|
+
console.error("Failed to answer call:", w), n("failed"), P(x, 0, "missed"), D(null), setTimeout(() => n("disconnected"), 3e3);
|
|
196
196
|
}
|
|
197
197
|
}, [E, i, P]), ne = U(() => {
|
|
198
198
|
if (!E) return;
|
|
@@ -202,7 +202,7 @@ function ge({
|
|
|
202
202
|
} catch (R) {
|
|
203
203
|
console.error("Failed to reject call:", R);
|
|
204
204
|
}
|
|
205
|
-
P(x, 0, "missed"),
|
|
205
|
+
P(x, 0, "missed"), D(null), n("disconnected");
|
|
206
206
|
}, [E, P]), W = U((r) => {
|
|
207
207
|
const x = H.current;
|
|
208
208
|
if (!r.trim() || !x) return;
|
|
@@ -243,7 +243,7 @@ function ge({
|
|
|
243
243
|
status: a,
|
|
244
244
|
callNumber: h,
|
|
245
245
|
setCallNumber: o,
|
|
246
|
-
callHistory:
|
|
246
|
+
callHistory: j,
|
|
247
247
|
currentCallDuration: N,
|
|
248
248
|
startCall: W,
|
|
249
249
|
endCall: ee,
|
|
@@ -284,8 +284,8 @@ function Ne({ className: e, labels: f }) {
|
|
|
284
284
|
isReady: z,
|
|
285
285
|
connectionStatus: N,
|
|
286
286
|
isInitialized: b,
|
|
287
|
-
initialize:
|
|
288
|
-
incomingCall:
|
|
287
|
+
initialize: j,
|
|
288
|
+
incomingCall: L
|
|
289
289
|
} = pe(), [T, C] = v(!1), c = { ...le, ...f }, m = (() => {
|
|
290
290
|
switch (i) {
|
|
291
291
|
case "ringing":
|
|
@@ -309,7 +309,7 @@ function Ne({ className: e, labels: f }) {
|
|
|
309
309
|
!b && /* @__PURE__ */ t("div", { className: "flex items-center justify-center", children: /* @__PURE__ */ t(
|
|
310
310
|
"button",
|
|
311
311
|
{
|
|
312
|
-
onClick:
|
|
312
|
+
onClick: j,
|
|
313
313
|
className: "h-8 w-8 flex items-center justify-center rounded-xl bg-gray-100 hover:bg-green-100 hover:text-green-600 text-gray-500 transition-colors",
|
|
314
314
|
type: "button",
|
|
315
315
|
title: c.turnOn,
|
|
@@ -347,7 +347,7 @@ function Ne({ className: e, labels: f }) {
|
|
|
347
347
|
className: "h-8 w-8 flex items-center justify-center rounded-xl bg-green-500 hover:bg-green-600 disabled:bg-gray-300 disabled:cursor-not-allowed text-white transition-colors",
|
|
348
348
|
type: "button",
|
|
349
349
|
title: z ? "Call" : "Connecting...",
|
|
350
|
-
children: N === "connecting" ? /* @__PURE__ */ t("div", { className: "w-4 h-4 border-2 border-white
|
|
350
|
+
children: N === "connecting" ? /* @__PURE__ */ t("div", { className: "w-4 h-4 border-2 border-white rounded-full animate-spin" }) : /* @__PURE__ */ t(F, { className: "w-4 h-4" })
|
|
351
351
|
}
|
|
352
352
|
)
|
|
353
353
|
] }),
|
|
@@ -377,15 +377,14 @@ function Ne({ className: e, labels: f }) {
|
|
|
377
377
|
}
|
|
378
378
|
)
|
|
379
379
|
] }),
|
|
380
|
-
i === "ringing" &&
|
|
380
|
+
i === "ringing" && L && /* @__PURE__ */ s("div", { className: "flex flex-col items-center gap-3 py-6", children: [
|
|
381
381
|
/* @__PURE__ */ s("div", { className: "relative", children: [
|
|
382
382
|
/* @__PURE__ */ t(m.Icon, { className: "w-12 h-12 text-blue-500 animate-bounce" }),
|
|
383
383
|
/* @__PURE__ */ t("div", { className: "absolute inset-0 rounded-full border-4 border-blue-500/30 animate-ping" })
|
|
384
384
|
] }),
|
|
385
385
|
/* @__PURE__ */ s("div", { className: "text-center", children: [
|
|
386
386
|
/* @__PURE__ */ t("p", { className: "text-sm text-gray-500", children: c.incomingCall }),
|
|
387
|
-
/* @__PURE__ */ t("p", { className: "text-base font-semibold", children:
|
|
388
|
-
D.callerName && /* @__PURE__ */ t("p", { className: "text-sm text-gray-600", children: D.callerName })
|
|
387
|
+
/* @__PURE__ */ t("p", { className: "text-base font-semibold", children: L.callerNumber })
|
|
389
388
|
] }),
|
|
390
389
|
/* @__PURE__ */ s("div", { className: "flex gap-3", children: [
|
|
391
390
|
/* @__PURE__ */ s(
|
|
@@ -572,8 +571,8 @@ function He(e, f = {}) {
|
|
|
572
571
|
onConnectionChange: y,
|
|
573
572
|
persistHistory: h = !0,
|
|
574
573
|
historyKey: o = "tbi-phone-call-history"
|
|
575
|
-
} = f, [a, n] = v("disconnected"), [g, z] = v(""), [N, b] = v([]), [
|
|
576
|
-
|
|
574
|
+
} = f, [a, n] = v("disconnected"), [g, z] = v(""), [N, b] = v([]), [j, L] = v(0), [T, C] = v(!1), [c, I] = v("connecting"), m = $(null);
|
|
575
|
+
M(() => {
|
|
577
576
|
const l = new ce(
|
|
578
577
|
e,
|
|
579
578
|
{
|
|
@@ -585,7 +584,7 @@ function He(e, f = {}) {
|
|
|
585
584
|
},
|
|
586
585
|
onCallStart: i,
|
|
587
586
|
onCallEnd: u,
|
|
588
|
-
onDurationUpdate:
|
|
587
|
+
onDurationUpdate: L,
|
|
589
588
|
onHistoryUpdate: b,
|
|
590
589
|
onRegistered: () => C(!0),
|
|
591
590
|
onUnregistered: () => C(!1)
|
|
@@ -607,7 +606,7 @@ function He(e, f = {}) {
|
|
|
607
606
|
e.authorizationUser,
|
|
608
607
|
h,
|
|
609
608
|
o
|
|
610
|
-
]),
|
|
609
|
+
]), M(() => {
|
|
611
610
|
m.current && m.current.setEvents({
|
|
612
611
|
onCallStart: i,
|
|
613
612
|
onCallEnd: u,
|
|
@@ -623,7 +622,7 @@ function He(e, f = {}) {
|
|
|
623
622
|
z(l), m.current?.setCallNumber(l);
|
|
624
623
|
}, []), E = U((l) => {
|
|
625
624
|
m.current?.startCall(l);
|
|
626
|
-
}, []),
|
|
625
|
+
}, []), D = U(() => {
|
|
627
626
|
m.current?.endCall();
|
|
628
627
|
}, []), S = U(() => {
|
|
629
628
|
m.current?.clearHistory(), b([]);
|
|
@@ -634,9 +633,9 @@ function He(e, f = {}) {
|
|
|
634
633
|
setCallNumber: k,
|
|
635
634
|
callHistory: N,
|
|
636
635
|
clearCallHistory: S,
|
|
637
|
-
currentCallDuration:
|
|
636
|
+
currentCallDuration: j,
|
|
638
637
|
startCall: E,
|
|
639
|
-
endCall:
|
|
638
|
+
endCall: D,
|
|
640
639
|
isReady: T,
|
|
641
640
|
connectionStatus: c,
|
|
642
641
|
ua: m.current?.ua ?? null
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const e=require("react/jsx-runtime"),s=require("react"),P=require("./index-1EBiLrV_.cjs");let L=null,O=null;function ee(t){return`${t.websocketUrl}|${t.sipUri}|${t.authorizationUser}`}function te(t){const h=ee(t);if(L&&O===h)return L;if(L&&O!==h){try{L.ua.stop()}catch{}L=null}O=h;const u={sockets:[new P.JsSIP.WebSocketInterface(t.websocketUrl)],uri:t.sipUri,password:t.password,registrar_server:t.registrarServer,display_name:t.displayName,authorization_user:t.authorizationUser,connection_recovery_min_interval:2,connection_recovery_max_interval:30},d=new P.JsSIP.UA(u),v=document.createElement("audio");v.autoplay=!0;const x={ua:d,audio:v,isStarted:!1,listeners:new Set};return d.on("connecting",()=>{x.listeners.forEach(a=>a.onConnecting?.())}),d.on("connected",()=>{x.listeners.forEach(a=>a.onConnected?.())}),d.on("disconnected",()=>{x.listeners.forEach(a=>a.onDisconnected?.())}),d.on("registered",()=>{x.listeners.forEach(a=>a.onRegistered?.())}),d.on("unregistered",()=>{x.listeners.forEach(a=>a.onUnregistered?.())}),d.on("registrationFailed",a=>{x.listeners.forEach(l=>l.onRegistrationFailed?.(a?.cause))}),d.on("newRTCSession",a=>{const l=a.session;if(l.direction==="incoming"){const n=l.remote_identity,f=n?.uri?.user||"Unknown",S=n?.display_name||void 0;x.listeners.forEach(C=>C.onIncomingSession?.(l,f,S)),l.on("peerconnection",()=>{l.connection.addEventListener("addstream",C=>{var j=document.createElement("audio");C.streams!==void 0&&C.streams.length!==0&&(j.srcObject=C.streams[0],j.play())}),l.connection.addEventListener("track",C=>{var j=document.createElement("audio");j.srcObject=C.streams[0],j.play()})});return}x.listeners.forEach(n=>n.onNewSession?.(l)),l.connection&&(l.connection.addEventListener("addstream",n=>{var f=document.createElement("audio");n.streams!==void 0&&n.streams.length!==0&&(f.srcObject=n.streams[0],f.play())}),l.connection.addEventListener("track",n=>{var f=document.createElement("audio");f.srcObject=n.streams[0],f.play()}))}),L=x,x}function se(t){t.isStarted||(t.ua.start(),t.isStarted=!0)}function ne(t,h){t.listeners.add(h)}function re(t,h){t.listeners.delete(h)}function ae(t){return{isReady:t.ua.isRegistered(),isConnected:t.ua.isConnected()}}const K=s.createContext(null);function J({config:t,children:h,onCallStart:i,onCallEnd:u,onStatusChange:d,onIncomingCall:v}){const[x,a]=s.useState(""),[l,n]=s.useState("disconnected"),[f,S]=s.useState(null),[C,j]=s.useState(0),[D,H]=s.useState([]),[M,b]=s.useState(!1),[c,w]=s.useState("disconnected"),[m,I]=s.useState(!1),[k,U]=s.useState(null),N=s.useRef(null),o=s.useRef(null),E=s.useRef(null);s.useEffect(()=>{o.current=f},[f]);const G=s.useCallback(()=>{m||(I(!0),w("connecting"))},[m]);s.useEffect(()=>{if(!m)return;const r=te(t);E.current=r;const p=ae(r);p.isReady?(b(!0),w("connected")):p.isConnected&&w("connected");const z={onConnecting:()=>w("connecting"),onConnected:()=>w("connected"),onDisconnected:()=>{w("disconnected"),b(!1)},onRegistered:()=>{b(!0),w("connected")},onUnregistered:()=>b(!1),onRegistrationFailed:y=>{console.error("Registration failed:",y),b(!1),w("failed")},onNewSession:y=>{N.current=y},onIncomingSession:(y,g,_)=>{if(N.current){y.terminate({status_code:486,reason_phrase:"Busy Here"});return}U({session:y,callerNumber:g,callerName:_}),a(g),n("ringing"),v?.(g,_),y.on("failed",B=>{console.error("Incoming call failed:",B?.cause),n("failed"),R(g,0,"missed"),U(null),N.current=null,setTimeout(()=>n("disconnected"),3e3)}),y.on("ended",()=>{n("ended");const B=o.current,F=B?Math.floor((Date.now()-B)/1e3):0;R(g,F,"completed"),u?.(g,F,"completed"),U(null),N.current=null,setTimeout(()=>{n("disconnected"),S(null)},2e3)}),y.on("confirmed",()=>{n("confirmed"),S(Date.now()),U(null)})}};return ne(r,z),se(r),()=>{re(r,z)}},[m,t.websocketUrl,t.sipUri,t.password,t.registrarServer,t.displayName,t.authorizationUser]),s.useEffect(()=>{d?.(l)},[l,d]),s.useEffect(()=>{const r=localStorage.getItem("tbi-phone-call-history");if(r)try{H(JSON.parse(r))}catch(p){console.error("Error loading call history",p)}},[]),s.useEffect(()=>{D.length>0&&localStorage.setItem("tbi-phone-call-history",JSON.stringify(D))},[D]),s.useEffect(()=>{if(l==="confirmed"&&f){const r=setInterval(()=>{j(Math.floor((Date.now()-f)/1e3))},1e3);return()=>clearInterval(r)}else j(0)},[l,f]),s.useEffect(()=>{const r=p=>{const z=p.detail.number;l==="disconnected"&&$(z)};return window.addEventListener("StartCallEvent",r),()=>{window.removeEventListener("StartCallEvent",r)}},[l]);const R=s.useCallback((r,p,z)=>{const y={id:Date.now().toString(),number:r,timestamp:Date.now(),duration:p,status:z};H(g=>[y,...g].slice(0,50))},[]),Q=s.useCallback(()=>{N.current&&(N.current.terminate(),N.current=null),U(null)},[]),X=s.useCallback(()=>{if(!k)return;const{session:r,callerNumber:p}=k,z={mediaConstraints:{audio:!0,video:!1}};try{r.answer(z),N.current=r,i?.(p)}catch(y){console.error("Failed to answer call:",y),n("failed"),R(p,0,"missed"),U(null),setTimeout(()=>n("disconnected"),3e3)}},[k,i,R]),Y=s.useCallback(()=>{if(!k)return;const{session:r,callerNumber:p}=k;try{r.terminate({status_code:603,reason_phrase:"Decline"})}catch(z){console.error("Failed to reject call:",z)}R(p,0,"missed"),U(null),n("disconnected")},[k,R]),$=s.useCallback(r=>{const p=E.current;if(!r.trim()||!p)return;if(!M){console.warn("Phone is not ready yet. Please wait for registration.");return}a(r),i?.(r);const y={eventHandlers:{progress:()=>{n("progress")},failed:g=>{console.error("Call failed:",g?.cause),n("failed"),R(r,0,"failed"),u?.(r,0,"failed"),N.current=null,setTimeout(()=>n("disconnected"),3e3)},ended:()=>{n("ended");const g=o.current,_=g?Math.floor((Date.now()-g)/1e3):0;R(r,_,"completed"),u?.(r,_,"completed"),N.current=null,setTimeout(()=>{n("disconnected"),S(null)},2e3)},confirmed:()=>{n("confirmed"),S(Date.now())}},mediaConstraints:{audio:!0,video:!1}};n("progress");try{const g=p.ua.call(r,y);N.current=g}catch(g){console.error("Failed to start call:",g),n("failed"),R(r,0,"failed"),setTimeout(()=>n("disconnected"),3e3)}},[R,i,u,M]),Z={status:l,callNumber:x,setCallNumber:a,callHistory:D,currentCallDuration:C,startCall:$,endCall:Q,answerCall:X,rejectCall:Y,isReady:M,connectionStatus:c,isInitialized:m,initialize:G,incomingCall:k};return e.jsx(K.Provider,{value:Z,children:h})}function q(){const t=s.useContext(K);if(!t)throw new Error("usePhone must be used within a PhoneProvider");return t}const A=({className:t})=>e.jsx("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:e.jsx("path",{d:"M6.62 10.79c1.44 2.83 3.76 5.14 6.59 6.59l2.2-2.2c.27-.27.67-.36 1.02-.24 1.12.37 2.33.57 3.57.57.55 0 1 .45 1 1V20c0 .55-.45 1-1 1-9.39 0-17-7.61-17-17 0-.55.45-1 1-1h3.5c.55 0 1 .45 1 1 0 1.25.2 2.45.57 3.57.11.35.03.74-.25 1.02l-2.2 2.2z"})}),V=({className:t})=>e.jsx("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:e.jsx("path",{d:"M15.05 5A7 7 0 0 1 19 8.95M15.05 1A11 11 0 0 1 23 8.94m-1 7.98v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z"})}),le=({className:t})=>e.jsx("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:e.jsx("path",{d:"M20 15.5c-1.25 0-2.45-.2-3.57-.57-.35-.12-.74-.03-1.02.24l-2.2 2.2c-2.83-1.45-5.15-3.76-6.59-6.59l2.2-2.21c.28-.26.36-.65.25-1C8.7 6.45 8.5 5.25 8.5 4c0-.55-.45-1-1-1H4c-.55 0-1 .45-1 1 0 9.39 7.61 17 17 17 .55 0 1-.45 1-1v-3.5c0-.55-.45-1-1-1zM19 12h2c0-4.97-4.03-9-9-9v2c3.87 0 7 3.13 7 7zm-4 0h2c0-2.76-2.24-5-5-5v2c1.66 0 3 1.34 3 3z"})}),T=({className:t})=>e.jsx("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:e.jsx("path",{d:"M12 9c-1.6 0-3.15.25-4.6.72v3.1c0 .39-.23.74-.56.9-.98.49-1.87 1.12-2.66 1.85-.18.18-.43.28-.7.28-.28 0-.53-.11-.71-.29L.29 13.08a.956.956 0 0 1-.29-.7c0-.28.11-.53.29-.71C3.34 8.78 7.46 7 12 7s8.66 1.78 11.71 4.67c.18.18.29.43.29.71 0 .28-.11.53-.29.71l-2.48 2.48c-.18.18-.43.29-.71.29-.27 0-.52-.11-.7-.28a11.27 11.27 0 0 0-2.67-1.85.996.996 0 0 1-.56-.9v-3.1C15.15 9.25 13.6 9 12 9z"})}),W=({className:t})=>e.jsx("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:e.jsx("path",{d:"M6.5 5.5 12 11l7-7-1-1-6 6-4.5-4.5H11V3H5v6h1.5V5.5zm17.21 11.17C20.66 13.78 16.54 12 12 12 7.46 12 3.34 13.78.29 16.67c-.18.18-.29.43-.29.71s.11.53.29.71l2.48 2.48c.18.18.43.29.71.29.27 0 .52-.11.7-.28.79-.74 1.69-1.36 2.66-1.85.33-.16.56-.5.56-.9v-3.1c1.45-.48 3-.73 4.6-.73 1.6 0 3.15.25 4.6.72v3.1c0 .39.23.74.56.9.98.49 1.87 1.12 2.67 1.85.18.18.43.28.7.28.28 0 .53-.11.71-.29l2.48-2.48c.18-.18.29-.43.29-.71s-.12-.52-.3-.7z"})}),oe=({className:t})=>e.jsxs("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:[e.jsx("path",{d:"M6.62 10.79c1.44 2.83 3.76 5.14 6.59 6.59l2.2-2.2c.27-.27.67-.36 1.02-.24 1.12.37 2.33.57 3.57.57.55 0 1 .45 1 1V20c0 .55-.45 1-1 1-9.39 0-17-7.61-17-17 0-.55.45-1 1-1h3.5c.55 0 1 .45 1 1 0 1.25.2 2.45.57 3.57.11.35.03.74-.25 1.02l-2.2 2.2z"}),e.jsx("path",{d:"M16 3l-5 5-2-2-1.5 1.5L11 11l6.5-6.5z"})]}),ce=({className:t})=>e.jsxs("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:[e.jsx("path",{d:"M6.62 10.79c1.44 2.83 3.76 5.14 6.59 6.59l2.2-2.2c.27-.27.67-.36 1.02-.24 1.12.37 2.33.57 3.57.57.55 0 1 .45 1 1V20c0 .55-.45 1-1 1-9.39 0-17-7.61-17-17 0-.55.45-1 1-1h3.5c.55 0 1 .45 1 1 0 1.25.2 2.45.57 3.57.11.35.03.74-.25 1.02l-2.2 2.2z"}),e.jsx("path",{d:"M19 6.41L17.59 5 15 7.59 12.41 5 11 6.41 13.59 9 11 11.59 12.41 13 15 10.41 17.59 13 19 11.59 16.41 9z"})]}),ie=({className:t})=>e.jsx("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:e.jsx("path",{d:"M13 3a9 9 0 0 0-9 9H1l3.89 3.89.07.14L9 12H6c0-3.87 3.13-7 7-7s7 3.13 7 7-3.13 7-7 7c-1.93 0-3.68-.79-4.94-2.06l-1.42 1.42A8.954 8.954 0 0 0 13 21a9 9 0 0 0 0-18zm-1 5v5l4.28 2.54.72-1.21-3.5-2.08V8H12z"})}),de=({className:t})=>e.jsx("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:e.jsx("path",{d:"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"})}),ue=({className:t})=>e.jsx("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:e.jsx("path",{d:"M13 3h-2v10h2V3zm4.83 2.17l-1.42 1.42C17.99 7.86 19 9.81 19 12c0 3.87-3.13 7-7 7s-7-3.13-7-7c0-2.19 1.01-4.14 2.58-5.42L6.17 5.17C4.23 6.82 3 9.26 3 12c0 4.97 4.03 9 9 9s9-4.03 9-9c0-2.74-1.23-5.18-3.17-6.83z"})});function me({className:t,labels:h}){const{status:i,callNumber:u,setCallNumber:d,callHistory:v,currentCallDuration:x,startCall:a,endCall:l,answerCall:n,rejectCall:f,isReady:S,connectionStatus:C,isInitialized:j,initialize:D,incomingCall:H}=q(),[M,b]=s.useState(!1),c={...P.defaultLabels,...h},m=(()=>{switch(i){case"ringing":return{text:c.incomingCall,color:"text-blue-500",Icon:V};case"progress":return{text:`${c.calling}...`,color:"text-yellow-500",Icon:V};case"confirmed":return{text:`${c.inCall} - ${P.formatDuration(x)}`,color:"text-green-500",Icon:le};case"failed":return{text:c.callEnded,color:"text-red-500",Icon:W};case"ended":return{text:c.callEnded,color:"text-gray-500",Icon:T};default:return{text:c.inactive,color:"text-gray-300",Icon:A}}})();return e.jsxs("div",{className:P.cn("tbi-phone w-full max-w-md mx-auto bg-white rounded-2xl shadow-lg border border-gray-200 p-2",t),children:[!j&&e.jsx("div",{className:"flex items-center justify-center",children:e.jsx("button",{onClick:D,className:"h-8 w-8 flex items-center justify-center rounded-xl bg-gray-100 hover:bg-green-100 hover:text-green-600 text-gray-500 transition-colors",type:"button",title:c.turnOn,children:e.jsx(ue,{className:"w-5 h-5"})})}),j&&i==="disconnected"&&e.jsxs("div",{className:"flex gap-2 items-center",children:[e.jsx("button",{onClick:()=>b(!0),className:"h-8 w-8 flex items-center justify-center rounded-xl border border-gray-200 hover:bg-gray-50 transition-colors",type:"button",children:e.jsx(ie,{className:"w-4 h-4"})}),e.jsx("input",{type:"text",value:u,onChange:I=>d(I.target.value),onKeyDown:I=>{I.key==="Enter"&&a(u)},placeholder:c.placeholder,className:"flex-1 w-full h-8 px-3 rounded-xl border border-gray-200 text-sm focus:outline-none focus:ring-2 focus:ring-green-500 focus:border-transparent"}),e.jsx("button",{onClick:()=>a(u),disabled:u.length<9||!S,className:"h-8 w-8 flex items-center justify-center rounded-xl bg-green-500 hover:bg-green-600 disabled:bg-gray-300 disabled:cursor-not-allowed text-white transition-colors",type:"button",title:S?"Call":"Connecting...",children:C==="connecting"?e.jsx("div",{className:"w-4 h-4 border-2 border-white border-t-transparent rounded-full animate-spin"}):e.jsx(A,{className:"w-4 h-4"})})]}),i==="progress"&&e.jsxs("div",{className:"flex flex-col items-center gap-3 py-6",children:[e.jsxs("div",{className:"relative",children:[e.jsx(m.Icon,{className:"w-12 h-12 text-yellow-500 animate-pulse"}),e.jsx("div",{className:"absolute inset-0 rounded-full border-4 border-yellow-500/30 animate-ping"})]}),e.jsxs("div",{className:"text-center",children:[e.jsxs("p",{className:"text-base font-semibold",children:[c.calling," ",u]}),e.jsx("p",{className:"text-sm text-gray-500",children:c.waitingResponse})]}),e.jsxs("button",{onClick:l,className:"flex items-center gap-2 px-6 py-2 rounded-full bg-red-500 hover:bg-red-600 text-white text-sm font-medium transition-colors",type:"button",children:[e.jsx(T,{className:"w-4 h-4"}),c.cancel]})]}),i==="ringing"&&H&&e.jsxs("div",{className:"flex flex-col items-center gap-3 py-6",children:[e.jsxs("div",{className:"relative",children:[e.jsx(m.Icon,{className:"w-12 h-12 text-blue-500 animate-bounce"}),e.jsx("div",{className:"absolute inset-0 rounded-full border-4 border-blue-500/30 animate-ping"})]}),e.jsxs("div",{className:"text-center",children:[e.jsx("p",{className:"text-sm text-gray-500",children:c.incomingCall}),e.jsx("p",{className:"text-base font-semibold",children:H.callerNumber}),H.callerName&&e.jsx("p",{className:"text-sm text-gray-600",children:H.callerName})]}),e.jsxs("div",{className:"flex gap-3",children:[e.jsxs("button",{onClick:f,className:"flex items-center gap-2 px-6 py-2 rounded-full bg-red-500 hover:bg-red-600 text-white text-sm font-medium transition-colors",type:"button",children:[e.jsx(T,{className:"w-4 h-4"}),c.reject]}),e.jsxs("button",{onClick:n,className:"flex items-center gap-2 px-6 py-2 rounded-full bg-green-500 hover:bg-green-600 text-white text-sm font-medium transition-colors",type:"button",children:[e.jsx(A,{className:"w-4 h-4"}),c.answer]})]})]}),i==="confirmed"&&e.jsxs("div",{className:"flex flex-col items-center gap-4 py-6",children:[e.jsxs("div",{className:"relative",children:[e.jsx(m.Icon,{className:"w-12 h-12 text-green-500"}),e.jsx("div",{className:"absolute inset-0 rounded-full bg-green-500/20 animate-pulse"})]}),e.jsxs("div",{className:"text-center space-y-1",children:[e.jsx("p",{className:"text-xl font-bold",children:u}),e.jsx("p",{className:"text-2xl font-mono text-green-600 tabular-nums",children:P.formatDuration(x)})]}),e.jsxs("button",{onClick:l,className:"flex items-center gap-2 px-6 py-2 rounded-full bg-red-500 hover:bg-red-600 text-white text-sm font-medium transition-colors",type:"button",children:[e.jsx(T,{className:"w-4 h-4"}),c.hangUp]})]}),(i==="failed"||i==="ended")&&e.jsxs("div",{className:"flex flex-col items-center gap-3 py-6",children:[e.jsx(m.Icon,{className:P.cn("w-12 h-12",i==="failed"?"text-red-500":"text-gray-500")}),e.jsx("div",{className:"text-center",children:e.jsx("p",{className:"text-base font-semibold",children:m.text})})]}),M&&e.jsxs("div",{className:"fixed inset-0 z-50 flex",children:[e.jsx("div",{className:"fixed inset-0 bg-black/50",onClick:()=>b(!1)}),e.jsx("div",{className:"fixed right-0 top-0 h-full w-full max-w-md bg-white shadow-xl",style:{backgroundColor:"white"},children:e.jsxs("div",{className:"flex flex-col h-full",children:[e.jsxs("div",{className:"flex items-center justify-between p-4 border-b",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-lg font-semibold",children:c.callHistory}),e.jsx("p",{className:"text-sm text-gray-500",children:v.length===0?c.noCallsRegistered:`${v.length} ${c.callsRegistered}`})]}),e.jsx("button",{onClick:()=>b(!1),className:"h-8 w-8 flex items-center justify-center rounded-lg hover:bg-gray-100 transition-colors",type:"button",children:e.jsx(de,{className:"w-5 h-5"})})]}),e.jsx("div",{className:"flex-1 overflow-y-auto p-4",children:v.length===0?e.jsxs("div",{className:"text-center py-12 text-gray-500",children:[e.jsx(T,{className:"w-12 h-12 mx-auto mb-2 opacity-50"}),e.jsx("p",{children:c.noCalls})]}):e.jsx("div",{className:"space-y-2",children:v.map((I,k)=>e.jsx(xe,{entry:I,index:k,onCall:()=>{d(I.number),b(!1),a(I.number)}},I.id))})})]})})]})]})}function xe({entry:t,index:h,onCall:i}){const u=()=>{switch(t.status){case"completed":return e.jsx(oe,{className:"w-4 h-4 text-green-600"});case"failed":return e.jsx(ce,{className:"w-4 h-4 text-red-600"});case"missed":return e.jsx(W,{className:"w-4 h-4 text-yellow-600"})}},d=()=>{switch(t.status){case"completed":return"bg-green-100";case"failed":return"bg-red-100";case"missed":return"bg-yellow-100"}};return e.jsxs("div",{className:"flex items-center gap-3 p-3 rounded-lg hover:bg-gray-50 transition-colors duration-200",style:{animationDelay:`${h*30}ms`},children:[e.jsx("div",{className:P.cn("w-9 h-9 rounded-full flex items-center justify-center shrink-0",d()),children:u()}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"font-medium text-sm truncate",children:t.number}),e.jsxs("div",{className:"flex items-center gap-2 text-xs text-gray-500",children:[e.jsx("span",{children:new Date(t.timestamp).toLocaleString("es-ES",{day:"2-digit",month:"2-digit",hour:"2-digit",minute:"2-digit"})}),t.duration>0&&e.jsxs(e.Fragment,{children:[e.jsx("span",{children:"•"}),e.jsx("span",{className:"font-mono tabular-nums",children:P.formatDuration(t.duration)})]})]})]}),e.jsx("button",{onClick:i,className:"h-8 w-8 flex items-center justify-center shrink-0 rounded-lg hover:bg-gray-100 transition-colors",type:"button",children:e.jsx(A,{className:"w-4 h-4"})})]})}function he({config:t,className:h,onCallStart:i,onCallEnd:u,onStatusChange:d,labels:v}){return e.jsx(J,{config:t,onCallStart:i,onCallEnd:u,onStatusChange:d,children:e.jsx(me,{className:h,labels:v})})}function fe(t,h={}){const{onCallStart:i,onCallEnd:u,onStatusChange:d,onConnectionChange:v,persistHistory:x=!0,historyKey:a="tbi-phone-call-history"}=h,[l,n]=s.useState("disconnected"),[f,S]=s.useState(""),[C,j]=s.useState([]),[D,H]=s.useState(0),[M,b]=s.useState(!1),[c,w]=s.useState("connecting"),m=s.useRef(null);s.useEffect(()=>{const o=new P.PhoneManager(t,{onStatusChange:E=>{n(E),d?.(E)},onConnectionChange:E=>{w(E),(E==="connected"||E==="disconnected"||E==="failed")&&b(o.state.isReady),v?.(E)},onCallStart:i,onCallEnd:u,onDurationUpdate:H,onHistoryUpdate:j,onRegistered:()=>b(!0),onUnregistered:()=>b(!1)},{persistHistory:x,historyKey:a});return o.initialize(),m.current=o,n(o.state.status),S(o.state.callNumber),j(o.state.callHistory),b(o.state.isReady),w(o.state.connectionStatus),()=>{o.destroy(),m.current=null}},[t.websocketUrl,t.sipUri,t.password,t.registrarServer,t.displayName,t.authorizationUser,x,a]),s.useEffect(()=>{m.current&&m.current.setEvents({onCallStart:i,onCallEnd:u,onStatusChange:o=>{n(o),d?.(o)},onConnectionChange:o=>{w(o),v?.(o)}})},[i,u,d,v]);const I=s.useCallback(o=>{S(o),m.current?.setCallNumber(o)},[]),k=s.useCallback(o=>{m.current?.startCall(o)},[]),U=s.useCallback(()=>{m.current?.endCall()},[]),N=s.useCallback(()=>{m.current?.clearHistory(),j([])},[]);return{status:l,callNumber:f,setCallNumber:I,callHistory:C,clearCallHistory:N,currentCallDuration:D,startCall:k,endCall:U,isReady:M,connectionStatus:c,ua:m.current?.ua??null}}exports.Phone=he;exports.PhoneProvider=J;exports.usePhone=q;exports.usePhoneManager=fe;
|
|
1
|
+
"use strict";const e=require("react/jsx-runtime"),s=require("react"),P=require("./index-1EBiLrV_.cjs");let L=null,O=null;function ee(t){return`${t.websocketUrl}|${t.sipUri}|${t.authorizationUser}`}function te(t){const h=ee(t);if(L&&O===h)return L;if(L&&O!==h){try{L.ua.stop()}catch{}L=null}O=h;const u={sockets:[new P.JsSIP.WebSocketInterface(t.websocketUrl)],uri:t.sipUri,password:t.password,registrar_server:t.registrarServer,display_name:t.displayName,authorization_user:t.authorizationUser,connection_recovery_min_interval:2,connection_recovery_max_interval:30},d=new P.JsSIP.UA(u),v=document.createElement("audio");v.autoplay=!0;const x={ua:d,audio:v,isStarted:!1,listeners:new Set};return d.on("connecting",()=>{x.listeners.forEach(a=>a.onConnecting?.())}),d.on("connected",()=>{x.listeners.forEach(a=>a.onConnected?.())}),d.on("disconnected",()=>{x.listeners.forEach(a=>a.onDisconnected?.())}),d.on("registered",()=>{x.listeners.forEach(a=>a.onRegistered?.())}),d.on("unregistered",()=>{x.listeners.forEach(a=>a.onUnregistered?.())}),d.on("registrationFailed",a=>{x.listeners.forEach(o=>o.onRegistrationFailed?.(a?.cause))}),d.on("newRTCSession",a=>{const o=a.session;if(o.direction==="incoming"){const n=o.remote_identity,f=n?.uri?.user||"Unknown",S=n?.display_name||void 0;x.listeners.forEach(C=>C.onIncomingSession?.(o,f,S)),o.on("peerconnection",()=>{o.connection.addEventListener("addstream",C=>{var j=document.createElement("audio");C.streams!==void 0&&C.streams.length!==0&&(j.srcObject=C.streams[0],j.play())}),o.connection.addEventListener("track",C=>{var j=document.createElement("audio");j.srcObject=C.streams[0],j.play()})});return}x.listeners.forEach(n=>n.onNewSession?.(o)),o.connection&&(o.connection.addEventListener("addstream",n=>{var f=document.createElement("audio");n.streams!==void 0&&n.streams.length!==0&&(f.srcObject=n.streams[0],f.play())}),o.connection.addEventListener("track",n=>{var f=document.createElement("audio");f.srcObject=n.streams[0],f.play()}))}),L=x,x}function se(t){t.isStarted||(t.ua.start(),t.isStarted=!0)}function ne(t,h){t.listeners.add(h)}function re(t,h){t.listeners.delete(h)}function ae(t){return{isReady:t.ua.isRegistered(),isConnected:t.ua.isConnected()}}const K=s.createContext(null);function J({config:t,children:h,onCallStart:i,onCallEnd:u,onStatusChange:d,onIncomingCall:v}){const[x,a]=s.useState(""),[o,n]=s.useState("disconnected"),[f,S]=s.useState(null),[C,j]=s.useState(0),[U,D]=s.useState([]),[M,b]=s.useState(!1),[c,w]=s.useState("disconnected"),[m,I]=s.useState(!1),[k,H]=s.useState(null),N=s.useRef(null),l=s.useRef(null),E=s.useRef(null);s.useEffect(()=>{l.current=f},[f]);const G=s.useCallback(()=>{m||(I(!0),w("connecting"))},[m]);s.useEffect(()=>{if(!m)return;const r=te(t);E.current=r;const p=ae(r);p.isReady?(b(!0),w("connected")):p.isConnected&&w("connected");const z={onConnecting:()=>w("connecting"),onConnected:()=>w("connected"),onDisconnected:()=>{w("disconnected"),b(!1)},onRegistered:()=>{b(!0),w("connected")},onUnregistered:()=>b(!1),onRegistrationFailed:y=>{console.error("Registration failed:",y),b(!1),w("failed")},onNewSession:y=>{N.current=y},onIncomingSession:(y,g,_)=>{if(N.current){y.terminate({status_code:486,reason_phrase:"Busy Here"});return}H({session:y,callerNumber:g,callerName:_}),a(g),n("ringing"),v?.(g,_),y.on("failed",B=>{console.error("Incoming call failed:",B?.cause),n("failed"),R(g,0,"missed"),H(null),N.current=null,setTimeout(()=>n("disconnected"),3e3)}),y.on("ended",()=>{n("ended");const B=l.current,F=B?Math.floor((Date.now()-B)/1e3):0;R(g,F,"completed"),u?.(g,F,"completed"),H(null),N.current=null,setTimeout(()=>{n("disconnected"),S(null)},2e3)}),y.on("confirmed",()=>{n("confirmed"),S(Date.now()),H(null)})}};return ne(r,z),se(r),()=>{re(r,z)}},[m,t.websocketUrl,t.sipUri,t.password,t.registrarServer,t.displayName,t.authorizationUser]),s.useEffect(()=>{d?.(o)},[o,d]),s.useEffect(()=>{const r=localStorage.getItem("tbi-phone-call-history");if(r)try{D(JSON.parse(r))}catch(p){console.error("Error loading call history",p)}},[]),s.useEffect(()=>{U.length>0&&localStorage.setItem("tbi-phone-call-history",JSON.stringify(U))},[U]),s.useEffect(()=>{if(o==="confirmed"&&f){const r=setInterval(()=>{j(Math.floor((Date.now()-f)/1e3))},1e3);return()=>clearInterval(r)}else j(0)},[o,f]),s.useEffect(()=>{const r=p=>{const z=p.detail.number;o==="disconnected"&&$(z)};return window.addEventListener("StartCallEvent",r),()=>{window.removeEventListener("StartCallEvent",r)}},[o]);const R=s.useCallback((r,p,z)=>{const y={id:Date.now().toString(),number:r,timestamp:Date.now(),duration:p,status:z};D(g=>[y,...g].slice(0,50))},[]),Q=s.useCallback(()=>{N.current&&(N.current.terminate(),N.current=null),H(null)},[]),X=s.useCallback(()=>{if(!k)return;const{session:r,callerNumber:p}=k,z={mediaConstraints:{audio:!0,video:!1}};try{r.answer(z),N.current=r,i?.(p)}catch(y){console.error("Failed to answer call:",y),n("failed"),R(p,0,"missed"),H(null),setTimeout(()=>n("disconnected"),3e3)}},[k,i,R]),Y=s.useCallback(()=>{if(!k)return;const{session:r,callerNumber:p}=k;try{r.terminate({status_code:603,reason_phrase:"Decline"})}catch(z){console.error("Failed to reject call:",z)}R(p,0,"missed"),H(null),n("disconnected")},[k,R]),$=s.useCallback(r=>{const p=E.current;if(!r.trim()||!p)return;if(!M){console.warn("Phone is not ready yet. Please wait for registration.");return}a(r),i?.(r);const y={eventHandlers:{progress:()=>{n("progress")},failed:g=>{console.error("Call failed:",g?.cause),n("failed"),R(r,0,"failed"),u?.(r,0,"failed"),N.current=null,setTimeout(()=>n("disconnected"),3e3)},ended:()=>{n("ended");const g=l.current,_=g?Math.floor((Date.now()-g)/1e3):0;R(r,_,"completed"),u?.(r,_,"completed"),N.current=null,setTimeout(()=>{n("disconnected"),S(null)},2e3)},confirmed:()=>{n("confirmed"),S(Date.now())}},mediaConstraints:{audio:!0,video:!1}};n("progress");try{const g=p.ua.call(r,y);N.current=g}catch(g){console.error("Failed to start call:",g),n("failed"),R(r,0,"failed"),setTimeout(()=>n("disconnected"),3e3)}},[R,i,u,M]),Z={status:o,callNumber:x,setCallNumber:a,callHistory:U,currentCallDuration:C,startCall:$,endCall:Q,answerCall:X,rejectCall:Y,isReady:M,connectionStatus:c,isInitialized:m,initialize:G,incomingCall:k};return e.jsx(K.Provider,{value:Z,children:h})}function q(){const t=s.useContext(K);if(!t)throw new Error("usePhone must be used within a PhoneProvider");return t}const A=({className:t})=>e.jsx("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:e.jsx("path",{d:"M6.62 10.79c1.44 2.83 3.76 5.14 6.59 6.59l2.2-2.2c.27-.27.67-.36 1.02-.24 1.12.37 2.33.57 3.57.57.55 0 1 .45 1 1V20c0 .55-.45 1-1 1-9.39 0-17-7.61-17-17 0-.55.45-1 1-1h3.5c.55 0 1 .45 1 1 0 1.25.2 2.45.57 3.57.11.35.03.74-.25 1.02l-2.2 2.2z"})}),V=({className:t})=>e.jsx("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:e.jsx("path",{d:"M15.05 5A7 7 0 0 1 19 8.95M15.05 1A11 11 0 0 1 23 8.94m-1 7.98v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z"})}),oe=({className:t})=>e.jsx("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:e.jsx("path",{d:"M20 15.5c-1.25 0-2.45-.2-3.57-.57-.35-.12-.74-.03-1.02.24l-2.2 2.2c-2.83-1.45-5.15-3.76-6.59-6.59l2.2-2.21c.28-.26.36-.65.25-1C8.7 6.45 8.5 5.25 8.5 4c0-.55-.45-1-1-1H4c-.55 0-1 .45-1 1 0 9.39 7.61 17 17 17 .55 0 1-.45 1-1v-3.5c0-.55-.45-1-1-1zM19 12h2c0-4.97-4.03-9-9-9v2c3.87 0 7 3.13 7 7zm-4 0h2c0-2.76-2.24-5-5-5v2c1.66 0 3 1.34 3 3z"})}),T=({className:t})=>e.jsx("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:e.jsx("path",{d:"M12 9c-1.6 0-3.15.25-4.6.72v3.1c0 .39-.23.74-.56.9-.98.49-1.87 1.12-2.66 1.85-.18.18-.43.28-.7.28-.28 0-.53-.11-.71-.29L.29 13.08a.956.956 0 0 1-.29-.7c0-.28.11-.53.29-.71C3.34 8.78 7.46 7 12 7s8.66 1.78 11.71 4.67c.18.18.29.43.29.71 0 .28-.11.53-.29.71l-2.48 2.48c-.18.18-.43.29-.71.29-.27 0-.52-.11-.7-.28a11.27 11.27 0 0 0-2.67-1.85.996.996 0 0 1-.56-.9v-3.1C15.15 9.25 13.6 9 12 9z"})}),W=({className:t})=>e.jsx("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:e.jsx("path",{d:"M6.5 5.5 12 11l7-7-1-1-6 6-4.5-4.5H11V3H5v6h1.5V5.5zm17.21 11.17C20.66 13.78 16.54 12 12 12 7.46 12 3.34 13.78.29 16.67c-.18.18-.29.43-.29.71s.11.53.29.71l2.48 2.48c.18.18.43.29.71.29.27 0 .52-.11.7-.28.79-.74 1.69-1.36 2.66-1.85.33-.16.56-.5.56-.9v-3.1c1.45-.48 3-.73 4.6-.73 1.6 0 3.15.25 4.6.72v3.1c0 .39.23.74.56.9.98.49 1.87 1.12 2.67 1.85.18.18.43.28.7.28.28 0 .53-.11.71-.29l2.48-2.48c.18-.18.29-.43.29-.71s-.12-.52-.3-.7z"})}),le=({className:t})=>e.jsxs("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:[e.jsx("path",{d:"M6.62 10.79c1.44 2.83 3.76 5.14 6.59 6.59l2.2-2.2c.27-.27.67-.36 1.02-.24 1.12.37 2.33.57 3.57.57.55 0 1 .45 1 1V20c0 .55-.45 1-1 1-9.39 0-17-7.61-17-17 0-.55.45-1 1-1h3.5c.55 0 1 .45 1 1 0 1.25.2 2.45.57 3.57.11.35.03.74-.25 1.02l-2.2 2.2z"}),e.jsx("path",{d:"M16 3l-5 5-2-2-1.5 1.5L11 11l6.5-6.5z"})]}),ce=({className:t})=>e.jsxs("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:[e.jsx("path",{d:"M6.62 10.79c1.44 2.83 3.76 5.14 6.59 6.59l2.2-2.2c.27-.27.67-.36 1.02-.24 1.12.37 2.33.57 3.57.57.55 0 1 .45 1 1V20c0 .55-.45 1-1 1-9.39 0-17-7.61-17-17 0-.55.45-1 1-1h3.5c.55 0 1 .45 1 1 0 1.25.2 2.45.57 3.57.11.35.03.74-.25 1.02l-2.2 2.2z"}),e.jsx("path",{d:"M19 6.41L17.59 5 15 7.59 12.41 5 11 6.41 13.59 9 11 11.59 12.41 13 15 10.41 17.59 13 19 11.59 16.41 9z"})]}),ie=({className:t})=>e.jsx("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:e.jsx("path",{d:"M13 3a9 9 0 0 0-9 9H1l3.89 3.89.07.14L9 12H6c0-3.87 3.13-7 7-7s7 3.13 7 7-3.13 7-7 7c-1.93 0-3.68-.79-4.94-2.06l-1.42 1.42A8.954 8.954 0 0 0 13 21a9 9 0 0 0 0-18zm-1 5v5l4.28 2.54.72-1.21-3.5-2.08V8H12z"})}),de=({className:t})=>e.jsx("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:e.jsx("path",{d:"M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"})}),ue=({className:t})=>e.jsx("svg",{className:t,viewBox:"0 0 24 24",fill:"currentColor",children:e.jsx("path",{d:"M13 3h-2v10h2V3zm4.83 2.17l-1.42 1.42C17.99 7.86 19 9.81 19 12c0 3.87-3.13 7-7 7s-7-3.13-7-7c0-2.19 1.01-4.14 2.58-5.42L6.17 5.17C4.23 6.82 3 9.26 3 12c0 4.97 4.03 9 9 9s9-4.03 9-9c0-2.74-1.23-5.18-3.17-6.83z"})});function me({className:t,labels:h}){const{status:i,callNumber:u,setCallNumber:d,callHistory:v,currentCallDuration:x,startCall:a,endCall:o,answerCall:n,rejectCall:f,isReady:S,connectionStatus:C,isInitialized:j,initialize:U,incomingCall:D}=q(),[M,b]=s.useState(!1),c={...P.defaultLabels,...h},m=(()=>{switch(i){case"ringing":return{text:c.incomingCall,color:"text-blue-500",Icon:V};case"progress":return{text:`${c.calling}...`,color:"text-yellow-500",Icon:V};case"confirmed":return{text:`${c.inCall} - ${P.formatDuration(x)}`,color:"text-green-500",Icon:oe};case"failed":return{text:c.callEnded,color:"text-red-500",Icon:W};case"ended":return{text:c.callEnded,color:"text-gray-500",Icon:T};default:return{text:c.inactive,color:"text-gray-300",Icon:A}}})();return e.jsxs("div",{className:P.cn("tbi-phone w-full max-w-md mx-auto bg-white rounded-2xl shadow-lg border border-gray-200 p-2",t),children:[!j&&e.jsx("div",{className:"flex items-center justify-center",children:e.jsx("button",{onClick:U,className:"h-8 w-8 flex items-center justify-center rounded-xl bg-gray-100 hover:bg-green-100 hover:text-green-600 text-gray-500 transition-colors",type:"button",title:c.turnOn,children:e.jsx(ue,{className:"w-5 h-5"})})}),j&&i==="disconnected"&&e.jsxs("div",{className:"flex gap-2 items-center",children:[e.jsx("button",{onClick:()=>b(!0),className:"h-8 w-8 flex items-center justify-center rounded-xl border border-gray-200 hover:bg-gray-50 transition-colors",type:"button",children:e.jsx(ie,{className:"w-4 h-4"})}),e.jsx("input",{type:"text",value:u,onChange:I=>d(I.target.value),onKeyDown:I=>{I.key==="Enter"&&a(u)},placeholder:c.placeholder,className:"flex-1 w-full h-8 px-3 rounded-xl border border-gray-200 text-sm focus:outline-none focus:ring-2 focus:ring-green-500 focus:border-transparent"}),e.jsx("button",{onClick:()=>a(u),disabled:u.length<9||!S,className:"h-8 w-8 flex items-center justify-center rounded-xl bg-green-500 hover:bg-green-600 disabled:bg-gray-300 disabled:cursor-not-allowed text-white transition-colors",type:"button",title:S?"Call":"Connecting...",children:C==="connecting"?e.jsx("div",{className:"w-4 h-4 border-2 border-white rounded-full animate-spin"}):e.jsx(A,{className:"w-4 h-4"})})]}),i==="progress"&&e.jsxs("div",{className:"flex flex-col items-center gap-3 py-6",children:[e.jsxs("div",{className:"relative",children:[e.jsx(m.Icon,{className:"w-12 h-12 text-yellow-500 animate-pulse"}),e.jsx("div",{className:"absolute inset-0 rounded-full border-4 border-yellow-500/30 animate-ping"})]}),e.jsxs("div",{className:"text-center",children:[e.jsxs("p",{className:"text-base font-semibold",children:[c.calling," ",u]}),e.jsx("p",{className:"text-sm text-gray-500",children:c.waitingResponse})]}),e.jsxs("button",{onClick:o,className:"flex items-center gap-2 px-6 py-2 rounded-full bg-red-500 hover:bg-red-600 text-white text-sm font-medium transition-colors",type:"button",children:[e.jsx(T,{className:"w-4 h-4"}),c.cancel]})]}),i==="ringing"&&D&&e.jsxs("div",{className:"flex flex-col items-center gap-3 py-6",children:[e.jsxs("div",{className:"relative",children:[e.jsx(m.Icon,{className:"w-12 h-12 text-blue-500 animate-bounce"}),e.jsx("div",{className:"absolute inset-0 rounded-full border-4 border-blue-500/30 animate-ping"})]}),e.jsxs("div",{className:"text-center",children:[e.jsx("p",{className:"text-sm text-gray-500",children:c.incomingCall}),e.jsx("p",{className:"text-base font-semibold",children:D.callerNumber})]}),e.jsxs("div",{className:"flex gap-3",children:[e.jsxs("button",{onClick:f,className:"flex items-center gap-2 px-6 py-2 rounded-full bg-red-500 hover:bg-red-600 text-white text-sm font-medium transition-colors",type:"button",children:[e.jsx(T,{className:"w-4 h-4"}),c.reject]}),e.jsxs("button",{onClick:n,className:"flex items-center gap-2 px-6 py-2 rounded-full bg-green-500 hover:bg-green-600 text-white text-sm font-medium transition-colors",type:"button",children:[e.jsx(A,{className:"w-4 h-4"}),c.answer]})]})]}),i==="confirmed"&&e.jsxs("div",{className:"flex flex-col items-center gap-4 py-6",children:[e.jsxs("div",{className:"relative",children:[e.jsx(m.Icon,{className:"w-12 h-12 text-green-500"}),e.jsx("div",{className:"absolute inset-0 rounded-full bg-green-500/20 animate-pulse"})]}),e.jsxs("div",{className:"text-center space-y-1",children:[e.jsx("p",{className:"text-xl font-bold",children:u}),e.jsx("p",{className:"text-2xl font-mono text-green-600 tabular-nums",children:P.formatDuration(x)})]}),e.jsxs("button",{onClick:o,className:"flex items-center gap-2 px-6 py-2 rounded-full bg-red-500 hover:bg-red-600 text-white text-sm font-medium transition-colors",type:"button",children:[e.jsx(T,{className:"w-4 h-4"}),c.hangUp]})]}),(i==="failed"||i==="ended")&&e.jsxs("div",{className:"flex flex-col items-center gap-3 py-6",children:[e.jsx(m.Icon,{className:P.cn("w-12 h-12",i==="failed"?"text-red-500":"text-gray-500")}),e.jsx("div",{className:"text-center",children:e.jsx("p",{className:"text-base font-semibold",children:m.text})})]}),M&&e.jsxs("div",{className:"fixed inset-0 z-50 flex",children:[e.jsx("div",{className:"fixed inset-0 bg-black/50",onClick:()=>b(!1)}),e.jsx("div",{className:"fixed right-0 top-0 h-full w-full max-w-md bg-white shadow-xl",style:{backgroundColor:"white"},children:e.jsxs("div",{className:"flex flex-col h-full",children:[e.jsxs("div",{className:"flex items-center justify-between p-4 border-b",children:[e.jsxs("div",{children:[e.jsx("h2",{className:"text-lg font-semibold",children:c.callHistory}),e.jsx("p",{className:"text-sm text-gray-500",children:v.length===0?c.noCallsRegistered:`${v.length} ${c.callsRegistered}`})]}),e.jsx("button",{onClick:()=>b(!1),className:"h-8 w-8 flex items-center justify-center rounded-lg hover:bg-gray-100 transition-colors",type:"button",children:e.jsx(de,{className:"w-5 h-5"})})]}),e.jsx("div",{className:"flex-1 overflow-y-auto p-4",children:v.length===0?e.jsxs("div",{className:"text-center py-12 text-gray-500",children:[e.jsx(T,{className:"w-12 h-12 mx-auto mb-2 opacity-50"}),e.jsx("p",{children:c.noCalls})]}):e.jsx("div",{className:"space-y-2",children:v.map((I,k)=>e.jsx(xe,{entry:I,index:k,onCall:()=>{d(I.number),b(!1),a(I.number)}},I.id))})})]})})]})]})}function xe({entry:t,index:h,onCall:i}){const u=()=>{switch(t.status){case"completed":return e.jsx(le,{className:"w-4 h-4 text-green-600"});case"failed":return e.jsx(ce,{className:"w-4 h-4 text-red-600"});case"missed":return e.jsx(W,{className:"w-4 h-4 text-yellow-600"})}},d=()=>{switch(t.status){case"completed":return"bg-green-100";case"failed":return"bg-red-100";case"missed":return"bg-yellow-100"}};return e.jsxs("div",{className:"flex items-center gap-3 p-3 rounded-lg hover:bg-gray-50 transition-colors duration-200",style:{animationDelay:`${h*30}ms`},children:[e.jsx("div",{className:P.cn("w-9 h-9 rounded-full flex items-center justify-center shrink-0",d()),children:u()}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"font-medium text-sm truncate",children:t.number}),e.jsxs("div",{className:"flex items-center gap-2 text-xs text-gray-500",children:[e.jsx("span",{children:new Date(t.timestamp).toLocaleString("es-ES",{day:"2-digit",month:"2-digit",hour:"2-digit",minute:"2-digit"})}),t.duration>0&&e.jsxs(e.Fragment,{children:[e.jsx("span",{children:"•"}),e.jsx("span",{className:"font-mono tabular-nums",children:P.formatDuration(t.duration)})]})]})]}),e.jsx("button",{onClick:i,className:"h-8 w-8 flex items-center justify-center shrink-0 rounded-lg hover:bg-gray-100 transition-colors",type:"button",children:e.jsx(A,{className:"w-4 h-4"})})]})}function he({config:t,className:h,onCallStart:i,onCallEnd:u,onStatusChange:d,labels:v}){return e.jsx(J,{config:t,onCallStart:i,onCallEnd:u,onStatusChange:d,children:e.jsx(me,{className:h,labels:v})})}function fe(t,h={}){const{onCallStart:i,onCallEnd:u,onStatusChange:d,onConnectionChange:v,persistHistory:x=!0,historyKey:a="tbi-phone-call-history"}=h,[o,n]=s.useState("disconnected"),[f,S]=s.useState(""),[C,j]=s.useState([]),[U,D]=s.useState(0),[M,b]=s.useState(!1),[c,w]=s.useState("connecting"),m=s.useRef(null);s.useEffect(()=>{const l=new P.PhoneManager(t,{onStatusChange:E=>{n(E),d?.(E)},onConnectionChange:E=>{w(E),(E==="connected"||E==="disconnected"||E==="failed")&&b(l.state.isReady),v?.(E)},onCallStart:i,onCallEnd:u,onDurationUpdate:D,onHistoryUpdate:j,onRegistered:()=>b(!0),onUnregistered:()=>b(!1)},{persistHistory:x,historyKey:a});return l.initialize(),m.current=l,n(l.state.status),S(l.state.callNumber),j(l.state.callHistory),b(l.state.isReady),w(l.state.connectionStatus),()=>{l.destroy(),m.current=null}},[t.websocketUrl,t.sipUri,t.password,t.registrarServer,t.displayName,t.authorizationUser,x,a]),s.useEffect(()=>{m.current&&m.current.setEvents({onCallStart:i,onCallEnd:u,onStatusChange:l=>{n(l),d?.(l)},onConnectionChange:l=>{w(l),v?.(l)}})},[i,u,d,v]);const I=s.useCallback(l=>{S(l),m.current?.setCallNumber(l)},[]),k=s.useCallback(l=>{m.current?.startCall(l)},[]),H=s.useCallback(()=>{m.current?.endCall()},[]),N=s.useCallback(()=>{m.current?.clearHistory(),j([])},[]);return{status:o,callNumber:f,setCallNumber:I,callHistory:C,clearCallHistory:N,currentCallDuration:U,startCall:k,endCall:H,isReady:M,connectionStatus:c,ua:m.current?.ua??null}}exports.Phone=he;exports.PhoneProvider=J;exports.usePhone=q;exports.usePhoneManager=fe;
|