2020-02-19 23:50:23 +07:00
|
|
|
import { Component, OnInit, OnDestroy, Input, OnChanges } from '@angular/core';
|
2019-07-24 23:08:28 +03:00
|
|
|
import { Subscription } from 'rxjs';
|
2020-02-16 22:15:07 +07:00
|
|
|
import { Block } from 'src/app/interfaces/electrs.interface';
|
|
|
|
import { StateService } from 'src/app/services/state.service';
|
2019-07-23 18:13:36 +03:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-blockchain-blocks',
|
|
|
|
templateUrl: './blockchain-blocks.component.html',
|
|
|
|
styleUrls: ['./blockchain-blocks.component.scss']
|
|
|
|
})
|
2020-02-19 23:50:23 +07:00
|
|
|
export class BlockchainBlocksComponent implements OnInit, OnChanges, OnDestroy {
|
|
|
|
@Input() markHeight = 0;
|
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
blocks: Block[] = [];
|
2019-07-24 23:08:28 +03:00
|
|
|
blocksSubscription: Subscription;
|
2019-11-10 16:44:00 +08:00
|
|
|
interval: any;
|
2020-02-24 22:51:27 +07:00
|
|
|
|
2020-02-24 03:42:29 +07:00
|
|
|
arrowVisible = false;
|
2020-02-24 22:51:27 +07:00
|
|
|
arrowLeftPx = 30;
|
2020-02-19 23:50:23 +07:00
|
|
|
|
2019-07-23 18:13:36 +03:00
|
|
|
constructor(
|
2020-02-16 22:15:07 +07:00
|
|
|
private stateService: StateService,
|
2019-07-23 18:13:36 +03:00
|
|
|
) { }
|
|
|
|
|
2019-07-24 23:08:28 +03:00
|
|
|
ngOnInit() {
|
2020-02-16 22:15:07 +07:00
|
|
|
this.blocksSubscription = this.stateService.blocks$
|
2019-07-27 18:43:17 +03:00
|
|
|
.subscribe((block) => {
|
2019-10-22 17:09:07 +08:00
|
|
|
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);
|
2020-02-19 23:50:23 +07:00
|
|
|
|
|
|
|
this.moveArrowToPosition();
|
2019-07-27 18:43:17 +03:00
|
|
|
});
|
2019-07-24 23:08:28 +03:00
|
|
|
}
|
|
|
|
|
2020-02-19 23:50:23 +07:00
|
|
|
ngOnChanges() {
|
|
|
|
this.moveArrowToPosition();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-19 23:50:23 +07:00
|
|
|
moveArrowToPosition() {
|
|
|
|
if (!this.markHeight) {
|
|
|
|
this.arrowVisible = false;
|
|
|
|
return;
|
|
|
|
}
|
2020-02-24 22:51:27 +07:00
|
|
|
const blockindex = this.blocks.findIndex((b) => b.height === this.markHeight);
|
|
|
|
if (blockindex !== -1) {
|
|
|
|
this.arrowVisible = true;
|
|
|
|
this.arrowLeftPx = blockindex * 155 + 30;
|
2020-02-19 23:50:23 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
trackByBlocksFn(index: number, item: Block) {
|
2019-07-23 18:13:36 +03:00
|
|
|
return item.height;
|
|
|
|
}
|
|
|
|
|
2020-02-16 22:15:07 +07:00
|
|
|
getStyleForBlock(block: Block) {
|
2019-07-23 18:13:36 +03:00
|
|
|
const greenBackgroundHeight = 100 - (block.weight / 4000000) * 100;
|
2020-03-05 16:13:46 +07:00
|
|
|
return {
|
|
|
|
left: 155 * this.blocks.indexOf(block) + 'px',
|
|
|
|
background: `repeating-linear-gradient(#2d3348, #2d3348 ${greenBackgroundHeight}%,
|
|
|
|
#9339f4 ${Math.max(greenBackgroundHeight, 0)}%, #105fb0 100%)`,
|
|
|
|
};
|
2019-07-23 18:13:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|