Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

4.6 KB
Raw Blame
/*
 * Copyright 2025 Daytona Platforms Inc.
 * Modified by BoxLite AI, 2025-2026
 * SPDX-License-Identifier: AGPL-3.0
 */

import { ResourceEmptyState, ResourceLoadingState, StatusIndicator } from '@/components/ResourceLayout'
import { CreditCardIcon } from '@/components/ui/icon'
import type { WalletTransaction } from '@/billing-api'
import type { WalletTransactionsTableProps } from './types'
import { walletTransactionLabel } from './columns'

const ROW = 'grid grid-cols-[100px_110px_1fr_90px_80px] items-center gap-x-4'

// Keyed loosely on purpose: `status` is a hand-copied union, so a value
// Commerce adds later arrives here before this map knows the key.
const STATUS: Record<string, { label: string; color: string } | undefined> = {
  settled: { label: 'ok', color: 'hsl(var(--success))' },
  pending: { label: 'pending', color: 'hsl(var(--warning))' },
  failed: { label: 'failed', color: 'hsl(var(--destructive))' },
}

/**
 * Returns the display label and color for a transaction status.
 * Falls back to the raw name rather than throwing on `undefined.color`.
 *
 * @param status - The wallet transaction status
 * @returns Object with label text and HSL color string
 */
function statusMark(status: WalletTransaction['status']): { label: string; color: string } {
  return STATUS[status] ?? { label: status, color: 'hsl(var(--muted-foreground))' }
}

/**
 * Extracts the date portion from a transaction's created timestamp.
 *
 * @param transaction - The wallet transaction
 * @returns Date string in YYYY-MM-DD format
 */
function transactionDate(transaction: WalletTransaction): string {
  return transaction.createdAt.slice(0, 10)
}

/**
 * Formats the transaction amount with a sign prefix indicating direction.
 *
 * @param transaction - The wallet transaction
 * @returns Formatted amount with '+' prefix for inbound or '-' for outbound
 */
function transactionAmount(transaction: WalletTransaction): string {
  const prefix = transaction.direction === 'inbound' ? '+' : '-'
  return `${prefix}${(transaction.amountCents / 100).toFixed(2)}`
}

/**
 * Renders a table of wallet credit transactions with columns for date, type, amount, and status.
 * Shows a loading state when data is being fetched.
 *
 * @param props - Component props
 * @param props.data - Array of wallet transactions to display
 * @param props.loading - Whether the data is currently loading
 * @returns The wallet transactions table component
 */
export function WalletTransactionsTable({ data, loading }: WalletTransactionsTableProps) {
  if (loading) {
    return <ResourceLoadingState title="Loading credit activity…" />
  }

  if (!data.length)
    return (
      <ResourceEmptyState
        title="No credit activity yet"
        icon={<CreditCardIcon className="size-6" />}
        description="Credit purchases, grants and usage appear here after they are recorded."
      />
    )

  return (
    // Bounded like its sibling in Billing history, and for the same reason: a
    // page holds 25 rows, and an unbounded one pushes whatever follows it off
    // the screen. Header inside the scroller and stuck to its top, so it cannot
    // drift out of alignment by the scrollbar's width.
    <div className="scrollbar-elevated max-h-[420px] overflow-y-auto">
      <div className={`${ROW} sticky top-0 z-10 resource-list-head bg-background`}>
        <span data-transaction-header>Date</span>
        <span data-transaction-header>Type</span>
        <span data-transaction-header />
        <span data-transaction-header className="text-right">
          Amount
        </span>
        <span data-transaction-header className="text-right">
          Status
        </span>
      </div>
      {data.map((transaction) => {
        const status = statusMark(transaction.status)
        return (
          <div key={transaction.id} className={`${ROW} resource-list-row font-sans`}>
            <span data-transaction-date className="font-mono text-foreground">
              {transactionDate(transaction)}
            </span>
            <span data-transaction-type className="text-muted-foreground">
              {walletTransactionLabel(transaction)}
            </span>
            <span />
            <span
              data-transaction-amount
              className={`text-right font-mono tabular-nums ${transaction.direction === 'inbound' ? 'text-success' : 'text-foreground'}`}
            >
              {transactionAmount(transaction)}
            </span>
            <span className="text-right">
              <StatusIndicator data-transaction-status {...status} normalizeLabel={false} />
            </span>
          </div>
        )
      })}
    </div>
  )
}