Skip to content

Commit f3ce4d3

Browse files
author
rvdwegen
committed
Don't load executive report data unless we click the button
1 parent 8bcbc1b commit f3ce4d3

File tree

2 files changed

+102
-61
lines changed

2 files changed

+102
-61
lines changed

src/components/ExecutiveReportButton.js

Lines changed: 99 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,68 +1943,93 @@ export const ExecutiveReportButton = (props) => {
19431943
infographics: true,
19441944
});
19451945

1946-
// Get real secure score data
1947-
const secureScore = useSecureScore();
1946+
// Only fetch additional data when preview dialog is opened
1947+
const secureScore = useSecureScore({ waiting: previewOpen });
19481948

1949-
// Get real license data
1949+
// Get real license data - only when preview is open
19501950
const licenseData = ApiGetCall({
19511951
url: "/api/ListLicenses",
19521952
data: {
19531953
tenantFilter: settings.currentTenant,
19541954
},
19551955
queryKey: `licenses-report-${settings.currentTenant}`,
1956+
waiting: previewOpen,
19561957
});
1957-
// Get real device data
1958+
1959+
// Get real device data - only when preview is open
19581960
const deviceData = ApiGetCall({
19591961
url: "/api/ListDevices",
19601962
data: {
19611963
tenantFilter: settings.currentTenant,
19621964
},
19631965
queryKey: `devices-report-${settings.currentTenant}`,
1966+
waiting: previewOpen,
19641967
});
19651968

1966-
// Get real conditional access policy data
1969+
// Get real conditional access policy data - only when preview is open
19671970
const conditionalAccessData = ApiGetCall({
19681971
url: "/api/ListConditionalAccessPolicies",
19691972
data: {
19701973
tenantFilter: settings.currentTenant,
19711974
},
19721975
queryKey: `ca-policies-report-${settings.currentTenant}`,
1976+
waiting: previewOpen,
19731977
});
19741978

1975-
// Get real standards data
1979+
// Get real standards data - only when preview is open
19761980
const standardsCompareData = ApiGetCall({
19771981
url: "/api/ListStandardsCompare",
19781982
data: {
19791983
tenantFilter: settings.currentTenant,
19801984
},
19811985
queryKey: `standards-compare-report-${settings.currentTenant}`,
1986+
waiting: previewOpen,
19821987
});
19831988

1984-
// Check if all data is loaded (either successful or failed)
1985-
const isDataLoading =
1989+
// Check if all data is loaded (either successful or failed) - only relevant when preview is open
1990+
const isDataLoading = previewOpen && (
19861991
secureScore.isFetching ||
19871992
licenseData.isFetching ||
19881993
deviceData.isFetching ||
19891994
conditionalAccessData.isFetching ||
1990-
standardsCompareData.isFetching;
1995+
standardsCompareData.isFetching
1996+
);
19911997

1992-
const hasAllDataFinished =
1998+
const hasAllDataFinished = !previewOpen || (
19931999
(secureScore.isSuccess || secureScore.isError) &&
19942000
(licenseData.isSuccess || licenseData.isError) &&
19952001
(deviceData.isSuccess || deviceData.isError) &&
19962002
(conditionalAccessData.isSuccess || conditionalAccessData.isError) &&
1997-
(standardsCompareData.isSuccess || standardsCompareData.isError);
2003+
(standardsCompareData.isSuccess || standardsCompareData.isError)
2004+
);
19982005

1999-
// Show button when all data is finished loading (regardless of success/failure)
2000-
const shouldShowButton = hasAllDataFinished && !isDataLoading;
2006+
// Button is always available now since we don't need to wait for data
2007+
const shouldShowButton = true;
20012008

20022009
const fileName = `Executive_Report_${tenantName?.replace(/[^a-zA-Z0-9]/g, "_") || "Tenant"}_${
20032010
new Date().toISOString().split("T")[0]
20042011
}.pdf`;
20052012

