From c1a79e3a332d49154b254917c6df4c7940a7f7f4 Mon Sep 17 00:00:00 2001 From: softsimon Date: Thu, 13 May 2021 03:01:47 +0400 Subject: [PATCH] Handle the 'B' prefix in the search bar autocomplete on /bisq refs #510 --- .../search-form/search-form.component.html | 2 +- .../search-form/search-form.component.ts | 51 +++++++++++++------ 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/frontend/src/app/components/search-form/search-form.component.html b/frontend/src/app/components/search-form/search-form.component.html index a4ad5d9d4..f3c70d40b 100644 --- a/frontend/src/app/components/search-form/search-form.component.html +++ b/frontend/src/app/components/search-form/search-form.component.html @@ -1,7 +1,7 @@
- +
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 c458d5a00..0d3fc2180 100644 --- a/frontend/src/app/components/search-form/search-form.component.ts +++ b/frontend/src/app/components/search-form/search-form.component.ts @@ -4,7 +4,7 @@ import { Router } from '@angular/router'; import { AssetsService } from 'src/app/services/assets.service'; import { StateService } from 'src/app/services/state.service'; import { Observable, of, Subject, merge } from 'rxjs'; -import { debounceTime, distinctUntilChanged, switchMap, filter, catchError } from 'rxjs/operators'; +import { debounceTime, distinctUntilChanged, switchMap, filter, catchError, map } from 'rxjs/operators'; import { ElectrsApiService } from 'src/app/services/electrs-api.service'; import { NgbTypeahead } from '@ng-bootstrap/ng-bootstrap'; @@ -18,6 +18,7 @@ export class SearchFormComponent implements OnInit { network = ''; assets: object = {}; isSearching = false; + typeaheadSearchFn: ((text: Observable) => Observable); searchForm: FormGroup; @Output() searchTriggered = new EventEmitter(); @@ -31,21 +32,6 @@ export class SearchFormComponent implements OnInit { focus$ = new Subject(); click$ = new Subject(); - typeaheadSearch = (text$: Observable) => { - const debouncedText$ = text$.pipe(debounceTime(200), distinctUntilChanged()); - const clicksWithClosedPopup$ = this.click$.pipe(filter(() => !this.instance.isPopupOpen())); - const inputFocus$ = this.focus$; - - return merge(debouncedText$, inputFocus$, clicksWithClosedPopup$) - .pipe( - switchMap((text) => { - if (!text.length) { return of([]); } - return this.electrsApiService.getAddressesByPrefix$(text) - .pipe(catchError(() => of([]))); - }) - ); - } - constructor( private formBuilder: FormBuilder, private router: Router, @@ -55,11 +41,13 @@ export class SearchFormComponent implements OnInit { ) { } ngOnInit() { + this.typeaheadSearchFn = this.typeaheadSearch; this.stateService.networkChanged$.subscribe((network) => this.network = network); this.searchForm = this.formBuilder.group({ searchText: ['', Validators.required], }); + if (this.network === 'liquid') { this.assetsService.getAssetsMinimalJson$ .subscribe((assets) => { @@ -68,6 +56,37 @@ export class SearchFormComponent implements OnInit { } } + typeaheadSearch = (text$: Observable) => { + const debouncedText$ = text$.pipe( + map((text) => { + if (this.network === 'bisq' && text.match(/^(b)[^c]/i)) { + return text.substr(1); + } + return text; + }), + debounceTime(200), + distinctUntilChanged() + ); + const clicksWithClosedPopup$ = this.click$.pipe(filter(() => !this.instance.isPopupOpen())); + const inputFocus$ = this.focus$; + + return merge(debouncedText$, inputFocus$, clicksWithClosedPopup$) + .pipe( + switchMap((text) => { + if (!text.length) { + return of([]); + } + return this.electrsApiService.getAddressesByPrefix$(text).pipe(catchError(() => of([]))); + }), + map((result: string[]) => { + if (this.network === 'bisq') { + return result.map((address: string) => 'B' + address); + } + return result; + }) + ); + } + itemSelected() { setTimeout(() => this.search()); }