2019-07-24 23:08:28 +03:00
|
|
|
import { Component, OnInit, OnDestroy } from '@angular/core';
|
2019-07-23 18:13:36 +03:00
|
|
|
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-07-23 18:13:36 +03:00
|
|
|
|
|
|
|
@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-07-23 18:13:36 +03:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private modalService: NgbModal,
|
2019-07-24 23:08:28 +03:00
|
|
|
private memPoolService: MemPoolService,
|
2019-07-23 18:13:36 +03:00
|
|
|
) { }
|
|
|
|
|
2019-07-24 23:08:28 +03:00
|
|
|
ngOnInit() {
|
|
|
|
this.blocksSubscription = this.memPoolService.blocks$
|
2019-07-27 18:43:17 +03:00
|
|
|
.subscribe((block) => {
|
|
|
|
this.blocks.unshift(block);
|
|
|
|
this.blocks = this.blocks.slice(0, 8);
|
|
|
|
});
|
2019-07-24 23:08:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
this.blocksSubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
2019-07-23 18:13:36 +03:00
|
|
|
getTimeSinceMined(block: IBlock): string {
|
|
|
|
const minutes = ((new Date().getTime()) - (new Date(block.time * 1000).getTime())) / 1000 / 60;
|
|
|
|
if (minutes >= 120) {
|
|
|
|
return Math.floor(minutes / 60) + ' hours';
|
|
|
|
}
|
|
|
|
if (minutes >= 60) {
|
|
|
|
return Math.floor(minutes / 60) + ' hour';
|
|
|
|
}
|
|
|
|
if (minutes <= 1) {
|
|
|
|
return '< 1 minute';
|
|
|
|
}
|
|
|
|
if (minutes === 1) {
|
|
|
|
return '1 minute';
|
|
|
|
}
|
|
|
|
return Math.round(minutes) + ' minutes';
|
|
|
|
}
|
|
|
|
|
|
|
|
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%)`,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|