Skip to content

Commit

Permalink
Loki: Fix text search in Label browser (grafana#32293) (grafana#32306)
Browse files Browse the repository at this point in the history
* Switch to simple search before implementing proper fuzzy search

* Just do simple search

* Finalize simple search

* Pass string to highlights

(cherry picked from commit a127e09)

Co-authored-by: Ivana Huckova <[email protected]>
  • Loading branch information
grafanabot and ivanahuckova authored Mar 25, 2021
1 parent f593179 commit c330762
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
5 changes: 3 additions & 2 deletions public/app/plugins/datasource/loki/components/LokiLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface Props extends Omit<HTMLAttributes<HTMLElement>, 'onClick'> {
name: string;
active?: boolean;
loading?: boolean;
searchTerm?: RegExp;
searchTerm?: string;
value?: string;
facets?: number;
onClick?: OnLabelClick;
Expand All @@ -24,6 +24,7 @@ export const LokiLabel = forwardRef<HTMLElement, Props>(
({ name, value, hidden, facets, onClick, className, loading, searchTerm, active, style, ...rest }, ref) => {
const theme = useTheme();
const styles = getLabelStyles(theme);
const searchWords = searchTerm ? [searchTerm] : [];

const onLabelClick = (event: React.MouseEvent<HTMLElement>) => {
if (onClick && !hidden) {
Expand Down Expand Up @@ -55,7 +56,7 @@ export const LokiLabel = forwardRef<HTMLElement, Props>(
)}
{...rest}
>
<Highlighter textToHighlight={text} searchWords={[searchTerm]} highlightClassName={styles.matchHighLight} />
<Highlighter textToHighlight={text} searchWords={searchWords} highlightClassName={styles.matchHighLight} />
</span>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,17 +364,13 @@ export class UnthemedLokiLabelBrowser extends React.Component<BrowserProps, Brow
return <LoadingPlaceholder text="Loading labels..." />;
}
const styles = getStyles(theme);
let matcher: RegExp;
let selectedLabels = labels.filter((label) => label.selected && label.values);
if (searchTerm) {
// TODO extract from render() and debounce
try {
matcher = new RegExp(searchTerm.split('').join('.*'), 'i');
selectedLabels = selectedLabels.map((label) => ({
...label,
values: label.values?.filter((value) => value.selected || matcher.test(value.name)),
}));
} catch (error) {}
selectedLabels = selectedLabels.map((label) => ({
...label,
values: label.values?.filter((value) => value.selected || value.name.includes(searchTerm)),
}));
}
const selector = buildSelector(this.state.labels);
const empty = selector === EMPTY_SELECTOR;
Expand Down Expand Up @@ -439,7 +435,7 @@ export class UnthemedLokiLabelBrowser extends React.Component<BrowserProps, Brow
value={value?.name}
active={value?.selected}
onClick={this.onClickValue}
searchTerm={matcher}
searchTerm={searchTerm}
/>
</div>
);
Expand Down

0 comments on commit c330762

Please sign in to comment.