@tbisoftware/phone 2.0.8 → 2.0.9
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/context/PhoneContext.d.ts +10 -1
- package/dist/context/PhoneContext.d.ts.map +1 -1
- package/dist/core/index.cjs +1 -1
- package/dist/core/index.js +1 -1
- package/dist/{index-BTMeEufy.cjs → index-1EBiLrV_.cjs} +1 -1
- package/dist/{index-D8BnjTav.js → index-8z_LWKp3.js} +4 -1
- package/dist/index.cjs +1 -1
- package/dist/index.js +3 -3
- package/dist/react/Phone.d.ts.map +1 -1
- package/dist/react/PhoneContext.d.ts +10 -1
- package/dist/react/PhoneContext.d.ts.map +1 -1
- package/dist/react/index.cjs +1 -1
- package/dist/react/index.js +2 -2
- package/dist/types/index.d.ts +6 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/usePhoneManager-CfG7kttP.cjs +1 -0
- package/dist/usePhoneManager-CjRcDBFA.js +650 -0
- package/dist/vue/index.cjs +1 -1
- package/dist/vue/index.js +557 -384
- package/dist/vue/usePhone.d.ts +9 -0
- package/dist/vue/usePhone.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/usePhoneManager-B0omS154.js +0 -555
- package/dist/usePhoneManager-DYQFwNDy.cjs +0 -1
|
@@ -0,0 +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;
|