Skip to content

Commit

Permalink
Sync the remote with calling window changes (#244)
Browse files Browse the repository at this point in the history
* Sync changes in the remote with calling window
  • Loading branch information
esme authored Nov 20, 2024
1 parent 6db0517 commit c5baa71
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 22 deletions.
4 changes: 2 additions & 2 deletions demos/demo-react-ts/src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ function App() {

cti.broadcastChannel.onmessage = ({
data,
}: MessageEvent<{ type: string }>) => {
}: MessageEvent<{ type: string; payload?: any }>) => {
// Send SDK message to HubSpot
if (iframeLocation === "window") {
const eventHandler = broadcastEventHandlers[data.type];
if (eventHandler) {
if (eventHandler && cti[eventHandler] && data.payload) {
cti[eventHandler](data.payload);
}
}
Expand Down
81 changes: 61 additions & 20 deletions demos/demo-react-ts/src/hooks/useCti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,32 @@ class CallingExtensionsWrapper implements CallingExtensionsContract {
this._iframeLocation = location;
}

/** Do not send messages to HubSpot in the remote */
get isFromRemote() {
return this._iframeLocation === "remote";
}

/** Send messages to HubSpot in the calling window */
get isFromWindow() {
return this._iframeLocation === "window";
}

/** Broadcast message from remote or window */
get shouldBroadcastMessage() {
return (
this._iframeLocation === "remote" || this._iframeLocation === "window"
);
}

initialized(userData: OnInitialized) {
if (this._iframeLocation === "remote") {
this.broadcastMessage({ type: thirdPartyToHostEvents.INITIALIZED });
if (this.shouldBroadcastMessage) {
this.broadcastMessage({
type: thirdPartyToHostEvents.INITIALIZED,
payload: userData,
});
}

if (this.isFromRemote) {
return;
}

Expand All @@ -104,57 +127,63 @@ class CallingExtensionsWrapper implements CallingExtensionsContract {
}

userAvailable() {
if (this._iframeLocation === "remote") {
if (this.shouldBroadcastMessage) {
this.broadcastMessage({ type: thirdPartyToHostEvents.USER_AVAILABLE });
}

if (this.isFromRemote) {
return;
}

return this._cti.userAvailable();
}

userUnavailable() {
if (this._iframeLocation === "remote") {
if (this.shouldBroadcastMessage) {
this.broadcastMessage({ type: thirdPartyToHostEvents.USER_UNAVAILABLE });
}

if (this.isFromRemote) {
return;
}

return this._cti.userUnavailable();
}

userLoggedIn() {
if (this._iframeLocation === "remote") {
if (this.shouldBroadcastMessage) {
this.broadcastMessage({ type: thirdPartyToHostEvents.LOGGED_IN });
}

if (this.isFromRemote) {
return;
}

return this._cti.userLoggedIn();
}

userLoggedOut() {
if (this._iframeLocation === "remote") {
if (this.shouldBroadcastMessage) {
this.broadcastMessage({ type: thirdPartyToHostEvents.LOGGED_OUT });
}

if (this.isFromRemote) {
return;
}

return this._cti.userLoggedOut();
}

incomingCall(callDetails: OnIncomingCall) {
// Triggered from remote
if (this._iframeLocation === "remote") {
if (this.shouldBroadcastMessage) {
this.broadcastMessage({
type: thirdPartyToHostEvents.INCOMING_CALL,
payload: callDetails,
});
return;
}

// Triggered from popup
if (this._iframeLocation === "window") {
this.broadcastMessage({
type: thirdPartyToHostEvents.INCOMING_CALL,
payload: callDetails,
});
if (this.isFromRemote) {
return;
}

// Send message to HubSpot
Expand All @@ -167,11 +196,14 @@ class CallingExtensionsWrapper implements CallingExtensionsContract {
}

outgoingCall(callDetails: OnOutgoingCall) {
if (this._iframeLocation === "remote") {
if (this.shouldBroadcastMessage) {
this.broadcastMessage({
type: thirdPartyToHostEvents.OUTGOING_CALL_STARTED,
payload: callDetails,
});
}

if (this.isFromRemote) {
return;
}

Expand All @@ -187,11 +219,14 @@ class CallingExtensionsWrapper implements CallingExtensionsContract {
}

callAnswered(data: OnCallAnswered) {
if (this._iframeLocation === "remote") {
if (this.shouldBroadcastMessage) {
this.broadcastMessage({
type: thirdPartyToHostEvents.CALL_ANSWERED,
payload: data,
});
}

if (this.isFromRemote) {
return;
}

Expand All @@ -206,11 +241,14 @@ class CallingExtensionsWrapper implements CallingExtensionsContract {
}

callEnded(engagementData: OnCallEnded) {
if (this._iframeLocation === "remote") {
if (this.shouldBroadcastMessage) {
this.broadcastMessage({
type: thirdPartyToHostEvents.CALL_ENDED,
payload: engagementData,
});
}

if (this.isFromRemote) {
return;
}

Expand All @@ -221,11 +259,14 @@ class CallingExtensionsWrapper implements CallingExtensionsContract {
}

callCompleted(callCompletedData: OnCallCompleted) {
if (this._iframeLocation === "remote") {
if (this.shouldBroadcastMessage) {
this.broadcastMessage({
type: thirdPartyToHostEvents.CALL_COMPLETED,
payload: callCompletedData,
});
}

if (this.isFromRemote) {
return;
}

Expand Down Expand Up @@ -256,7 +297,7 @@ class CallingExtensionsWrapper implements CallingExtensionsContract {
return this._cti.logDebugMessage(messageData);
}

broadcastMessage({ type, payload }: { type: string; payload: any }) {
broadcastMessage({ type, payload }: { type: string; payload?: any }) {
this.broadcastChannel.postMessage({
type,
payload: { ...payload, externalCallId: this.externalCallId },
Expand Down
2 changes: 2 additions & 0 deletions demos/demo-react-ts/src/types/ScreenTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface ScreenProps {
callDurationString: string;
startTimer: Function;
stopTimer: Function;
handleOutgoingCallStarted: Function;
handleIncomingCall: Function;
handleCallEnded: Function;
handleCallCompleted: Function;
fromNumber: string;
Expand Down

0 comments on commit c5baa71

Please sign in to comment.