2024-11-03 19:48:11 +05:30
|
|
|
import {useEffect, useReducer} from 'react';
|
2025-01-22 14:44:40 +05:30
|
|
|
import {Agent, 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-12-17 13:15:53 +05:30
|
|
|
import MetricsProcessor from "@universal-call-sdk/common/lib/utils/metricsProcessor.ts";
|
|
|
|
|
import ClickStreamProcessor from "@universal-call-sdk/common/lib/utils/clickStreamProcessor.ts";
|
|
|
|
|
import noop from "@universal-call-sdk/common/lib/utils/noop.ts";
|
2024-12-20 19:04:52 +05:30
|
|
|
import {CALL_STATES} from "@universal-call-sdk/adapter-ameyo/lib/types.ts";
|
2024-10-29 18:07:27 +05:30
|
|
|
|
|
|
|
|
|
2024-12-01 01:30:41 +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',
|
2024-10-29 18:07:27 +05:30
|
|
|
}
|
2024-12-01 01:30:41 +05:30
|
|
|
|
2024-10-29 18:07:27 +05:30
|
|
|
type Actions = {
|
|
|
|
|
type: actionTypes,
|
|
|
|
|
payload: GenericObject
|
|
|
|
|
}
|
2024-12-01 01:30:41 +05:30
|
|
|
|
|
|
|
|
function reducer(state: StateType, action: Actions): GenericObject {
|
|
|
|
|
if (action.type === actionTypes.CALL_INCOMING) {
|
2024-10-29 18:07:27 +05:30
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
connectedCustomerdata: action.payload,
|
|
|
|
|
isRinging: true
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-01 01:30:41 +05:30
|
|
|
if (action.type === actionTypes.CALL_CONNECTED) {
|
2024-10-29 18:07:27 +05:30
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-12-01 01:30:41 +05:30
|
|
|
const initialState: StateType = {
|
2024-10-29 18:07:27 +05:30
|
|
|
connectedCustomerdata: {},
|
|
|
|
|
isRinging: false,
|
|
|
|
|
isCallConnected: false,
|
|
|
|
|
isCallDisconnected: false,
|
|
|
|
|
callStartTime: Date.now(),
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-16 19:42:48 +05:30
|
|
|
let adapter: IAdapter
|
2024-12-01 01:30:41 +05:30
|
|
|
|
2024-12-17 13:15:53 +05:30
|
|
|
function UseCallSdk({AdapterClass, adapterOptions, metricsConfig, clickStreamConfig}: {
|
2024-12-01 01:30:41 +05:30
|
|
|
AdapterClass: new (adapterOptions: IAdapter) => IAdapter,
|
2024-12-17 13:15:53 +05:30
|
|
|
adapterOptions: IAdapter,
|
|
|
|
|
metricsConfig?: {
|
|
|
|
|
isMetricEnabled: boolean,
|
|
|
|
|
metricFlowName: string,
|
|
|
|
|
metricUrl: string,
|
|
|
|
|
metricTransporter: (metricPayload: GenericObject) => void
|
|
|
|
|
},
|
|
|
|
|
clickStreamConfig?: {
|
|
|
|
|
clickStreamTransporter: (clickStreamPayload: GenericObject) => void
|
|
|
|
|
}
|
2024-12-01 01:30:41 +05:30
|
|
|
}) {
|
2024-11-03 19:48:11 +05:30
|
|
|
useEffect(() => {
|
2025-01-16 19:42:48 +05:30
|
|
|
if(adapterOptions) {
|
|
|
|
|
adapter = new AdapterClass(adapterOptions);
|
|
|
|
|
}
|
2025-01-30 15:37:51 +05:30
|
|
|
}, [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: {}})
|
2025-01-16 19:42:48 +05:30
|
|
|
adapter?.registerOnCallIncoming(callback);
|
2024-10-29 18:07:27 +05:30
|
|
|
}
|
|
|
|
|
|
2024-11-02 12:14:34 +05:30
|
|
|
function registerOnCallConnected(callback : (callState: GenericObject)=>void) {
|
2025-01-16 19:42:48 +05:30
|
|
|
adapter?.registerOnCallConnected(callback);
|
2024-10-29 18:07:27 +05:30
|
|
|
}
|
|
|
|
|
|
2024-11-02 12:14:34 +05:30
|
|
|
function registerOnCallDisconnected(callback : (callState: GenericObject)=>void) {
|
2025-01-16 19:42:48 +05:30
|
|
|
adapter?.registerOnCallDisconnected(callback);
|
2024-10-29 18:07:27 +05:30
|
|
|
}
|
|
|
|
|
|
2024-11-03 14:44:11 +05:30
|
|
|
function registerOnAdapterReady(callback : ()=> void) {
|
2025-01-16 19:42:48 +05:30
|
|
|
adapter?.registerOnAdapterReady(callback);
|
2024-11-03 14:44:11 +05:30
|
|
|
}
|
|
|
|
|
|
2024-11-05 12:54:41 +05:30
|
|
|
|
2025-01-06 15:19:34 +05:30
|
|
|
function registerOnAgentAvailabilityChange(callback : (isAgentAvailable: boolean, reason: string) => void) {
|
2025-01-16 19:42:48 +05:30
|
|
|
adapter?.registerOnAgentAvailabilityChange(callback);
|
2024-11-05 12:54:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function registerOnForcedLogoutListener(callback:()=>void) {
|
2025-01-16 19:42:48 +05:30
|
|
|
adapter?.registerOnForcedLogoutListener(callback);
|
2024-11-05 12:54:41 +05:30
|
|
|
}
|
|
|
|
|
|
2024-12-10 16:16:33 +05:30
|
|
|
function registerOnCallTransferStatus(callback: (data: GenericObject) => void) {
|
2025-01-16 19:42:48 +05:30
|
|
|
adapter?.registerOnCallTransferStatus(callback);
|
2024-12-02 20:34:09 +05:30
|
|
|
}
|
|
|
|
|
|
2024-12-06 12:44:35 +05:30
|
|
|
function registerOnLoginFailedListener(callback:()=>void) {
|
2025-01-16 19:42:48 +05:30
|
|
|
adapter?.registerOnLoginFailedListener(callback);
|
2024-12-06 12:44:35 +05:30
|
|
|
}
|
|
|
|
|
|
2024-10-29 18:07:27 +05:30
|
|
|
function acceptCall() {
|
2025-01-16 19:42:48 +05:30
|
|
|
adapter?.acceptCall();
|
2024-10-29 18:07:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function rejectCall() {
|
2025-01-16 19:42:48 +05:30
|
|
|
adapter?.rejectCall();
|
2024-10-29 18:07:27 +05:30
|
|
|
}
|
|
|
|
|
|
2024-11-25 07:59:57 +05:30
|
|
|
function disposeCall() {
|
2025-01-16 19:42:48 +05:30
|
|
|
adapter?.disposeCall();
|
2024-11-25 07:59:57 +05:30
|
|
|
}
|
|
|
|
|
|
2024-10-29 18:07:27 +05:30
|
|
|
function muteCall() {
|
2025-01-16 19:42:48 +05:30
|
|
|
adapter?.muteCall();
|
2024-10-29 18:07:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function unmuteCall() {
|
2025-01-16 19:42:48 +05:30
|
|
|
adapter?.unmuteCall();
|
2024-10-29 18:07:27 +05:30
|
|
|
}
|
|
|
|
|
|
2025-01-06 20:21:39 +05:30
|
|
|
function logOut() {
|
2025-01-16 19:42:48 +05:30
|
|
|
adapter?.logOut();
|
2025-01-06 20:21:39 +05:30
|
|
|
}
|
|
|
|
|
|
2024-11-03 13:42:38 +05:30
|
|
|
function initialize() {
|
2024-12-17 13:15:53 +05:30
|
|
|
let metricProcessor;
|
|
|
|
|
let clickStreamProcessor;
|
|
|
|
|
if(metricsConfig && metricsConfig?.isMetricEnabled) {
|
|
|
|
|
metricProcessor = new MetricsProcessor(metricsConfig?.metricUrl || '', metricsConfig?.metricFlowName || '', metricsConfig?.metricTransporter || noop);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if(clickStreamConfig?.clickStreamTransporter) {
|
|
|
|
|
clickStreamProcessor = new ClickStreamProcessor(clickStreamConfig?.clickStreamTransporter || noop);
|
|
|
|
|
}
|
2025-01-16 19:42:48 +05:30
|
|
|
adapter?.init(metricProcessor, clickStreamProcessor);
|
2024-11-03 13:42:38 +05:30
|
|
|
}
|
|
|
|
|
|
2024-11-03 14:41:51 +05:30
|
|
|
function setOnBreak() {
|
2025-01-16 19:42:48 +05:30
|
|
|
adapter?.setOnBreak();
|
2024-11-03 14:41:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setAvailable() {
|
2025-01-16 19:42:48 +05:30
|
|
|
adapter?.setAvailable();
|
2024-11-03 14:41:51 +05:30
|
|
|
}
|
|
|
|
|
|
2024-11-03 19:19:27 +05:30
|
|
|
function getAgentAvailability(): boolean {
|
2024-12-02 20:39:09 +05:30
|
|
|
console.log('printing adapter', adapter);
|
2024-12-01 01:30:41 +05:30
|
|
|
return adapter.getAgentAvailability();
|
2024-11-03 19:14:35 +05:30
|
|
|
}
|
|
|
|
|
|
2024-12-20 19:04:52 +05:30
|
|
|
function getLatestCallState(): CALL_STATES {
|
2024-11-06 09:59:26 +05:30
|
|
|
return adapter.getLatestCallState();
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-22 14:44:40 +05:30
|
|
|
function getAvailableAgentsForCallTransfer(): Promise<Agent[]> {
|
|
|
|
|
return adapter.getAvailableAgentsForCallTransfer();
|
2024-12-01 01:30:41 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function transferCallToAgent(data: GenericObject) {
|
|
|
|
|
adapter.transferCallToAgent(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
2024-10-29 18:07:27 +05:30
|
|
|
callState,
|
|
|
|
|
registerOnCallIncoming,
|
|
|
|
|
registerOnCallConnected,
|
|
|
|
|
registerOnCallDisconnected,
|
2024-11-05 12:54:41 +05:30
|
|
|
registerOnAgentAvailabilityChange,
|
|
|
|
|
registerOnForcedLogoutListener,
|
2024-12-10 16:16:33 +05:30
|
|
|
registerOnCallTransferStatus,
|
2024-12-06 12:44:35 +05:30
|
|
|
registerOnLoginFailedListener,
|
2024-10-29 18:07:27 +05:30
|
|
|
acceptCall,
|
|
|
|
|
rejectCall,
|
2024-11-25 07:59:57 +05:30
|
|
|
disposeCall,
|
2024-10-29 18:07:27 +05:30
|
|
|
muteCall,
|
2024-11-03 13:42:38 +05:30
|
|
|
unmuteCall,
|
|
|
|
|
initialize,
|
2025-01-06 20:21:39 +05:30
|
|
|
logOut,
|
2024-11-03 14:41:51 +05:30
|
|
|
setAvailable,
|
2024-11-06 11:33:40 +05:30
|
|
|
getLatestCallState,
|
2024-11-03 14:44:11 +05:30
|
|
|
setOnBreak,
|
2024-11-03 19:14:35 +05:30
|
|
|
registerOnAdapterReady,
|
2024-12-01 01:30:41 +05:30
|
|
|
getAgentAvailability,
|
|
|
|
|
getAvailableAgentsForCallTransfer,
|
|
|
|
|
transferCallToAgent
|
|
|
|
|
}
|
2024-10-29 18:07:27 +05:30
|
|
|
}
|
|
|
|
|
|
2024-11-25 07:59:57 +05:30
|
|
|
export default UseCallSdk;
|