File size: 9,610 Bytes
49e53ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
'use client'

import { useState, useMemo } from 'react'
import { Search, ChevronDown, ChevronUp, AlertTriangle, CheckCircle } from 'lucide-react'

interface Transaction {
  id: number
  amount: number
  merchant: string
  category: string
  is_fraud: number
  prediction: string
  final_score: number
  classical_score: number
  quantum_score: number
}

interface TransactionTableProps {
  transactions: Transaction[]
  compact?: boolean
}

export default function TransactionTable({ transactions, compact = false }: TransactionTableProps) {
  const [search, setSearch] = useState('')
  const [sortField, setSortField] = useState<keyof Transaction>('id')
  const [sortDirection, setSortDirection] = useState<'asc' | 'desc'>('desc')
  const [filterPrediction, setFilterPrediction] = useState<'all' | 'Fraud' | 'Safe'>('all')
  const [page, setPage] = useState(0)
  const pageSize = compact ? 10 : 20

  const filteredTransactions = useMemo(() => {
    let result = [...transactions]
    
    // Filter by search
    if (search) {
      const searchLower = search.toLowerCase()
      result = result.filter(t => 
        t.merchant?.toLowerCase().includes(searchLower) ||
        t.category?.toLowerCase().includes(searchLower) ||
        t.id.toString().includes(searchLower)
      )
    }
    
    // Filter by prediction
    if (filterPrediction !== 'all') {
      result = result.filter(t => t.prediction === filterPrediction)
    }
    
    // Sort
    result.sort((a, b) => {
      const aVal = a[sortField]
      const bVal = b[sortField]
      
      if (typeof aVal === 'number' && typeof bVal === 'number') {
        return sortDirection === 'asc' ? aVal - bVal : bVal - aVal
      }
      
      const aStr = String(aVal).toLowerCase()
      const bStr = String(bVal).toLowerCase()
      return sortDirection === 'asc' 
        ? aStr.localeCompare(bStr) 
        : bStr.localeCompare(aStr)
    })
    
    return result
  }, [transactions, search, sortField, sortDirection, filterPrediction])

  const paginatedTransactions = useMemo(() => {
    const start = page * pageSize
    return filteredTransactions.slice(start, start + pageSize)
  }, [filteredTransactions, page, pageSize])

  const totalPages = Math.ceil(filteredTransactions.length / pageSize)

  const handleSort = (field: keyof Transaction) => {
    if (sortField === field) {
      setSortDirection(prev => prev === 'asc' ? 'desc' : 'asc')
    } else {
      setSortField(field)
      setSortDirection('desc')
    }
  }

  const SortIcon = ({ field }: { field: keyof Transaction }) => {
    if (sortField !== field) return null
    return sortDirection === 'asc' 
      ? <ChevronUp className="w-4 h-4" />
      : <ChevronDown className="w-4 h-4" />
  }

  if (transactions.length === 0) {
    return (
      <div className="text-center py-8 text-dark-400">

        <p>No transactions to display</p>

      </div>
    )
  }

  return (
    <div className="space-y-4">

      {/* Filters - only show if not compact */}

      {!compact && (

        <div className="flex flex-wrap gap-4">

          {/* Search */}

          <div className="relative flex-1 min-w-[200px]">

            <Search className="absolute left-3 top-1/2 transform -translate-y-1/2 w-4 h-4 text-dark-400" />

            <input

              type="text"

              placeholder="Search merchant, category, ID..."

              value={search}

              onChange={(e) => setSearch(e.target.value)}

              className="w-full bg-dark-700 border border-dark-600 rounded-lg pl-10 pr-4 py-2 text-sm focus:border-primary-500 focus:outline-none"

            />

          </div>

          

          {/* Filter dropdown */}

          <select

            value={filterPrediction}

            onChange={(e) => setFilterPrediction(e.target.value as any)}

            className="bg-dark-700 border border-dark-600 rounded-lg px-4 py-2 text-sm focus:border-primary-500 focus:outline-none"

          >

            <option value="all">All Predictions</option>

            <option value="Fraud">Fraud Only</option>

            <option value="Safe">Safe Only</option>

          </select>

        </div>

      )}

      

      {/* Table */}

      <div className="overflow-x-auto">

        <table className="w-full text-sm">

          <thead>

            <tr className="border-b border-dark-700">

              <th 

                className="text-left py-3 px-4 text-dark-300 font-medium cursor-pointer hover:text-white"

                onClick={() => handleSort('id')}

              >

                <div className="flex items-center gap-1">

                  ID <SortIcon field="id" />

                </div>

              </th>

              <th 

                className="text-left py-3 px-4 text-dark-300 font-medium cursor-pointer hover:text-white"

                onClick={() => handleSort('amount')}

              >

                <div className="flex items-center gap-1">

                  Amount <SortIcon field="amount" />

                </div>

              </th>

              {!compact && (

                <>

                  <th className="text-left py-3 px-4 text-dark-300 font-medium">Merchant</th>

                  <th className="text-left py-3 px-4 text-dark-300 font-medium">Category</th>

                </>

              )}

              <th 

                className="text-left py-3 px-4 text-dark-300 font-medium cursor-pointer hover:text-white"

                onClick={() => handleSort('final_score')}

              >

                <div className="flex items-center gap-1">

                  Risk Score <SortIcon field="final_score" />

                </div>

              </th>

              <th className="text-left py-3 px-4 text-dark-300 font-medium">Status</th>

              <th className="text-left py-3 px-4 text-dark-300 font-medium">Actual</th>

            </tr>

          </thead>

          <tbody>

            {paginatedTransactions.map((t) => (

              <tr 

                key={t.id} 

                className={`

                  border-b border-dark-800 hover:bg-dark-800/50 transition-colors

                  ${t.prediction === 'Fraud' ? 'bg-red-500/5' : ''}

                `}

              >

                <td className="py-3 px-4 font-mono text-dark-300">#{t.id}</td>

                <td className="py-3 px-4 font-semibold">${t.amount.toFixed(2)}</td>

                {!compact && (

                  <>

                    <td className="py-3 px-4 text-dark-300 truncate max-w-[150px]">{t.merchant}</td>

                    <td className="py-3 px-4 text-dark-400">{t.category}</td>

                  </>

                )}

                <td className="py-3 px-4">

                  <div className="flex items-center gap-2">

                    <div className="w-20 h-2 bg-dark-700 rounded-full overflow-hidden">

                      <div 

                        className={`h-full rounded-full ${

                          t.final_score > 0.7 ? 'bg-red-500' : 

                          t.final_score > 0.4 ? 'bg-yellow-500' : 'bg-green-500'

                        }`}

                        style={{ width: `${t.final_score * 100}%` }}

                      />

                    </div>

                    <span className="text-xs text-dark-400">

                      {(t.final_score * 100).toFixed(0)}%

                    </span>

                  </div>

                </td>

                <td className="py-3 px-4">

                  {t.prediction === 'Fraud' ? (

                    <span className="badge-fraud flex items-center gap-1 w-fit">

                      <AlertTriangle className="w-3 h-3" /> Fraud

                    </span>

                  ) : (

                    <span className="badge-safe flex items-center gap-1 w-fit">

                      <CheckCircle className="w-3 h-3" /> Safe

                    </span>

                  )}

                </td>

                <td className="py-3 px-4">

                  <span className={t.is_fraud ? 'text-red-400' : 'text-green-400'}>

                    {t.is_fraud ? '⚠️ Fraud' : '✓ Safe'}

                  </span>

                </td>

              </tr>

            ))}

          </tbody>

        </table>
      </div>
      
      {/* Pagination - only show if not compact and multiple pages */}
      {!compact && totalPages > 1 && (
        <div className="flex items-center justify-between pt-4">

          <p className="text-sm text-dark-400">

            Showing {page * pageSize + 1} - {Math.min((page + 1) * pageSize, filteredTransactions.length)} of {filteredTransactions.length}

          </p>

          <div className="flex items-center gap-2">

            <button

              onClick={() => setPage(Math.max(0, page - 1))}

              disabled={page === 0}

              className="btn-secondary text-sm py-1 px-3 disabled:opacity-50"

            >

              Previous

            </button>

            <span className="text-sm text-dark-400">

              Page {page + 1} of {totalPages}

            </span>

            <button

              onClick={() => setPage(Math.min(totalPages - 1, page + 1))}

              disabled={page >= totalPages - 1}

              className="btn-secondary text-sm py-1 px-3 disabled:opacity-50"

            >

              Next

            </button>

          </div>

        </div>
      )}
    </div>
  )
}