import { CircleMinusIcon, CirclePlusIcon, CopyIcon, Trash2Icon, } from "lucide-react"; import React from "react"; import { FormattedBitcoinAmount } from "src/components/FormattedBitcoinAmount"; import FormattedFiatAmount from "src/components/FormattedFiatAmount"; import { IsolatedAppDrawDownDialog } from "src/components/IsolatedAppDrawDownDialog"; import { IsolatedAppTopupDialog } from "src/components/IsolatedAppTopupDialog"; import { Button } from "src/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from "src/components/ui/card"; import { InputWithAdornment } from "src/components/ui/custom/input-with-adornment"; import { LoadingButton } from "src/components/ui/custom/loading-button"; import { Progress } from "src/components/ui/progress"; import { UpgradeDialog } from "src/components/UpgradeDialog"; import { SUBWALLET_APPSTORE_APP_ID } from "src/constants"; import { useAlbyMe } from "src/hooks/useAlbyMe"; import { useCreateLightningAddress } from "src/hooks/useCreateLightningAddress"; import { useDeleteLightningAddress } from "src/hooks/useDeleteLightningAddress"; import { useTransactions } from "src/hooks/useTransactions"; import { copyToClipboard } from "src/lib/clipboard"; import { cn, getBudgetRenewalLabel } from "src/lib/utils"; import { App, Transaction } from "src/types"; export function AppUsage({ app }: { app: App }) { const [page, setPage] = React.useState(1); const { data: transactionsResponse } = useTransactions( app.id, false, 100, page ); const [allTransactions, setAllTransactions] = React.useState( [] ); React.useEffect(() => { if (transactionsResponse?.transactions.length) { setAllTransactions((current) => [...current, ...transactionsResponse.transactions].filter( (v, i, a) => a.findIndex((t) => t.paymentHash === v.paymentHash) === i // remove duplicates ) ); setPage((current) => current + 1); } }, [transactionsResponse?.transactions]); const totalSpentSat = allTransactions .filter((tx) => tx.type === "outgoing" && tx.state === "settled") .map((tx) => Math.floor((tx.amountMsat + tx.feesPaidMsat) / 1000)) .reduce((a, b) => a + b, 0); const totalReceivedSat = allTransactions .filter((tx) => tx.type === "incoming") .map((tx) => tx.amountSat) .reduce((a, b) => a + b, 0); const { data: albyMe } = useAlbyMe(); const [intendedLightningAddress, setIntendedLightningAddress] = React.useState(""); const { createLightningAddress, creatingLightningAddress } = useCreateLightningAddress(app.id); const { deleteLightningAddress: deleteSubwalletLightningAddress, deletingLightningAddress, } = useDeleteLightningAddress(app.id); return ( <> {app.isolated && (
Isolated Balance

{app.balanceMsat > 0 && ( )}
{app.metadata?.app_store_app_id === SUBWALLET_APPSTORE_APP_ID && ( Lightning Address {!app.metadata.lud16 && (

Create a lightning address for this particular app connection

)}
{!app.metadata.lud16 && (
setIntendedLightningAddress(e.target.value) } required autoComplete="off" endAdornment={ @getalby.com } className="flex-1" /> {!albyMe?.subscription.plan_code ? ( ) : ( createLightningAddress(intendedLightningAddress) } > Create )}
)} {app.metadata.lud16 && (

{app.metadata.lud16}

Remove
)}
)}
)}
Total Spent

Total Received

{app.maxAmountSat > 0 && ( Budget

Left in budget

Budget renewal

{app.budgetRenewal !== "never" && ( <> / {getBudgetRenewalLabel(app.budgetRenewal)} )}

)} ); }