import { useState, useEffect, useMemo } from "react" import { useLanguage } from "@/contexts/LanguageContext" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Badge } from "@/components/ui/badge" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { Plus, Search, Edit, Trash2, Wallet, Filter, X } from "lucide-react" import { Label } from "@/components/ui/label" import axios from "axios" import { toast } from "sonner" import { WalletDialog } from "@/components/dialogs/WalletDialog" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/components/ui/alert-dialog" interface Wallet { id: string name: string kind: "money" | "asset" currency?: string | null unit?: string | null deletedAt?: string | null createdAt: string updatedAt: string } const API = "/api" export function Wallets() { const { t } = useLanguage() const [wallets, setWallets] = useState([]) const [loading, setLoading] = useState(true) const [searchTerm, setSearchTerm] = useState("") const [kindFilter, setKindFilter] = useState("all") const [currencyFilter, setCurrencyFilter] = useState("all") const [showFilters, setShowFilters] = useState(false) const [walletDialogOpen, setWalletDialogOpen] = useState(false) const [editingWallet, setEditingWallet] = useState(null) useEffect(() => { loadWallets() }, []) const loadWallets = async () => { try { setLoading(true) const response = await axios.get(`${API}/wallets`) setWallets(response.data.filter((w: Wallet) => !w.deletedAt)) } catch (error) { console.error('Failed to load wallets:', error) } finally { setLoading(false) } } const deleteWallet = async (id: string) => { try { await axios.delete(`${API}/wallets/${id}`) toast.success('Wallet berhasil dihapus') await loadWallets() } catch (error) { console.error('Failed to delete wallet:', error) toast.error('Gagal menghapus wallet') } } const handleEditWallet = (wallet: Wallet) => { setEditingWallet(wallet) setWalletDialogOpen(true) } const handleDialogClose = () => { setWalletDialogOpen(false) setEditingWallet(null) } const clearFilters = () => { setSearchTerm("") setKindFilter("all") setCurrencyFilter("all") } // Filter wallets const filteredWallets = useMemo(() => { return wallets.filter(wallet => { const matchesSearch = wallet.name.toLowerCase().includes(searchTerm.toLowerCase()) const matchesKind = kindFilter === "all" || wallet.kind === kindFilter const matchesCurrency = currencyFilter === "all" || wallet.currency === currencyFilter || wallet.unit === currencyFilter return matchesSearch && matchesKind && matchesCurrency }) }, [wallets, searchTerm, kindFilter, currencyFilter]) // Get unique currencies for filter const availableCurrencies = useMemo(() => { const currencies = new Set(wallets.map(w => w.currency).filter(Boolean) as string[]) return Array.from(currencies).sort() }, [wallets]) // Calculate stats const stats = useMemo(() => { const totalWallets = filteredWallets.length const moneyWallets = filteredWallets.filter(w => w.kind === 'money').length const assetWallets = filteredWallets.filter(w => w.kind === 'asset').length const currencyCount = new Set(filteredWallets.map(w => w.currency).filter(Boolean)).size return { totalWallets, moneyWallets, assetWallets, currencyCount } }, [filteredWallets]) if (loading) { return (
{[...Array(4)].map((_, i) => (
))}
) } return (
{/* Header */}

Wallets

Manage your wallets and accounts

{/* Stats Cards */}
{t.wallets.title}
{stats.totalWallets}
{t.wallets.moneyWallets}
{stats.moneyWallets}
{t.wallets.assetWallets}
{stats.assetWallets}
{t.wallets.currency}
{stats.currencyCount}
{/* Filters */} {showFilters && (
{t.common.filter}
{/* Row 1: Search, Type, Currency */}
{/* Search */}
setSearchTerm(e.target.value)} className="pl-9 h-9" />
{/* Type Filter */}
{/* Currency Filter */}
)} {/* Active Filters Badge */} {(searchTerm || kindFilter !== "all" || currencyFilter !== "all") && (
{searchTerm && (
{searchTerm}
)} {kindFilter !== "all" && (
Type: {kindFilter === "money" ? "Money" : "Asset"}
)} {currencyFilter !== "all" && (
Currency: {currencyFilter}
)}
)} {/* Wallets Table */} {t.wallets.title} ({filteredWallets.length}) {filteredWallets.length !== wallets.length ? `Filtered from ${wallets.length} total wallets` : "All your wallets" } {t.wallets.name} {t.wallets.currency}/{t.wallets.unit} {t.wallets.type} {t.common.date} {t.common.actions} {filteredWallets.length === 0 ? ( {filteredWallets.length !== wallets.length ? t.wallets.noWallets : t.wallets.createFirst } ) : ( filteredWallets.map((wallet) => ( {wallet.name} {wallet.kind === 'money' ? ( {wallet.currency} ) : ( {wallet.unit} )} {wallet.kind} {new Date(wallet.createdAt).toLocaleDateString()}
{t.common.delete} {t.wallets.title} {t.wallets.deleteConfirm} {t.common.cancel} deleteWallet(wallet.id)}> {t.common.delete}
)) )}
{/* Dialog */}
) }