Files
PrivateChat/client/src/app/home-page.component.ts

156 lines
4.2 KiB
TypeScript
Raw Normal View History

2026-03-09 19:35:08 +01:00
import { CommonModule } from '@angular/common';
2026-03-10 02:49:27 +01:00
import { Component, effect, inject, signal } from '@angular/core';
2026-03-09 19:35:08 +01:00
import { FormsModule } from '@angular/forms';
import { Router, RouterLink } from '@angular/router';
import { ChatSessionService } from './chat-session.service';
2026-03-10 02:49:27 +01:00
import type { AdminUserSummary } from './models';
2026-03-09 19:35:08 +01:00
import { ThemeService } from './theme.service';
@Component({
selector: 'app-home-page',
imports: [CommonModule, FormsModule, RouterLink],
templateUrl: './home-page.component.html',
styleUrl: './home-page.component.scss',
})
export class HomePageComponent {
private readonly router = inject(Router);
readonly theme = inject(ThemeService);
authMode: 'login' | 'register' = 'login';
readonly embeddedMode =
typeof window !== 'undefined' && window.localStorage.getItem('privatechat.embeddedMode') === '1';
serverUrl = '';
displayName = '';
username = '';
password = '';
accessKeyLabel = '';
2026-03-10 02:49:27 +01:00
readonly adminUsers = signal<AdminUserSummary[]>([]);
readonly loadingAdminUsers = signal(false);
readonly deletingUserId = signal<string | null>(null);
readonly adminUsersError = signal<string | null>(null);
2026-03-09 19:35:08 +01:00
constructor(readonly session: ChatSessionService) {
this.serverUrl = session.serverUrl();
if (this.embeddedMode) {
effect(() => {
const currentUser = this.session.currentUser();
const activePeerId = this.session.activePeerId();
if (!currentUser || !activePeerId) {
return;
}
void this.router.navigate(['/chat', activePeerId], { replaceUrl: true });
});
}
2026-03-10 02:49:27 +01:00
effect(() => {
const currentUser = this.session.currentUser();
if (!currentUser || !this.session.isApprovalAdmin()) {
this.adminUsers.set([]);
this.adminUsersError.set(null);
this.loadingAdminUsers.set(false);
return;
}
void this.reloadAdminUsers();
});
2026-03-09 19:35:08 +01:00
}
async submitAuth(): Promise<void> {
this.applyServerUrl();
if (this.authMode === 'register') {
const authenticated = await this.session.register(this.username, this.password, this.displayName);
this.password = '';
if (!authenticated) {
this.authMode = 'login';
}
return;
}
await this.session.login(this.username, this.password);
}
applyServerUrl(): void {
this.session.setServerUrl(this.serverUrl);
}
async logout(): Promise<void> {
await this.session.logout();
this.authMode = 'login';
this.displayName = '';
this.password = '';
}
async loginWithAccessKey(): Promise<void> {
this.applyServerUrl();
await this.session.loginWithAccessKey(this.username);
this.password = '';
}
async registerAccessKey(): Promise<void> {
await this.session.registerAccessKey(this.accessKeyLabel);
this.accessKeyLabel = '';
}
2026-03-10 02:49:27 +01:00
async reloadAdminUsers(): Promise<void> {
this.loadingAdminUsers.set(true);
this.adminUsersError.set(null);
try {
this.adminUsers.set(await this.session.loadAdminUsers());
} catch (error) {
this.adminUsersError.set(
error instanceof Error ? error.message : 'Could not load users.',
);
} finally {
this.loadingAdminUsers.set(false);
}
}
async deleteUser(user: AdminUserSummary): Promise<void> {
if (
typeof window !== 'undefined' &&
!window.confirm(`Delete user ${user.username}? This removes the account from SQLite.`)
) {
return;
}
this.deletingUserId.set(user.id);
this.adminUsersError.set(null);
try {
await this.session.deleteUserAccount(user.id);
this.adminUsers.update((users) => users.filter((candidate) => candidate.id !== user.id));
} catch (error) {
this.adminUsersError.set(
error instanceof Error ? error.message : 'Could not delete that user.',
);
} finally {
this.deletingUserId.set(null);
}
}
2026-03-09 19:35:08 +01:00
async openChatUi(): Promise<void> {
const peerId = this.session.activePeerId() ?? this.session.peers()[0]?.id;
2026-03-09 20:40:21 +01:00
if (peerId) {
this.session.selectPeer(peerId);
await this.router.navigate(['/chat', peerId]);
2026-03-09 19:35:08 +01:00
return;
}
2026-03-09 20:40:21 +01:00
this.session.error.set(null);
await this.router.navigate(['/chat']);
2026-03-09 19:35:08 +01:00
}
cycleTheme(): void {
this.theme.cycleMode();
}
}