2019-11-06 15:35:02 +08:00
|
|
|
import { Component, OnInit } from '@angular/core';
|
2019-11-12 16:39:59 +08:00
|
|
|
import { ActivatedRoute, ParamMap } from '@angular/router';
|
2020-02-16 22:15:07 +07:00
|
|
|
import { ElectrsApiService } from '../../services/electrs-api.service';
|
2019-11-12 16:39:59 +08:00
|
|
|
import { switchMap } from 'rxjs/operators';
|
2020-02-16 22:15:07 +07:00
|
|
|
import { Block, Transaction } from '../../interfaces/electrs.interface';
|
|
|
|
import { of } from 'rxjs';
|
|
|
|
import { StateService } from '../../services/state.service';
|
2020-02-17 20:39:20 +07:00
|
|
|
import { WebsocketService } from 'src/app/services/websocket.service';
|
2019-11-06 15:35:02 +08:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-block',
|
|
|
|
templateUrl: './block.component.html',
|
|
|
|
styleUrls: ['./block.component.scss']
|
|
|
|
})
|
|
|
|
export class BlockComponent implements OnInit {
|
2020-02-16 22:15:07 +07:00
|
|
|
block: Block;
|
|
|
|
blockHeight: number;
|
|
|
|
blockHash: string;
|
2019-11-12 16:39:59 +08:00
|
|
|
isLoadingBlock = true;
|
2020-02-16 22:15:07 +07:00
|
|
|
latestBlock: Block;
|
|
|
|
transactions: Transaction[];
|
2019-11-12 16:39:59 +08:00
|
|
|
isLoadingTransactions = true;
|
2019-11-13 14:51:44 +08:00
|
|
|
error: any;
|
2019-11-06 15:35:02 +08:00
|
|
|
|
2019-11-12 16:39:59 +08:00
|
|
|
constructor(
|
|
|
|
private route: ActivatedRoute,
|
2020-02-16 22:15:07 +07:00
|
|
|
private electrsApiService: ElectrsApiService,
|
|
|
|
private stateService: StateService,
|
2020-02-17 20:39:20 +07:00
|
|
|
private websocketService: WebsocketService,
|
2019-11-12 16:39:59 +08:00
|
|
|
) { }
|
2019-11-06 15:35:02 +08:00
|
|
|
|
|
|
|
ngOnInit() {
|
2020-02-17 20:39:20 +07:00
|
|
|
this.websocketService.want(['blocks', 'mempool-blocks']);
|
|
|
|
|
2019-11-12 16:39:59 +08:00
|
|
|
this.route.paramMap.pipe(
|
|
|
|
switchMap((params: ParamMap) => {
|
|
|
|
const blockHash: string = params.get('id') || '';
|
2020-02-16 22:15:07 +07:00
|
|
|
this.error = undefined;
|
|
|
|
|
|
|
|
if (history.state.data && history.state.data.blockHeight) {
|
|
|
|
this.blockHeight = history.state.data.blockHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.blockHash = blockHash;
|
|
|
|
|
|
|
|
if (history.state.data && history.state.data.block) {
|
2020-02-17 20:39:20 +07:00
|
|
|
this.blockHeight = history.state.data.block.height;
|
2020-02-16 22:15:07 +07:00
|
|
|
return of(history.state.data.block);
|
|
|
|
} else {
|
|
|
|
this.isLoadingBlock = true;
|
|
|
|
return this.electrsApiService.getBlock$(blockHash);
|
|
|
|
}
|
2019-11-12 16:39:59 +08:00
|
|
|
})
|
|
|
|
)
|
2020-02-16 22:15:07 +07:00
|
|
|
.subscribe((block: Block) => {
|
2019-11-12 16:39:59 +08:00
|
|
|
this.block = block;
|
2020-02-16 22:15:07 +07:00
|
|
|
this.blockHeight = block.height;
|
2019-11-12 16:39:59 +08:00
|
|
|
this.isLoadingBlock = false;
|
2019-11-13 14:51:44 +08:00
|
|
|
this.getBlockTransactions(block.id);
|
2020-02-23 05:23:24 +07:00
|
|
|
document.body.scrollTo({ top: 0, behavior: 'smooth' });
|
2019-11-13 14:51:44 +08:00
|
|
|
},
|
|
|
|
(error) => {
|
|
|
|
this.error = error;
|
|
|
|
this.isLoadingBlock = false;
|
2019-11-12 16:39:59 +08:00
|
|
|
});
|
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
this.stateService.blocks$
|
|
|
|
.subscribe((block) => this.latestBlock = block);
|
2019-11-12 16:39:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
getBlockTransactions(hash: string) {
|
2020-02-16 22:15:07 +07:00
|
|
|
this.electrsApiService.getBlockTransactions$(hash)
|
2019-11-12 16:39:59 +08:00
|
|
|
.subscribe((transactions: any) => {
|
|
|
|
this.transactions = transactions;
|
|
|
|
this.isLoadingTransactions = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
loadMore() {
|
|
|
|
this.isLoadingTransactions = true;
|
2020-02-16 22:15:07 +07:00
|
|
|
this.electrsApiService.getBlockTransactions$(this.block.id, this.transactions.length)
|
2019-11-12 16:39:59 +08:00
|
|
|
.subscribe((transactions) => {
|
|
|
|
this.transactions = this.transactions.concat(transactions);
|
|
|
|
this.isLoadingTransactions = false;
|
|
|
|
});
|
2019-11-06 15:35:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|