mirror of
https://github.com/Dvorinka/Primora.git
synced 2026-10-29 08:04:50 +00:00
109 lines
2.9 KiB
TypeScript
109 lines
2.9 KiB
TypeScript
/**
|
|
* Export utilities for downloading data in various formats
|
|
*/
|
|
|
|
/**
|
|
* Export data as JSON file
|
|
*/
|
|
export function exportJSON(data: any, filename: string) {
|
|
const json = JSON.stringify(data, null, 2);
|
|
const blob = new Blob([json], { type: "application/json" });
|
|
downloadBlob(blob, `${filename}.json`);
|
|
}
|
|
|
|
/**
|
|
* Export data as CSV file
|
|
*/
|
|
export function exportCSV(data: any[], filename: string, headers?: string[]) {
|
|
if (data.length === 0) {
|
|
throw new Error("No data to export");
|
|
}
|
|
|
|
// Get headers from first object if not provided
|
|
const csvHeaders = headers || Object.keys(data[0]);
|
|
|
|
// Create CSV content
|
|
const csvRows = [
|
|
csvHeaders.join(","), // Header row
|
|
...data.map(row =>
|
|
csvHeaders.map(header => {
|
|
const value = row[header];
|
|
// Escape quotes and wrap in quotes if contains comma or quote
|
|
const stringValue = String(value ?? "");
|
|
if (stringValue.includes(",") || stringValue.includes('"') || stringValue.includes("\n")) {
|
|
return `"${stringValue.replace(/"/g, '""')}"`;
|
|
}
|
|
return stringValue;
|
|
}).join(",")
|
|
)
|
|
];
|
|
|
|
const csv = csvRows.join("\n");
|
|
const blob = new Blob([csv], { type: "text/csv" });
|
|
downloadBlob(blob, `${filename}.csv`);
|
|
}
|
|
|
|
/**
|
|
* Export data as text file
|
|
*/
|
|
export function exportText(text: string, filename: string) {
|
|
const blob = new Blob([text], { type: "text/plain" });
|
|
downloadBlob(blob, `${filename}.txt`);
|
|
}
|
|
|
|
/**
|
|
* Download a blob as a file
|
|
*/
|
|
function downloadBlob(blob: Blob, filename: string) {
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* Copy text to clipboard
|
|
*/
|
|
export async function copyToClipboard(text: string): Promise<boolean> {
|
|
try {
|
|
await navigator.clipboard.writeText(text);
|
|
return true;
|
|
} catch (err) {
|
|
// Fallback for older browsers
|
|
const textarea = document.createElement("textarea");
|
|
textarea.value = text;
|
|
textarea.style.position = "fixed";
|
|
textarea.style.opacity = "0";
|
|
document.body.appendChild(textarea);
|
|
textarea.select();
|
|
const success = document.execCommand("copy");
|
|
document.body.removeChild(textarea);
|
|
return success;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Format data for export with timestamps
|
|
*/
|
|
export function prepareExportData(data: any[], includeTimestamp = true) {
|
|
if (!includeTimestamp) return data;
|
|
|
|
return data.map(item => ({
|
|
...item,
|
|
exported_at: new Date().toISOString(),
|
|
}));
|
|
}
|
|
|
|
/**
|
|
* Generate filename with timestamp
|
|
*/
|
|
export function generateFilename(base: string, extension?: string): string {
|
|
const timestamp = new Date().toISOString().replace(/[:.]/g, "-").slice(0, -5);
|
|
const ext = extension ? `.${extension}` : "";
|
|
return `${base}_${timestamp}${ext}`;
|
|
}
|