import React, { useState, useEffect, useCallback, useRef } from "react"; import { View, ActivityIndicator, Pressable } from "react-native"; import { useSirouRouter } from "@sirou/react-native"; import { AppRoutes } from "@/lib/routes"; import { Text } from "@/components/ui/text"; import { Stack } from "expo-router"; import { ScreenWrapper } from "@/components/ScreenWrapper"; import { Keypad } from "@/components/Keypad"; import { ShieldCheck } from "@/lib/icons"; import { api } from "@/lib/api"; import { usePinStore } from "@/lib/pin-store"; import { toast } from "@/lib/toast-store"; import { useColorScheme } from "nativewind"; type Step = "create" | "confirm"; export default function SetPinScreen() { const nav = useSirouRouter(); const { colorScheme } = useColorScheme(); const isDark = colorScheme === "dark"; const setHasPin = usePinStore((s) => s.setHasPin); const unlock = usePinStore((s) => s.unlock); const mounted = useRef(true); useEffect(() => { return () => { mounted.current = false; }; }, []); const [step, setStep] = useState("create"); const [pin, setPin] = useState(""); const [confirmPin, setConfirmPin] = useState(""); const [loading, setLoading] = useState(false); const currentPin = step === "create" ? pin : confirmPin; const setCurrentPin = step === "create" ? setPin : setConfirmPin; useEffect(() => { if (currentPin.length === 6 && !loading) { if (step === "create") { setTimeout(() => { setStep("confirm"); setConfirmPin(""); }, 200); } else { handleSave(); } } }, [currentPin]); const handleSave = async () => { if (confirmPin !== pin) { toast.error("Mismatch", "PINs do not match"); setStep("create"); setPin(""); setConfirmPin(""); return; } setLoading(true); try { await api.auth.setPin({ body: { pin } }); setHasPin(true); unlock(); toast.success("PIN Set", "Your security PIN has been saved."); nav.go("(tabs)"); } catch (err: any) { toast.error("Failed", err.message || "Could not set PIN"); } finally { if (mounted.current) setLoading(false); } }; const handlePress = useCallback( (key: string) => { setCurrentPin((prev) => (prev + key).slice(0, 6)); }, [step], ); const handleDelete = useCallback(() => { setCurrentPin((prev) => prev.slice(0, -1)); }, [step]); const arr = currentPin.split(""); const iconColor = isDark ? "#f1f5f9" : "#251615"; return ( {step === "create" ? "Step 1 of 2" : "Step 2 of 2"} {step === "create" ? "Create a PIN" : "Confirm your PIN"} {step === "create" ? "Set a 6-digit PIN to secure your account" : "Enter the same PIN again to confirm"} {[0, 1, 2, 3, 4, 5].map((i) => ( ))} {loading && } ); }