"use client"; import { Wallet } from "lucide-react"; import Image from "next/image"; import { useCallback, useEffect, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { getWalletBalance } from "@/api/wallet"; import { Skeleton } from "@/components/ui/skeleton"; import { TransferInDialog, TransferOutDialog, } from "@/features/wallet/wallet-transfer-dialogs"; import { useActivePlayerCurrency } from "@/hooks/use-active-player-currency"; import { formatMinorAsCurrency } from "@/lib/money"; import { PLAYER_CURRENCY_CHANGE_EVENT } from "@/lib/player-currency-preference"; import { cn } from "@/lib/utils"; import { useNetworkConnectionStore } from "@/stores/network-connection-store"; import type { WalletBalanceData } from "@/types/api/wallet-balance"; export function HallWalletStrip() { const mode = useNetworkConnectionStore((s) => s.mode); const { t } = useTranslation("player"); const { activeCurrency } = useActivePlayerCurrency(); const [balance, setBalance] = useState(null); const [loading, setLoading] = useState(true); const degradedWalletPollRef = useRef(null); const currency = activeCurrency; const refresh = useCallback(async () => { const b = await getWalletBalance({ currency: activeCurrency }); setBalance(b); }, [activeCurrency]); useEffect(() => { let cancelled = false; void (async () => { setLoading(true); try { await refresh(); } finally { if (!cancelled) setLoading(false); } })(); return () => { cancelled = true; }; }, [refresh]); useEffect(() => { const onRefresh = () => void refresh(); window.addEventListener("lottery-wallet-refresh", onRefresh); window.addEventListener(PLAYER_CURRENCY_CHANGE_EVENT, onRefresh); return () => { window.removeEventListener("lottery-wallet-refresh", onRefresh); window.removeEventListener(PLAYER_CURRENCY_CHANGE_EVENT, onRefresh); }; }, [refresh]); useEffect(() => { if (mode !== "polling" && mode !== "offline") { if (degradedWalletPollRef.current !== null) { window.clearInterval(degradedWalletPollRef.current); degradedWalletPollRef.current = null; } return; } if (degradedWalletPollRef.current !== null) { return; } degradedWalletPollRef.current = window.setInterval(() => { void refresh(); }, 60_000); return () => { if (degradedWalletPollRef.current !== null) { window.clearInterval(degradedWalletPollRef.current); degradedWalletPollRef.current = null; } }; }, [mode, refresh]); const availableMinor = Number(balance?.available_balance ?? balance?.balance ?? 0); const isCreditPlayer = balance?.credit_line_mode === true || balance?.funding_mode === "credit"; const mainMinor = balance?.main_balance === null || balance?.main_balance === undefined ? null : Number(balance.main_balance); return (

{balance?.credit_line_mode ? t("wallet.creditAvailable", { defaultValue: "可用信用" }) : t("wallet.balance")}

{loading ? ( ) : (

{formatMinorAsCurrency(availableMinor, currency)}

)}
{isCreditPlayer ? null : (
)}
); }