|
|
|
|
|
import { downloadImageProxy } from "@/lib/api/endpoints"; |
|
|
|
|
|
export const downloadImage = async (url: string | null | undefined, filename: string | null | undefined, adId?: string): Promise<void> => { |
|
|
if (!url && !adId) { |
|
|
throw new Error("No image URL or ad ID provided"); |
|
|
} |
|
|
|
|
|
try { |
|
|
|
|
|
const blob = await downloadImageProxy({ |
|
|
image_url: url || undefined, |
|
|
image_id: adId || undefined, |
|
|
}); |
|
|
|
|
|
const blobUrl = window.URL.createObjectURL(blob); |
|
|
const link = document.createElement("a"); |
|
|
link.href = blobUrl; |
|
|
link.download = filename || "image.png"; |
|
|
document.body.appendChild(link); |
|
|
link.click(); |
|
|
document.body.removeChild(link); |
|
|
window.URL.revokeObjectURL(blobUrl); |
|
|
} catch (error) { |
|
|
console.error("Error downloading image:", error); |
|
|
throw error; |
|
|
} |
|
|
}; |
|
|
|
|
|
export const copyToClipboard = async (text: string): Promise<void> => { |
|
|
try { |
|
|
await navigator.clipboard.writeText(text); |
|
|
} catch (error) { |
|
|
console.error("Error copying to clipboard:", error); |
|
|
throw error; |
|
|
} |
|
|
}; |
|
|
|
|
|
export const exportAsJSON = (data: any, filename: string): void => { |
|
|
const jsonString = JSON.stringify(data, null, 2); |
|
|
const blob = new Blob([jsonString], { type: "application/json" }); |
|
|
const url = window.URL.createObjectURL(blob); |
|
|
const link = document.createElement("a"); |
|
|
link.href = url; |
|
|
link.download = filename; |
|
|
document.body.appendChild(link); |
|
|
link.click(); |
|
|
document.body.removeChild(link); |
|
|
window.URL.revokeObjectURL(url); |
|
|
}; |
|
|
|
|
|
export const exportAsCSV = (data: any[], filename: string): void => { |
|
|
if (data.length === 0) return; |
|
|
|
|
|
const headers = Object.keys(data[0]); |
|
|
const csvRows = [ |
|
|
headers.join(","), |
|
|
...data.map((row) => |
|
|
headers.map((header) => { |
|
|
const value = row[header]; |
|
|
|
|
|
if (typeof value === "string" && (value.includes(",") || value.includes('"'))) { |
|
|
return `"${value.replace(/"/g, '""')}"`; |
|
|
} |
|
|
return value ?? ""; |
|
|
}).join(",") |
|
|
), |
|
|
]; |
|
|
|
|
|
const csvString = csvRows.join("\n"); |
|
|
const blob = new Blob([csvString], { type: "text/csv" }); |
|
|
const url = window.URL.createObjectURL(blob); |
|
|
const link = document.createElement("a"); |
|
|
link.href = url; |
|
|
link.download = filename; |
|
|
document.body.appendChild(link); |
|
|
link.click(); |
|
|
document.body.removeChild(link); |
|
|
window.URL.revokeObjectURL(url); |
|
|
}; |
|
|
|