import { Injectable, computed, inject, signal } from '@angular/core'; import { ApiService } from './api.service'; import { Folder, Profile } from './models'; /** Folders and the profile, shared by the sidebar, the lists and the header; refreshed after changes. */ @Injectable({ providedIn: 'root' }) export class MailStateService { private api = inject(ApiService); readonly folders = signal([]); readonly profile = signal(null); readonly inbox = computed(() => this.folders().find(f => f.system && f.name === 'INBOX') ?? null); readonly unreadTotal = computed(() => this.inbox()?.unreadCount ?? 0); system(name: string): Folder | null { return this.folders().find(f => f.system && f.name === name) ?? null; } folder(id: number): Folder | null { return this.folders().find(f => f.id === id) ?? null; } refreshFolders(): void { this.api.folders().subscribe({ next: folders => this.folders.set(folders), error: () => {} }); } refreshProfile(): void { this.api.me().subscribe({ next: profile => this.profile.set(profile), error: () => {} }); } reset(): void { this.folders.set([]); this.profile.set(null); } }