import { useEffect, useState } from "react"; import { Modal, Pressable, StyleSheet, Text, TextInput, View, } from "react-native"; import { Button } from "@/components/ui/Button"; import { fonts, spacing } from "@/constants/theme"; import { useAppTheme } from "@/contexts/ThemeContext"; import { isValidPin } from "@/lib/app-lock"; type PinPromptProps = { visible: boolean; title: string; message: string; confirmLabel?: string; requireConfirmation?: boolean; onCancel: () => void; onSubmit: (pin: string) => void; }; export function PinPrompt({ visible, title, message, confirmLabel = "Continue", requireConfirmation = false, onCancel, onSubmit, }: PinPromptProps) { const { colors } = useAppTheme(); const [pin, setPin] = useState(""); const [confirmPin, setConfirmPin] = useState(""); const [error, setError] = useState(""); useEffect(() => { if (!visible) { setPin(""); setConfirmPin(""); setError(""); } }, [visible]); function handleSubmit() { if (!isValidPin(pin)) { setError("PIN must be 4–6 digits"); return; } if (requireConfirmation && pin !== confirmPin) { setError("PINs do not match"); return; } onSubmit(pin); } return ( event.stopPropagation()} > {title} {message} { setError(""); setPin(value.replace(/\D/g, "").slice(0, 6)); }} keyboardType="number-pad" secureTextEntry maxLength={6} placeholder="PIN" placeholderTextColor={colors.mutedForeground} style={[ styles.input, { color: colors.foreground, borderColor: colors.border, backgroundColor: colors.background }, ]} /> {requireConfirmation ? ( { setError(""); setConfirmPin(value.replace(/\D/g, "").slice(0, 6)); }} keyboardType="number-pad" secureTextEntry maxLength={6} placeholder="Confirm PIN" placeholderTextColor={colors.mutedForeground} style={[ styles.input, { color: colors.foreground, borderColor: colors.border, backgroundColor: colors.background }, ]} /> ) : null} {error ? {error} : null}