Loading...
Loading...
> Displays credit transaction history with type icons, amounts, descriptions, and timestamps. Supports all transaction types: usage, purchases, refills, bonuses, and refunds.
1import { TransactionTable } from "@/components/credits"
1<TransactionTable transactions={transactions} />
1// Usage transactions show red arrows and negative amounts
1// Refills show refresh icon and green amounts
1// Bonuses and refunds show distinct icons
1<TransactionTable transactions={[]} />
| Prop | Type | Default | Description |
|---|---|---|---|
| transactions* | Transaction[] | - | Array of transaction objects with id, amount, type, description, endpoint, and createdAt. |
| className | string | - | Additional CSS classes to apply. |
Fetch and display recent transaction history:
1'use client';23import { useEffect, useState } from 'react';4import { TransactionTable } from '@/components/credits';56export function TransactionHistory() {7 const [transactions, setTransactions] = useState([]);89 useEffect(() => {10 // Fetch recent transactions11 fetch('/api/credits/history?limit=20')12 .then(r => r.json())13 .then(data => setTransactions(data.transactions));14 }, []);1516 return (17 <div className="space-y-4">18 <h3>Recent Transactions</h3>19 <TransactionTable transactions={transactions} />20 </div>21 );22}
TypeScript interface for transaction objects:
1interface Transaction {2 id: string;3 amount: number; // Positive = added, Negative = deducted4 type: CreditTransactionType;5 description: string | null;6 endpoint: string | null; // API endpoint that triggered this7 createdAt: string; // ISO 8601 date string8}910enum CreditTransactionType {11 USAGE, // Red down arrow (-)12 PURCHASE, // Green up arrow (+)13 SUBSCRIPTION_REFILL, // Green refresh icon (+)14 BONUS, // Blue gift icon (+)15 REFUND, // Yellow undo icon (+)16}
Expected format from GET /api/credits/history:
1// GET /api/credits/history?limit=20 response2{3 "transactions": [4 {5 "id": "1",6 "amount": -10,7 "type": "USAGE",8 "description": "Form generation",9 "endpoint": "/api/ai/generate-form",10 "createdAt": "2024-12-08T14:55:00.000Z"11 },12 {13 "amount": 500,14 "type": "PURCHASE",15 "description": "Purchased 500 credit pack",16 "endpoint": null,17 "createdAt": "2024-12-08T13:00:00.000Z"18 },19 {20 "amount": 1000,21 "type": "SUBSCRIPTION_REFILL",22 "description": "Monthly refill for starter plan",23 "endpoint": null,24 "createdAt": "2024-12-07T15:00:00.000Z"25 }26 ],27 "stats": [...], // If ?stats=true was passed28 "totalUsage": 24529}
Show only specific transaction types:
1'use client';23import { useState, useEffect } from 'react';4import { TransactionTable } from '@/components/credits';56export function UsageOnlyHistory() {7 const [transactions, setTransactions] = useState([]);89 useEffect(() => {10 // Only fetch USAGE transactions11 fetch('/api/credits/history?type=USAGE&limit=50')12 .then(r => r.json())13 .then(data => setTransactions(data.transactions));14 }, []);1516 return (17 <div>18 <h3>Usage History</h3>19 <TransactionTable transactions={transactions} />20 </div>21 );22}
Component shows "No transactions yet" when array is empty:
1// Automatically handles empty state2<TransactionTable transactions={[]} />34// Renders: "No transactions yet" message