Support RegEx search

Summary:
Changelog: Restored the possibility to use Regex in logs search

Fixes:
https://github.com/facebook/flipper/issues/2076
https://fb.workplace.com/groups/flipperfyi/permalink/912753022824327/

Reviewed By: passy

Differential Revision: D27188241

fbshipit-source-id: 38ae2972c7dd3dd5cf24df87535d5ad74598cd88
This commit is contained in:
Michel Weststrate
2021-03-19 08:57:24 -07:00
committed by Facebook GitHub Bot
parent 2ae7d13a64
commit c648c58825
4 changed files with 133 additions and 22 deletions

View File

@@ -32,6 +32,7 @@ const emptySelection: Selection = {
type PersistedState = {
/** Active search value */
search: string;
useRegex: boolean;
/** current selection, describes the index index in the datasources's current output (not window!) */
selection: {current: number; items: number[]};
/** The currently applicable sorting, if any */
@@ -77,7 +78,8 @@ type DataManagerActions<T> =
| Action<'removeColumnFilter', {column: keyof T; index: number}>
| Action<'toggleColumnFilter', {column: keyof T; index: number}>
| Action<'setColumnFilterFromSelection', {column: keyof T}>
| Action<'appliedInitialScroll'>;
| Action<'appliedInitialScroll'>
| Action<'toggleUseRegex'>;
type DataManagerConfig<T> = {
dataSource: DataSource<T>;
@@ -96,6 +98,7 @@ type DataManagerState<T> = {
sorting: Sorting<T> | undefined;
selection: Selection;
searchValue: string;
useRegex: boolean;
};
export type DataTableReducer<T> = Reducer<
@@ -142,6 +145,10 @@ export const dataTableManagerReducer = produce(function <T>(
draft.searchValue = action.value;
break;
}
case 'toggleUseRegex': {
draft.useRegex = !draft.useRegex;
break;
}
case 'selectItem': {
const {nextIndex, addToSelection} = action;
draft.selection = computeSetSelection(
@@ -301,6 +308,7 @@ export function createInitialState<T>(
}
: emptySelection,
searchValue: prefs?.search ?? '',
useRegex: prefs?.useRegex ?? false,
};
// @ts-ignore
res.config[immerable] = false; // optimization: never proxy anything in config
@@ -363,6 +371,7 @@ export function savePreferences(
}
const prefs: PersistedState = {
search: state.searchValue,
useRegex: state.useRegex,
selection: {
current: state.selection.current,
items: Array.from(state.selection.items),
@@ -411,9 +420,11 @@ function computeInitialColumns(
export function computeDataTableFilter(
searchValue: string,
useRegex: boolean,
columns: DataTableColumn[],
) {
const searchString = searchValue.toLowerCase();
const searchRegex = useRegex ? safeCreateRegExp(searchValue) : undefined;
// the columns with an active filter are those that have filters defined,
// with at least one enabled
const filteringColumns = columns.filter((c) =>
@@ -438,11 +449,21 @@ export function computeDataTableFilter(
}
}
return Object.values(item).some((v) =>
String(v).toLowerCase().includes(searchString),
searchRegex
? searchRegex.test(String(v))
: String(v).toLowerCase().includes(searchString),
);
};
}
export function safeCreateRegExp(source: string): RegExp | undefined {
try {
return new RegExp(source);
} catch (_e) {
return undefined;
}
}
export function computeSetSelection(
base: Selection,
nextIndex: number | ((currentIndex: number) => number),