69 lines
2.5 KiB
TypeScript
69 lines
2.5 KiB
TypeScript
import { Component, OnInit, inject } from '@angular/core';
|
|
import { Router, RouterOutlet, NavigationEnd } from '@angular/router';
|
|
import { CommonModule } from '@angular/common';
|
|
import { Store } from '@ngrx/store';
|
|
|
|
import { DatabaseService } from './core/services/database.service';
|
|
import { ServerDirectoryService } from './core/services/server-directory.service';
|
|
import { TimeSyncService } from './core/services/time-sync.service';
|
|
import { ServersRailComponent } from './features/servers/servers-rail.component';
|
|
import { TitleBarComponent } from './features/shell/title-bar.component';
|
|
import * as UsersActions from './store/users/users.actions';
|
|
import * as RoomsActions from './store/rooms/rooms.actions';
|
|
|
|
@Component({
|
|
selector: 'app-root',
|
|
imports: [CommonModule, RouterOutlet, ServersRailComponent, TitleBarComponent],
|
|
templateUrl: './app.html',
|
|
styleUrl: './app.scss',
|
|
})
|
|
export class App implements OnInit {
|
|
private databaseService = inject(DatabaseService);
|
|
private store = inject(Store);
|
|
private router = inject(Router);
|
|
private servers = inject(ServerDirectoryService);
|
|
private timeSync = inject(TimeSyncService);
|
|
|
|
async ngOnInit(): Promise<void> {
|
|
// Initialize database
|
|
await this.databaseService.initialize();
|
|
|
|
// Initial time sync with active server
|
|
try {
|
|
const apiBase = this.servers.getApiBaseUrl();
|
|
await this.timeSync.syncWithEndpoint(apiBase);
|
|
} catch {}
|
|
|
|
// Load user data from local storage or create new user
|
|
this.store.dispatch(UsersActions.loadCurrentUser());
|
|
|
|
// Load saved rooms
|
|
this.store.dispatch(RoomsActions.loadRooms());
|
|
|
|
// If not authenticated, redirect to login; else restore last route
|
|
const currentUserId = localStorage.getItem('metoyou_currentUserId');
|
|
if (!currentUserId) {
|
|
if (this.router.url !== '/login' && this.router.url !== '/register') {
|
|
this.router.navigate(['/login']).catch(() => {});
|
|
}
|
|
} else {
|
|
const last = localStorage.getItem('metoyou_lastVisitedRoute');
|
|
if (last && typeof last === 'string') {
|
|
const current = this.router.url;
|
|
if (current === '/' || current === '/search') {
|
|
this.router.navigate([last], { replaceUrl: true }).catch(() => {});
|
|
}
|
|
}
|
|
}
|
|
|
|
// Persist last visited on navigation
|
|
this.router.events.subscribe((evt) => {
|
|
if (evt instanceof NavigationEnd) {
|
|
const url = evt.urlAfterRedirects || evt.url;
|
|
// Store room route or search
|
|
localStorage.setItem('metoyou_lastVisitedRoute', url);
|
|
}
|
|
});
|
|
}
|
|
}
|