|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useState, useEffect } from 'react'; |
| 4 | +import { useHathor } from '@/contexts/HathorContext'; |
| 5 | +import { formatAddress } from '@/lib/utils'; |
| 6 | + |
| 7 | +interface Operation { |
| 8 | + tx_id: string; |
| 9 | + timestamp: number; |
| 10 | + nc_method: string; |
| 11 | + nc_caller: string; |
| 12 | + first_block: string | null; |
| 13 | + is_voided: boolean; |
| 14 | +} |
| 15 | + |
| 16 | +interface RecentOperationsTableProps { |
| 17 | + selectedToken: string; |
| 18 | +} |
| 19 | + |
| 20 | +export default function RecentOperationsTable({ selectedToken }: RecentOperationsTableProps) { |
| 21 | + const { getContractIdForToken, coreAPI, isConnected } = useHathor(); |
| 22 | + const [operations, setOperations] = useState<Operation[]>([]); |
| 23 | + const [isLoading, setIsLoading] = useState(false); |
| 24 | + const [lastUpdated, setLastUpdated] = useState<Date | null>(null); |
| 25 | + |
| 26 | + const fetchOperations = async () => { |
| 27 | + if (!isConnected) { |
| 28 | + setOperations([]); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + const contractId = getContractIdForToken(selectedToken); |
| 33 | + if (!contractId) { |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + setIsLoading(true); |
| 38 | + try { |
| 39 | + const history = await coreAPI.getContractHistory(contractId, 50); |
| 40 | + // Filter for claim_balance, remove_liquidity, and add_liquidity operations |
| 41 | + const relevantOps = history.transactions.filter( |
| 42 | + (tx: any) => |
| 43 | + tx.nc_method === 'claim_balance' || |
| 44 | + tx.nc_method === 'remove_liquidity' || |
| 45 | + tx.nc_method === 'add_liquidity' |
| 46 | + ); |
| 47 | + setOperations(relevantOps); |
| 48 | + setLastUpdated(new Date()); |
| 49 | + } catch (error) { |
| 50 | + console.error('Failed to fetch operations:', error); |
| 51 | + setOperations([]); |
| 52 | + } finally { |
| 53 | + setIsLoading(false); |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + fetchOperations(); |
| 59 | + // Refresh every 10 seconds |
| 60 | + const interval = setInterval(fetchOperations, 10000); |
| 61 | + return () => clearInterval(interval); |
| 62 | + }, [selectedToken, isConnected, getContractIdForToken, coreAPI]); |
| 63 | + |
| 64 | + const getOperationStatus = (op: Operation) => { |
| 65 | + if (op.is_voided) { |
| 66 | + return { text: 'Failed', color: 'text-red-400', bg: 'bg-red-900/20', border: 'border-red-700' }; |
| 67 | + } |
| 68 | + if (!op.first_block) { |
| 69 | + return { text: 'Pending', color: 'text-yellow-400', bg: 'bg-yellow-900/20', border: 'border-yellow-700' }; |
| 70 | + } |
| 71 | + return { text: 'Executed', color: 'text-green-400', bg: 'bg-green-900/20', border: 'border-green-700' }; |
| 72 | + }; |
| 73 | + |
| 74 | + const getOperationLabel = (method: string) => { |
| 75 | + if (method === 'claim_balance') return '💸 Withdraw'; |
| 76 | + if (method === 'remove_liquidity') return '💸 Remove Liquidity'; |
| 77 | + if (method === 'add_liquidity') return '💰 Add Liquidity'; |
| 78 | + return method; |
| 79 | + }; |
| 80 | + |
| 81 | + const formatTimestamp = (timestamp: number) => { |
| 82 | + const date = new Date(timestamp * 1000); |
| 83 | + return date.toLocaleString(); |
| 84 | + }; |
| 85 | + |
| 86 | + const formatLastUpdated = () => { |
| 87 | + if (!lastUpdated) return ''; |
| 88 | + const date = lastUpdated.toLocaleDateString(); |
| 89 | + const time = lastUpdated.toLocaleTimeString(); |
| 90 | + return `${date} ${time}`; |
| 91 | + }; |
| 92 | + |
| 93 | + if (!isConnected) { |
| 94 | + return null; |
| 95 | + } |
| 96 | + |
| 97 | + return ( |
| 98 | + <div className="bg-slate-800 rounded-xl border border-slate-700 p-6"> |
| 99 | + <div className="flex items-center justify-between mb-4"> |
| 100 | + <div> |
| 101 | + <h2 className="text-lg font-bold text-white">Recent Operations</h2> |
| 102 | + {lastUpdated && ( |
| 103 | + <p className="text-xs text-slate-400 mt-1"> |
| 104 | + Last updated at {formatLastUpdated()} |
| 105 | + </p> |
| 106 | + )} |
| 107 | + </div> |
| 108 | + <button |
| 109 | + onClick={fetchOperations} |
| 110 | + disabled={isLoading} |
| 111 | + className="px-3 py-1.5 bg-slate-700 hover:bg-slate-600 disabled:opacity-50 disabled:cursor-not-allowed text-white text-sm rounded-lg transition-colors flex items-center gap-2" |
| 112 | + > |
| 113 | + <span className={isLoading ? 'animate-spin' : ''}>🔄</span> |
| 114 | + Refresh |
| 115 | + </button> |
| 116 | + </div> |
| 117 | + |
| 118 | + {operations.length === 0 ? ( |
| 119 | + <div className="text-center py-8 text-slate-400"> |
| 120 | + No recent operations found |
| 121 | + </div> |
| 122 | + ) : ( |
| 123 | + <div className="overflow-x-auto"> |
| 124 | + <table className="w-full"> |
| 125 | + <thead> |
| 126 | + <tr className="border-b border-slate-700"> |
| 127 | + <th className="text-left text-sm font-medium text-slate-400 pb-3">Operation</th> |
| 128 | + <th className="text-left text-sm font-medium text-slate-400 pb-3">Status</th> |
| 129 | + <th className="text-left text-sm font-medium text-slate-400 pb-3">Time</th> |
| 130 | + <th className="text-left text-sm font-medium text-slate-400 pb-3">Address</th> |
| 131 | + <th className="text-left text-sm font-medium text-slate-400 pb-3">TX ID</th> |
| 132 | + </tr> |
| 133 | + </thead> |
| 134 | + <tbody> |
| 135 | + {operations.map((op) => { |
| 136 | + const status = getOperationStatus(op); |
| 137 | + return ( |
| 138 | + <tr key={op.tx_id} className="border-b border-slate-700/50"> |
| 139 | + <td className="py-3 text-white text-sm"> |
| 140 | + {getOperationLabel(op.nc_method)} |
| 141 | + </td> |
| 142 | + <td className="py-3"> |
| 143 | + <span className={`px-2 py-1 rounded text-xs font-medium ${status.color} ${status.bg} border ${status.border}`}> |
| 144 | + {status.text} |
| 145 | + </span> |
| 146 | + </td> |
| 147 | + <td className="py-3 text-slate-300 text-sm"> |
| 148 | + {formatTimestamp(op.timestamp)} |
| 149 | + </td> |
| 150 | + <td className="py-3 text-slate-300 text-sm font-mono"> |
| 151 | + {formatAddress(op.nc_caller)} |
| 152 | + </td> |
| 153 | + <td className="py-3 text-blue-400 text-sm font-mono"> |
| 154 | + <a |
| 155 | + href={`https://explorer.testnet.hathor.network/transaction/${op.tx_id}`} |
| 156 | + target="_blank" |
| 157 | + rel="noopener noreferrer" |
| 158 | + className="hover:underline" |
| 159 | + > |
| 160 | + {formatAddress(op.tx_id)} |
| 161 | + </a> |
| 162 | + </td> |
| 163 | + </tr> |
| 164 | + ); |
| 165 | + })} |
| 166 | + </tbody> |
| 167 | + </table> |
| 168 | + </div> |
| 169 | + )} |
| 170 | + </div> |
| 171 | + ); |
| 172 | +} |
0 commit comments