import type { BlitzPage } from "blitz"; import { Routes } from "blitz"; import type { FunctionComponent } from "react"; import { useRef } from "react"; import { atom, useAtom } from "jotai"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faBackspace, faPhoneAlt as faPhone } from "@fortawesome/pro-solid-svg-icons"; import { usePress } from "@react-aria/interactions"; import Layout from "../../core/layouts/layout"; import useRequireOnboarding from "../../core/hooks/use-require-onboarding"; const Keypad: BlitzPage = () => { useRequireOnboarding(); const phoneNumber = useAtom(phoneNumberAtom)[0]; const pressBackspace = useAtom(pressBackspaceAtom)[1]; return (
{phoneNumber}
ABC DEF GHI JKL MNO PQRS TUV WXYZ
); }; const ZeroDigit: FunctionComponent = () => { const timeoutRef = useRef | null>(null); const pressDigit = useAtom(pressDigitAtom)[1]; const longPressDigit = useAtom(longPressDigitAtom)[1]; const { pressProps } = usePress({ onPressStart() { pressDigit("0"); timeoutRef.current = setTimeout(() => { longPressDigit("+"); }, 750); }, onPressEnd() { if (timeoutRef.current) { clearTimeout(timeoutRef.current); } }, }); return (
0 +
); }; const Row: FunctionComponent = ({ children }) => (
{children}
); const Digit: FunctionComponent<{ digit: string }> = ({ children, digit }) => { const pressDigit = useAtom(pressDigitAtom)[1]; const onClick = () => pressDigit(digit); return (
{digit} {children}
); }; const DigitLetters: FunctionComponent = ({ children }) => (
{children}
); 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)); }); Keypad.getLayout = (page) => {page}; Keypad.authenticate = { redirectTo: Routes.SignIn() }; export default Keypad;