mempool/frontend/src/app/components/pool/pool.component.ts

85 lines
2.8 KiB
TypeScript
Raw Normal View History

import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
2022-02-09 19:41:05 +09:00
import { ActivatedRoute } from '@angular/router';
import { BehaviorSubject, combineLatest, Observable } from 'rxjs';
import { distinctUntilChanged, map, startWith, switchMap, tap } from 'rxjs/operators';
2022-02-09 19:41:05 +09:00
import { BlockExtended, PoolStat } from 'src/app/interfaces/node-api.interface';
import { ApiService } from 'src/app/services/api.service';
import { StateService } from 'src/app/services/state.service';
2022-02-08 18:56:51 +09:00
@Component({
selector: 'app-pool',
templateUrl: './pool.component.html',
styleUrls: ['./pool.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
2022-02-08 18:56:51 +09:00
})
export class PoolComponent implements OnInit {
2022-02-09 19:41:05 +09:00
poolStats$: Observable<PoolStat>;
blocks$: Observable<BlockExtended[]>;
2022-02-09 19:41:05 +09:00
fromHeight: number = -1;
fromHeightSubject: BehaviorSubject<number> = new BehaviorSubject(this.fromHeight);
2022-02-09 19:41:05 +09:00
blocks: BlockExtended[] = [];
poolId: number = undefined;
radioGroupForm: FormGroup;
2022-02-09 19:41:05 +09:00
2022-02-08 18:56:51 +09:00
constructor(
2022-02-09 19:41:05 +09:00
private apiService: ApiService,
private route: ActivatedRoute,
public stateService: StateService,
private formBuilder: FormBuilder,
) {
this.radioGroupForm = this.formBuilder.group({ dateSpan: '1w' });
this.radioGroupForm.controls.dateSpan.setValue('1w');
}
2022-02-08 18:56:51 +09:00
ngOnInit(): void {
this.poolStats$ = combineLatest([
this.route.params.pipe(map((params) => params.poolId)),
this.radioGroupForm.get('dateSpan').valueChanges.pipe(startWith('1w')),
])
2022-02-09 19:41:05 +09:00
.pipe(
switchMap((params: any) => {
this.poolId = params[0];
if (this.blocks.length === 0) {
this.fromHeightSubject.next(undefined);
}
return this.apiService.getPoolStats$(this.poolId, params[1] ?? '1w');
2022-02-09 19:41:05 +09:00
}),
map((poolStats) => {
let regexes = '"';
for (const regex of poolStats.pool.regexes) {
regexes += regex + '", "';
}
poolStats.pool.regexes = regexes.slice(0, -3);
poolStats.pool.addresses = poolStats.pool.addresses;
return Object.assign({
logo: `./resources/mining-pools/` + poolStats.pool.name.toLowerCase().replace(' ', '').replace('.', '') + '.svg'
}, poolStats);
})
2022-02-09 19:41:05 +09:00
);
this.blocks$ = this.fromHeightSubject
.pipe(
distinctUntilChanged(),
switchMap((fromHeight) => {
return this.apiService.getPoolBlocks$(this.poolId, fromHeight);
}),
tap((newBlocks) => {
this.blocks = this.blocks.concat(newBlocks);
}),
map(() => this.blocks)
)
}
2022-02-09 19:41:05 +09:00
loadMore() {
this.fromHeightSubject.next(this.blocks[this.blocks.length - 1]?.height);
2022-02-09 19:41:05 +09:00
}
trackByBlock(index: number, block: BlockExtended) {
return block.height;
2022-02-08 18:56:51 +09:00
}
}