Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import React, { useCallback, useState } from "react";
import clsx from "clsx";
import {
generateMemoryReport,
downloadReport,
canGenerateReport,
MIN_SNAPSHOTS_FOR_REPORT,
} from "../../utils/reportGenerator";
import type { PanelSnapshot } from "../../types";
import styles from "./ReportButton.module.scss";
export interface ReportButtonProps {
/** Array of snapshots to generate report from */
snapshots: PanelSnapshot[];
/** Minimum number of snapshots required (default: 5) */
minSnapshots?: number;
/** Disabled state */
disabled?: boolean;
/** Custom class name */
className?: string;
/** Application name for report header */
appName?: string;
}
/**
* Document icon SVG
*/
function DocumentIcon({ className }: { className?: string }) {
return (
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className={className}
>
<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" />
<polyline points="14 2 14 8 20 8" />
<line x1="16" y1="13" x2="8" y2="13" />
<line x1="16" y1="17" x2="8" y2="17" />
<polyline points="10 9 9 9 8 9" />
</svg>
);
}
/**
* Loading spinner icon
*/
function SpinnerIcon({ className }: { className?: string }) {
return (
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className={className}
>
<path d="M21 12a9 9 0 1 1-6.219-8.56" />
</svg>
);
}
/**
* Button to generate and download memory diagnostic report
*/
export function ReportButton({
snapshots,
minSnapshots = MIN_SNAPSHOTS_FOR_REPORT,
disabled = false,
className,
appName,
}: ReportButtonProps) {
const [isGenerating, setIsGenerating] = useState(false);
const canGenerate = canGenerateReport(snapshots, minSnapshots);
const isDisabled = disabled || !canGenerate || isGenerating;
const remaining = Math.max(0, minSnapshots - snapshots.length);
const handleGenerateReport = useCallback(async () => {
if (!canGenerate || isGenerating) return;
setIsGenerating(true);
try {
// Use setTimeout to allow UI to update before heavy computation
await new Promise((resolve) => setTimeout(resolve, 50));
const html = generateMemoryReport(snapshots, {
minSnapshots,
appName,
includeLeakAnalysis: true,
includeDOMAnalysis: true,
});
downloadReport(html);
} catch (error) {
console.error("Failed to generate report:", error);
} finally {
setIsGenerating(false);
}
}, [snapshots, minSnapshots, appName, canGenerate, isGenerating]);
const getTooltip = () => {
Iif (isGenerating) {
return "Generating report...";
}
Eif (!canGenerate) {
return `${remaining} more snapshot${remaining !== 1 ? "s" : ""} needed`;
}
return "Generate memory diagnostic report";
};
return (
<button
type="button"
onClick={handleGenerateReport}
disabled={isDisabled}
title={getTooltip()}
className={clsx(
styles.button,
canGenerate ? styles.buttonEnabled : styles.buttonDisabled,
className
)}
>
{isGenerating ? (
<SpinnerIcon className={clsx(styles.icon, styles.spinner)} />
) : (
<DocumentIcon className={styles.icon} />
)}
<span>
{isGenerating
? "Generating..."
: canGenerate
? "Generate Report"
: `Generate Report (${snapshots.length}/${minSnapshots})`}
</span>
</button>
);
}
ReportButton.displayName = "ReportButton";
|