2024-03-20 16:33:19 +09:00
|
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
|
|
import { Component, OnInit, ChangeDetectionStrategy, Input, Inject, LOCALE_ID, ChangeDetectorRef } from '@angular/core';
|
|
|
|
import { BehaviorSubject, Observable, Subject, Subscription, combineLatest, of, timer } from 'rxjs';
|
|
|
|
import { delayWhen, filter, map, share, shareReplay, switchMap, take, takeUntil, tap, throttleTime } from 'rxjs/operators';
|
2024-01-29 17:06:25 +01:00
|
|
|
import { ApiService } from '../../../services/api.service';
|
|
|
|
import { Env, StateService } from '../../../services/state.service';
|
2024-02-12 11:34:47 +01:00
|
|
|
import { AuditStatus, CurrentPegs, RecentPeg } from '../../../interfaces/node-api.interface';
|
2024-01-29 17:06:25 +01:00
|
|
|
import { WebsocketService } from '../../../services/websocket.service';
|
2024-02-04 12:34:40 +01:00
|
|
|
import { SeoService } from '../../../services/seo.service';
|
2024-01-29 17:06:25 +01:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-recent-pegs-list',
|
|
|
|
templateUrl: './recent-pegs-list.component.html',
|
|
|
|
styleUrls: ['./recent-pegs-list.component.scss'],
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
|
|
})
|
|
|
|
export class RecentPegsListComponent implements OnInit {
|
|
|
|
@Input() widget: boolean = false;
|
2024-02-13 00:39:34 +01:00
|
|
|
@Input() recentPegsList$: Observable<RecentPeg[]>;
|
2024-01-29 17:06:25 +01:00
|
|
|
|
|
|
|
env: Env;
|
|
|
|
isLoading = true;
|
2024-02-12 11:34:47 +01:00
|
|
|
isPegCountLoading = true;
|
2024-01-29 17:06:25 +01:00
|
|
|
page = 1;
|
|
|
|
pageSize = 15;
|
|
|
|
maxSize = window.innerWidth <= 767.98 ? 3 : 5;
|
|
|
|
skeletonLines: number[] = [];
|
|
|
|
auditStatus$: Observable<AuditStatus>;
|
|
|
|
auditUpdated$: Observable<boolean>;
|
|
|
|
lastReservesBlockUpdate: number = 0;
|
|
|
|
currentPeg$: Observable<CurrentPegs>;
|
2024-02-12 11:34:47 +01:00
|
|
|
pegsCount$: Observable<number>;
|
2024-03-20 16:33:19 +09:00
|
|
|
pegsCount: number;
|
2024-02-12 11:34:47 +01:00
|
|
|
startingIndexSubject: BehaviorSubject<number> = new BehaviorSubject(0);
|
|
|
|
currentIndex: number = 0;
|
2024-01-29 17:06:25 +01:00
|
|
|
lastPegBlockUpdate: number = 0;
|
|
|
|
lastPegAmount: string = '';
|
|
|
|
isLoad: boolean = true;
|
2024-03-20 16:33:19 +09:00
|
|
|
queryParamSubscription: Subscription;
|
|
|
|
keyNavigationSubscription: Subscription;
|
|
|
|
dir: 'rtl' | 'ltr' = 'ltr';
|
2024-03-20 18:32:39 +09:00
|
|
|
lastKeyNavTime = 0;
|
|
|
|
isArrowKeyPressed = false;
|
|
|
|
keydownListener: EventListener;
|
|
|
|
keyupListener: EventListener;
|
2024-01-29 17:06:25 +01:00
|
|
|
|
|
|
|
private destroy$ = new Subject();
|
2024-03-20 16:33:19 +09:00
|
|
|
|
2024-01-29 17:06:25 +01:00
|
|
|
constructor(
|
|
|
|
private apiService: ApiService,
|
2024-03-20 16:33:19 +09:00
|
|
|
private cd: ChangeDetectorRef,
|
2024-01-29 17:06:25 +01:00
|
|
|
public stateService: StateService,
|
|
|
|
private websocketService: WebsocketService,
|
2024-03-20 16:33:19 +09:00
|
|
|
private seoService: SeoService,
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private router: Router,
|
|
|
|
@Inject(LOCALE_ID) private locale: string,
|
2024-01-29 17:06:25 +01:00
|
|
|
) {
|
2024-03-20 16:33:19 +09:00
|
|
|
if (this.locale.startsWith('ar') || this.locale.startsWith('fa') || this.locale.startsWith('he')) {
|
|
|
|
this.dir = 'rtl';
|
|
|
|
}
|
2024-03-20 18:32:39 +09:00
|
|
|
this.keydownListener = this.onKeyDown.bind(this);
|
|
|
|
this.keyupListener = this.onKeyUp.bind(this);
|
|
|
|
window.addEventListener('keydown', this.keydownListener);
|
|
|
|
window.addEventListener('keyup', this.keyupListener);
|
2024-01-29 17:06:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
this.isLoading = !this.widget;
|
|
|
|
this.env = this.stateService.env;
|
2024-02-06 17:35:02 +01:00
|
|
|
this.skeletonLines = this.widget === true ? [...Array(5).keys()] : [...Array(15).keys()];
|
2024-01-29 17:06:25 +01:00
|
|
|
|
|
|
|
if (!this.widget) {
|
2024-02-04 12:34:40 +01:00
|
|
|
this.seoService.setTitle($localize`:@@a8b0889ea1b41888f1e247f2731cc9322198ca04:Recent Peg-In / Out's`);
|
2024-01-29 17:06:25 +01:00
|
|
|
this.websocketService.want(['blocks']);
|
2024-03-20 16:33:19 +09:00
|
|
|
|
|
|
|
this.queryParamSubscription = this.route.queryParams.pipe(
|
2024-03-20 18:32:39 +09:00
|
|
|
tap((params) => {
|
|
|
|
this.page = +params['page'] || 1;
|
|
|
|
this.startingIndexSubject.next((this.page - 1) * 15);
|
|
|
|
}),
|
2024-03-20 16:33:19 +09:00
|
|
|
).subscribe();
|
|
|
|
|
|
|
|
this.keyNavigationSubscription = this.stateService.keyNavigation$.subscribe((event) => {
|
|
|
|
const prevKey = this.dir === 'ltr' ? 'ArrowLeft' : 'ArrowRight';
|
|
|
|
const nextKey = this.dir === 'ltr' ? 'ArrowRight' : 'ArrowLeft';
|
|
|
|
if (event.key === prevKey && this.page > 1) {
|
|
|
|
this.page--;
|
2024-03-20 18:32:39 +09:00
|
|
|
this.page === 1 ? this.isArrowKeyPressed = false : null;
|
|
|
|
this.keyNavPageChange(this.page);
|
|
|
|
this.lastKeyNavTime = Date.now();
|
2024-03-20 16:33:19 +09:00
|
|
|
this.cd.markForCheck();
|
|
|
|
}
|
|
|
|
if (event.key === nextKey && this.page < this.pegsCount / this.pageSize) {
|
|
|
|
this.page++;
|
2024-03-20 18:32:39 +09:00
|
|
|
this.page >= this.pegsCount / this.pageSize ? this.isArrowKeyPressed = false : null;
|
|
|
|
this.keyNavPageChange(this.page);
|
|
|
|
this.lastKeyNavTime = Date.now();
|
2024-03-20 16:33:19 +09:00
|
|
|
this.cd.markForCheck();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-01-29 17:06:25 +01:00
|
|
|
this.auditStatus$ = this.stateService.blocks$.pipe(
|
|
|
|
takeUntil(this.destroy$),
|
|
|
|
throttleTime(40000),
|
|
|
|
delayWhen(_ => this.isLoad ? timer(0) : timer(2000)),
|
|
|
|
tap(() => this.isLoad = false),
|
|
|
|
switchMap(() => this.apiService.federationAuditSynced$()),
|
|
|
|
shareReplay(1)
|
|
|
|
);
|
|
|
|
|
|
|
|
this.currentPeg$ = this.auditStatus$.pipe(
|
|
|
|
filter(auditStatus => auditStatus.isAuditSynced === true),
|
|
|
|
switchMap(_ =>
|
|
|
|
this.apiService.liquidPegs$().pipe(
|
|
|
|
filter((currentPegs) => currentPegs.lastBlockUpdate >= this.lastPegBlockUpdate),
|
|
|
|
tap((currentPegs) => {
|
|
|
|
this.lastPegBlockUpdate = currentPegs.lastBlockUpdate;
|
|
|
|
})
|
|
|
|
)
|
|
|
|
),
|
|
|
|
share()
|
|
|
|
);
|
|
|
|
|
|
|
|
this.auditUpdated$ = combineLatest([
|
|
|
|
this.auditStatus$,
|
|
|
|
this.currentPeg$
|
|
|
|
]).pipe(
|
|
|
|
filter(([auditStatus, _]) => auditStatus.isAuditSynced === true),
|
|
|
|
map(([auditStatus, currentPeg]) => ({
|
|
|
|
lastBlockAudit: auditStatus.lastBlockAudit,
|
|
|
|
currentPegAmount: currentPeg.amount
|
|
|
|
})),
|
|
|
|
switchMap(({ lastBlockAudit, currentPegAmount }) => {
|
|
|
|
const blockAuditCheck = lastBlockAudit > this.lastReservesBlockUpdate;
|
|
|
|
const amountCheck = currentPegAmount !== this.lastPegAmount;
|
|
|
|
this.lastReservesBlockUpdate = lastBlockAudit;
|
|
|
|
this.lastPegAmount = currentPegAmount;
|
|
|
|
return of(blockAuditCheck || amountCheck);
|
|
|
|
}),
|
|
|
|
share()
|
|
|
|
);
|
|
|
|
|
2024-02-12 11:34:47 +01:00
|
|
|
this.pegsCount$ = this.auditUpdated$.pipe(
|
2024-01-29 17:06:25 +01:00
|
|
|
filter(auditUpdated => auditUpdated === true),
|
2024-02-12 11:34:47 +01:00
|
|
|
tap(() => this.isPegCountLoading = true),
|
|
|
|
switchMap(_ => this.apiService.pegsCount$()),
|
|
|
|
map((data) => data.pegs_count),
|
2024-03-20 16:33:19 +09:00
|
|
|
tap((pegsCount) => {
|
|
|
|
this.isPegCountLoading = false;
|
|
|
|
this.pegsCount = pegsCount;
|
|
|
|
}),
|
2024-01-29 17:06:25 +01:00
|
|
|
share()
|
|
|
|
);
|
2024-01-30 11:11:30 +01:00
|
|
|
|
2024-02-12 11:34:47 +01:00
|
|
|
this.recentPegsList$ = combineLatest([
|
|
|
|
this.auditStatus$,
|
|
|
|
this.auditUpdated$,
|
|
|
|
this.startingIndexSubject
|
|
|
|
]).pipe(
|
|
|
|
filter(([auditStatus, auditUpdated, startingIndex]) => {
|
|
|
|
const auditStatusCheck = auditStatus.isAuditSynced === true;
|
|
|
|
const auditUpdatedCheck = auditUpdated === true;
|
|
|
|
const startingIndexCheck = startingIndex !== this.currentIndex;
|
|
|
|
return auditStatusCheck && (auditUpdatedCheck || startingIndexCheck);
|
|
|
|
}),
|
|
|
|
tap(([_, __, startingIndex]) => {
|
|
|
|
this.currentIndex = startingIndex;
|
|
|
|
this.isLoading = true;
|
|
|
|
}),
|
|
|
|
switchMap(([_, __, startingIndex]) => this.apiService.recentPegsList$(startingIndex)),
|
|
|
|
tap(() => this.isLoading = false),
|
2024-01-30 11:11:30 +01:00
|
|
|
share()
|
|
|
|
);
|
2024-03-20 16:33:19 +09:00
|
|
|
|
2024-01-29 17:06:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
this.destroy$.next(1);
|
|
|
|
this.destroy$.complete();
|
2024-03-20 16:33:19 +09:00
|
|
|
this.queryParamSubscription?.unsubscribe();
|
|
|
|
this.keyNavigationSubscription?.unsubscribe();
|
2024-03-20 18:32:39 +09:00
|
|
|
window.removeEventListener('keydown', this.keydownListener);
|
|
|
|
window.removeEventListener('keyup', this.keyupListener);
|
|
|
|
|
2024-01-29 17:06:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pageChange(page: number): void {
|
2024-03-20 16:33:19 +09:00
|
|
|
this.router.navigate([], { queryParams: { page: page } });
|
2024-03-20 18:32:39 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
keyNavPageChange(page: number): void {
|
|
|
|
this.isLoading = true;
|
|
|
|
if (this.isArrowKeyPressed) {
|
|
|
|
timer(400).pipe(
|
|
|
|
take(1),
|
|
|
|
filter(() => Date.now() - this.lastKeyNavTime >= 400 && this.isArrowKeyPressed === false),
|
|
|
|
).subscribe(() => {
|
|
|
|
this.pageChange(page);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.pageChange(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onKeyDown(event: KeyboardEvent) {
|
|
|
|
if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
|
|
|
|
this.isArrowKeyPressed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onKeyUp(event: KeyboardEvent) {
|
|
|
|
if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
|
|
|
|
this.isArrowKeyPressed = false;
|
|
|
|
}
|
2024-01-29 17:06:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|