Skip to content

Commit

Permalink
Add component for downloading the static report
Browse files Browse the repository at this point in the history
Signed-off-by: ibolton336 <ibolton@redhat.com>
  • Loading branch information
ibolton336 committed Aug 24, 2023
1 parent f8e2b7e commit b992f78
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import { EmptyTextMessage } from "@app/components/EmptyTextMessage";
import { useFetchFacts } from "@app/queries/facts";
import { ApplicationFacts } from "./application-facts";
import { SimpleDocumentViewerModal } from "@app/components/SimpleDocumentViewer";
import { getTaskById } from "@app/api/rest";
import { APPLICATIONS, getTaskById } from "@app/api/rest";
import { COLOR_HEX_VALUES_BY_NAME } from "@app/Constants";
import { Link } from "react-router-dom";
import DownloadButton from "./components/download-button";

export interface IApplicationDetailDrawerAnalysisProps
extends Pick<
Expand Down Expand Up @@ -111,12 +113,26 @@ export const ApplicationDetailDrawerAnalysis: React.FC<
}
type="button"
variant="link"
isInline
onClick={() => setAppAnalysisToView(application.id)}
>
View analysis
</Button>
</Tooltip>
<Text component="small">
<Tooltip
content="Click to download Analysis report"
position="top"
>
<DownloadButton application={application} mimeType="tar" />
</Tooltip>
{" | "}
<Tooltip
content="Click to download Analysis report"
position="top"
>
<DownloadButton application={application} mimeType="yaml" />
</Tooltip>
</Text>
</>
) : task?.state === "Failed" ? (
task ? (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import React, { useState } from "react";
import { APPLICATIONS } from "@app/api/rest";
import { Button } from "@patternfly/react-core";
import spacing from "@patternfly/react-styles/css/utilities/Spacing/spacing";
import { Application } from "@app/api/models";
import { Spinner } from "@patternfly/react-core";

function DownloadButton({
application,
mimeType,
}: {
application: Application;
mimeType: string;
}) {
const [isLoading, setIsLoading] = useState(false);

const handleDownload = async () => {
let acceptHeader = "application/x-tar";

switch (mimeType) {
case "yaml":
acceptHeader = "application/x-yaml";
break;
case "tar":
default:
acceptHeader = "application/x-tar";
}
setIsLoading(true);

try {
const response = await fetch(
`${APPLICATIONS}/${application?.id}/analysis`,
{
headers: {
Accept: acceptHeader,
},
}
);

if (!response.ok) {
throw new Error("Network response was not ok when downloading file.");
}

const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a");
a.style.display = "none";
a.href = url;
a.download = `analysis-${application.name}.` + mimeType;

document.body.appendChild(a);
a.click();

window.URL.revokeObjectURL(url);
} catch (error) {
console.error("There was an error downloading the file:", error);
} finally {
setIsLoading(false);
}
};

return (
<>
{isLoading ? (
<Spinner size="sm" />
) : (
<Button
onClick={handleDownload}
id="download-html"
variant="link"
className={spacing.pXs}
>
{`Download ${mimeType.toUpperCase()}`}
</Button>
)}
</>
);
}

export default DownloadButton;
3 changes: 0 additions & 3 deletions common/src/proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ export const proxyMap: Record<string, Options> = {
},

onProxyReq: (proxyReq, req, res) => {
if (req.originalUrl.includes("windup/report/?filter")) {
proxyReq.setHeader("Accept", "");
}
if (req.cookies?.keycloak_cookie && !req.headers["authorization"]) {
proxyReq.setHeader(
"Authorization",
Expand Down

0 comments on commit b992f78

Please sign in to comment.