"use client"; import { RiBarChartBoxLine, RiExternalLinkLine, RiEyeLine, RiEyeOffLine, } from "@remixicon/react"; import Image from "next/image"; import Link from "next/link"; import { useTransition } from "react"; import { toast } from "sonner"; import type { DashboardAccount } from "@/features/dashboard/accounts-queries"; import { updateMyAccountsWidgetPreference } from "@/features/dashboard/widget-registry/widget-actions"; import MoneyValues from "@/shared/components/money-values"; import { Badge } from "@/shared/components/ui/badge"; import { Button } from "@/shared/components/ui/button"; import { CardFooter } from "@/shared/components/ui/card"; import { Tooltip, TooltipContent, TooltipTrigger, } from "@/shared/components/ui/tooltip"; import { WidgetEmptyState } from "@/shared/components/widget-empty-state"; import { isAccountInactive } from "@/shared/lib/accounts/constants"; import { resolveLogoSrc } from "@/shared/lib/logo"; import { formatPeriodForUrl } from "@/shared/utils/period"; type MyAccountsWidgetProps = { accounts: DashboardAccount[]; showExcludedAccounts: boolean; onShowExcludedAccountsChange?: (value: boolean) => void; totalBalance: number; period: string; }; export function MyAccountsWidget({ accounts, showExcludedAccounts, onShowExcludedAccountsChange, totalBalance, period, }: MyAccountsWidgetProps) { const [isPending, startTransition] = useTransition(); const activeAccounts = accounts.filter( (account) => !isAccountInactive(account.status), ); const excludedAccountsCount = activeAccounts.filter( (account) => account.excludeFromBalance, ).length; const visibleAccounts = showExcludedAccounts ? activeAccounts : activeAccounts.filter((account) => !account.excludeFromBalance); const displayedAccounts = visibleAccounts.slice(0, 5); const remainingCount = visibleAccounts.length - displayedAccounts.length; const hiddenExcludedAccountsCount = showExcludedAccounts ? 0 : excludedAccountsCount; const toggleButtonLabel = showExcludedAccounts ? "Ocultar contas não consideradas" : "Mostrar contas não consideradas"; const handleToggleExcludedAccounts = () => { const nextShowExcludedAccounts = !showExcludedAccounts; onShowExcludedAccountsChange?.(nextShowExcludedAccounts); startTransition(async () => { const result = await updateMyAccountsWidgetPreference({ showExcludedAccounts: nextShowExcludedAccounts, }); if (!result.success) { onShowExcludedAccountsChange?.(!nextShowExcludedAccounts); toast.error(result.error ?? "Erro ao salvar preferência"); } }); }; return ( <>

Saldo Total

{excludedAccountsCount > 0 ? (

{toggleButtonLabel}

) : null}
{hiddenExcludedAccountsCount > 0 ? (

{hiddenExcludedAccountsCount}{" "} {hiddenExcludedAccountsCount === 1 ? "conta não considerada oculta" : "contas não consideradas ocultas"}

) : null}
{activeAccounts.length === 0 ? (
} title="Você ainda não adicionou nenhuma conta" description="Cadastre suas contas bancárias para acompanhar os saldos e movimentações." />
) : displayedAccounts.length === 0 ? (
} title="As contas não consideradas estão ocultas" description="Use o botão no topo do widget para mostrá-las novamente." />
) : ( )}
{remainingCount > 0 ? ( +{remainingCount} contas não exibidas ) : null} ); }