From 7761ea53c6ef1992d0990fca47c2ccdcd0b1d187 Mon Sep 17 00:00:00 2001 From: natsoni Date: Thu, 30 May 2024 09:31:44 +0200 Subject: [PATCH 1/5] Add mining pools to search bar --- .../search-form/search-form.component.ts | 41 ++++++++++++++++++- .../search-results.component.html | 10 ++++- .../search-results.component.scss | 4 ++ .../search-results.component.ts | 2 +- frontend/src/app/services/api.service.ts | 3 +- 5 files changed, 55 insertions(+), 5 deletions(-) diff --git a/frontend/src/app/components/search-form/search-form.component.ts b/frontend/src/app/components/search-form/search-form.component.ts index 605dca962..effd92499 100644 --- a/frontend/src/app/components/search-form/search-form.component.ts +++ b/frontend/src/app/components/search-form/search-form.component.ts @@ -22,6 +22,7 @@ export class SearchFormComponent implements OnInit { env: Env; network = ''; assets: object = {}; + pools: object[] = []; isSearching = false; isTypeaheading$ = new BehaviorSubject(false); typeAhead$: Observable; @@ -118,7 +119,8 @@ export class SearchFormComponent implements OnInit { if (!text.length) { return of([ [], - { nodes: [], channels: [] } + { nodes: [], channels: [] }, + this.pools ]); } this.isTypeaheading$.next(true); @@ -126,6 +128,7 @@ export class SearchFormComponent implements OnInit { return zip( this.electrsApiService.getAddressesByPrefix$(text).pipe(catchError(() => of([]))), [{ nodes: [], channels: [] }], + this.getMiningPools() ); } return zip( @@ -134,6 +137,7 @@ export class SearchFormComponent implements OnInit { nodes: [], channels: [], }))), + this.getMiningPools() ); }), map((result: any[]) => { @@ -153,11 +157,14 @@ export class SearchFormComponent implements OnInit { { nodes: [], channels: [], - } + }, + this.pools ])) ] ).pipe( map((latestData) => { + this.pools = latestData[1][2] || []; + let searchText = latestData[0]; if (!searchText.length) { return { @@ -171,6 +178,7 @@ export class SearchFormComponent implements OnInit { nodes: [], channels: [], liquidAsset: [], + pools: [] }; } @@ -189,6 +197,7 @@ export class SearchFormComponent implements OnInit { const matchesAddress = !matchesTxId && this.regexAddress.test(searchText); const otherNetworks = findOtherNetworks(searchText, this.network as any || 'mainnet', this.env); const liquidAsset = this.assets ? (this.assets[searchText] || []) : []; + const pools = this.pools.filter(pool => pool["name"].toLowerCase().startsWith(searchText.toLowerCase())).slice(0, 10); if (matchesDateTime && searchText.indexOf('/') !== -1) { searchText = searchText.replace(/\//g, '-'); @@ -208,6 +217,7 @@ export class SearchFormComponent implements OnInit { nodes: lightningResults.nodes, channels: lightningResults.channels, liquidAsset: liquidAsset, + pools: pools }; }) ); @@ -239,6 +249,8 @@ export class SearchFormComponent implements OnInit { }); this.isSearching = false; } + } else if (result.slug) { + this.navigate('/mining/pool/', result.slug); } } @@ -304,4 +316,29 @@ export class SearchFormComponent implements OnInit { this.isSearching = false; } } + + getMiningPools(): Observable { + return this.pools.length ? of(this.pools) : combineLatest([ + this.apiService.listPools$(undefined), + this.apiService.listPools$('1y') + ]).pipe( + map(([poolsResponse, activePoolsResponse]) => { + const activePoolSlugs = new Set(activePoolsResponse.body.pools.map(pool => pool.slug)); + + return poolsResponse.body.map(pool => ({ + name: pool.name, + slug: pool.slug, + active: activePoolSlugs.has(pool.slug) + })) + // Sort: active pools first, then alphabetically + .sort((a, b) => { + if (a.active && !b.active) return -1; + if (!a.active && b.active) return 1; + return a.slug < b.slug ? -1 : 1; + }); + + }), + catchError(() => of([])) + ); + } } diff --git a/frontend/src/app/components/search-form/search-results/search-results.component.html b/frontend/src/app/components/search-form/search-results/search-results.component.html index f447d3837..35ce2a0a7 100644 --- a/frontend/src/app/components/search-form/search-results/search-results.component.html +++ b/frontend/src/app/components/search-form/search-results/search-results.component.html @@ -1,4 +1,4 @@ -