2006-
// Memoize the document to prevent unnecessary re-renders
2013+
// Memoize the document to prevent unnecessary re-renders - only when dialog is open
20072014
const reportDocument = useMemo(() => {
2015+
// Don't create document if dialog is closed
2016+
if (!previewOpen) {
2017+
return null;
2018+
}
2019+
2020+
// Only create document if preview is open and data is ready
2021+
if (!hasAllDataFinished) {
2022+
return (
2023+
<Document>
2024+
<Page size="A4" style={{ padding: 40, fontFamily: "Helvetica" }}>
2025+
<Text style={{ fontSize: 14, textAlign: "center", marginTop: 100 }}>
2026+
Loading report data...
2027+
</Text>
2028+
</Page>
2029+
</Document>
2030+
);
2031+
}
2032+
20082033
console.log("Creating report document with:", {
20092034
tenantName,
20102035
tenantId,
@@ -2052,18 +2077,20 @@ export const ExecutiveReportButton = (props) => {
20522077
);
20532078
}
20542079
}, [
2080+
previewOpen, // Most important - prevents creation when dialog is closed
2081+
hasAllDataFinished,
20552082
tenantName,
20562083
tenantId,
20572084
userStats,
20582085
standardsData,
20592086
organizationData,
20602087
brandingSettings,
2061-
secureScore,
2062-
licenseData,
2063-
deviceData,
2064-
conditionalAccessData,
2065-
standardsCompareData,
2066-
sectionConfig,
2088+
secureScore?.isSuccess,
2089+
licenseData?.isSuccess,
2090+
deviceData?.isSuccess,
2091+
conditionalAccessData?.isSuccess,
2092+
standardsCompareData?.isSuccess,
2093+
JSON.stringify(sectionConfig), // Stringify to prevent reference issues
20672094
]);
20682095

20692096
// Handle section toggle
@@ -2084,6 +2111,11 @@ export const ExecutiveReportButton = (props) => {
20842111
});
20852112
};
20862113

