mempool/frontend/src/app/components/clock/clock.component.ts

106 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-04-19 03:34:13 +09:00
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, HostListener, Input, OnInit } from '@angular/core';
2023-04-19 09:41:53 +09:00
import { Observable, Subscription } from 'rxjs';
2023-04-19 03:34:13 +09:00
import { StateService } from '../../services/state.service';
import { BlockExtended } from '../../interfaces/node-api.interface';
import { WebsocketService } from '../../services/websocket.service';
2023-04-19 09:41:53 +09:00
import { MempoolInfo, Recommendedfees } from '../../interfaces/websocket.interface';
2023-04-20 00:30:55 +09:00
import { ActivatedRoute } from '@angular/router';
2023-04-19 03:34:13 +09:00
@Component({
selector: 'app-clock',
templateUrl: './clock.component.html',
styleUrls: ['./clock.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ClockComponent implements OnInit {
2023-04-19 09:41:53 +09:00
@Input() mode: 'block' | 'mempool' = 'block';
2023-04-20 00:30:55 +09:00
hideStats: boolean = false;
2023-04-19 03:34:13 +09:00
blocksSubscription: Subscription;
2023-04-19 09:41:53 +09:00
recommendedFees$: Observable<Recommendedfees>;
mempoolInfo$: Observable<MempoolInfo>;
2023-04-19 03:34:13 +09:00
block: BlockExtended;
clockSize: number = 300;
chainWidth: number = 384;
chainHeight: number = 60;
blockStyle;
blockSizerStyle;
wrapperStyle;
2023-04-20 05:30:24 +09:00
limitWidth: number;
limitHeight: number;
2023-04-19 03:34:13 +09:00
gradientColors = {
'': ['#9339f4', '#105fb0'],
bisq: ['#9339f4', '#105fb0'],
liquid: ['#116761', '#183550'],
'liquidtestnet': ['#494a4a', '#272e46'],
testnet: ['#1d486f', '#183550'],
signet: ['#6f1d5d', '#471850'],
};
constructor(
public stateService: StateService,
private websocketService: WebsocketService,
2023-04-20 00:30:55 +09:00
private route: ActivatedRoute,
2023-04-19 03:34:13 +09:00
private cd: ChangeDetectorRef,
2023-04-20 05:30:24 +09:00
) {
this.route.queryParams.subscribe((params) => {
this.hideStats = params && params.stats === 'false';
this.limitWidth = Number.parseInt(params.width) || null;
this.limitHeight = Number.parseInt(params.height) || null;
});
}
2023-04-19 03:34:13 +09:00
ngOnInit(): void {
this.resizeCanvas();
this.websocketService.want(['blocks', 'stats', 'mempool-blocks']);
2023-04-20 00:30:55 +09:00
2023-04-19 03:34:13 +09:00
this.blocksSubscription = this.stateService.blocks$
.subscribe(([block]) => {
if (block) {
this.block = block;
this.blockStyle = this.getStyleForBlock(this.block);
this.cd.markForCheck();
}
});
2023-04-20 00:30:55 +09:00
2023-04-19 09:41:53 +09:00
this.recommendedFees$ = this.stateService.recommendedFees$;
this.mempoolInfo$ = this.stateService.mempoolInfo$;
2023-04-19 03:34:13 +09:00
}
getStyleForBlock(block: BlockExtended) {
const greenBackgroundHeight = 100 - (block.weight / this.stateService.env.BLOCK_WEIGHT_UNITS) * 100;
return {
background: `repeating-linear-gradient(
#2d3348,
#2d3348 ${greenBackgroundHeight}%,
${this.gradientColors[''][0]} ${Math.max(greenBackgroundHeight, 0)}%,
${this.gradientColors[''][1]} 100%
)`,
};
}
@HostListener('window:resize', ['$event'])
resizeCanvas(): void {
2023-04-20 05:30:24 +09:00
const windowWidth = this.limitWidth || window.innerWidth;
const windowHeight = this.limitHeight || window.innerHeight;
this.chainWidth = windowWidth;
this.chainHeight = Math.max(60, windowHeight / 8);
this.clockSize = Math.min(800, windowWidth, windowHeight - (1.4 * this.chainHeight));
2023-04-19 03:34:13 +09:00
const size = Math.ceil(this.clockSize / 75) * 75;
const margin = (this.clockSize - size) / 2;
this.blockSizerStyle = {
transform: `translate(${margin}px, ${margin}px)`,
width: `${size}px`,
height: `${size}px`,
};
this.wrapperStyle = {
2023-04-19 09:41:53 +09:00
'--clock-width': `${this.clockSize}px`,
2023-04-20 05:30:24 +09:00
'--chain-height': `${this.chainHeight}px`,
'width': this.limitWidth ? `${this.limitWidth}px` : undefined,
'height': this.limitHeight ? `${this.limitHeight}px` : undefined,
2023-04-19 03:34:13 +09:00
};
this.cd.markForCheck();
}
}