11import html2canvas from 'html2canvas' ;
22import { toast } from 'react-toastify' ;
33
4- /**
5- * Interface for report data structure
6- */
74export interface ReportData {
85 name : string ;
96 completed : number ;
107 ongoing : number ;
118}
129
13- /**
14- * Generates and downloads a CSV file from report data
15- * @param data - Array of report data objects
16- * @param reportType - Type of report (e.g., "Daily", "Weekly", "Monthly")
17- */
1810export const exportReportToCSV = (
1911 data : ReportData [ ] ,
2012 reportType : string
2113) : void => {
2214 try {
23- // Create CSV header
2415 const headers = [ 'Report Type' , 'Completed' , 'Ongoing' ] ;
25-
26- // Create CSV rows
2716 const rows = data . map ( ( item ) => [
2817 item . name ,
2918 item . completed . toString ( ) ,
3019 item . ongoing . toString ( ) ,
3120 ] ) ;
3221
33- // Combine headers and rows
3422 const csvContent = [
3523 headers . join ( ',' ) ,
3624 ...rows . map ( ( row ) => row . join ( ',' ) ) ,
3725 ] . join ( '\n' ) ;
3826
39- // Create blob and download
4027 const blob = new Blob ( [ csvContent ] , { type : 'text/csv;charset=utf-8;' } ) ;
4128 const filename = generateFileName ( reportType , 'csv' ) ;
4229 downloadFile ( blob , filename ) ;
@@ -54,12 +41,6 @@ export const exportReportToCSV = (
5441 }
5542} ;
5643
57- /**
58- * Captures a chart element as PNG and downloads it
59- * @param elementId - DOM element ID of the chart container
60- * @param reportType - Type of report (e.g., "Daily", "Weekly", "Monthly")
61- * @returns Promise that resolves when download is complete
62- */
6344export const exportChartToPNG = async (
6445 elementId : string ,
6546 reportType : string
@@ -71,15 +52,13 @@ export const exportChartToPNG = async (
7152 throw new Error ( `Element with ID "${ elementId } " not found` ) ;
7253 }
7354
74- // Create canvas from the element
7555 const canvas = await html2canvas ( element , {
7656 backgroundColor : '#1c1c1c' ,
7757 scale : 2 , // Higher quality
7858 logging : false ,
7959 useCORS : true ,
8060 } ) ;
8161
82- // Convert canvas to blob
8362 const blob = await new Promise < Blob > ( ( resolve , reject ) => {
8463 canvas . toBlob ( ( blob ) => {
8564 if ( blob ) {
@@ -106,11 +85,6 @@ export const exportChartToPNG = async (
10685 }
10786} ;
10887
109- /**
110- * Triggers a file download in the browser
111- * @param blob - File blob to download
112- * @param filename - Name of the file to save
113- */
11488const downloadFile = ( blob : Blob , filename : string ) : void => {
11589 const url = URL . createObjectURL ( blob ) ;
11690 const link = document . createElement ( 'a' ) ;
@@ -121,17 +95,10 @@ const downloadFile = (blob: Blob, filename: string): void => {
12195 document . body . appendChild ( link ) ;
12296 link . click ( ) ;
12397
124- // Cleanup
12598 document . body . removeChild ( link ) ;
12699 URL . revokeObjectURL ( url ) ;
127100} ;
128101
129- /**
130- * Generates a standardized filename for downloads
131- * @param reportType - Type of report (e.g., "Daily", "Weekly", "Monthly")
132- * @param extension - File extension (csv or png)
133- * @returns Formatted filename string
134- */
135102const generateFileName = ( reportType : string , extension : string ) : string => {
136103 const date = new Date ( ) ;
137104 const dateStr = date . toISOString ( ) . split ( 'T' ) [ 0 ] ; // YYYY-MM-DD format
0 commit comments