import { Component, computed, inject } from '@angular/core'; import { toSignal } from '@angular/core/rxjs-interop'; import { MatIconModule } from '@angular/material/icon'; import { ActivatedRoute, NavigationEnd, Router, RouterLink, RouterOutlet } from '@angular/router'; import { filter, map } from 'rxjs'; import { AuthService } from './auth/auth.service'; import { DomainSelector } from './domain-selector'; import { Dominio, DOMINIOS } from './features/indicadores/indicadores.models'; @Component({ imports: [MatIconModule, RouterLink, RouterOutlet, DomainSelector], selector: 'app-root', styleUrl: './app.scss', templateUrl: './app.html', }) export class App { private readonly router = inject(Router); private readonly route = inject(ActivatedRoute); private readonly auth = inject(AuthService); private readonly leafRouteData = toSignal( this.router.events.pipe( filter((event) => event instanceof NavigationEnd), map(() => { let route = this.route; while (route.firstChild) route = route.firstChild; return route.snapshot.data; }), ), { initialValue: {} as Record }, ); /** Full-height workspace routes (`data.workspace`) drop the marketing footer. */ readonly workspace = computed(() => !!this.leafRouteData()['workspace']); readonly breadcrumb = computed(() => this.leafRouteData()['breadcrumb'] as string | undefined); /** The login screen (`data.sinCromo`) owns the whole window: no header, no footer. */ readonly sinCromo = computed(() => !!this.leafRouteData()['sinCromo']); protected readonly usuario = this.auth.usuario; protected readonly title = 'Plataforma de Indicadores'; protected readonly domainLabels: Record = { tea: 'Trastorno del espectro autista', cacu: 'Cáncer cervicouterino', cama: 'Cáncer de mama', }; private readonly url = toSignal(this.router.events.pipe( filter((event) => event instanceof NavigationEnd), map(event => event.urlAfterRedirects)), { initialValue: this.router.url }); /** On an indicators page the header names its domain and switches to the others. */ protected readonly activeDomain = computed(() => DOMINIOS.find(domain => new RegExp(`^/indicadores/${domain}(?:[/?#]|$)`).test(this.url()))); /** Only the domains the account may see: the header never offers a forbidden one. */ private readonly permitidos = computed(() => { const sesion = this.auth.usuario(); return sesion ? DOMINIOS.filter(dominio => sesion.dominios.includes(dominio)) : DOMINIOS; }); protected readonly otherDomains = computed(() => this.permitidos().filter(domain => domain !== this.activeDomain())); protected salir(): void { // Whether the server session was already gone, the browser ends up at the login screen. this.auth.logout().subscribe({ next: () => void this.router.navigate(['/login']), error: () => void this.router.navigate(['/login']), }); } }