import { Component, inject, signal, OnInit } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { Router } from '@angular/router';
import { NgIcon, provideIcons } from '@ng-icons/core';
import {
lucideServer,
lucidePlus,
lucideTrash2,
lucideCheck,
lucideX,
lucideSettings,
lucideRefreshCw,
lucideGlobe,
lucideArrowLeft,
} from '@ng-icons/lucide';
import { ServerDirectoryService, ServerEndpoint } from '../../core/services/server-directory.service';
@Component({
selector: 'app-settings',
standalone: true,
imports: [CommonModule, FormsModule, NgIcon],
viewProviders: [
provideIcons({
lucideServer,
lucidePlus,
lucideTrash2,
lucideCheck,
lucideX,
lucideSettings,
lucideRefreshCw,
lucideGlobe,
lucideArrowLeft,
}),
],
template: `
Settings
Server Endpoints
Add multiple server directories to search for rooms across different networks.
The active server will be used for creating and registering new rooms.
@for (server of servers(); track server.id) {
{{ server.name }}
@if (server.isActive) {
Active
}
{{ server.url }}
@if (server.latency !== undefined && server.status === 'online') {
{{ server.latency }}ms
}
@if (!server.isActive) {
}
@if (!server.isDefault) {
}
}
Add New Server
@if (addError()) {
{{ addError() }}
}
Connection Settings
Auto-reconnect
Automatically reconnect when connection is lost
Search all servers
Search across all configured server directories
`,
})
export class SettingsComponent implements OnInit {
private serverDirectory = inject(ServerDirectoryService);
private router = inject(Router);
servers = this.serverDirectory.servers;
isTesting = signal(false);
addError = signal(null);
newServerName = '';
newServerUrl = '';
autoReconnect = true;
searchAllServers = true;
ngOnInit(): void {
this.loadConnectionSettings();
}
addServer(): void {
this.addError.set(null);
// Validate URL
try {
new URL(this.newServerUrl);
} catch {
this.addError.set('Please enter a valid URL');
return;
}
// Check for duplicates
if (this.servers().some((s) => s.url === this.newServerUrl)) {
this.addError.set('This server URL already exists');
return;
}
this.serverDirectory.addServer({
name: this.newServerName.trim(),
url: this.newServerUrl.trim().replace(/\/$/, ''), // Remove trailing slash
});
// Clear form
this.newServerName = '';
this.newServerUrl = '';
// Test the new server
const servers = this.servers();
const newServer = servers[servers.length - 1];
if (newServer) {
this.serverDirectory.testServer(newServer.id);
}
}
removeServer(id: string): void {
this.serverDirectory.removeServer(id);
}
setActiveServer(id: string): void {
this.serverDirectory.setActiveServer(id);
}
async testAllServers(): Promise {
this.isTesting.set(true);
await this.serverDirectory.testAllServers();
this.isTesting.set(false);
}
loadConnectionSettings(): void {
const settings = localStorage.getItem('metoyou_connection_settings');
if (settings) {
const parsed = JSON.parse(settings);
this.autoReconnect = parsed.autoReconnect ?? true;
this.searchAllServers = parsed.searchAllServers ?? true;
this.serverDirectory.setSearchAllServers(this.searchAllServers);
}
}
saveConnectionSettings(): void {
localStorage.setItem(
'metoyou_connection_settings',
JSON.stringify({
autoReconnect: this.autoReconnect,
searchAllServers: this.searchAllServers,
})
);
this.serverDirectory.setSearchAllServers(this.searchAllServers);
}
goBack(): void {
this.router.navigate(['/']);
}
}