2021-10-15 22:24:28 +00:00
|
|
|
import { Fragment, useRef, useState } from "react";
|
2021-07-31 17:22:48 +00:00
|
|
|
import type { BlitzPage } from "blitz";
|
2021-08-31 22:42:39 +00:00
|
|
|
import { Routes, useRouter } from "blitz";
|
2021-07-31 17:22:48 +00:00
|
|
|
import { atom, useAtom } from "jotai";
|
2021-08-29 23:53:22 +00:00
|
|
|
import { usePress } from "@react-aria/interactions";
|
|
|
|
import { Transition } from "@headlessui/react";
|
2021-09-07 20:59:38 +00:00
|
|
|
import { IoBackspace, IoCall } from "react-icons/io5";
|
2021-07-31 17:22:48 +00:00
|
|
|
|
2021-08-30 00:43:39 +00:00
|
|
|
import { Direction } from "db";
|
2021-10-15 22:24:28 +00:00
|
|
|
import Layout from "app/core/layouts/layout";
|
2021-08-08 11:22:34 +00:00
|
|
|
import Keypad from "../components/keypad";
|
2021-08-30 00:43:39 +00:00
|
|
|
import usePhoneCalls from "../hooks/use-phone-calls";
|
2021-08-31 22:42:39 +00:00
|
|
|
import useKeyPress from "../hooks/use-key-press";
|
2021-10-15 22:24:28 +00:00
|
|
|
import useCurrentUser from "app/core/hooks/use-current-user";
|
|
|
|
import KeypadErrorModal from "../components/keypad-error-modal";
|
2021-07-31 17:22:48 +00:00
|
|
|
|
2021-08-08 11:22:34 +00:00
|
|
|
const KeypadPage: BlitzPage = () => {
|
2021-10-15 23:25:13 +00:00
|
|
|
const { hasFilledTwilioCredentials, hasPhoneNumber, hasActiveSubscription } = useCurrentUser();
|
2021-08-13 11:55:05 +00:00
|
|
|
const router = useRouter();
|
2021-10-15 22:24:28 +00:00
|
|
|
const [isKeypadErrorModalOpen, setIsKeypadErrorModalOpen] = useState(false);
|
2021-08-30 00:43:39 +00:00
|
|
|
const [phoneCalls] = usePhoneCalls();
|
2021-08-08 11:46:04 +00:00
|
|
|
const [phoneNumber, setPhoneNumber] = useAtom(phoneNumberAtom);
|
2021-08-29 23:53:22 +00:00
|
|
|
const removeDigit = useAtom(pressBackspaceAtom)[1];
|
2021-08-01 04:29:41 +00:00
|
|
|
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
2021-08-29 23:53:22 +00:00
|
|
|
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
2021-07-31 17:22:48 +00:00
|
|
|
const pressDigit = useAtom(pressDigitAtom)[1];
|
2021-08-31 22:42:39 +00:00
|
|
|
useKeyPress((key) => {
|
|
|
|
if (key === "Backspace") {
|
|
|
|
return removeDigit();
|
|
|
|
}
|
|
|
|
|
|
|
|
pressDigit(key);
|
|
|
|
});
|
2021-08-01 04:29:41 +00:00
|
|
|
const longPressDigit = useAtom(longPressDigitAtom)[1];
|
2021-08-08 11:22:34 +00:00
|
|
|
const onZeroPressProps = {
|
2021-08-01 04:29:41 +00:00
|
|
|
onPressStart() {
|
|
|
|
pressDigit("0");
|
|
|
|
timeoutRef.current = setTimeout(() => {
|
|
|
|
longPressDigit("+");
|
|
|
|
}, 750);
|
|
|
|
},
|
|
|
|
onPressEnd() {
|
|
|
|
if (timeoutRef.current) {
|
|
|
|
clearTimeout(timeoutRef.current);
|
2021-08-29 23:53:22 +00:00
|
|
|
timeoutRef.current = null;
|
2021-08-01 04:29:41 +00:00
|
|
|
}
|
|
|
|
},
|
2021-08-08 11:22:34 +00:00
|
|
|
};
|
|
|
|
const onDigitPressProps = (digit: string) => ({
|
|
|
|
onPress() {
|
2021-08-13 11:55:05 +00:00
|
|
|
// navigator.vibrate(1); // removed in webkit
|
2021-08-08 11:22:34 +00:00
|
|
|
pressDigit(digit);
|
|
|
|
},
|
2021-08-01 04:29:41 +00:00
|
|
|
});
|
2021-08-29 23:53:22 +00:00
|
|
|
const { pressProps: onBackspacePress } = usePress({
|
|
|
|
onPressStart() {
|
|
|
|
timeoutRef.current = setTimeout(() => {
|
|
|
|
removeDigit();
|
|
|
|
intervalRef.current = setInterval(removeDigit, 75);
|
|
|
|
}, 325);
|
|
|
|
},
|
|
|
|
onPressEnd() {
|
|
|
|
if (timeoutRef.current) {
|
|
|
|
clearTimeout(timeoutRef.current);
|
|
|
|
timeoutRef.current = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (intervalRef.current) {
|
|
|
|
clearInterval(intervalRef.current);
|
|
|
|
intervalRef.current = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
removeDigit();
|
|
|
|
},
|
|
|
|
});
|
2021-07-31 17:22:48 +00:00
|
|
|
|
|
|
|
return (
|
2021-10-15 22:24:28 +00:00
|
|
|
<>
|
|
|
|
<div className="w-96 h-full flex flex-col justify-around py-5 mx-auto text-center text-black">
|
|
|
|
<div className="h-16 text-3xl text-gray-700">
|
|
|
|
<span>{phoneNumber}</span>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<Keypad onDigitPressProps={onDigitPressProps} onZeroPressProps={onZeroPressProps}>
|
|
|
|
<button
|
|
|
|
onClick={async () => {
|
2021-10-15 23:25:13 +00:00
|
|
|
if (!hasFilledTwilioCredentials || !hasPhoneNumber) {
|
2021-10-15 22:24:28 +00:00
|
|
|
setIsKeypadErrorModalOpen(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!hasActiveSubscription) {
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
|
|
|
if (phoneNumber === "") {
|
|
|
|
const lastCall = phoneCalls?.[0];
|
|
|
|
if (lastCall) {
|
|
|
|
const lastCallRecipient =
|
|
|
|
lastCall.direction === Direction.Inbound ? lastCall.from : lastCall.to;
|
|
|
|
setPhoneNumber(lastCallRecipient);
|
|
|
|
}
|
2021-07-31 17:22:48 +00:00
|
|
|
|
2021-10-15 22:24:28 +00:00
|
|
|
return;
|
2021-08-13 11:55:05 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 22:24:28 +00:00
|
|
|
await router.push(Routes.OutgoingCall({ recipient: encodeURI(phoneNumber) }));
|
|
|
|
setPhoneNumber("");
|
|
|
|
}}
|
|
|
|
className="cursor-pointer select-none col-start-2 h-12 w-12 flex justify-center items-center mx-auto bg-green-800 rounded-full"
|
|
|
|
>
|
|
|
|
<IoCall className="w-6 h-6 text-white" />
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<Transition
|
|
|
|
as={Fragment}
|
|
|
|
show={phoneNumber.length > 0}
|
|
|
|
enter="transition duration-300 ease-in-out"
|
|
|
|
enterFrom="transform scale-95 opacity-0"
|
|
|
|
enterTo="transform scale-100 opacity-100"
|
|
|
|
leave="transition duration-100 ease-out"
|
|
|
|
leaveFrom="transform scale-100 opacity-100"
|
|
|
|
leaveTo="transform scale-95 opacity-0"
|
|
|
|
>
|
|
|
|
<div {...onBackspacePress} className="cursor-pointer select-none m-auto">
|
|
|
|
<IoBackspace className="w-6 h-6" />
|
|
|
|
</div>
|
|
|
|
</Transition>
|
|
|
|
</Keypad>
|
|
|
|
</div>
|
|
|
|
<KeypadErrorModal closeModal={() => setIsKeypadErrorModalOpen(false)} isOpen={isKeypadErrorModalOpen} />
|
|
|
|
</>
|
2021-07-31 17:22:48 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const phoneNumberAtom = atom("");
|
2021-08-01 04:29:41 +00:00
|
|
|
const pressDigitAtom = atom(null, (get, set, digit: string) => {
|
2021-07-31 17:22:48 +00:00
|
|
|
if (get(phoneNumberAtom).length > 17) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-31 22:42:39 +00:00
|
|
|
if ("0123456789+#*".indexOf(digit) === -1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-31 17:22:48 +00:00
|
|
|
set(phoneNumberAtom, (prevState) => prevState + digit);
|
|
|
|
});
|
2021-08-01 04:29:41 +00:00
|
|
|
const longPressDigitAtom = atom(null, (get, set, replaceWith: string) => {
|
|
|
|
if (get(phoneNumberAtom).length > 17) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
set(phoneNumberAtom, (prevState) => prevState.slice(0, -1) + replaceWith);
|
|
|
|
});
|
2021-07-31 17:22:48 +00:00
|
|
|
const pressBackspaceAtom = atom(null, (get, set) => {
|
|
|
|
if (get(phoneNumberAtom).length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
set(phoneNumberAtom, (prevState) => prevState.slice(0, -1));
|
|
|
|
});
|
|
|
|
|
2021-08-08 11:22:34 +00:00
|
|
|
KeypadPage.getLayout = (page) => <Layout title="Keypad">{page}</Layout>;
|
2021-08-01 03:05:40 +00:00
|
|
|
|
2021-08-08 11:22:34 +00:00
|
|
|
KeypadPage.authenticate = { redirectTo: Routes.SignIn() };
|
2021-08-01 03:05:40 +00:00
|
|
|
|
2021-08-08 11:22:34 +00:00
|
|
|
export default KeypadPage;
|