@@ -2,6 +2,7 @@ import { Download, Loader2, RefreshCw, Settings2, TrendingDown, TrendingUp } fro
22import { useCallback , useEffect , useMemo , useState } from 'react'
33import { tradingApi } from '@/api/trading'
44import { Badge } from '@/components/ui/badge'
5+ import { showToast } from '@/utils/toast'
56import { Button } from '@/components/ui/button'
67import { Card , CardContent , CardDescription , CardHeader , CardTitle } from '@/components/ui/card'
78import {
@@ -131,36 +132,48 @@ export default function TradeBook() {
131132 } , [ fetchTrades ] )
132133
133134 const exportToCSV = ( ) => {
134- const headers = [
135- 'Symbol' ,
136- 'Exchange' ,
137- 'Product' ,
138- 'Action' ,
139- 'Qty' ,
140- 'Price' ,
141- 'Trade Value' ,
142- 'Order ID' ,
143- 'Time' ,
144- ]
145- const rows = trades . map ( ( t ) => [
146- sanitizeCSV ( t . symbol ) ,
147- sanitizeCSV ( t . exchange ) ,
148- sanitizeCSV ( t . product ) ,
149- sanitizeCSV ( t . action ) ,
150- sanitizeCSV ( t . quantity ) ,
151- sanitizeCSV ( t . average_price ) ,
152- sanitizeCSV ( t . trade_value ) ,
153- sanitizeCSV ( t . orderid ) ,
154- sanitizeCSV ( t . timestamp ) ,
155- ] )
135+ if ( filteredTrades . length === 0 ) {
136+ showToast . error ( 'No data to export' , 'system' )
137+ return
138+ }
156139
157- const csv = [ headers , ...rows ] . map ( ( row ) => row . join ( ',' ) ) . join ( '\n' )
158- const blob = new Blob ( [ csv ] , { type : 'text/csv' } )
159- const url = URL . createObjectURL ( blob )
160- const a = document . createElement ( 'a' )
161- a . href = url
162- a . download = `tradebook_${ new Date ( ) . toISOString ( ) . split ( 'T' ) [ 0 ] } .csv`
163- a . click ( )
140+ try {
141+ const headers = [
142+ 'Symbol' ,
143+ 'Exchange' ,
144+ 'Product' ,
145+ 'Action' ,
146+ 'Qty' ,
147+ 'Price' ,
148+ 'Trade Value' ,
149+ 'Order ID' ,
150+ 'Time' ,
151+ ]
152+ const rows = filteredTrades . map ( ( t ) => [
153+ sanitizeCSV ( t . symbol ) ,
154+ sanitizeCSV ( t . exchange ) ,
155+ sanitizeCSV ( t . product ) ,
156+ sanitizeCSV ( t . action ) ,
157+ sanitizeCSV ( t . quantity ) ,
158+ sanitizeCSV ( t . average_price ) ,
159+ sanitizeCSV ( t . trade_value ) ,
160+ sanitizeCSV ( t . orderid ) ,
161+ sanitizeCSV ( t . timestamp ) ,
162+ ] )
163+
164+ const csv = [ headers , ...rows ] . map ( ( row ) => row . join ( ',' ) ) . join ( '\n' )
165+ const blob = new Blob ( [ csv ] , { type : 'text/csv' } )
166+ const url = URL . createObjectURL ( blob )
167+ const a = document . createElement ( 'a' )
168+ a . href = url
169+ const filename = `tradebook_${ new Date ( ) . toISOString ( ) . split ( 'T' ) [ 0 ] } .csv`
170+ a . download = filename
171+ a . click ( )
172+ URL . revokeObjectURL ( url )
173+ showToast . success ( `Downloaded ${ filename } ` , 'clipboard' )
174+ } catch {
175+ showToast . error ( 'Failed to export CSV' , 'system' )
176+ }
164177 }
165178
166179 const stats = {
0 commit comments