Skip to content

Commit b3a7f60

Browse files
committed
feat(start-button): display loading state and let users cancel the update process
1 parent f7f53a8 commit b3a7f60

File tree

4 files changed

+69
-27
lines changed

4 files changed

+69
-27
lines changed

src/app/components/mod-panel/mod-panel.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="flex w-full flex-col items-center px-10">
2-
<app-start-button (notify)="startUpdateMods()" />
2+
<app-start-button [loading]="this.loading" (notify)="startUpdateMods()" />
33

44
<div class="mt-4 flex w-full flex-col">
55
<div class="w-full">

src/app/components/mod-panel/mod-panel.component.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ export class ModPanelComponent implements OnInit, OnDestroy {
9999
versionsSubscription!: Subscription;
100100
loaderSubscription!: Subscription;
101101
curseforgeSupscription!: Subscription;
102-
private finishedSubscription!: Subscription;
103-
private percentSubscription!: Subscription;
102+
private finishedSubscription?: Subscription;
103+
private percentSubscription?: Subscription;
104104

105105
constructor() {}
106106

@@ -195,6 +195,8 @@ export class ModPanelComponent implements OnInit, OnDestroy {
195195
this.versionsSubscription.unsubscribe();
196196
this.loaderSubscription.unsubscribe();
197197
this.curseforgeSupscription.unsubscribe();
198+
this.finishedSubscription?.unsubscribe();
199+
this.percentSubscription?.unsubscribe();
198200
}
199201

200202
/**
@@ -984,16 +986,26 @@ export class ModPanelComponent implements OnInit, OnDestroy {
984986
* Orchestrates the entire update process, including file processing and dependency fetching.
985987
*/
986988
async startUpdateMods() {
989+
if (this.loading) {
990+
this.finishedSubscription?.unsubscribe();
991+
this.percentSubscription?.unsubscribe();
992+
this.loading = false;
993+
this.loadingPercent = 0;
994+
return;
995+
}
996+
997+
this.loading = true;
998+
this.loadingPercent = 0;
999+
9871000
const [anyToProcess, initialFinished$, initialPercent$] =
9881001
await this.updateMods();
9891002
if (!anyToProcess) {
1003+
this.loading = false;
9901004
return;
9911005
}
9921006

993-
this.loadingPercent = 0;
994-
this.loading = true;
995-
if (this.finishedSubscription) this.finishedSubscription.unsubscribe();
996-
if (this.percentSubscription) this.percentSubscription.unsubscribe();
1007+
this.finishedSubscription?.unsubscribe();
1008+
this.percentSubscription?.unsubscribe();
9971009

9981010
// assuming an average of 1-2 dependencies per mod batch
9991011
const dependencyProcessingWeight = 1.5 / this.files.length;
@@ -1014,7 +1026,7 @@ export class ModPanelComponent implements OnInit, OnDestroy {
10141026
const { progress$, finished$ } = this.processDependencies(mcVersion);
10151027

10161028
// Switch from file progress to dependency progress
1017-
if (this.percentSubscription) this.percentSubscription.unsubscribe();
1029+
this.percentSubscription?.unsubscribe();
10181030
this.percentSubscription = progress$.subscribe((depPercent) => {
10191031
this.loadingPercent =
10201032
fileProcessingWeight + depPercent * dependencyProcessingWeight;
@@ -1030,7 +1042,7 @@ export class ModPanelComponent implements OnInit, OnDestroy {
10301042
)
10311043
.subscribe(() => {
10321044
this.loading = false;
1033-
if (this.percentSubscription) this.percentSubscription.unsubscribe();
1045+
this.percentSubscription?.unsubscribe();
10341046
});
10351047
}
10361048

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,50 @@
11
<button
22
(click)="notify.emit()"
3-
class="flex items-center gap-x-2 rounded-lg bg-gradient-to-r from-indigo-400 to-purple-600 py-2 px-3 text-lg font-bold text-white hover:from-indigo-400 hover:to-purple-700"
3+
[ngClass]="{
4+
'cursor-wait bg-indigo-300': loading,
5+
'bg-gradient-to-r from-indigo-400 to-purple-600 hover:from-indigo-400 hover:to-purple-700':
6+
!loading
7+
}"
8+
class="flex items-center justify-center gap-x-2 rounded-lg py-2 px-3 text-lg font-bold text-white"
49
>
5-
Update
6-
<svg
7-
xmlns="http://www.w3.org/2000/svg"
8-
fill="none"
9-
viewBox="0 0 24 24"
10-
stroke-width="1.5"
11-
stroke="currentColor"
12-
class="h-6 w-6"
13-
aria-hidden="true"
14-
>
15-
<path
16-
stroke-linecap="round"
17-
stroke-linejoin="round"
18-
d="M12 9.75v6.75m0 0l-3-3m3 3l3-3m-8.25 6a4.5 4.5 0 01-1.41-8.775 5.25 5.25 0 0110.233-2.33 3 3 0 013.758 3.848A3.752 3.752 0 0118 19.5H6.75z"
19-
/>
20-
</svg>
10+
@if (loading) {
11+
<span>Loading</span>
12+
<svg
13+
class="h-5 w-5 animate-spin text-white"
14+
xmlns="http://www.w3.org/2000/svg"
15+
fill="none"
16+
viewBox="0 0 24 24"
17+
>
18+
<circle
19+
class="opacity-25"
20+
cx="12"
21+
cy="12"
22+
r="10"
23+
stroke="currentColor"
24+
stroke-width="4"
25+
></circle>
26+
<path
27+
class="opacity-75"
28+
fill="currentColor"
29+
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
30+
></path>
31+
</svg>
32+
} @else {
33+
<span>Update</span>
34+
<svg
35+
xmlns="http://www.w3.org/2000/svg"
36+
fill="none"
37+
viewBox="0 0 24 24"
38+
stroke-width="1.5"
39+
stroke="currentColor"
40+
class="h-6 w-6"
41+
aria-hidden="true"
42+
>
43+
<path
44+
stroke-linecap="round"
45+
stroke-linejoin="round"
46+
d="M12 9.75v6.75m0 0l-3-3m3 3l3-3m-8.25 6a4.5 4.5 0 01-1.41-8.775 5.25 5.25 0 0110.233-2.33 3 3 0 013.758 3.848A3.752 3.752 0 0118 19.5H6.75z"
47+
/>
48+
</svg>
49+
}
2150
</button>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, EventEmitter, Output } from '@angular/core';
1+
import { Component, EventEmitter, Input, Output } from '@angular/core';
22

33
@Component({
44
selector: 'app-start-button',
@@ -7,5 +7,6 @@ import { Component, EventEmitter, Output } from '@angular/core';
77
standalone: false
88
})
99
export class StartButtonComponent {
10+
@Input() loading: boolean = false;
1011
@Output() notify = new EventEmitter();
1112
}

0 commit comments

Comments
 (0)