import { BottomSheetBackdrop, type BottomSheetBackdropProps, BottomSheetModal, BottomSheetTextInput, BottomSheetView, } from "@gorhom/bottom-sheet"; import type React from "react"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { ActivityIndicator, Platform, View } from "react-native"; import { useSafeAreaInsets } from "react-native-safe-area-context"; import { useHaptic } from "@/hooks/useHaptic"; import { Button } from "./Button"; import { Text } from "./common/Text"; interface PasswordEntryModalProps { visible: boolean; onClose: () => void; onSubmit: (password: string) => Promise; username: string; } export const PasswordEntryModal: React.FC = ({ visible, onClose, onSubmit, username, }) => { const { t } = useTranslation(); const insets = useSafeAreaInsets(); const bottomSheetModalRef = useRef(null); const [password, setPassword] = useState(""); const [error, setError] = useState(null); const [isLoading, setIsLoading] = useState(false); const errorHaptic = useHaptic("error"); const isAndroid = Platform.OS === "android"; const snapPoints = useMemo( () => (isAndroid ? ["100%"] : ["50%"]), [isAndroid], ); useEffect(() => { if (visible) { bottomSheetModalRef.current?.present(); setPassword(""); setError(null); } else { bottomSheetModalRef.current?.dismiss(); } }, [visible]); const handleSheetChanges = useCallback( (index: number) => { if (index === -1) { setPassword(""); setError(null); onClose(); } }, [onClose], ); const renderBackdrop = useCallback( (props: BottomSheetBackdropProps) => ( ), [], ); const handleSubmit = async () => { if (!password) { setError(t("password.enter_password")); return; } setIsLoading(true); setError(null); try { await onSubmit(password); setPassword(""); } catch { errorHaptic(); setError(t("password.invalid_password")); } finally { setIsLoading(false); } }; return ( {/* Header */} {t("password.enter_password")} {t("password.enter_password_for", { username })} {/* Password Input */} {t("login.password")} { setPassword(text); setError(null); }} placeholder={t("login.password")} placeholderTextColor='#6B7280' secureTextEntry autoFocus autoCapitalize='none' autoCorrect={false} style={{ backgroundColor: "#1F2937", borderRadius: 8, padding: 12, color: "white", fontSize: 16, }} onSubmitEditing={handleSubmit} returnKeyType='done' /> {error && {error}} {/* Buttons */} ); };