Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix redirects after diagnostics download #142

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/pages/conversion/imdf-diagnostics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ import {
DialogTitle,
DialogTrigger,
} from '@fluentui/react-components';
import { useReviewManifestStore } from 'common/store';
import dayjs from 'dayjs';
import { saveAs } from 'file-saver';
import { useEffect, useState } from 'react';
import toast from 'react-hot-toast';
import nextId from 'react-id-generator';
import { failedLogsButton, logsButton } from '../style';
import { extractMessages, processZip } from './utils';

export const ImdfDiagnostics = ({ isFailed, link }) => {
const [getOriginalPackageName] = useReviewManifestStore(s => [s.getOriginalPackageName]);
const [diagnosticsMessages, setDiagnosticsMessages] = useState({ errors: [], warnings: [] });

useEffect(() => {
Expand All @@ -38,18 +42,26 @@ export const ImdfDiagnostics = ({ isFailed, link }) => {
});
}, [link]);

const handleDownload = () => {
const handleDownload = async () => {
if (!link) {
toast.error('No diagnostics available');
return;
}

const linkUrl = link;
const anchor = document.createElement('a');
anchor.href = linkUrl;
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
try {
const response = await fetch(link);
if (!response.ok) {
throw new Error('Bad Network Response');
}
const blob = await response.blob();

const fileName = `conversionDiagnostics_${getOriginalPackageName()}_${dayjs().format('YYYY-MM-DD_HH-mm-ss')}.zip`;
saveAs(blob, fileName);
toast.success('The download has started');
} catch (error) {
toast.error('Failed to download file');
console.error('Download error:', error);
}
};

return (
Expand Down Expand Up @@ -86,7 +98,9 @@ export const ImdfDiagnostics = ({ isFailed, link }) => {
<DialogTrigger disableButtonEnhancement>
<DefaultButton>Close</DefaultButton>
</DialogTrigger>
<PrimaryButton onClick={handleDownload}>Download Full Report</PrimaryButton>
<DialogTrigger disableButtonEnhancement>
<PrimaryButton onClick={handleDownload}>Download Full Report</PrimaryButton>
</DialogTrigger>
</DialogActions>
</DialogBody>
</DialogSurface>
Expand Down
Loading