Updated database query code to use consistent parameter naming and revised query structure for clarity. Added logic in the history component to return existing data when encountering non-429 HTTP errors.
130 lines
3.9 KiB
TypeScript
130 lines
3.9 KiB
TypeScript
import {Component, EventEmitter, Input, OnChanges, OnInit, Output} from '@angular/core';
|
|
import {BASE_PATH, HistoryService, PunishmentHistoryInner} from '../../../api';
|
|
import {catchError, map, Observable, shareReplay} from 'rxjs';
|
|
import {NgForOf, NgIf, NgOptimizedImage} from '@angular/common';
|
|
import {CookieService} from 'ngx-cookie-service';
|
|
import {RemoveTrailingPeriodPipe} from '../../util/RemoveTrailingPeriodPipe';
|
|
import {HttpErrorResponse} from '@angular/common/http';
|
|
|
|
@Component({
|
|
selector: 'app-history',
|
|
imports: [
|
|
NgIf,
|
|
NgForOf,
|
|
NgOptimizedImage,
|
|
RemoveTrailingPeriodPipe
|
|
],
|
|
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();
|
|
}
|
|
|
|
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;
|
|
this.pageChange.emit(this.history.length);
|
|
}),
|
|
catchError(err => {
|
|
this.pageChange.emit(-1);
|
|
let retrySeconds = 5;
|
|
|
|
if (err instanceof HttpErrorResponse) {
|
|
if (err.status !== 429) {
|
|
return this.history;
|
|
}
|
|
const headers = err.headers;
|
|
const retryAfterHeader = headers.get('Retry-After');
|
|
console.warn(err.error);
|
|
if (retryAfterHeader) {
|
|
const retryAfter = parseInt(retryAfterHeader || '0', 10);
|
|
if (!isNaN(retryAfter) && retryAfter > 0) {
|
|
retrySeconds = retryAfter + 1;
|
|
}
|
|
}
|
|
} else {
|
|
console.error(err);
|
|
}
|
|
setTimeout(() => this.reloadHistory(), retrySeconds * 1000);
|
|
return this.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') {
|
|
uuid = "f78a4d8dd51b4b3998a3230f2de0c670"
|
|
}
|
|
return `https://crafatar.com/avatars/${uuid}?size=25&overlay`;
|
|
}
|
|
}
|