2022-02-06 03:31:50 +04:00
import { Component , OnInit , ViewChild } from '@angular/core' ;
2022-11-28 11:55:23 +09:00
import { UntypedFormBuilder , UntypedFormGroup , Validators } from '@angular/forms' ;
2022-02-06 03:31:50 +04:00
import { Router } from '@angular/router' ;
import { NgbTypeahead } from '@ng-bootstrap/ng-bootstrap' ;
import { merge , Observable , of , Subject } from 'rxjs' ;
import { distinctUntilChanged , filter , map , switchMap } from 'rxjs/operators' ;
2022-09-21 17:23:45 +02:00
import { AssetExtended } from '../../../interfaces/electrs.interface' ;
import { AssetsService } from '../../../services/assets.service' ;
import { SeoService } from '../../../services/seo.service' ;
import { StateService } from '../../../services/state.service' ;
import { RelativeUrlPipe } from '../../../shared/pipes/relative-url/relative-url.pipe' ;
2022-11-24 12:19:19 +09:00
import { environment } from '../../../../environments/environment' ;
2022-02-06 01:20:26 +04:00
@Component ( {
selector : 'app-assets-nav' ,
templateUrl : './assets-nav.component.html' ,
styleUrls : [ './assets-nav.component.scss' ]
} )
export class AssetsNavComponent implements OnInit {
2022-02-06 03:31:50 +04:00
@ViewChild ( 'instance' , { static : true } ) instance : NgbTypeahead ;
nativeAssetId = this . stateService . network === 'liquidtestnet' ? environment.nativeTestAssetId : environment.nativeAssetId ;
2022-11-28 11:55:23 +09:00
searchForm : UntypedFormGroup ;
2022-02-06 03:31:50 +04:00
assetsCache : AssetExtended [ ] ;
typeaheadSearchFn : ( ( text : Observable < string > ) = > Observable < readonly any [ ] > ) ;
formatterFn = ( asset : AssetExtended ) = > asset . name + ' (' + asset . ticker + ')' ;
focus $ = new Subject < string > ( ) ;
click $ = new Subject < string > ( ) ;
itemsPerPage = 15 ;
2022-02-06 01:20:26 +04:00
constructor (
2022-11-28 11:55:23 +09:00
private formBuilder : UntypedFormBuilder ,
2022-02-06 03:31:50 +04:00
private seoService : SeoService ,
private router : Router ,
private assetsService : AssetsService ,
private stateService : StateService ,
private relativeUrlPipe : RelativeUrlPipe ,
2022-02-06 01:20:26 +04:00
) { }
ngOnInit ( ) : void {
2022-02-06 04:18:56 +04:00
this . seoService . setTitle ( $localize ` :@@ee8f8008bae6ce3a49840c4e1d39b4af23d4c263:Assets ` ) ;
2023-08-30 20:26:07 +09:00
this . seoService . setDescription ( $localize ` :@@meta.description.liquid.assets:Explore all the assets issued on the Liquid network like L-BTC, L-CAD, USDT, and more. ` ) ;
2022-02-06 03:31:50 +04:00
this . typeaheadSearchFn = this . typeaheadSearch ;
2022-02-06 01:20:26 +04:00
this . searchForm = this . formBuilder . group ( {
2022-02-06 03:31:50 +04:00
searchText : [ { value : '' , disabled : false } , Validators . required ]
} ) ;
}
typeaheadSearch = ( text$ : Observable < string > ) = > {
const debouncedText $ = text $ . pipe (
distinctUntilChanged ( )
) ;
const clicksWithClosedPopup $ = this . click $ . pipe ( filter ( ( ) = > ! this . instance . isPopupOpen ( ) ) ) ;
const inputFocus $ = this . focus $ ;
return merge ( debouncedText $ , inputFocus $ , clicksWithClosedPopup $ )
. pipe (
switchMap ( ( searchText ) = > {
if ( ! searchText . length ) {
return of ( [ ] ) ;
}
return this . assetsService . getAssetsJson $ . pipe (
map ( ( assets ) = > {
if ( searchText . length ) {
2022-02-06 04:18:56 +04:00
const filteredAssets = assets . array . filter ( ( asset ) = > asset . name . toLowerCase ( ) . indexOf ( searchText . toLowerCase ( ) ) > - 1
2022-02-06 03:31:50 +04:00
|| ( asset . ticker || '' ) . toLowerCase ( ) . indexOf ( searchText . toLowerCase ( ) ) > - 1
|| ( asset . entity && asset . entity . domain || '' ) . toLowerCase ( ) . indexOf ( searchText . toLowerCase ( ) ) > - 1 ) ;
return filteredAssets . slice ( 0 , this . itemsPerPage ) ;
} else {
2022-02-06 04:18:56 +04:00
return assets . array . slice ( 0 , this . itemsPerPage ) ;
2022-02-06 03:31:50 +04:00
}
} )
)
} ) ,
) ;
}
itemSelected() {
setTimeout ( ( ) = > this . search ( ) ) ;
}
search() {
const searchText = this . searchForm . value . searchText ;
this . navigate ( '/assets/asset/' , searchText . asset_id ) ;
}
navigate ( url : string , searchText : string , extras? : any ) {
this . router . navigate ( [ this . relativeUrlPipe . transform ( url ) , searchText ] , extras ) ;
this . searchForm . setValue ( {
searchText : '' ,
2022-02-06 01:20:26 +04:00
} ) ;
}
}