AltitudeWeb/frontend/src/app/bans/history/history.component.ts
Teriuihi 48fd133b6d Fix pageChange emission to use history length
Modified the `ngOnChanges` method to emit the length of the history instead of the current page. This ensures accurate data is provided to parent components during changes.
2025-04-12 23:01:35 +02:00

104 lines
3.0 KiB
TypeScript

import {Component, EventEmitter, Input, OnChanges, OnInit, Output} from '@angular/core';
import {BASE_PATH, HistoryService, PunishmentHistoryInner} from '../../../api';
import {map, Observable, shareReplay} from 'rxjs';
import {NgForOf, NgIf, NgOptimizedImage} from '@angular/common';
import {CookieService} from 'ngx-cookie-service';
@Component({
selector: 'app-history',
imports: [
NgIf,
NgForOf,
NgOptimizedImage
],
templateUrl: './history.component.html',
styleUrl: './history.component.scss',
providers: [
CookieService,
{provide: BASE_PATH, useValue: 'http://localhost:8080/'}
],
})
export class HistoryComponent implements OnInit, OnChanges {
@Input() userType: 'player' | 'staff' = "player";
@Input() punishmentType: 'all' | 'ban' | 'mute' | 'kick' | 'warn' = "all";
@Input() page: number = 0;
@Input() searchTerm: string = '';
@Output() pageChange = new EventEmitter<number>();
private uuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
public history: PunishmentHistoryInner[] = []
constructor(private historyApi: HistoryService) {
}
ngOnChanges(): void {
this.reloadHistory();
this.pageChange.emit(this.history.length);
}
ngOnInit(): void {
this.reloadHistory();
}
private reloadHistory(): void {
let historyObservable: Observable<PunishmentHistoryInner[]>;
if (this.searchTerm.length === 0) {
historyObservable = this.historyApi.getHistoryForAll(this.userType, this.punishmentType, this.page);
} else {
if (this.uuidRegex.test(this.searchTerm)) {
historyObservable = this.historyApi.getHistoryForUuid(this.userType, this.punishmentType, this.searchTerm, this.page);
} else {
historyObservable = this.historyApi.getHistoryForUsers(this.userType, this.punishmentType, this.searchTerm, this.page)
}
}
historyObservable.pipe(
map(history => {
this.history = history;
}),
shareReplay(1)
).subscribe();
}
public getPunishmentTime(entry: PunishmentHistoryInner) {
const date = new Date(entry.punishmentTime);
return date.toLocaleString(navigator.language, {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
hour12: false
});
}
public getType(entry: PunishmentHistoryInner) {
return entry.type.charAt(0).toUpperCase() + entry.type.slice(1);
}
public getExpiredTime(entry: PunishmentHistoryInner) {
if (entry.expiryTime === 0) {
return "Permanent " + this.getType(entry);
}
const date = new Date(entry.expiryTime);
return date.toLocaleString(navigator.language, {
year: 'numeric',
month: 'short',
day: 'numeric',
hour: '2-digit',
minute: '2-digit',
hour12: false
});
}
public getAvatarUrl(entry: string): string {
let uuid = entry.replace('-', '');
if (uuid === 'C') {
return '/public/img/random/console.png';
}
return `https://crafatar.com/avatars/${uuid}?size=25&overlay`;
}
}