import React, { useState, useRef, useCallback } from "react"; import { Box, Text, Flex } from "rebass"; import { Input } from "@rebass/forms"; import Dialog, { showDialog } from "./dialog"; import * as Icon from "react-feather"; function PasswordDialog(props) { const [isWrong, setIsWrong] = useState(false); const passwordRef = useRef(); const submit = useCallback(async () => { const password = passwordRef.current.value; if (await props.validate(password)) { props.onDone(); } else { setIsWrong(true); passwordRef.current.focus(); } }, [setIsWrong, props]); return ( { if (e.key === "Enter") { await submit(); } else { setIsWrong(false); } }} /> {isWrong && ( Wrong password )} } positiveButton={{ text: props.positiveButtonText, onClick: submit }} negativeButton={{ text: "Cancel", onClick: props.onCancel }} /> ); } function getDialogData(type) { switch (type) { case "create_vault": return { title: "Set Up Your Vault", icon: Icon.Shield, positiveButtonText: "Done" }; case "unlock_vault": return { title: "Unlock Vault", icon: Icon.Unlock, positiveButtonText: "Unlock" }; case "unlock_note": return { title: "Unlock Note", icon: Icon.Unlock, positiveButtonText: "Unlock" }; default: return; } } export const showPasswordDialog = (type, validate) => { const { title, icon, positiveButtonText } = getDialogData(type); return showDialog(perform => ( perform(false)} validate={validate} onDone={() => perform(true)} /> )); };