-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathexport.ts
More file actions
169 lines (150 loc) · 5.57 KB
/
Copy pathexport.ts
File metadata and controls
169 lines (150 loc) · 5.57 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
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
/**
* Export utilities for SoroSave
* Issue #60 — Add data export feature (CSV/PDF)
*/
export interface ContributionRecord {
round: number;
member: string;
amount: string;
date: string;
status: "paid" | "pending" | "missed";
}
export interface GroupExportData {
groupName: string;
admin: string;
totalRounds: number;
currentRound: number;
members: string[];
payoutOrder: string[];
cycleLength: number;
contributionAmount: string;
contributions: ContributionRecord[];
}
// ── CSV Export ────────────────────────────────────────────────────────────────
export function exportToCSV(data: GroupExportData): void {
const headers = ["Round", "Member", "Amount", "Date", "Status"];
const rows = data.contributions.map((c) => [
String(c.round),
c.member,
c.amount,
c.date,
c.status,
]);
const csvContent = [headers, ...rows]
.map((row) => row.map((cell) => `"${cell.replace(/"/g, '""')}"`).join(","))
.join("\n");
const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.setAttribute("href", url);
link.setAttribute(
"download",
`${data.groupName.replace(/\s+/g, "_")}_contributions.csv`
);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
}
// ── PDF Export ────────────────────────────────────────────────────────────────
export async function exportToPDF(data: GroupExportData): Promise<void> {
// Build an HTML string and use the browser's print-to-PDF
const html = buildPDFHTML(data);
const printWindow = window.open("", "_blank");
if (!printWindow) {
alert("Please allow pop-ups to export PDF.");
return;
}
printWindow.document.write(html);
printWindow.document.close();
// Wait for resources then trigger print
printWindow.onload = () => {
printWindow.focus();
printWindow.print();
printWindow.close();
};
}
function buildPDFHTML(data: GroupExportData): string {
const memberRows = data.members
.map(
(m, i) =>
`<tr>
<td>${i + 1}</td>
<td style="font-family:monospace;font-size:11px">${m}</td>
<td>${data.payoutOrder.indexOf(m) + 1}</td>
</tr>`
)
.join("");
const contributionRows = data.contributions
.map(
(c) =>
`<tr>
<td>${c.round}</td>
<td style="font-family:monospace;font-size:10px">${c.member.slice(0, 12)}...${c.member.slice(-4)}</td>
<td>${c.amount}</td>
<td>${c.date}</td>
<td style="color:${c.status === "paid" ? "green" : c.status === "pending" ? "orange" : "red"}">${c.status.toUpperCase()}</td>
</tr>`
)
.join("");
return `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>SoroSave — ${data.groupName} Report</title>
<style>
body { font-family: Arial, sans-serif; padding: 32px; color: #111; }
h1 { color: #4f46e5; margin-bottom: 4px; }
.subtitle { color: #6b7280; font-size: 14px; margin-bottom: 24px; }
.section { margin-bottom: 28px; }
h2 { font-size: 16px; border-bottom: 2px solid #e5e7eb; padding-bottom: 6px; margin-bottom: 12px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { background: #f3f4f6; text-align: left; padding: 8px 10px; }
td { padding: 7px 10px; border-bottom: 1px solid #e5e7eb; }
.meta-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 24px; }
.meta-item { background: #f9fafb; padding: 12px 16px; border-radius: 8px; }
.meta-label { font-size: 11px; color: #6b7280; text-transform: uppercase; letter-spacing: .5px; }
.meta-value { font-size: 18px; font-weight: 700; margin-top: 2px; }
.footer { margin-top: 40px; font-size: 11px; color: #9ca3af; text-align: center; }
@media print { body { padding: 16px; } }
</style>
</head>
<body>
<h1>SoroSave Group Report</h1>
<div class="subtitle">Generated on ${new Date().toLocaleDateString("en-US", { dateStyle: "long" })}</div>
<div class="meta-grid">
<div class="meta-item">
<div class="meta-label">Group Name</div>
<div class="meta-value">${data.groupName}</div>
</div>
<div class="meta-item">
<div class="meta-label">Round Progress</div>
<div class="meta-value">${data.currentRound} / ${data.totalRounds}</div>
</div>
<div class="meta-item">
<div class="meta-label">Members</div>
<div class="meta-value">${data.members.length}</div>
</div>
<div class="meta-item">
<div class="meta-label">Contribution / Cycle</div>
<div class="meta-value">${data.contributionAmount}</div>
</div>
</div>
<div class="section">
<h2>Members & Payout Order</h2>
<table>
<thead><tr><th>#</th><th>Wallet Address</th><th>Payout Round</th></tr></thead>
<tbody>${memberRows}</tbody>
</table>
</div>
<div class="section">
<h2>Contribution History</h2>
<table>
<thead><tr><th>Round</th><th>Member</th><th>Amount</th><th>Date</th><th>Status</th></tr></thead>
<tbody>${contributionRows || '<tr><td colspan="5" style="color:#9ca3af;text-align:center">No contribution records yet</td></tr>'}</tbody>
</table>
</div>
<div class="footer">SoroSave — Decentralized Group Savings on Stellar · sorosave-protocol</div>
</body>
</html>`;
}