diff --git a/ui/components/ui/download-threatscore-button/download-threascore-button.tsx b/ui/components/compliance/download-threatscore-button.tsx similarity index 95% rename from ui/components/ui/download-threatscore-button/download-threascore-button.tsx rename to ui/components/compliance/download-threatscore-button.tsx index 7c753f7a5e..daa904a763 100644 --- a/ui/components/ui/download-threatscore-button/download-threascore-button.tsx +++ b/ui/components/compliance/download-threatscore-button.tsx @@ -3,7 +3,7 @@ import { Tooltip } from "@nextui-org/react"; import { DownloadIcon } from "lucide-react"; -import { CustomButton } from "../custom/custom-button"; +import { CustomButton } from "@/components/ui/custom/custom-button"; interface DownloadThreatscoreButtonProps { paramId: string; diff --git a/ui/components/compliance/threatscore-download-button.tsx b/ui/components/compliance/threatscore-download-button.tsx index 79da164f66..4bd533c117 100644 --- a/ui/components/compliance/threatscore-download-button.tsx +++ b/ui/components/compliance/threatscore-download-button.tsx @@ -2,7 +2,8 @@ import { useState } from "react"; -import { DownloadThreatscoreButton, useToast } from "@/components/ui"; +import { useToast } from "@/components/ui"; +import { DownloadThreatscoreButton } from "./download-threatscore-button"; import { downloadThreatscoreReport } from "@/lib/helper"; interface ThreatscoreDownloadButtonProps { diff --git a/ui/components/ui/index.ts b/ui/components/ui/index.ts index 71c8ad0989..205ec7fd50 100644 --- a/ui/components/ui/index.ts +++ b/ui/components/ui/index.ts @@ -7,7 +7,6 @@ export * from "./chart/Chart"; export * from "./content-layout/content-layout"; export * from "./dialog/dialog"; export * from "./download-icon-button/download-icon-button"; -export * from "./download-threatscore-button/download-threascore-button"; export * from "./dropdown/Dropdown"; export * from "./feedback-banner/feedback-banner"; export * from "./headers/navigation-header"; diff --git a/ui/lib/helper.ts b/ui/lib/helper.ts index 6f51797bb0..e773d6d242 100644 --- a/ui/lib/helper.ts +++ b/ui/lib/helper.ts @@ -141,13 +141,20 @@ export const downloadScanZip = async ( } }; -export const downloadComplianceCsv = async ( - scanId: string, - complianceId: string, +/** + * Generic download helper function + * @param result - The result of the download + * @param toast - The toast to display + * @param mimeType - The mime type of the file + * @param successMessage - The success message to display + * @returns void + */ +const handleDownloadResponse = async ( + result: any, toast: ReturnType["toast"], + mimeType: string, + successMessage: string, ): Promise => { - const result = await getComplianceCsv(scanId, complianceId); - if (result?.pending) { toast({ title: "The report is still being generated", @@ -164,7 +171,7 @@ export const downloadComplianceCsv = async ( bytes[i] = binaryString.charCodeAt(i); } - const blob = new Blob([bytes], { type: "text/csv" }); + const blob = new Blob([bytes], { type: mimeType }); const url = window.URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; @@ -176,7 +183,7 @@ export const downloadComplianceCsv = async ( toast({ title: "Download Complete", - description: "The compliance report has been downloaded successfully.", + description: successMessage, }); } catch (error) { toast({ @@ -205,67 +212,31 @@ export const downloadComplianceCsv = async ( }); }; +export const downloadComplianceCsv = async ( + scanId: string, + complianceId: string, + toast: ReturnType["toast"], +): Promise => { + const result = await getComplianceCsv(scanId, complianceId); + await handleDownloadResponse( + result, + toast, + "text/csv", + "The compliance report has been downloaded successfully.", + ); +}; + export const downloadThreatscoreReport = async ( scanId: string, toast: ReturnType["toast"], ): Promise => { const result = await getThreatscoreReport(scanId); - - if (result?.pending) { - toast({ - title: "The report is still being generated", - description: "Please try again in a few minutes.", - }); - return; - } - - if (result?.success && result.data) { - try { - const binaryString = window.atob(result.data); - const bytes = new Uint8Array(binaryString.length); - for (let i = 0; i < binaryString.length; i++) { - bytes[i] = binaryString.charCodeAt(i); - } - - const blob = new Blob([bytes], { type: "application/pdf" }); - const url = window.URL.createObjectURL(blob); - const a = document.createElement("a"); - a.href = url; - a.download = result.filename; - document.body.appendChild(a); - a.click(); - document.body.removeChild(a); - window.URL.revokeObjectURL(url); - - toast({ - title: "Download Complete", - description: "The threatscore report has been downloaded successfully.", - }); - } catch (error) { - toast({ - variant: "destructive", - title: "Download Failed", - description: "An error occurred while processing the file.", - }); - } - return; - } - - if (result?.error) { - toast({ - variant: "destructive", - title: "Download Failed", - description: result.error, - }); - return; - } - - // Unexpected case - toast({ - variant: "destructive", - title: "Download Failed", - description: "Unexpected response. Please try again later.", - }); + await handleDownloadResponse( + result, + toast, + "application/pdf", + "The threatscore report has been downloaded successfully.", + ); }; export const isGoogleOAuthEnabled =