import { DOCUMENT } from '@angular/common'; import { Component, computed, ElementRef, inject, OnDestroy, OnInit, signal, ViewChild } from '@angular/core'; import { NavigationEnd, Router, RouterLink, RouterOutlet } from '@angular/router'; import { MatButtonModule } from '@angular/material/button'; import { MatMenuModule } from '@angular/material/menu'; import { filter } from 'rxjs'; @Component({ imports: [RouterOutlet, RouterLink, MatButtonModule, MatMenuModule], selector: 'app-root', styleUrl: './app.scss', templateUrl: './app.html', }) export class App implements OnInit, OnDestroy { protected readonly title = 'Plataforma de Indicadores'; private readonly router = inject(Router); private readonly currentUrl = signal(this.router.url); protected readonly activeDomain = computed(() => { const match = /^\/indicadores\/(tea|cacu|cama)(?:\/|$)/.exec(this.currentUrl()); return match?.[1] as 'tea' | 'cacu' | 'cama' | undefined; }); protected readonly domainLabels = { tea: 'TEA', cacu: 'CaCu', cama: 'CaMa' } as const; protected readonly otherDomains = computed(() => (['tea', 'cacu', 'cama'] as const) .filter(domain => domain !== this.activeDomain())); private readonly routeSubscription = this.router.events.pipe(filter(event => event instanceof NavigationEnd)) .subscribe(event => this.currentUrl.set(event.urlAfterRedirects)); readonly showBackToTop = signal(false); @ViewChild('mainContent') private mainContent?: ElementRef; private readonly browserWindow = inject(DOCUMENT).defaultView; private readonly updateScroll = () => this.showBackToTop.set((this.browserWindow?.scrollY ?? 0) > 400); ngOnInit() { this.updateScroll(); this.browserWindow?.addEventListener('scroll', this.updateScroll, { passive: true }); } backToTop() { const reducedMotion = this.browserWindow?.matchMedia?.('(prefers-reduced-motion: reduce)').matches; // Keep keyboard focus near the destination when the floating button disappears. this.mainContent?.nativeElement.focus({ preventScroll: true }); this.browserWindow?.scrollTo({ top: 0, behavior: reducedMotion ? 'instant' : 'smooth' }); } ngOnDestroy() { this.routeSubscription.unsubscribe(); this.browserWindow?.removeEventListener('scroll', this.updateScroll); } }