fix(ui): use signals in icon picker / uploader for reactivity (#1881)

This commit is contained in:
James Ward
2026-06-29 21:50:13 -04:00
committed by GitHub
parent 448d0d440c
commit 5014bb9c9a
2 changed files with 109 additions and 83 deletions
@@ -74,15 +74,15 @@
<!-- SVG Library Tab --> <!-- SVG Library Tab -->
<p-tabpanel value="1"> <p-tabpanel value="1">
<div class="tab-content"> <div class="tab-content">
@if (isLoadingSvgIcons) { @if (isLoadingSvgIcons()) {
<div class="loading-state"> <div class="loading-state">
<i class="pi pi-spin pi-spinner"></i> <i class="pi pi-spin pi-spinner"></i>
<span>Loading icons...</span> <span>Loading icons...</span>
</div> </div>
} @else if (svgIconsError) { } @else if (svgIconsError()) {
<div class="error-state"> <div class="error-state">
<i class="pi pi-exclamation-triangle"></i> <i class="pi pi-exclamation-triangle"></i>
<span>{{ svgIconsError }}</span> <span>{{ svgIconsError() }}</span>
</div> </div>
} @else { } @else {
<div class="search-container"> <div class="search-container">
@@ -176,10 +176,10 @@
</div> </div>
} }
@if (errorMessage) { @if (errorMessage()) {
<div class="error-message"> <div class="error-message">
<i class="pi pi-exclamation-circle"></i> <i class="pi pi-exclamation-circle"></i>
<span>{{ errorMessage }}</span> <span>{{ errorMessage() }}</span>
</div> </div>
} }
</div> </div>
@@ -195,12 +195,12 @@
</div> </div>
</section> </section>
@if (svgEntries.length > 0) { @if (svgEntries().length > 0) {
<section class="form-section queue-section"> <section class="form-section queue-section">
<div class="section-header"> <div class="section-header">
<i class="pi pi-list"></i> <i class="pi pi-list"></i>
<span>Queue</span> <span>Queue</span>
<span class="section-badge">{{ svgEntries.length }}</span> <span class="section-badge">{{ svgEntries().length }}</span>
<div class="section-actions"> <div class="section-actions">
<p-button <p-button
label="Clear All" label="Clear All"
@@ -214,7 +214,7 @@
</div> </div>
<div class="queue-grid"> <div class="queue-grid">
@for (entry of svgEntries; track entry.name; let i = $index) { @for (entry of svgEntries(); track entry.name; let i = $index) {
<div class="queue-item" [class.has-error]="entry.error"> <div class="queue-item" [class.has-error]="entry.error">
<div class="queue-item-header"> <div class="queue-item-header">
<span class="queue-item-name">{{ entry.name }}</span> <span class="queue-item-name">{{ entry.name }}</span>
@@ -230,10 +230,10 @@
} }
</div> </div>
@if (batchErrorMessage) { @if (batchErrorMessage()) {
<div class="error-message"> <div class="error-message">
<i class="pi pi-exclamation-circle"></i> <i class="pi pi-exclamation-circle"></i>
<span>{{ batchErrorMessage }}</span> <span>{{ batchErrorMessage() }}</span>
</div> </div>
} }
@@ -242,8 +242,8 @@
label="Save All Icons" label="Save All Icons"
icon="pi pi-save" icon="pi pi-save"
severity="success" severity="success"
[loading]="isSavingBatch" [loading]="isSavingBatch()"
[disabled]="svgEntries.length === 0" [disabled]="svgEntries().length === 0"
(onClick)="saveAllSvgs()" (onClick)="saveAllSvgs()"
/> />
</div> </div>
@@ -1,4 +1,4 @@
import {Component, inject, OnInit} from '@angular/core'; import {Component, inject, OnInit, signal, WritableSignal, computed} from '@angular/core';
import {FormsModule} from '@angular/forms'; import {FormsModule} from '@angular/forms';
import {DynamicDialogRef} from 'primeng/dynamicdialog'; import {DynamicDialogRef} from 'primeng/dynamicdialog';
import {CustomSvgService} from '../../services/custom-svg.service'; import {CustomSvgService} from '../../services/custom-svg.service';
@@ -57,7 +57,7 @@ const lucideIconNodes = iconNodes as unknown as Record<string, LucideIconNode[]>
}) })
export class IconPickerComponent implements OnInit { export class IconPickerComponent implements OnInit {
private hasLoadedSvgIcons = false; private readonly hasLoadedSvgIcons: WritableSignal<boolean> = signal(false);
private readonly MAX_ICON_NAME_LENGTH = 255; private readonly MAX_ICON_NAME_LENGTH = 255;
private readonly MAX_SVG_SIZE = 1048576; // 1MB private readonly MAX_SVG_SIZE = 1048576; // 1MB
@@ -99,7 +99,7 @@ export class IconPickerComponent implements OnInit {
set activeTabIndex(value: string) { set activeTabIndex(value: string) {
this._activeTabIndex = value; this._activeTabIndex = value;
if (value === '1' && !this.hasLoadedSvgIcons && !this.isLoadingSvgIcons) { if (value === '1' && !this.hasLoadedSvgIcons() && !this.isLoadingSvgIcons()) {
this.loadSvgIcons(); this.loadSvgIcons();
} }
} }
@@ -107,16 +107,16 @@ export class IconPickerComponent implements OnInit {
svgContent: string = ''; svgContent: string = '';
svgName: string = ''; svgName: string = '';
svgPreview: string | null = null; svgPreview: string | null = null;
errorMessage: string = ''; errorMessage: WritableSignal<string> = signal('');
svgEntries: SvgEntry[] = []; svgEntries: WritableSignal<SvgEntry[]> = signal([]);
isSavingBatch: boolean = false; isSavingBatch: WritableSignal<boolean> = signal(false);
batchErrorMessage: string = ''; batchErrorMessage: WritableSignal<string> = signal('');
svgIcons: string[] = []; svgIcons: WritableSignal<string[]> = signal([]);
svgSearchText: string = ''; svgSearchText: WritableSignal<string> = signal('');
isLoadingSvgIcons: boolean = false; isLoadingSvgIcons: WritableSignal<boolean> = signal(false);
svgIconsError: string = ''; svgIconsError: WritableSignal<string> = signal('');
selectedSvgIcon: string | null = null; selectedSvgIcon: string | null = null;
draggedSvgIcon: string | null = null; draggedSvgIcon: string | null = null;
@@ -127,9 +127,8 @@ export class IconPickerComponent implements OnInit {
return user?.permissions.canManageIcons || user?.permissions.admin || false; return user?.permissions.canManageIcons || user?.permissions.admin || false;
} }
ngOnInit(): void { ngOnInit(): void {
if (this.activeTabIndex === '1' && !this.hasLoadedSvgIcons && !this.isLoadingSvgIcons) { if (this.activeTabIndex === '1' && !this.hasLoadedSvgIcons() && !this.isLoadingSvgIcons()) {
this.loadSvgIcons(); this.loadSvgIcons();
} }
} }
@@ -139,10 +138,11 @@ export class IconPickerComponent implements OnInit {
return this.icons.filter(icon => icon.toLowerCase().includes(this.searchText.toLowerCase())); return this.icons.filter(icon => icon.toLowerCase().includes(this.searchText.toLowerCase()));
} }
filteredSvgIcons(): string[] { readonly filteredSvgIcons = computed(
if (!this.svgSearchText) return this.svgIcons; () => this.svgIcons().filter(
return this.svgIcons.filter(icon => icon.toLowerCase().includes(this.svgSearchText.toLowerCase())); icon => icon.toLowerCase().includes(this.svgSearchText().toLowerCase())
} )
);
selectIcon(icon: string): void { selectIcon(icon: string): void {
this.selectedIcon = icon; this.selectedIcon = icon;
@@ -158,15 +158,15 @@ export class IconPickerComponent implements OnInit {
} }
private loadSvgIcons(): void { private loadSvgIcons(): void {
this.isLoadingSvgIcons = true; this.isLoadingSvgIcons.set(true);
this.svgIconsError = ''; this.svgIconsError.set('');
this.customSvgService.getIconNames().subscribe({ this.customSvgService.getIconNames().subscribe({
next: (names) => { next: (names) => {
this.hasLoadedSvgIcons = true; this.hasLoadedSvgIcons.set(true);
if (names.length === 0) { if (names.length === 0) {
this.svgIcons = []; this.svgIcons.set([]);
this.isLoadingSvgIcons = false; this.isLoadingSvgIcons.set(false);
return; return;
} }
from(names).pipe( from(names).pipe(
@@ -178,14 +178,14 @@ export class IconPickerComponent implements OnInit {
), ),
toArray() toArray()
).subscribe(() => { ).subscribe(() => {
this.svgIcons = names; this.svgIcons.set(names);
this.isLoadingSvgIcons = false; this.isLoadingSvgIcons.set(false);
}); });
}, },
error: () => { error: () => {
this.isLoadingSvgIcons = false; this.isLoadingSvgIcons.set(false);
this.hasLoadedSvgIcons = false; this.hasLoadedSvgIcons.set(false);
this.svgIconsError = this.ERROR_MESSAGES.LOAD_ICONS_ERROR; this.svgIconsError.set(this.ERROR_MESSAGES.LOAD_ICONS_ERROR);
} }
}); });
} }
@@ -200,7 +200,7 @@ export class IconPickerComponent implements OnInit {
} }
onSvgContentChange(): void { onSvgContentChange(): void {
this.errorMessage = ''; this.errorMessage.set('');
if (!this.svgContent.trim()) { if (!this.svgContent.trim()) {
this.svgPreview = null; this.svgPreview = null;
@@ -210,7 +210,7 @@ export class IconPickerComponent implements OnInit {
const trimmedContent = this.svgContent.trim(); const trimmedContent = this.svgContent.trim();
if (!trimmedContent.includes('<svg')) { if (!trimmedContent.includes('<svg')) {
this.svgPreview = null; this.svgPreview = null;
this.errorMessage = this.ERROR_MESSAGES.MISSING_SVG_TAG; this.errorMessage.set(this.ERROR_MESSAGES.MISSING_SVG_TAG);
return; return;
} }
@@ -222,27 +222,30 @@ export class IconPickerComponent implements OnInit {
this.svgPreview = sanitized; this.svgPreview = sanitized;
} catch { } catch {
this.svgPreview = null; this.svgPreview = null;
this.errorMessage = this.ERROR_MESSAGES.PARSE_ERROR; this.errorMessage.set(this.ERROR_MESSAGES.PARSE_ERROR);
} }
} }
addSvgEntry(): void { addSvgEntry(): void {
const validationError = this.validateSvgInput(); const validationError = this.validateSvgInput();
if (validationError) { if (validationError) {
this.errorMessage = validationError; this.errorMessage.set(validationError);
return; return;
} }
const existingIndex = this.svgEntries.findIndex(entry => entry.name === this.svgName); this.svgEntries.update(
s => {
s = [...s];
const existingIndex = s.findIndex(entry => entry.name === this.svgName);
if (existingIndex !== -1) { if (existingIndex !== -1) {
this.svgEntries[existingIndex] = { s[existingIndex] = {
name: this.svgName, name: this.svgName,
content: this.svgContent, content: this.svgContent,
preview: this.svgPreview, preview: this.svgPreview,
error: '' error: ''
}; };
} else { } else {
this.svgEntries.push({ s.push({
name: this.svgName, name: this.svgName,
content: this.svgContent, content: this.svgContent,
preview: this.svgPreview, preview: this.svgPreview,
@@ -250,52 +253,73 @@ export class IconPickerComponent implements OnInit {
}); });
} }
return s;
}
)
this.resetSvgForm(); this.resetSvgForm();
this.errorMessage = ''; this.errorMessage.set('');
} }
removeSvgEntry(index: number): void { removeSvgEntry(index: number): void {
this.svgEntries.splice(index, 1); this.svgEntries.update(s => s.filter((_, i) => i !== index));
} }
clearAllEntries(): void { clearAllEntries(): void {
this.svgEntries = []; this.svgEntries.set([]);
this.batchErrorMessage = ''; this.batchErrorMessage.set('');
} }
saveAllSvgs(): void { saveAllSvgs(): void {
if (this.svgEntries.length === 0) { if (this.svgEntries().length === 0) {
this.batchErrorMessage = 'No SVG icons to save'; this.batchErrorMessage.set('No SVG icons to save');
return; return;
} }
this.isSavingBatch = true; this.isSavingBatch.set(true);
this.batchErrorMessage = ''; this.batchErrorMessage.set('');
this.svgEntries.forEach(entry => entry.error = ''); this.svgEntries.update(
s => s.map(
entry => ({
...entry,
error: '',
})
)
);
const svgData = this.svgEntries.map(entry => ({ const svgData = this.svgEntries().map(entry => ({
svgName: entry.name, svgName: entry.name,
svgData: entry.content svgData: entry.content
})); }));
this.customSvgService.saveBatchSvgIcons(svgData).subscribe({ this.customSvgService.saveBatchSvgIcons(svgData).subscribe({
next: (response: SvgIconBatchResponse) => { next: (response: SvgIconBatchResponse) => {
this.isSavingBatch = false; this.isSavingBatch.set(false);
let successCount = 0; let successCount = 0;
let failureCount = 0; let failureCount = 0;
const errors: Record<string, string> = {}
response.results.forEach(result => { response.results.forEach(result => {
if (result.success) { if (result.success) {
successCount++; successCount++;
} else { } else {
failureCount++; failureCount++;
const entry = this.svgEntries.find(e => e.name === result.iconName); errors[result.iconName] = result.errorMessage;
if (entry) {
entry.error = result.errorMessage;
}
} }
}); });
this.svgEntries.update(
s => s.map(
entry => ({
...entry,
error: errors[entry.name] ?? '',
})
)
)
if (successCount > 0 && failureCount === 0) { if (successCount > 0 && failureCount === 0) {
this.messageService.add({ this.messageService.add({
severity: 'success', severity: 'success',
@@ -320,18 +344,18 @@ export class IconPickerComponent implements OnInit {
} }
this.clearAllEntries(); this.clearAllEntries();
this.hasLoadedSvgIcons = false; this.hasLoadedSvgIcons.set(false);
this.loadSvgIcons(); this.loadSvgIcons();
}, },
error: () => { error: () => {
this.isSavingBatch = false; this.isSavingBatch.set(false);
this.batchErrorMessage = 'Failed to save SVG icons. Please try again.'; this.batchErrorMessage.set('Failed to save SVG icons. Please try again.');
} }
}); });
} }
private deleteSvgIcon(iconName: string): void { private deleteSvgIcon(iconName: string): void {
this.isLoadingSvgIcons = true; this.isLoadingSvgIcons.set(true);
this.customSvgService.deleteSvgIcon(iconName).subscribe({ this.customSvgService.deleteSvgIcon(iconName).subscribe({
next: () => { next: () => {
@@ -342,11 +366,13 @@ export class IconPickerComponent implements OnInit {
life: 2500 life: 2500
}); });
this.svgIcons = this.svgIcons.filter(name => name !== iconName); this.svgIcons.update(
this.isLoadingSvgIcons = false; value => value.filter(name => name !== iconName)
);
this.isLoadingSvgIcons.set(false);
}, },
error: (error) => { error: (error) => {
this.isLoadingSvgIcons = false; this.isLoadingSvgIcons.set(false);
this.messageService.add({ this.messageService.add({
severity: 'error', severity: 'error',
summary: 'Delete Failed', summary: 'Delete Failed',
@@ -386,7 +412,7 @@ export class IconPickerComponent implements OnInit {
this.svgContent = ''; this.svgContent = '';
this.svgName = ''; this.svgName = '';
this.svgPreview = null; this.svgPreview = null;
this.errorMessage = ''; this.errorMessage.set('');
} }
onSvgIconDragStart(iconName: string): void { onSvgIconDragStart(iconName: string): void {