Files
call-sdk/packages/core/lib/useCallSdk.ts

167 lines
4.0 KiB
TypeScript
Raw Normal View History

2024-11-03 19:48:11 +05:30
import {useEffect, useReducer} from 'react';
2024-10-29 18:07:27 +05:30
import {GenericObject, StateType} from "./types.ts";
2024-11-03 12:06:19 +05:30
import IAdapter from "@universal-call-sdk/common/lib/Interfaces/IAdapter.ts";
2024-10-29 18:07:27 +05:30
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(),
}
2024-11-03 19:56:30 +05:30
let adapter: IAdapter;
2024-11-03 19:48:11 +05:30
function UseCallSdk({AdapterClass, adapterOptions} : {AdapterClass: new (adapterOptions: IAdapter)=> IAdapter, adapterOptions: IAdapter}) {
useEffect(() => {
adapter = new AdapterClass(adapterOptions);
}, []);
2024-10-29 18:07:27 +05:30
// @ts-expect-error sdfsf
const [callState] = useReducer<any>(reducer, initialState,()=> initialState);
2024-11-02 12:14:34 +05:30
function registerOnCallIncoming(callback : (callState: GenericObject)=>void) {
2024-10-29 18:07:27 +05:30
//dispatch({type: actionTypes.CALL_INCOMING, payload: {}})
adapter.registerOnCallIncoming(callback);
}
2024-11-02 12:14:34 +05:30
function registerOnCallConnected(callback : (callState: GenericObject)=>void) {
2024-10-29 18:07:27 +05:30
adapter.registerOnCallConnected(callback);
}
2024-11-02 12:14:34 +05:30
function registerOnCallDisconnected(callback : (callState: GenericObject)=>void) {
2024-10-29 18:07:27 +05:30
adapter.registerOnCallDisconnected(callback);
}
2024-11-03 14:44:11 +05:30
function registerOnAdapterReady(callback : ()=> void) {
adapter.registerOnAdapterReady(callback);
}
2024-11-05 12:54:41 +05:30
function registerOnAgentAvailabilityChange(callback : (isAgentAvailable: boolean) => void) {
adapter.registerOnAgentAvailabilityChange(callback);
}
function registerOnForcedLogoutListener(callback:()=>void) {
adapter.registerOnForcedLogoutListener(callback);
}
2024-10-29 18:07:27 +05:30
function acceptCall() {
adapter.acceptCall();
}
function rejectCall() {
adapter.rejectCall();
}
function muteCall() {
adapter.muteCall();
}
function unmuteCall() {
adapter.unmuteCall();
}
2024-11-03 13:42:38 +05:30
function initialize() {
adapter.init();
}
2024-11-03 14:41:51 +05:30
function setOnBreak() {
adapter.setOnBreak();
}
function setAvailable() {
adapter.setAvailable();
}
2024-11-03 19:19:27 +05:30
function getAgentAvailability(): boolean {
2024-11-03 19:51:36 +05:30
console.log('prinitng adapter', adapter);
2024-11-03 19:19:27 +05:30
return adapter.getAgentAvailability();
2024-11-03 19:14:35 +05:30
}
function getLatestCallState(): GenericObject {
return adapter.getLatestCallState();
}
2024-10-29 18:07:27 +05:30
return {
callState,
registerOnCallIncoming,
registerOnCallConnected,
registerOnCallDisconnected,
2024-11-05 12:54:41 +05:30
registerOnAgentAvailabilityChange,
registerOnForcedLogoutListener,
2024-10-29 18:07:27 +05:30
acceptCall,
rejectCall,
muteCall,
2024-11-03 13:42:38 +05:30
unmuteCall,
initialize,
2024-11-03 14:41:51 +05:30
setAvailable,
getLatestCallState,
2024-11-03 14:44:11 +05:30
setOnBreak,
2024-11-03 19:14:35 +05:30
registerOnAdapterReady,
getAgentAvailability
2024-10-29 18:07:27 +05:30
}
}
export default UseCallSdk;