2021-09-03 16:19:32 +00:00
|
|
|
import type { BlitzPage, ErrorFallbackProps, WithRouterProps } from "blitz";
|
|
|
|
import { ErrorBoundary, Routes, useRouter, withRouter } from "blitz";
|
2021-09-01 21:54:14 +00:00
|
|
|
import { useCallback, useEffect } from "react";
|
2021-09-03 16:19:32 +00:00
|
|
|
import type { TwilioError } from "@twilio/voice-sdk";
|
2021-08-08 06:37:53 +00:00
|
|
|
import { atom, useAtom } from "jotai";
|
|
|
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
|
|
|
import { faPhoneAlt as faPhone } from "@fortawesome/pro-solid-svg-icons";
|
|
|
|
|
|
|
|
import useRequireOnboarding from "../../../core/hooks/use-require-onboarding";
|
2021-08-13 04:43:57 +00:00
|
|
|
import useMakeCall from "../../hooks/use-make-call";
|
2021-08-31 22:42:15 +00:00
|
|
|
import useDevice from "../../hooks/use-device";
|
|
|
|
|
|
|
|
import Keypad from "../../components/keypad";
|
2021-08-08 06:37:53 +00:00
|
|
|
|
|
|
|
const OutgoingCall: BlitzPage = () => {
|
2021-08-13 15:52:38 +00:00
|
|
|
useRequireOnboarding();
|
|
|
|
const [phoneNumber, setPhoneNumber] = useAtom(phoneNumberAtom);
|
2021-08-08 06:37:53 +00:00
|
|
|
const router = useRouter();
|
|
|
|
const recipient = decodeURIComponent(router.params.recipient);
|
2021-09-01 21:54:14 +00:00
|
|
|
const onHangUp = useCallback(() => setPhoneNumber(""), [setPhoneNumber]);
|
2021-08-13 15:52:38 +00:00
|
|
|
const call = useMakeCall({ recipient, onHangUp });
|
2021-09-04 16:29:25 +00:00
|
|
|
const { isDeviceReady } = useDevice();
|
2021-08-08 11:46:32 +00:00
|
|
|
const pressDigit = useAtom(pressDigitAtom)[1];
|
2021-09-01 21:54:14 +00:00
|
|
|
const onDigitPressProps = useCallback(
|
|
|
|
(digit: string) => ({
|
2021-08-08 11:46:32 +00:00
|
|
|
onPress() {
|
|
|
|
pressDigit(digit);
|
|
|
|
|
2021-08-13 04:43:57 +00:00
|
|
|
call.sendDigits(digit);
|
2021-08-08 11:46:32 +00:00
|
|
|
},
|
|
|
|
}),
|
2021-08-13 04:43:57 +00:00
|
|
|
[call, pressDigit],
|
2021-08-08 06:37:53 +00:00
|
|
|
);
|
|
|
|
|
2021-08-13 15:52:38 +00:00
|
|
|
useEffect(() => {
|
2021-09-01 21:54:14 +00:00
|
|
|
if (isDeviceReady) {
|
2021-08-13 15:52:38 +00:00
|
|
|
call.makeCall();
|
|
|
|
}
|
2021-09-04 16:29:25 +00:00
|
|
|
}, [call, isDeviceReady]);
|
2021-08-08 06:37:53 +00:00
|
|
|
|
2021-08-08 11:46:32 +00:00
|
|
|
return (
|
|
|
|
<div className="w-96 h-full flex flex-col justify-around py-5 mx-auto text-center text-black bg-white">
|
|
|
|
<div className="h-16 text-3xl text-gray-700">
|
|
|
|
<span>{recipient}</span>
|
|
|
|
</div>
|
2021-08-08 06:37:53 +00:00
|
|
|
|
2021-08-13 04:43:57 +00:00
|
|
|
<div className="mb-4 text-gray-600">
|
|
|
|
<div className="h-8 text-2xl">{phoneNumber}</div>
|
|
|
|
<div className="h-8 text-lg">{translateState(call.state)}</div>
|
2021-08-08 11:46:32 +00:00
|
|
|
</div>
|
2021-08-08 06:37:53 +00:00
|
|
|
|
2021-08-08 11:46:32 +00:00
|
|
|
<Keypad onDigitPressProps={onDigitPressProps} onZeroPressProps={onDigitPressProps("0")}>
|
2021-08-13 11:55:05 +00:00
|
|
|
<button
|
2021-08-13 15:52:38 +00:00
|
|
|
onClick={call.hangUp}
|
2021-08-08 11:46:32 +00:00
|
|
|
className="cursor-pointer select-none col-start-2 h-12 w-12 flex justify-center items-center mx-auto bg-red-800 rounded-full"
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon className="w-6 h-6" icon={faPhone} color="white" size="lg" />
|
2021-08-13 11:55:05 +00:00
|
|
|
</button>
|
2021-08-08 11:46:32 +00:00
|
|
|
</Keypad>
|
2021-08-08 06:37:53 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
|
2021-08-13 04:43:57 +00:00
|
|
|
function translateState(state: typeof call.state): string {
|
|
|
|
switch (state) {
|
|
|
|
case "initial":
|
|
|
|
case "ready":
|
|
|
|
return "Connecting...";
|
|
|
|
case "calling":
|
|
|
|
return "Calling...";
|
|
|
|
case "call_in_progress":
|
2021-09-03 16:19:32 +00:00
|
|
|
return "In call"; // TODO display time elapsed
|
2021-08-13 04:43:57 +00:00
|
|
|
case "call_ending":
|
|
|
|
return "Call ending...";
|
|
|
|
case "call_ended":
|
|
|
|
return "Call ended";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-08-08 06:37:53 +00:00
|
|
|
|
|
|
|
const phoneNumberAtom = atom("");
|
|
|
|
const pressDigitAtom = atom(null, (get, set, digit: string) => {
|
|
|
|
if (get(phoneNumberAtom).length > 17) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
set(phoneNumberAtom, (prevState) => prevState + digit);
|
|
|
|
});
|
|
|
|
|
|
|
|
OutgoingCall.authenticate = { redirectTo: Routes.SignIn() };
|
2021-09-03 16:19:32 +00:00
|
|
|
OutgoingCall.getLayout = (page) => <ErrorBoundary FallbackComponent={ErrorFallback}>{page}</ErrorBoundary>;
|
|
|
|
|
|
|
|
const ErrorFallback = withRouter(function WrappedErrorFallback({
|
|
|
|
error,
|
|
|
|
router,
|
|
|
|
}: ErrorFallbackProps & WithRouterProps) {
|
|
|
|
console.log("error", JSON.parse(JSON.stringify(error)));
|
|
|
|
return (
|
|
|
|
<div className="w-screen h-screen flex">
|
|
|
|
<div className="max-w-md m-auto p-4">
|
|
|
|
<h2 className="text-lg py-3 leading-relaxed font-medium text-gray-900">
|
|
|
|
Sorry, an error has occurred during your call
|
|
|
|
</h2>
|
|
|
|
{isTwilioError(error) ? (
|
|
|
|
<pre className="break-normal whitespace-normal mt-2 text-sm rounded py-3 px-5 bg-[#111] text-gray-300">
|
|
|
|
<div>
|
|
|
|
{error.description} ({error.code})
|
|
|
|
</div>
|
|
|
|
<div>{error.explanation}</div>
|
|
|
|
</pre>
|
|
|
|
) : null}
|
|
|
|
<p className="mt-2 text-sm text-gray-500">
|
|
|
|
We have been automatically notified and we're doing our best to make sure this does not happen
|
|
|
|
again!
|
|
|
|
</p>
|
|
|
|
|
|
|
|
<div className="mt-5 md:mt-4 md:flex md:flex-row-reverse">
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
className="transition-colors duration-150 mt-3 w-full inline-flex justify-center rounded-md border border-gray-300 shadow-sm px-4 py-2 bg-white text-base font-medium text-gray-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500 md:mt-0 md:w-auto md:text-sm"
|
|
|
|
onClick={() => router.replace(Routes.KeypadPage())}
|
|
|
|
>
|
|
|
|
Take me back to the app
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
function isTwilioError(error: any): error is typeof TwilioError {
|
|
|
|
return error.hasOwnProperty("explanation");
|
|
|
|
}
|
2021-08-08 06:37:53 +00:00
|
|
|
|
|
|
|
export default OutgoingCall;
|