Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Enable search history by default for Search Box #2566

Merged
merged 13 commits into from
Jan 4, 2024
Merged
32 changes: 23 additions & 9 deletions projects/components/src/search-box/search-box.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
TypedSimpleChanges,
} from '@hypertrace/common';
import { combineLatest, Observable, Subject, timer } from 'rxjs';
import { debounce, map } from 'rxjs/operators';
import { debounce, debounceTime, map } from 'rxjs/operators';
import { IconSize } from '../icon/icon-size';
import { PopoverService } from '../popover/popover.service';
import { PopoverRef } from '../popover/popover-ref';
Expand Down Expand Up @@ -111,11 +111,14 @@ export class SearchBoxComponent implements OnInit, OnChanges {
public searchMode: SearchBoxEmitMode = SearchBoxEmitMode.Incremental;

@Input()
public enableSearchHistory: boolean = false; // Experimental
public enableSearchHistory: boolean = true;

@Input()
public collapsable: boolean = false;

@Input()
public searchHistoryDebounceTime: number | undefined;
aneeshsharma marked this conversation as resolved.
Show resolved Hide resolved

@Output()
public readonly valueChange: EventEmitter<string> = new EventEmitter();

Expand Down Expand Up @@ -222,7 +225,7 @@ export class SearchBoxComponent implements OnInit, OnChanges {
this.subscriptionLifecycle.unsubscribe();
this.subscriptionLifecycle.add(
combineLatest([this.debouncedValueSubject, this.getDebounceTime()])
.pipe(debounce(([_, debounceTime]) => timer(debounceTime)))
.pipe(debounce(([_, valueDebounceTime]) => timer(valueDebounceTime)))
.subscribe(([value, _]) => this.valueChange.emit(value)),
);
}
Expand Down Expand Up @@ -261,11 +264,22 @@ export class SearchBoxComponent implements OnInit, OnChanges {
);

this.subscriptionLifecycle.add(
this.valueChange.asObservable().subscribe(emittedValue => {
if (!isEmpty(emittedValue)) {
this.lastEmittedValues = [emittedValue, ...this.lastEmittedValues];
}
}),
this.debounceTime && this.debounceTime > 0 ?
this.valueChange
aneeshsharma marked this conversation as resolved.
Show resolved Hide resolved
.asObservable()
.subscribe(emittedValue => {
if (!isEmpty(emittedValue)) {
this.lastEmittedValues = [emittedValue, ...this.lastEmittedValues];
}
}) :
this.valueChange
.asObservable()
.pipe(debounceTime(400))
.subscribe(emittedValue => {
if (!isEmpty(emittedValue)) {
this.lastEmittedValues = [emittedValue, ...this.lastEmittedValues];
}
}),
);
}

Expand Down Expand Up @@ -296,7 +310,7 @@ export class SearchBoxComponent implements OnInit, OnChanges {
}

private handleSearchHistoryOnInputBlur(): void {
this.searchHistory = [...uniq(this.lastEmittedValues), ...this.searchHistory];
this.searchHistory = uniq([...this.lastEmittedValues, ...this.searchHistory]);
this.filteredSearchHistory = [...this.searchHistory];
this.lastEmittedValues = [];
}
Expand Down
Loading