2022-08-11 17:19:12 +00:00
import { ChangeDetectionStrategy , Component , OnInit } from '@angular/core' ;
import { ActivatedRoute , ParamMap } from '@angular/router' ;
import { Observable } from 'rxjs' ;
import { catchError , map , switchMap } from 'rxjs/operators' ;
2022-09-21 17:23:45 +02:00
import { SeoService } from '../../services/seo.service' ;
import { OpenGraphService } from '../../services/opengraph.service' ;
import { getFlagEmoji } from '../../shared/common.utils' ;
2022-08-11 17:19:12 +00:00
import { LightningApiService } from '../lightning-api.service' ;
import { isMobile } from '../../shared/common.utils' ;
@Component ( {
selector : 'app-node-preview' ,
templateUrl : './node-preview.component.html' ,
styleUrls : [ './node-preview.component.scss' ] ,
changeDetection : ChangeDetectionStrategy.OnPush ,
} )
export class NodePreviewComponent implements OnInit {
node$ : Observable < any > ;
statistics$ : Observable < any > ;
publicKey$ : Observable < string > ;
selectedSocketIndex = 0 ;
qrCodeVisible = false ;
channelsListStatus : string ;
error : Error ;
publicKey : string ;
2022-08-11 17:42:29 +00:00
socketTypes : string [ ] ;
2022-08-11 17:19:12 +00:00
publicKeySize = 99 ;
constructor (
private lightningApiService : LightningApiService ,
private activatedRoute : ActivatedRoute ,
private seoService : SeoService ,
private openGraphService : OpenGraphService ,
) {
if ( isMobile ( ) ) {
this . publicKeySize = 12 ;
}
}
ngOnInit ( ) : void {
this . node $ = this . activatedRoute . paramMap
. pipe (
switchMap ( ( params : ParamMap ) = > {
2022-09-10 01:00:45 +00:00
this . publicKey = params . get ( 'public_key' ) ;
2022-08-31 17:24:56 +00:00
this . openGraphService . waitFor ( 'node-map-' + this . publicKey ) ;
this . openGraphService . waitFor ( 'node-data-' + this . publicKey ) ;
2022-08-11 17:19:12 +00:00
return this . lightningApiService . getNode $ ( params . get ( 'public_key' ) ) ;
} ) ,
map ( ( node ) = > {
this . seoService . setTitle ( ` Node: ${ node . alias } ` ) ;
2023-08-30 20:26:07 +09:00
this . seoService . setDescription ( $localize ` :@@meta.description.lightning.node:Overview for the Lightning network node named ${ node . alias } . See channels, capacity, location, fee stats, and more. ` ) ;
2022-08-11 17:19:12 +00:00
const socketsObject = [ ] ;
2022-08-11 17:42:29 +00:00
const socketTypesMap = { } ;
2022-08-11 17:19:12 +00:00
for ( const socket of node . sockets . split ( ',' ) ) {
if ( socket === '' ) {
continue ;
}
let label = '' ;
if ( socket . match ( /(?:[0-9]{1,3}\.){3}[0-9]{1,3}/ ) ) {
label = 'IPv4' ;
} else if ( socket . indexOf ( '[' ) > - 1 ) {
label = 'IPv6' ;
} else if ( socket . indexOf ( 'onion' ) > - 1 ) {
label = 'Tor' ;
}
node . flag = getFlagEmoji ( node . iso_code ) ;
socketsObject . push ( {
label : label ,
socket : node.public_key + '@' + socket ,
} ) ;
2022-08-11 17:42:29 +00:00
socketTypesMap [ label ] = true
2022-08-11 17:19:12 +00:00
}
node . socketsObject = socketsObject ;
2022-08-11 17:42:29 +00:00
this . socketTypes = Object . keys ( socketTypesMap ) ;
2022-08-11 17:19:12 +00:00
node . avgCapacity = node . capacity / Math . max ( 1 , node . active_channel_count ) ;
2022-08-31 17:24:56 +00:00
this . openGraphService . waitOver ( 'node-data-' + this . publicKey ) ;
2022-08-11 17:19:12 +00:00
return node ;
} ) ,
catchError ( err = > {
this . error = err ;
2023-03-09 02:34:21 -06:00
this . seoService . logSoft404 ( ) ;
2022-08-31 17:24:56 +00:00
this . openGraphService . fail ( 'node-map-' + this . publicKey ) ;
this . openGraphService . fail ( 'node-data-' + this . publicKey ) ;
2022-08-11 17:19:12 +00:00
return [ {
alias : this.publicKey ,
public_key : this.publicKey ,
} ] ;
} )
) ;
}
changeSocket ( index : number ) {
this . selectedSocketIndex = index ;
}
onChannelsListStatusChanged ( e ) {
this . channelsListStatus = e ;
}
onMapReady() {
2022-08-31 17:24:56 +00:00
this . openGraphService . waitOver ( 'node-map-' + this . publicKey ) ;
2022-08-11 17:19:12 +00:00
}
}