send notifications over SSE to be displayed inside the app

This commit is contained in:
m5r
2022-06-19 17:57:51 +02:00
parent a46a4a3861
commit 6cf2f8cb94
13 changed files with 146 additions and 63 deletions

View File

@ -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 = [

View File

@ -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],
);