Files
marketplaces/src/app/app.ts

107 lines
3.6 KiB
TypeScript
Raw Normal View History

2026-01-18 18:57:06 +04:00
import { Component, OnInit, OnDestroy, signal, ApplicationRef } from '@angular/core';
import { CommonModule } from '@angular/common';
2026-02-14 01:28:08 +04:00
import { Router, RouterOutlet, NavigationEnd } from '@angular/router';
2026-01-18 18:57:06 +04:00
import { Title } from '@angular/platform-browser';
import { HeaderComponent } from './components/header/header.component';
import { FooterComponent } from './components/footer/footer.component';
2026-02-14 01:28:08 +04:00
import { BackButtonComponent } from './components/back-button/back-button.component';
2026-01-18 18:57:06 +04:00
import { ApiService } from './services';
import { Subscription, interval, concat } from 'rxjs';
2026-02-14 01:28:08 +04:00
import { filter, first } from 'rxjs/operators';
2026-01-18 18:57:06 +04:00
import { environment } from '../environments/environment';
import { SwUpdate } from '@angular/service-worker';
@Component({
selector: 'app-root',
2026-02-14 01:28:08 +04:00
imports: [RouterOutlet, HeaderComponent, FooterComponent, BackButtonComponent, CommonModule],
2026-01-18 18:57:06 +04:00
templateUrl: './app.html',
styleUrl: './app.scss'
})
export class App implements OnInit, OnDestroy {
protected title = environment.brandName;
serverAvailable = signal(true);
checkingServer = signal(true);
2026-02-14 01:28:08 +04:00
isHomePage = signal(true);
2026-01-18 18:57:06 +04:00
private pingSubscription?: Subscription;
private updateSubscription?: Subscription;
2026-02-14 01:28:08 +04:00
private routerSubscription?: Subscription;
2026-01-18 18:57:06 +04:00
constructor(
private apiService: ApiService,
private titleService: Title,
private swUpdate: SwUpdate,
2026-02-14 01:28:08 +04:00
private appRef: ApplicationRef,
private router: Router
2026-01-18 18:57:06 +04:00
) {}
ngOnInit(): void {
// Устанавливаем заголовок страницы в зависимости от бренда
this.titleService.setTitle(`${environment.brandFullName} - Маркетплейс товаров и услуг`);
this.checkServerHealth();
this.setupAutoUpdates();
2026-02-14 01:28:08 +04:00
// Track route changes to show/hide back button
this.routerSubscription = this.router.events
.pipe(filter(event => event instanceof NavigationEnd))
.subscribe((event) => {
const url = (event as NavigationEnd).urlAfterRedirects || (event as NavigationEnd).url;
this.isHomePage.set(url === '/' || url === '/home' || url === '');
});
2026-01-18 18:57:06 +04:00
}
checkServerHealth(): void {
this.pingSubscription = this.apiService.ping().subscribe({
next: (response) => {
// Server is available
this.serverAvailable.set(true);
this.checkingServer.set(false);
},
error: (err) => {
console.error('Server health check failed:', err);
// Allow app to continue even if server is unreachable
this.serverAvailable.set(true);
this.checkingServer.set(false);
}
});
}
setupAutoUpdates(): void {
if (!this.swUpdate.isEnabled) {
return;
}
// Check for updates every 6 hours
const appIsStable$ = this.appRef.isStable.pipe(first(isStable => isStable === true));
const every6Hours$ = interval(6 * 60 * 60 * 1000);
const checkInterval$ = concat(appIsStable$, every6Hours$);
this.updateSubscription = checkInterval$.subscribe(async () => {
try {
await this.swUpdate.checkForUpdate();
} catch (err) {
console.error('Update check failed:', err);
}
});
// Silently activate updates when ready
this.swUpdate.versionUpdates.subscribe(event => {
if (event.type === 'VERSION_READY') {
// Update will activate on next navigation/reload automatically
console.log('New app version ready');
}
});
}
ngOnDestroy(): void {
this.pingSubscription?.unsubscribe();
this.updateSubscription?.unsubscribe();
2026-02-14 01:28:08 +04:00
this.routerSubscription?.unsubscribe();
2026-01-18 18:57:06 +04:00
}
retryConnection(): void {
this.checkingServer.set(true);
this.checkServerHealth();
}
}