Fix browser freezing when downloading large files (#1304)

This commit is contained in:
Aditya Chandel
2025-10-08 20:25:04 -06:00
committed by GitHub
parent 5b16a25b47
commit 33a0b32b77
10 changed files with 406 additions and 40 deletions
@@ -0,0 +1,33 @@
<p-dialog
[visible]="currentProgress.visible"
[header]="'Downloading File'"
[modal]="true"
[closable]="false"
[draggable]="false"
[resizable]="false"
[style]="{width: '450px'}">
<div class="download-content">
<div class="filename">{{ currentProgress.filename }}</div>
<div class="progress-bar-container">
<div class="progress-bar-background">
<div class="progress-bar-fill" [style.width.%]="currentProgress.progress"></div>
<div class="progress-bar-label">{{ currentProgress.progress }}%</div>
</div>
</div>
<div class="download-info">
<span>{{ formatBytes(currentProgress.loaded) }} / {{ formatBytes(currentProgress.total) }}</span>
</div>
</div>
<ng-template pTemplate="footer">
<p-button
label="Cancel"
icon="pi pi-times"
severity="secondary"
(onClick)="cancelDownload()">
</p-button>
</ng-template>
</p-dialog>
@@ -0,0 +1,53 @@
.download-content {
padding: 1rem 0;
}
.filename {
font-weight: 600;
margin-bottom: 1rem;
word-break: break-word;
}
.progress-bar-container {
margin: 1rem 0;
}
.progress-bar-background {
width: 100%;
height: 1rem;
background-color: var(--p-surface-400);
border-radius: var(--border-radius, 4px);
overflow: hidden;
position: relative;
}
.progress-bar-fill {
height: 100%;
background: linear-gradient(90deg, var(--p-primary-500), var(--p-primary-800));
transition: width 0.3s ease-out;
will-change: width;
border-radius: var(--border-radius, 6px);
position: absolute;
top: 0;
left: 0;
}
.progress-bar-label {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-weight: 600;
font-size: 0.875rem;
color: var(--p-text-color);
z-index: 1;
text-shadow: 0 0 2px rgba(255, 255, 255, 0.8);
}
.download-info {
display: flex;
justify-content: space-between;
margin-top: 0.5rem;
font-size: 0.875rem;
color: var(--p-text-color);
}
@@ -0,0 +1,51 @@
import {Component, inject, ChangeDetectionStrategy, ChangeDetectorRef, OnDestroy} from '@angular/core';
import {CommonModule} from '@angular/common';
import {DialogModule} from 'primeng/dialog';
import {ButtonModule} from 'primeng/button';
import {Subscription} from 'rxjs';
import {DownloadProgress, DownloadProgressService} from '../../service/download-progress.service';
@Component({
selector: 'app-download-progress-dialog',
standalone: true,
imports: [CommonModule, DialogModule, ButtonModule],
changeDetection: ChangeDetectionStrategy.OnPush,
templateUrl: './download-progress-dialog.component.html',
styleUrl: './download-progress-dialog.component.scss'
})
export class DownloadProgressDialogComponent implements OnDestroy {
private downloadProgressService = inject(DownloadProgressService);
private cdr = inject(ChangeDetectorRef);
private subscription: Subscription;
currentProgress: DownloadProgress = {
visible: false,
filename: '',
progress: 0,
loaded: 0,
total: 0
};
constructor() {
this.subscription = this.downloadProgressService.downloadProgress$.subscribe(progress => {
this.currentProgress = progress;
this.cdr.markForCheck();
});
}
ngOnDestroy(): void {
this.subscription.unsubscribe();
}
cancelDownload(): void {
this.downloadProgressService.cancelDownload();
}
formatBytes(bytes: number): string {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return Math.round((bytes / Math.pow(k, i)) * 100) / 100 + ' ' + sizes[i];
}
}