2114+
// Close handler with cleanup
2115+
const handleClose = () => {
2116+
setPreviewOpen(false);
2117+
};
2118+
20872119
// Section configuration options
20882120
const sectionOptions = [
20892121
{
@@ -2123,32 +2155,9 @@ export const ExecutiveReportButton = (props) => {
21232155
},
21242156
];
21252157

2126-
// Don't render the button if data is not ready
2127-
if (!shouldShowButton) {
2128-
return (
2129-
<Tooltip title="Loading report data...">
2130-
<Button
2131-
variant="contained"
2132-
startIcon={<PictureAsPdf />}
2133-
disabled={true}
2134-
sx={{
2135-
fontWeight: "bold",
2136-
textTransform: "none",
2137-
borderRadius: 2,
2138-
boxShadow: "0 2px 8px rgba(0,0,0,0.15)",
2139-
transition: "all 0.2s ease-in-out",
2140-
}}
2141-
{...other}
2142-
>
2143-
Loading Data...
2144-
</Button>
2145-
</Tooltip>
2146-
);
2147-
}
2148-
21492158
return (
21502159
<>
2151-
{/* Main Executive Summary Button */}
2160+
{/* Main Executive Summary Button - Always available */}
21522161
<Tooltip title="Generate Executive Report with preview and configuration">
21532162
<Button
21542163
variant="contained"
@@ -2170,7 +2179,7 @@ export const ExecutiveReportButton = (props) => {
21702179
{/* Combined Preview and Configuration Dialog */}
21712180
<Dialog
21722181
open={previewOpen}
2173-
onClose={() => setPreviewOpen(false)}
2182+
onClose={handleClose}
21742183
maxWidth="xl"
21752184
fullWidth
21762185
sx={{
@@ -2193,7 +2202,7 @@ export const ExecutiveReportButton = (props) => {
21932202
<Typography variant="h6" component="div">
21942203
Executive Report - {tenantName}
21952204
</Typography>
2196-
<IconButton onClick={() => setPreviewOpen(false)} size="small">
2205+
<IconButton onClick={handleClose} size="small">
21972206
<Close />
21982207
</IconButton>
21992208
</DialogTitle>
@@ -2303,17 +2312,47 @@ export const ExecutiveReportButton = (props) => {
23032312

23042313
{/* Right Panel - PDF Preview */}
23052314
<Box sx={{ flex: 1, height: "100%" }}>
2306-
<PDFViewer
2307-
key={JSON.stringify(sectionConfig)} // Force re-render when sections change
2308-
style={{
2309-
width: "100%",
2310-
height: "100%",
2311-
border: "none",
2312-
}}
2313-
showToolbar={true}
2314-
>
2315-
{reportDocument}
2316-
</PDFViewer>
2315+
{isDataLoading ? (
2316+
<Box
2317+
sx={{
2318+
display: "flex",
2319+
flexDirection: "column",
2320+
alignItems: "center",
2321+
justifyContent: "center",
2322+
height: "100%",
2323+
gap: 2,
2324+
}}
2325+
>
2326+
<Typography variant="h6">Loading Report Data...</Typography>
2327+
<Typography variant="body2" color="text.secondary">
2328+
Fetching additional data for comprehensive report generation
2329+
</Typography>
2330+
</Box>
2331+
) : reportDocument ? (
2332+
<PDFViewer
2333+
style={{
2334+
width: "100%",
2335+
height: "100%",
2336+
border: "none",
2337+
}}
2338+
showToolbar={true}
2339+
>
2340+
{reportDocument}
2341+
</PDFViewer>
2342+
) : (
2343+
<Box
2344+
sx={{
2345+
display: "flex",
2346+
alignItems: "center",
2347+
justifyContent: "center",
2348+
height: "100%",
2349+
}}
2350+
>
2351+
<Typography variant="body1" color="text.secondary">
2352+
Report preview will appear here
2353+
</Typography>
2354+
</Box>
2355+
)}
23172356
</Box>
23182357
</DialogContent>
23192358

@@ -2328,7 +2367,7 @@ export const ExecutiveReportButton = (props) => {
23282367
<Button
23292368
variant="contained"
23302369
startIcon={<Download />}
2331-
disabled={!shouldShowButton}
2370+
disabled={isDataLoading}
23322371
sx={{ minWidth: 140 }}
23332372
onClick={() => {
23342373
// Create document dynamically when download is clicked
@@ -2373,10 +2412,10 @@ export const ExecutiveReportButton = (props) => {
23732412
});
23742413
}}
23752414
>
2376-
Download PDF
2415+
{isDataLoading ? "Loading..." : "Download PDF"}
23772416
</Button>
23782417

2379-
<Button onClick={() => setPreviewOpen(false)} variant="outlined">
2418+
<Button onClick={handleClose} variant="outlined">
23802419
Close
23812420
</Button>
23822421
</DialogActions>

src/hooks/use-securescore.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ApiGetCall } from "../api/ApiCall";
33
import { useSettings } from "./use-settings";
44
import standards from "/src/data/standards.json";
55

6-
export function useSecureScore() {
6+
export function useSecureScore({ waiting = true } = {}) {
77
const currentTenant = useSettings().currentTenant;
88
if (currentTenant === "AllTenants") {
99
return {
@@ -27,6 +27,7 @@ export function useSecureScore() {
2727
$top: 999,
2828
},
2929
queryKey: `controlScore-${currentTenant}`,
30+
waiting: waiting,
3031
});
3132

3233
const secureScore = ApiGetCall({
@@ -39,6 +40,7 @@ export function useSecureScore() {
3940
$top: 7,
4041
},
4142
queryKey: `secureScore-${currentTenant}`,
43+
waiting: waiting,
4244
});
4345

4446
useEffect(() => {

0 commit comments

Comments
 (0)