|
| 1 | +import type { Address } from 'ox' |
| 2 | +import * as React from 'react' |
| 3 | + |
| 4 | +import { cx } from '#lib/css' |
| 5 | +import { getApiUrl } from '#lib/env.ts' |
| 6 | +import type { HistorySources } from '#lib/queries/account' |
| 7 | +import DownloadIcon from '~icons/lucide/download' |
| 8 | + |
| 9 | +function getDownloadFilename( |
| 10 | + contentDisposition: string | null, |
| 11 | + fallback: string, |
| 12 | +): string { |
| 13 | + if (!contentDisposition) return fallback |
| 14 | + |
| 15 | + const match = /filename="([^"]+)"/.exec(contentDisposition) |
| 16 | + return match?.[1] ?? fallback |
| 17 | +} |
| 18 | + |
| 19 | +export function AddressCsvExportButton( |
| 20 | + props: AddressCsvExportButton.Props, |
| 21 | +): React.JSX.Element { |
| 22 | + const { address, kind } = props |
| 23 | + const status = kind === 'transactions' ? props.status : undefined |
| 24 | + const include = kind === 'transactions' ? props.include : 'all' |
| 25 | + const after = kind === 'transactions' ? props.after : undefined |
| 26 | + const sources = kind === 'transactions' ? props.sources : [] |
| 27 | + const [isExporting, setIsExporting] = React.useState(false) |
| 28 | + const [error, setError] = React.useState<string | null>(null) |
| 29 | + |
| 30 | + const handleExport = React.useCallback(async () => { |
| 31 | + setIsExporting(true) |
| 32 | + setError(null) |
| 33 | + |
| 34 | + try { |
| 35 | + const searchParams = new URLSearchParams({ format: 'csv' }) |
| 36 | + searchParams.set('sort', 'desc') |
| 37 | + |
| 38 | + let url: URL |
| 39 | + let fallbackFilename: string |
| 40 | + |
| 41 | + if (kind === 'balances') { |
| 42 | + url = getApiUrl(`/api/address/balances/${address}`) |
| 43 | + fallbackFilename = `balances-${address.toLowerCase()}.csv` |
| 44 | + } else { |
| 45 | + url = getApiUrl(`/api/address/history/${address}`) |
| 46 | + fallbackFilename = `transactions-${address.toLowerCase()}.csv` |
| 47 | + |
| 48 | + if (status) searchParams.set('status', status) |
| 49 | + if (include !== 'all') searchParams.set('include', include) |
| 50 | + if (after) searchParams.set('after', String(after)) |
| 51 | + if (sources.length > 0) { |
| 52 | + searchParams.set('sources', sources.join(',')) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + url.search = searchParams.toString() |
| 57 | + |
| 58 | + const response = await fetch(url, { |
| 59 | + headers: { Accept: 'text/csv' }, |
| 60 | + }) |
| 61 | + |
| 62 | + if (!response.ok) { |
| 63 | + let message = |
| 64 | + kind === 'balances' |
| 65 | + ? 'Failed to export balances.' |
| 66 | + : 'Failed to export transactions.' |
| 67 | + |
| 68 | + const contentType = response.headers.get('Content-Type') ?? '' |
| 69 | + if (contentType.includes('application/json')) { |
| 70 | + const payload = (await response.json()) as { error?: string } |
| 71 | + if (payload.error) message = payload.error |
| 72 | + } else { |
| 73 | + const text = (await response.text()).trim() |
| 74 | + if (text) message = text |
| 75 | + } |
| 76 | + |
| 77 | + throw new Error(message) |
| 78 | + } |
| 79 | + |
| 80 | + const blob = await response.blob() |
| 81 | + const objectUrl = URL.createObjectURL(blob) |
| 82 | + |
| 83 | + try { |
| 84 | + const anchor = document.createElement('a') |
| 85 | + anchor.href = objectUrl |
| 86 | + anchor.download = getDownloadFilename( |
| 87 | + response.headers.get('Content-Disposition'), |
| 88 | + fallbackFilename, |
| 89 | + ) |
| 90 | + document.body.appendChild(anchor) |
| 91 | + anchor.click() |
| 92 | + document.body.removeChild(anchor) |
| 93 | + } finally { |
| 94 | + URL.revokeObjectURL(objectUrl) |
| 95 | + } |
| 96 | + } catch (error) { |
| 97 | + setError(error instanceof Error ? error.message : 'CSV export failed.') |
| 98 | + } finally { |
| 99 | + setIsExporting(false) |
| 100 | + } |
| 101 | + }, [address, after, include, kind, sources, status]) |
| 102 | + |
| 103 | + return ( |
| 104 | + <div className="flex flex-col gap-[4px] min-[800px]:items-end"> |
| 105 | + <button |
| 106 | + type="button" |
| 107 | + onClick={handleExport} |
| 108 | + disabled={isExporting} |
| 109 | + aria-label={ |
| 110 | + kind === 'balances' |
| 111 | + ? 'Export balances as CSV' |
| 112 | + : 'Export transactions as CSV' |
| 113 | + } |
| 114 | + className={cx( |
| 115 | + 'flex size-[28px] items-center justify-center rounded-[6px] border text-[12px] transition-colors', |
| 116 | + 'border-transparent text-tertiary hover:text-secondary hover:bg-base-alt', |
| 117 | + isExporting && 'cursor-default opacity-60', |
| 118 | + !isExporting && 'cursor-pointer', |
| 119 | + )} |
| 120 | + title={ |
| 121 | + kind === 'balances' |
| 122 | + ? 'Export balances as CSV' |
| 123 | + : 'Export transactions as CSV' |
| 124 | + } |
| 125 | + > |
| 126 | + <DownloadIcon className="size-[14px]" /> |
| 127 | + </button> |
| 128 | + {error && <span className="text-[11px] text-red-400">{error}</span>} |
| 129 | + </div> |
| 130 | + ) |
| 131 | +} |
| 132 | + |
| 133 | +export declare namespace AddressCsvExportButton { |
| 134 | + type Props = |
| 135 | + | { |
| 136 | + address: Address.Address |
| 137 | + kind: 'balances' |
| 138 | + } |
| 139 | + | { |
| 140 | + address: Address.Address |
| 141 | + kind: 'transactions' |
| 142 | + status?: 'success' | 'reverted' | undefined |
| 143 | + include: 'all' | 'sent' | 'received' |
| 144 | + after?: number | undefined |
| 145 | + sources: ReadonlyArray<HistorySources> |
| 146 | + } |
| 147 | +} |
0 commit comments