import { ChangeDetectionStrategy, Component, computed, inject, signal } from '@angular/core'; import { FormsModule } from '@angular/forms'; import { MatIconModule } from '@angular/material/icon'; import { ApiService } from '../../core/api.service'; import { MailStateService } from '../../core/mail-state.service'; import { Mailbox, errorText, formatBytes } from '../../core/models'; import { ToastService } from '../../ui/toast.service'; /** Settings → Mailboxes (administrators): create no-reply@, monitor@, support@ …, new password, switch off. */ @Component({ selector: 'app-mailboxes-section', imports: [FormsModule, MatIconModule], changeDetection: ChangeDetectionStrategy.OnPush, template: ` group MAILBOXES Address Name Password (empty — generate) add CREATE @if (error()) { {{ error() }} } @if (secret(); as s) { Password of {{ s.email }} — shown once, copy it now {{ s.password }} content_copy close } @for (m of mailboxes(); track m.id) { {{ m.email }} {{ m.displayName }} @if (m.admin) { ADMIN } {{ size(m.usedBytes) }} key @if (m.email !== me()) { {{ m.active ? 'toggle_on' : 'toggle_off' }} } } ` }) export class MailboxesSection { private api = inject(ApiService); private state = inject(MailStateService); private toasts = inject(ToastService); readonly mailboxes = signal([]); readonly secret = signal<{ email: string; password: string } | null>(null); readonly error = signal(''); readonly busy = signal(false); readonly me = computed(() => this.state.profile()?.email ?? ''); readonly domain = computed(() => this.me().split('@')[1] ?? 'example.com'); email = ''; name = ''; password = ''; constructor() { this.load(); } size(bytes: number): string { return formatBytes(bytes); } load(): void { this.api.mailboxes().subscribe({ next: list => this.mailboxes.set(list), error: e => this.error.set(errorText(e)) }); } create(): void { const email = this.email.trim(); if (!email) { this.error.set('Enter an address'); return; } this.error.set(''); this.busy.set(true); this.api.createMailbox(email, this.name.trim(), this.password).subscribe({ next: created => { this.busy.set(false); this.email = this.name = this.password = ''; if (created.password) { this.secret.set({ email: created.mailbox.email, password: created.password }); } this.toasts.ok('Mailbox ' + created.mailbox.email + ' created'); this.load(); }, error: e => { this.busy.set(false); this.error.set(errorText(e)); } }); } newPassword(m: Mailbox): void { if (!confirm('New password for ' + m.email + '? The old one stops working at once.')) return; this.api.newMailboxPassword(m.id).subscribe({ next: created => created.password && this.secret.set({ email: m.email, password: created.password }), error: e => this.toasts.error(errorText(e)) }); } toggle(m: Mailbox): void { this.api.setMailboxActive(m.id, !m.active).subscribe({ next: () => this.load(), error: e => this.toasts.error(errorText(e)) }); } copy(text: string): void { navigator.clipboard?.writeText(text).then(() => this.toasts.ok('Copied'), () => this.toasts.error('Copy it by hand')); } }
{{ error() }}
{{ s.password }}