import React, { useEffect, useRef, useState } from "react"; import { View, Text, ScrollView, TouchableOpacity, FlatList, } from "react-native"; import { Input } from "~/components/ui/input"; import { Button } from "~/components/ui/button"; import { Filter, LucideX, LucideCreditCard } from "lucide-react-native"; import AccountCard from "~/components/ui/accCard"; import BackButton from "~/components/ui/backButton"; import { useLocalSearchParams, router } from "expo-router"; import { useAuthWithProfile } from "~/lib/hooks/useAuthWithProfile"; import { useUserWallet } from "~/lib/hooks/useUserWallet"; import { CreditCard, WalletService } from "~/lib/services/walletService"; import { ROUTES } from "~/lib/routes"; import { showAlert } from "~/lib/utils/alertUtils"; import ScreenWrapper from "~/components/ui/ScreenWrapper"; import ModalToast from "~/components/ui/toast"; import { useTranslation } from "react-i18next"; export default function SelectAccount() { const { t } = useTranslation(); const { amount } = useLocalSearchParams<{ amount: string }>(); const { user } = useAuthWithProfile(); const { wallet, loading, error, refreshWallet } = useUserWallet(user); const [selectedCardId, setSelectedCardId] = React.useState( null ); const [isProcessing, setIsProcessing] = useState(false); const [toastVisible, setToastVisible] = useState(false); const [toastTitle, setToastTitle] = useState(""); const [toastDescription, setToastDescription] = useState( undefined ); const [toastVariant, setToastVariant] = useState< "success" | "error" | "warning" | "info" >("info"); const toastTimeoutRef = useRef | null>(null); const showToast = ( title: string, description?: string, variant: "success" | "error" | "warning" | "info" = "info" ) => { if (toastTimeoutRef.current) { clearTimeout(toastTimeoutRef.current); } setToastTitle(title); setToastDescription(description); setToastVariant(variant); setToastVisible(true); toastTimeoutRef.current = setTimeout(() => { setToastVisible(false); toastTimeoutRef.current = null; }, 2500); }; useEffect(() => { return () => { if (toastTimeoutRef.current) { clearTimeout(toastTimeoutRef.current); } }; }, []); const handleCardSelect = (card: CreditCard) => { setSelectedCardId(card.id); console.log("Selected card:", card.id); }; const renderCards = () => { if (loading) { return ( {t("selectacc.loadingCards")} ); } if (error) { return ( {t("selectacc.errorTitle")} {t("selectacc.errorWithMessage", { error })} ); } if (!wallet?.cards || wallet.cards.length === 0) { return ( {t("selectacc.emptyTitle")} {t("selectacc.emptySubtitle")} ); } return ( item.id} scrollEnabled={false} ItemSeparatorComponent={() => } renderItem={({ item: card }) => ( handleCardSelect(card)} className="w-full" > )} /> ); }; return ( {t("selectacc.title")} {amount && ( {t("selectacc.addingAmount", { amount: (parseInt(amount) / 100).toFixed(2), })} )} {t("selectacc.accountsTitle")} {selectedCardId ? t("selectacc.accountsDescriptionSelected") : t("selectacc.accountsDescriptionUnselected")} {renderCards()} {selectedCardId && ( )} ); }