Repository navigation
Expand file tree
/
Copy pathexport.ts
More file actions
146 lines (126 loc) · 4.26 KB
/
Copy pathexport.ts
File metadata and controls
146 lines (126 loc) · 4.26 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
import type { NewsItem, ClusteredEvent, MarketData, PredictionMarket } from '@/types';
type ExportFormat = 'json' | 'csv';
interface ExportData {
news?: NewsItem[] | ClusteredEvent[];
markets?: MarketData[];
predictions?: PredictionMarket[];
signals?: unknown[];
timestamp: number;
}
export function exportToJSON(data: ExportData, filename = 'worldmonitor-export'): void {
const jsonStr = JSON.stringify(data, null, 2);
downloadFile(jsonStr, `${filename}.json`, 'application/json');
}
export function exportToCSV(data: ExportData, filename = 'worldmonitor-export'): void {
const lines: string[] = [];
if (data.news && data.news.length > 0) {
lines.push('=== NEWS ===');
lines.push('Title,Source,Link,Published,IsAlert');
data.news.forEach(item => {
if ('primaryTitle' in item) {
const cluster = item as ClusteredEvent;
lines.push(csvRow([
cluster.primaryTitle,
cluster.primarySource,
cluster.primaryLink,
cluster.lastUpdated.toISOString(),
String(cluster.isAlert),
]));
} else {
const news = item as NewsItem;
lines.push(csvRow([
news.title,
news.source,
news.link,
news.pubDate?.toISOString() || '',
String(news.isAlert),
]));
}
});
lines.push('');
}
if (data.markets && data.markets.length > 0) {
lines.push('=== MARKETS ===');
lines.push('Symbol,Name,Price,Change');
data.markets.forEach(m => {
lines.push(csvRow([m.symbol, m.name, String(m.price ?? ''), String(m.change ?? '')]));
});
lines.push('');
}
if (data.predictions && data.predictions.length > 0) {
lines.push('=== PREDICTIONS ===');
lines.push('Title,Yes Price,Volume');
data.predictions.forEach(p => {
lines.push(csvRow([p.title, String(p.yesPrice), String(p.volume ?? '')]));
});
lines.push('');
}
downloadFile(lines.join('\n'), `${filename}.csv`, 'text/csv');
}
function csvRow(values: string[]): string {
return values.map(v => `"${(v || '').replace(/"/g, '""')}"`).join(',');
}
function downloadFile(content: string, filename: string, mimeType: string): void {
const blob = new Blob([content], { type: mimeType });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);
}
export class ExportPanel {
private element: HTMLElement;
private isOpen = false;
private getData: () => ExportData;
constructor(getDataFn: () => ExportData) {
this.getData = getDataFn;
this.element = document.createElement('div');
this.element.className = 'export-panel-container';
this.element.innerHTML = `
<button class="export-btn" title="Export Data">⬇</button>
<div class="export-menu hidden">
<button class="export-option" data-format="csv">Export CSV</button>
<button class="export-option" data-format="json">Export JSON</button>
</div>
`;
this.setupEventListeners();
}
private setupEventListeners(): void {
const btn = this.element.querySelector('.export-btn')!;
const menu = this.element.querySelector('.export-menu')!;
btn.addEventListener('click', () => {
this.isOpen = !this.isOpen;
menu.classList.toggle('hidden', !this.isOpen);
});
document.addEventListener('click', (e) => {
if (!this.element.contains(e.target as Node)) {
this.isOpen = false;
menu.classList.add('hidden');
}
});
this.element.querySelectorAll('.export-option').forEach(option => {
option.addEventListener('click', () => {
const format = (option as HTMLElement).dataset.format as ExportFormat;
this.export(format);
this.isOpen = false;
menu.classList.add('hidden');
});
});
}
private export(format: ExportFormat): void {
const data = this.getData();
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const filename = `worldmonitor-${timestamp}`;
if (format === 'json') {
exportToJSON(data, filename);
} else {
exportToCSV(data, filename);
}
}
public getElement(): HTMLElement {
return this.element;
}
}