Add mining pools to search bar

This commit is contained in:
natsoni
2024-05-30 09:31:44 +02:00
parent 9186f664da
commit 7761ea53c6
5 changed files with 55 additions and 5 deletions

View File

@@ -22,6 +22,7 @@ export class SearchFormComponent implements OnInit {
env: Env;
network = '';
assets: object = {};
pools: object[] = [];
isSearching = false;
isTypeaheading$ = new BehaviorSubject<boolean>(false);
typeAhead$: Observable<any>;
@@ -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<any> {
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([]))
);
}
}