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

Feature: Update report downloader function to download sitemap report #368

Merged
merged 5 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions packages/cli-dashboard/src/components/siteMapReport/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import SiteMapAffectedCookies from './sitemapAffectedCookies';
import CookiesLandingContainer from '../siteReport/tabs/cookies/cookiesLandingContainer';
import reshapeCookies from '../utils/reshapeCookies';
import sidebarData from './sidebarData';
import { genereateAndDownloadCSVReports } from '../utils/reportDownloader';

interface SiteMapReportProps {
landingPageCookies: CookieFrameStorageType;
Expand Down Expand Up @@ -134,6 +135,13 @@ const SiteMapReport = ({
tabCookies={reshapedCookies}
tabFrames={frames}
affectedCookies={affectedCookies}
downloadReport={() => {
if (!Array.isArray(completeJson)) {
return;
}

genereateAndDownloadCSVReports(completeJson, null, true);
}}
/>
);

Expand Down
63 changes: 37 additions & 26 deletions packages/cli-dashboard/src/components/utils/reportDownloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,42 +30,53 @@ import {
generateTechnologyCSV,
} from './generateReports';

export const genereateAndDownloadCSVReports = (
export const genereateAndDownloadCSVReports = async (
JSONReport: CompleteJson[],
selectedPageUrl?: string | null
selectedPageUrl?: string | null,
processAllPages?: boolean
) => {
if (!JSONReport) {
if (!JSONReport.length) {
return;
}

let siteAnalysisData: CompleteJson = JSONReport[0];
let siteAnalysisData: CompleteJson[];

if (selectedPageUrl) {
siteAnalysisData = JSONReport.find(
({ pageUrl }) => pageUrl === selectedPageUrl
) as CompleteJson;
siteAnalysisData = [
JSONReport.find(({ pageUrl }) => pageUrl === selectedPageUrl),
] as CompleteJson[];
} else if (processAllPages) {
siteAnalysisData = JSONReport;
} else {
siteAnalysisData = JSONReport[0];
siteAnalysisData = [JSONReport[0]];
}

const allCookiesCSV = generateAllCookiesCSV(siteAnalysisData);
let technologyDataCSV = null;
if (siteAnalysisData.technologyData.length > 0) {
technologyDataCSV = generateTechnologyCSV(siteAnalysisData);
}
const affectedCookiesDataCSV = generateAffectedCookiesCSV(siteAnalysisData);
const summaryDataCSV = generateSummaryDataCSV(siteAnalysisData);

const zip = new JSZip();
zip.file('cookies.csv', allCookiesCSV);
if (technologyDataCSV) {
zip.file('technologies.csv', technologyDataCSV);
}
zip.file('affected-cookies.csv', affectedCookiesDataCSV);
zip.file('report.csv', summaryDataCSV);
zip.file('report.json', JSON.stringify(JSONReport, null, 4));
zip.generateAsync({ type: 'blob' }).then((content) => {
// see FileSaver.js
saveAs(content, 'report.zip');

siteAnalysisData.forEach((data) => {
const allCookiesCSV = generateAllCookiesCSV(data);
let technologyDataCSV = null;
if (data.technologyData.length > 0) {
technologyDataCSV = generateTechnologyCSV(data);
}
const affectedCookiesDataCSV = generateAffectedCookiesCSV(data);
const summaryDataCSV = generateSummaryDataCSV(data);

let folderName = data.pageUrl
.replace(/^https?:\/\//, '')
.replace(/\/+/g, '-');
zip.folder(folderName);
folderName = folderName + '/';

zip.file(folderName + 'cookies.csv', allCookiesCSV);
if (technologyDataCSV) {
zip.file(folderName + 'technologies.csv', technologyDataCSV);
}
zip.file(folderName + 'affected-cookies.csv', affectedCookiesDataCSV);
zip.file(folderName + 'report.csv', summaryDataCSV);
zip.file(folderName + 'report.json', JSON.stringify(JSONReport, null, 4));
});

const content = await zip.generateAsync({ type: 'blob' });
saveAs(content, 'report.zip');
};
Loading