mempool/frontend/src/app/blockchain-blocks/blockchain-blocks.component.ts

71 lines
2.2 KiB
TypeScript
Raw Normal View History

2019-07-24 23:08:28 +03:00
import { Component, OnInit, OnDestroy } from '@angular/core';
import { IBlock } from '../blockchain/interfaces';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { BlockModalComponent } from './block-modal/block-modal.component';
2019-07-24 23:08:28 +03:00
import { MemPoolService } from '../services/mem-pool.service';
import { Subscription } from 'rxjs';
2019-11-10 16:44:00 +08:00
import { environment } from '../../environments/environment';
@Component({
selector: 'app-blockchain-blocks',
templateUrl: './blockchain-blocks.component.html',
styleUrls: ['./blockchain-blocks.component.scss']
})
2019-07-24 23:08:28 +03:00
export class BlockchainBlocksComponent implements OnInit, OnDestroy {
blocks: IBlock[] = [];
blocksSubscription: Subscription;
2019-11-10 16:44:00 +08:00
interval: any;
trigger = 0;
isEsploraEnabled = !!environment.esplora;
constructor(
private modalService: NgbModal,
2019-07-24 23:08:28 +03:00
private memPoolService: MemPoolService,
) { }
2019-07-24 23:08:28 +03:00
ngOnInit() {
this.blocksSubscription = this.memPoolService.blocks$
2019-07-27 18:43:17 +03:00
.subscribe((block) => {
if (this.blocks.some((b) => b.height === block.height)) {
return;
}
2019-07-27 18:43:17 +03:00
this.blocks.unshift(block);
this.blocks = this.blocks.slice(0, 8);
});
2019-11-10 16:44:00 +08:00
this.interval = setInterval(() => this.trigger++, 10 * 1000);
2019-07-24 23:08:28 +03:00
}
ngOnDestroy() {
this.blocksSubscription.unsubscribe();
2019-11-10 16:44:00 +08:00
clearInterval(this.interval);
2019-07-24 23:08:28 +03:00
}
trackByBlocksFn(index: number, item: IBlock) {
return item.height;
}
openBlockModal(block: IBlock) {
const modalRef = this.modalService.open(BlockModalComponent, { size: 'lg' });
modalRef.componentInstance.block = block;
}
getStyleForBlock(block: IBlock) {
const greenBackgroundHeight = 100 - (block.weight / 4000000) * 100;
if (window.innerWidth <= 768) {
return {
'top': 155 * this.blocks.indexOf(block) + 'px',
'background': `repeating-linear-gradient(#2d3348, #2d3348 ${greenBackgroundHeight}%,
#9339f4 ${Math.max(greenBackgroundHeight, 0)}%, #105fb0 100%)`,
};
} else {
return {
'left': 155 * this.blocks.indexOf(block) + 'px',
'background': `repeating-linear-gradient(#2d3348, #2d3348 ${greenBackgroundHeight}%,
#9339f4 ${Math.max(greenBackgroundHeight, 0)}%, #105fb0 100%)`,
};
}
}
}