Skip to content

Commit bac353c

Browse files
committed
Optimize: collapse the useless info
1 parent 11945bf commit bac353c

2 files changed

Lines changed: 71 additions & 25 deletions

File tree

src/index.ts

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,28 @@ async function processSingleFile(
423423

424424
// Generate summary table for quick overview
425425
if (reportsWithCurrent.length > 0) {
426-
commentBody += '<details>\n<summary><b>📊 Quick Summary</b> (Click to expand)</summary>\n\n';
426+
// Check if any project has significant changes (> 1% or no baseline)
427+
let hasChanges = false;
428+
for (const report of reportsWithCurrent) {
429+
if (!report.current) continue;
430+
if (!report.baseline) {
431+
hasChanges = true; // No baseline means we can't compare, show it
432+
break;
433+
}
434+
const currentSize = report.current.totalSize;
435+
const baselineSize = report.baseline.totalSize;
436+
if (baselineSize === 0 || isNaN(baselineSize)) continue;
437+
const diff = currentSize - baselineSize;
438+
const percent = Math.abs((diff / baselineSize) * 100);
439+
if (percent >= 1) {
440+
hasChanges = true;
441+
break;
442+
}
443+
}
444+
445+
// Use 'open' attribute if there are changes, otherwise keep it collapsed
446+
const detailsTag = hasChanges ? '<details open>\n' : '<details>\n';
447+
commentBody += `${detailsTag}<summary><b>📊 Quick Summary</b></summary>\n\n`;
427448
commentBody += '| Project | Total Size | Change |\n';
428449
commentBody += '|---------|------------|--------|\n';
429450

@@ -439,27 +460,46 @@ async function processSingleFile(
439460
commentBody += '\n</details>\n\n';
440461
}
441462

442-
// Generate detailed reports (collapsed by default)
443-
if (reportsWithCurrent.length > 1) {
444-
commentBody += '<details>\n<summary><b>📋 Detailed Reports</b> (Click to expand)</summary>\n\n';
445-
}
463+
// Helper function to check if a report has significant changes
464+
const hasSignificantChanges = (report: ProjectReport): boolean => {
465+
if (!report.current) return false;
466+
if (!report.baseline) return true; // No baseline means we can't compare, show it
467+
const currentSize = report.current.totalSize;
468+
const baselineSize = report.baseline.totalSize;
469+
if (baselineSize === 0 || isNaN(baselineSize)) return false;
470+
const diff = currentSize - baselineSize;
471+
const percent = Math.abs((diff / baselineSize) * 100);
472+
return percent >= 1;
473+
};
446474

447-
for (const report of projectReports) {
448-
if (!report.current) continue;
475+
// Filter reports with changes
476+
const reportsWithChanges = projectReports.filter(report => {
477+
if (!report.current) return false;
478+
return hasSignificantChanges(report);
479+
});
480+
481+
// Generate detailed reports only for projects with changes
482+
if (reportsWithChanges.length > 0) {
483+
// Only add collapse wrapper if there are multiple reports with changes
484+
if (reportsWithChanges.length > 1) {
485+
commentBody += '<details>\n<summary><b>📋 Detailed Reports</b> (Click to expand)</summary>\n\n';
486+
}
449487

450-
commentBody += generateProjectMarkdown(report.projectName, report.filePath, report.current, report.baseline || undefined, report.baselineCommitHash, report.baselinePRs);
488+
for (const report of reportsWithChanges) {
489+
commentBody += generateProjectMarkdown(report.projectName, report.filePath, report.current!, report.baseline || undefined, report.baselineCommitHash, report.baselinePRs);
490+
491+
// Add diff HTML link if available
492+
if (report.diffHtmlArtifactId) {
493+
const artifactDownloadLink = `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts/${report.diffHtmlArtifactId}`;
494+
commentBody += `\n📦 **Download Diff Report**: [${report.projectName} Bundle Diff](${artifactDownloadLink})\n\n`;
495+
}
496+
}
451497

452-
// Add diff HTML link if available
453-
if (report.diffHtmlArtifactId) {
454-
const artifactDownloadLink = `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}/artifacts/${report.diffHtmlArtifactId}`;
455-
commentBody += `\n📦 **Download Diff Report**: [${report.projectName} Bundle Diff](${artifactDownloadLink})\n\n`;
498+
if (reportsWithChanges.length > 1) {
499+
commentBody += '</details>\n\n';
456500
}
457501
}
458502

459-
if (reportsWithCurrent.length > 1) {
460-
commentBody += '</details>\n\n';
461-
}
462-
463503
commentBody += '*Generated by [Rsdoctor GitHub Action](https://rsdoctor.rs/guide/start/action)*';
464504

465505
try {

src/report.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,24 @@ export function loadSizeData(filePath: string): SizeData | null {
157157

158158
export function calculateDiff(current: number, baseline: number): { value: string; emoji: string } {
159159
if (!baseline || baseline === 0 || isNaN(baseline)) {
160-
return { value: 'N/A', emoji: '❓' };
160+
return { value: '0', emoji: '❓' };
161161
}
162162

163163
if (isNaN(current)) {
164-
return { value: 'N/A', emoji: '❓' };
164+
return { value: '0', emoji: '❓' };
165165
}
166166

167167
const diff = current - baseline;
168+
169+
// If diff is 0, just return "0"
170+
if (diff === 0) {
171+
return { value: '0', emoji: '' };
172+
}
173+
168174
const percent = (diff / baseline) * 100;
169175

170176
if (Math.abs(percent) < 1) {
171-
return { value: `${formatBytes(diff)} (${percent.toFixed(1)}%)`, emoji: '➡️' };
177+
return { value: `${formatBytes(diff)} (${percent.toFixed(1)}%)`, emoji: ''};
172178
} else if (diff > 0) {
173179
return { value: `+${formatBytes(diff)} (+${percent.toFixed(1)}%)`, emoji: '📈' };
174180
} else {
@@ -256,31 +262,31 @@ export async function generateBundleAnalysisReport(
256262
{ data: '📊 Total Size', header: false },
257263
{ data: formatBytes(current.totalSize), header: false },
258264
{ data: baseline ? formatBytes(baseline.totalSize) : formatBytes(current.totalSize), header: false },
259-
{ data: baseline ? calculateDiff(current.totalSize, baseline.totalSize).value : 'N/A', header: false }
265+
{ data: baseline ? calculateDiff(current.totalSize, baseline.totalSize).value : '0', header: false }
260266
],
261267
[
262268
{ data: '📄 JavaScript', header: false },
263269
{ data: formatBytes(current.jsSize), header: false },
264270
{ data: baseline ? formatBytes(baseline.jsSize) : formatBytes(current.jsSize), header: false },
265-
{ data: baseline ? calculateDiff(current.jsSize, baseline.jsSize).value : 'N/A', header: false }
271+
{ data: baseline ? calculateDiff(current.jsSize, baseline.jsSize).value : '0', header: false }
266272
],
267273
[
268274
{ data: '🎨 CSS', header: false },
269275
{ data: formatBytes(current.cssSize), header: false },
270276
{ data: baseline ? formatBytes(baseline.cssSize) : formatBytes(current.cssSize), header: false },
271-
{ data: baseline ? calculateDiff(current.cssSize, baseline.cssSize).value : 'N/A', header: false }
277+
{ data: baseline ? calculateDiff(current.cssSize, baseline.cssSize).value : '0', header: false }
272278
],
273279
[
274280
{ data: '🌐 HTML', header: false },
275281
{ data: formatBytes(current.htmlSize), header: false },
276282
{ data: baseline ? formatBytes(baseline.htmlSize) : formatBytes(current.htmlSize), header: false },
277-
{ data: baseline ? calculateDiff(current.htmlSize, baseline.htmlSize).value : 'N/A', header: false }
283+
{ data: baseline ? calculateDiff(current.htmlSize, baseline.htmlSize).value : '0', header: false }
278284
],
279285
[
280286
{ data: '📁 Other Assets', header: false },
281287
{ data: formatBytes(current.otherSize), header: false },
282288
{ data: baseline ? formatBytes(baseline.otherSize) : formatBytes(current.otherSize), header: false },
283-
{ data: baseline ? calculateDiff(current.otherSize, baseline.otherSize).value : 'N/A', header: false }
289+
{ data: baseline ? calculateDiff(current.otherSize, baseline.otherSize).value : '0', header: false }
284290
]
285291
];
286292

@@ -310,7 +316,7 @@ export async function generateSizeReport(current: SizeData, baseline?: SizeData)
310316
[
311317
{ data: '📊 Total Size', header: false },
312318
{ data: formatBytes(current.totalSize), header: false },
313-
{ data: baseline ? formatBytes(baseline.totalSize) : 'N/A', header: false }
319+
{ data: baseline ? formatBytes(baseline.totalSize) : '0', header: false }
314320
]
315321
];
316322

0 commit comments

Comments
 (0)