forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportStreams.ts
More file actions
66 lines (60 loc) · 2.17 KB
/
Copy pathexportStreams.ts
File metadata and controls
66 lines (60 loc) · 2.17 KB
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
export interface Stream {
id: string;
token: string;
sender: string;
recipient: string;
totalAmount: number; // in stroops
withdrawn: number; // in stroops
status: string;
startDate: string;
endDate: string;
createdAt: string;
cliff?: string;
rate?: number;
txHistory?: unknown[];
}
const STROOP = 10_000_000;
function formatAmount(stroops: number): string {
return (stroops / STROOP).toFixed(7);
}
function downloadFile(content: string, filename: string, type: string) {
const blob = new Blob([content], { type });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url; a.download = filename; a.click();
URL.revokeObjectURL(url);
}
export function exportToCSV(streams: Stream[], filename?: string) {
const headers = [
'Stream ID','Token','Sender','Recipient',
'Total Amount','Withdrawn','Status',
'Start Date','End Date','Created At',
];
const rows = streams.map(s => [
s.id, s.token, s.sender, s.recipient,
formatAmount(s.totalAmount), formatAmount(s.withdrawn),
s.status,
new Date(s.startDate).toISOString(),
new Date(s.endDate).toISOString(),
new Date(s.createdAt).toISOString(),
]);
const csv = [headers, ...rows].map(r => r.join(',')).join('\n');
const name = filename ?? `flowstar-streams-${new Date().toISOString().slice(0,10)}.csv`;
downloadFile(csv, name, 'text/csv');
}
export function exportToJSON(streams: Stream[], filename?: string) {
const data = streams.map(s => ({
...s,
totalAmountFormatted: formatAmount(s.totalAmount),
withdrawnFormatted: formatAmount(s.withdrawn),
startDate: new Date(s.startDate).toISOString(),
endDate: new Date(s.endDate).toISOString(),
createdAt: new Date(s.createdAt).toISOString(),
}));
const name = filename ?? `flowstar-streams-${new Date().toISOString().slice(0,10)}.json`;
downloadFile(JSON.stringify(data, null, 2), name, 'application/json');
}
export function copyToClipboard(streams: Stream[]) {
const json = JSON.stringify(streams, null, 2);
navigator.clipboard.writeText(json);
}