Skip to content

Commit

Permalink
Fix event handlers in broadcast message (#245)
Browse files Browse the repository at this point in the history
* Fix event handlers in broadcast message
  • Loading branch information
esme authored Nov 21, 2024
1 parent c5baa71 commit 05bfdaa
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
56 changes: 43 additions & 13 deletions demos/demo-react-ts/src/components/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useState, useMemo, useCallback } from "react";
import { v4 as uuidv4 } from "uuid";
import { ThemeProvider } from "styled-components";
import { Constants } from "@hubspot/calling-extensions-sdk";
import { createTheme } from "../visitor-ui-component-library/theme/createTheme";
Expand Down Expand Up @@ -48,7 +49,7 @@ export const INBOUND_SCREENS = [
export const broadcastEventHandlers = {
[thirdPartyToHostEvents.LOGGED_IN]: "userLoggedIn",
[thirdPartyToHostEvents.LOGGED_OUT]: "userLoggedOut",
[thirdPartyToHostEvents.INITIALIZE]: "initialize",
[thirdPartyToHostEvents.INITIALIZED]: "initialized",
[thirdPartyToHostEvents.OUTGOING_CALL_STARTED]: "outgoingCall",
[thirdPartyToHostEvents.USER_AVAILABLE]: "userAvailable",
[thirdPartyToHostEvents.USER_UNAVAILABLE]: "userUnavailable",
Expand Down Expand Up @@ -110,13 +111,9 @@ function App() {
setIncomingNumber(incomingNumber);
};

const {
cti,
phoneNumber,
engagementId,
incomingContactName,
iframeLocation,
} = useCti(initializeCallingStateForExistingCall);
const { cti, phoneNumber, engagementId, incomingContactName } = useCti(
initializeCallingStateForExistingCall
);

const screens = direction === "OUTBOUND" ? OUTBOUND_SCREENS : INBOUND_SCREENS;

Expand Down Expand Up @@ -175,11 +172,44 @@ function App() {
cti.broadcastChannel.onmessage = ({
data,
}: MessageEvent<{ type: string; payload?: any }>) => {
// Send SDK message to HubSpot
if (iframeLocation === "window") {
const eventHandler = broadcastEventHandlers[data.type];
if (eventHandler && cti[eventHandler] && data.payload) {
cti[eventHandler](data.payload);
// Send SDK message to HubSpot in the calling window
if (cti.isFromWindow) {
// TODO: Refactor to use eventHandler to invoke the appropriate function
// const eventHandler = broadcastEventHandlers[data.type];
// cti.contract[eventHandler](data.payload);

if (data.type === thirdPartyToHostEvents.INITIALIZED) {
cti.contract.initialized(data.payload);
} else if (data.type === thirdPartyToHostEvents.LOGGED_IN) {
cti.contract.userLoggedIn();
} else if (data.type === thirdPartyToHostEvents.LOGGED_OUT) {
cti.contract.userLoggedOut();
} else if (data.type === thirdPartyToHostEvents.USER_AVAILABLE) {
cti.contract.userAvailable();
} else if (data.type === thirdPartyToHostEvents.USER_UNAVAILABLE) {
cti.contract.userUnavailable();
} else if (data.type === thirdPartyToHostEvents.INCOMING_CALL) {
cti.externalCallId = uuidv4();
cti.contract.incomingCall({
...data.payload,
externalCallId: cti.externalCallId,
});
} else if (data.type === thirdPartyToHostEvents.OUTGOING_CALL_STARTED) {
cti.externalCallId = uuidv4();
cti.contract.outgoingCall({
...data.payload,
externalCallId: cti.externalCallId,
});
} else if (data.type === thirdPartyToHostEvents.CALL_ENDED) {
cti.contract.callEnded({
...data.payload,
externalCallId: cti.externalCallId,
});
} else if (data.type === thirdPartyToHostEvents.CALL_COMPLETED) {
cti.contract.callCompleted({
...data.payload,
externalCallId: cti.externalCallId,
});
}
}

Expand Down
12 changes: 6 additions & 6 deletions demos/demo-react-ts/src/hooks/useCti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class CallingExtensionsWrapper implements CallingExtensionsContract {
window.broadcastChannel = this.broadcastChannel;
}

get contract() {
return this._cti;
}

get externalCallId() {
return this._externalCallId;
}
Expand Down Expand Up @@ -189,6 +193,7 @@ class CallingExtensionsWrapper implements CallingExtensionsContract {
// Send message to HubSpot
this.incomingNumber = callDetails.fromNumber;
this.externalCallId = uuidv4();

this._cti.incomingCall({
...callDetails,
externalCallId: this.externalCallId,
Expand Down Expand Up @@ -300,7 +305,7 @@ class CallingExtensionsWrapper implements CallingExtensionsContract {
broadcastMessage({ type, payload }: { type: string; payload?: any }) {
this.broadcastChannel.postMessage({
type,
payload: { ...payload, externalCallId: this.externalCallId },
payload,
});
}
}
Expand All @@ -311,7 +316,6 @@ export const useCti = (
const [phoneNumber, setPhoneNumber] = useState("");
const [engagementId, setEngagementId] = useState<number | null>(null);
const [incomingContactName, setIncomingContactName] = useState<string>("");
const [iframeLocation, setIframeLocation] = useState("");

const cti = useMemo(() => {
return new CallingExtensionsWrapper({
Expand Down Expand Up @@ -350,9 +354,6 @@ export const useCti = (
window.localStorage.removeItem(INCOMING_NUMBER_KEY);
window.localStorage.removeItem(INCOMING_CONTACT_NAME_KEY);
}
if (data.iframeLocation) {
setIframeLocation(data.iframeLocation);
}
},
onDialNumber: (data: any, _rawEvent: any) => {
const { phoneNumber } = data;
Expand Down Expand Up @@ -456,6 +457,5 @@ export const useCti = (
engagementId,
cti,
incomingContactName,
iframeLocation,
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const props: Partial<ScreenProps> = {
callDurationString: "",
startTimer: noop,
stopTimer: noop,
handleEndCall: noop,
handleSaveCall: noop,
handleCallEnded: noop,
handleCallCompleted: noop,
fromNumber: "",
setFromNumber: noop,
};
Expand Down

0 comments on commit 05bfdaa

Please sign in to comment.