send notifications over SSE to be displayed inside the app
This commit is contained in:
@ -1,8 +1,10 @@
|
||||
import { useCallback, useEffect } from "react";
|
||||
import type { Call } from "@twilio/voice-sdk";
|
||||
import { atom, useAtom } from "jotai";
|
||||
import { notificationDataAtom } from "~/features/core/hooks/use-notifications";
|
||||
|
||||
export default function useCall() {
|
||||
const [, setNotificationData] = useAtom(notificationDataAtom);
|
||||
const [call, setCall] = useAtom(callAtom);
|
||||
const endCall = useCallback(
|
||||
function endCallFn() {
|
||||
@ -10,8 +12,9 @@ export default function useCall() {
|
||||
call?.removeListener("disconnect", endCall);
|
||||
call?.disconnect();
|
||||
setCall(null);
|
||||
setNotificationData(null);
|
||||
},
|
||||
[call, setCall],
|
||||
[call, setCall, setNotificationData],
|
||||
);
|
||||
const onError = useCallback(
|
||||
function onErrorFn(error: any) {
|
||||
@ -19,9 +22,10 @@ export default function useCall() {
|
||||
call?.removeListener("disconnect", endCall);
|
||||
call?.disconnect();
|
||||
setCall(null);
|
||||
setNotificationData(null);
|
||||
throw error; // TODO: might not get caught by error boundary
|
||||
},
|
||||
[call, setCall, endCall],
|
||||
[call, setCall, endCall, setNotificationData],
|
||||
);
|
||||
|
||||
const eventHandlers = [
|
||||
|
@ -82,8 +82,7 @@ export default function useDevice() {
|
||||
|
||||
setCall(incomingCall);
|
||||
console.log("incomingCall.parameters", incomingCall.parameters);
|
||||
// TODO prevent making a new call when there is a pending incoming call
|
||||
const channel = new BroadcastChannel("notifications");
|
||||
const notifyChannel = new BroadcastChannel("notifications");
|
||||
const recipient = incomingCall.parameters.From;
|
||||
const message: NotificationPayload = {
|
||||
title: recipient, // TODO:
|
||||
@ -100,7 +99,8 @@ export default function useDevice() {
|
||||
],
|
||||
data: { recipient, type: "call" },
|
||||
};
|
||||
channel.postMessage(JSON.stringify(message));
|
||||
notifyChannel.postMessage(JSON.stringify(message));
|
||||
notifyChannel.close();
|
||||
},
|
||||
[call, setCall],
|
||||
);
|
||||
|
Reference in New Issue
Block a user