2022-02-15 18:36:58 +09:00
|
|
|
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
|
2022-02-10 19:16:00 +09:00
|
|
|
import { FormBuilder, FormGroup } from '@angular/forms';
|
2022-02-09 19:41:05 +09:00
|
|
|
import { ActivatedRoute } from '@angular/router';
|
2022-02-15 18:36:58 +09:00
|
|
|
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',
|
2022-02-15 18:36:58 +09:00
|
|
|
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>;
|
2022-02-10 19:16:00 +09:00
|
|
|
blocks$: Observable<BlockExtended[]>;
|
2022-02-09 19:41:05 +09:00
|
|
|
|
2022-02-10 19:16:00 +09:00
|
|
|
fromHeight: number = -1;
|
|
|
|
fromHeightSubject: BehaviorSubject<number> = new BehaviorSubject(this.fromHeight);
|
2022-02-09 19:41:05 +09:00
|
|
|
|
2022-02-10 19:16:00 +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,
|
2022-02-10 19:16:00 +09:00
|
|
|
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 {
|
2022-02-10 19:16:00 +09:00
|
|
|
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(
|
2022-02-10 19:16:00 +09:00
|
|
|
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
|
|
|
}),
|
2022-02-11 18:29:38 +09:00
|
|
|
map((poolStats) => {
|
2022-02-11 19:10:29 +09:00
|
|
|
let regexes = '"';
|
2022-02-15 18:45:53 +09:00
|
|
|
for (const regex of poolStats.pool.regexes) {
|
2022-02-11 19:10:29 +09:00
|
|
|
regexes += regex + '", "';
|
|
|
|
}
|
|
|
|
poolStats.pool.regexes = regexes.slice(0, -3);
|
2022-02-15 18:45:53 +09:00
|
|
|
poolStats.pool.addresses = poolStats.pool.addresses;
|
2022-02-11 19:10:29 +09:00
|
|
|
|
2022-02-11 18:29:38 +09:00
|
|
|
return Object.assign({
|
|
|
|
logo: `./resources/mining-pools/` + poolStats.pool.name.toLowerCase().replace(' ', '').replace('.', '') + '.svg'
|
|
|
|
}, poolStats);
|
|
|
|
})
|
2022-02-09 19:41:05 +09:00
|
|
|
);
|
|
|
|
|
2022-02-10 19:16:00 +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
|
|
|
|
2022-02-10 19:16:00 +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
|
|
|
}
|
|
|
|
}
|