import { Fragment, useRef } from "react"; import type { BlitzPage } from "blitz"; import { Link, Routes, useRouter } from "blitz"; import { atom, useAtom } from "jotai"; import { usePress } from "@react-aria/interactions"; import { Transition } from "@headlessui/react"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faBackspace, faPhoneAlt as faPhone } from "@fortawesome/pro-solid-svg-icons"; import { Direction } from "db"; import Layout from "../../core/layouts/layout"; import Keypad from "../components/keypad"; import useRequireOnboarding from "../../core/hooks/use-require-onboarding"; import usePhoneCalls from "../hooks/use-phone-calls"; const KeypadPage: BlitzPage = () => { useRequireOnboarding(); const router = useRouter(); const [phoneCalls] = usePhoneCalls(); const [phoneNumber, setPhoneNumber] = useAtom(phoneNumberAtom); const removeDigit = useAtom(pressBackspaceAtom)[1]; const timeoutRef = useRef | null>(null); const intervalRef = useRef | null>(null); const pressDigit = useAtom(pressDigitAtom)[1]; const longPressDigit = useAtom(longPressDigitAtom)[1]; const onZeroPressProps = { onPressStart() { console.log("0"); pressDigit("0"); timeoutRef.current = setTimeout(() => { longPressDigit("+"); }, 750); }, onPressEnd() { if (timeoutRef.current) { clearTimeout(timeoutRef.current); timeoutRef.current = null; } }, }; const onDigitPressProps = (digit: string) => ({ onPress() { // navigator.vibrate(1); // removed in webkit pressDigit(digit); }, }); 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(); }, }); return (
{phoneNumber}
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" >
); }; const phoneNumberAtom = atom(""); const pressDigitAtom = atom(null, (get, set, digit: string) => { if (get(phoneNumberAtom).length > 17) { return; } set(phoneNumberAtom, (prevState) => prevState + digit); }); const longPressDigitAtom = atom(null, (get, set, replaceWith: string) => { if (get(phoneNumberAtom).length > 17) { return; } set(phoneNumberAtom, (prevState) => prevState.slice(0, -1) + replaceWith); }); const pressBackspaceAtom = atom(null, (get, set) => { if (get(phoneNumberAtom).length === 0) { return; } set(phoneNumberAtom, (prevState) => prevState.slice(0, -1)); }); KeypadPage.getLayout = (page) => {page}; KeypadPage.authenticate = { redirectTo: Routes.SignIn() }; export default KeypadPage;