Files
call-sdk/packages/core/lib/useCallSdk.ts
2024-12-06 12:44:35 +05:30

178 lines
4.3 KiB
TypeScript

import {useEffect, useReducer} from 'react';
import {GenericObject, StateType} from "./types.ts";
import IAdapter from "@universal-call-sdk/common/lib/Interfaces/IAdapter.ts";
enum actionTypes {
CALL_INCOMING= 'CALL_INCOMING',
CALL_CONNECTED= 'CALL_CONNECTED',
CALL_DISCONNECTED= 'CALL_DISCONNECTED',
CALL_REJECTED= 'CALL_REJECTED',
CALL_MUTED= 'CALL_MUTED',
CALL_UNMUTED= 'CALL_UNMUTED',
}
type Actions = {
type: actionTypes,
payload: GenericObject
}
function reducer(state : StateType, action: Actions) : GenericObject {
if(action.type === actionTypes.CALL_INCOMING) {
return {
...state,
connectedCustomerdata: action.payload,
isRinging: true
}
}
if(action.type === actionTypes.CALL_CONNECTED) {
return {
...state,
isCallConnected: true
}
}
if(action.type === actionTypes.CALL_DISCONNECTED) {
return {
...state,
connectedCustomerData: {},
isCallDisconnected: true
}
}
if(action.type === actionTypes.CALL_REJECTED) {
return {
connectedCustomerData: {},
isRinging: false
}
}
if(action.type === actionTypes.CALL_MUTED) {
return {
...state,
isMuted: true
}
}
if(action.type === actionTypes.CALL_UNMUTED) {
return {
...state,
isMuted: false
}
}
return state;
}
const initialState : StateType = {
connectedCustomerdata: {},
isRinging: false,
isCallConnected: false,
isCallDisconnected: false,
callStartTime: Date.now(),
}
let adapter: IAdapter;
function UseCallSdk({AdapterClass, adapterOptions} : {AdapterClass: new (adapterOptions: IAdapter)=> IAdapter, adapterOptions: IAdapter}) {
useEffect(() => {
adapter = new AdapterClass(adapterOptions);
}, []);
// @ts-expect-error sdfsf
const [callState] = useReducer<any>(reducer, initialState,()=> initialState);
function registerOnCallIncoming(callback : (callState: GenericObject)=>void) {
//dispatch({type: actionTypes.CALL_INCOMING, payload: {}})
adapter.registerOnCallIncoming(callback);
}
function registerOnCallConnected(callback : (callState: GenericObject)=>void) {
adapter.registerOnCallConnected(callback);
}
function registerOnCallDisconnected(callback : (callState: GenericObject)=>void) {
adapter.registerOnCallDisconnected(callback);
}
function registerOnAdapterReady(callback : ()=> void) {
adapter.registerOnAdapterReady(callback);
}
function registerOnAgentAvailabilityChange(callback : (isAgentAvailable: boolean) => void) {
adapter.registerOnAgentAvailabilityChange(callback);
}
function registerOnForcedLogoutListener(callback:()=>void) {
adapter.registerOnForcedLogoutListener(callback);
}
function registerOnLoginFailedListener(callback:()=>void) {
adapter.registerOnLoginFailedListener(callback);
}
function acceptCall() {
adapter.acceptCall();
}
function rejectCall() {
adapter.rejectCall();
}
function disposeCall() {
adapter.disposeCall();
}
function muteCall() {
adapter.muteCall();
}
function unmuteCall() {
adapter.unmuteCall();
}
function initialize() {
adapter.init();
}
function setOnBreak() {
adapter.setOnBreak();
}
function setAvailable() {
adapter.setAvailable();
}
function getAgentAvailability(): boolean {
console.log('prinitng adapter', adapter);
return adapter.getAgentAvailability();
}
function getLatestCallState(): GenericObject {
return adapter.getLatestCallState();
}
return {
callState,
registerOnCallIncoming,
registerOnCallConnected,
registerOnCallDisconnected,
registerOnAgentAvailabilityChange,
registerOnForcedLogoutListener,
registerOnLoginFailedListener,
acceptCall,
rejectCall,
disposeCall,
muteCall,
unmuteCall,
initialize,
setAvailable,
getLatestCallState,
setOnBreak,
registerOnAdapterReady,
getAgentAvailability
}
}
export default UseCallSdk;