2022-05-24 21:13:08 +00:00
|
|
|
import { useCallback, useState } from "react";
|
|
|
|
import { useNavigate } from "@remix-run/react";
|
|
|
|
import type { Call } from "@twilio/voice-sdk";
|
|
|
|
|
|
|
|
import useDevice from "./use-device";
|
2022-06-14 23:28:32 +00:00
|
|
|
import useCall from "./use-call";
|
2022-05-24 21:13:08 +00:00
|
|
|
|
|
|
|
type Params = {
|
|
|
|
recipient: string;
|
|
|
|
onHangUp?: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function useMakeCall({ recipient, onHangUp }: Params) {
|
|
|
|
const navigate = useNavigate();
|
2022-06-14 23:28:32 +00:00
|
|
|
const [call, setCall] = useCall();
|
2022-05-24 21:13:08 +00:00
|
|
|
const [state, setState] = useState<State>("initial");
|
|
|
|
const { device, isDeviceReady } = useDevice();
|
|
|
|
|
|
|
|
const endCall = useCallback(
|
|
|
|
function endCall() {
|
|
|
|
setState("call_ending");
|
|
|
|
setTimeout(() => {
|
|
|
|
setState("call_ended");
|
|
|
|
setTimeout(() => navigate("/keypad"), 100);
|
|
|
|
}, 150);
|
|
|
|
},
|
2022-06-14 23:28:32 +00:00
|
|
|
[navigate],
|
2022-05-24 21:13:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const makeCall = useCallback(
|
|
|
|
async function makeCall() {
|
|
|
|
if (!device || !isDeviceReady) {
|
|
|
|
console.warn("device is not ready yet, can't make the call");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state !== "initial") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-14 23:28:32 +00:00
|
|
|
if (device.isBusy || Boolean(call)) {
|
2022-05-24 21:13:08 +00:00
|
|
|
console.error("device is busy, this shouldn't happen");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setState("calling");
|
|
|
|
|
|
|
|
const params = { To: recipient };
|
|
|
|
const outgoingConnection = await device.connect({ params });
|
2022-06-14 23:28:32 +00:00
|
|
|
setCall(outgoingConnection);
|
2022-05-24 21:13:08 +00:00
|
|
|
|
|
|
|
outgoingConnection.once("accept", (call: Call) => setState("call_in_progress"));
|
2022-06-14 23:28:32 +00:00
|
|
|
outgoingConnection.once("cancel", endCall);
|
|
|
|
outgoingConnection.once("disconnect", endCall);
|
2022-05-24 21:13:08 +00:00
|
|
|
},
|
2022-06-14 23:28:32 +00:00
|
|
|
[call, device, endCall, isDeviceReady, recipient, setCall, state],
|
2022-05-24 21:13:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const sendDigits = useCallback(
|
|
|
|
function sendDigits(digits: string) {
|
2022-06-14 23:28:32 +00:00
|
|
|
return call?.sendDigits(digits);
|
2022-05-24 21:13:08 +00:00
|
|
|
},
|
2022-06-14 23:28:32 +00:00
|
|
|
[call],
|
2022-05-24 21:13:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const hangUp = useCallback(
|
|
|
|
function hangUp() {
|
|
|
|
setState("call_ending");
|
2022-06-14 23:28:32 +00:00
|
|
|
call?.disconnect();
|
2022-05-24 21:13:08 +00:00
|
|
|
onHangUp?.();
|
|
|
|
navigate("/keypad");
|
|
|
|
},
|
2022-06-14 23:28:32 +00:00
|
|
|
[call, onHangUp, navigate],
|
2022-05-24 21:13:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
makeCall,
|
|
|
|
sendDigits,
|
|
|
|
hangUp,
|
|
|
|
state,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
type State = "initial" | "ready" | "calling" | "call_in_progress" | "call_ending" | "call_ended";
|