From 010de365fa0b6f66f04690e898357bd4d1bd12d9 Mon Sep 17 00:00:00 2001 From: Andrey Goncharov Date: Mon, 2 Oct 2023 08:27:37 -0700 Subject: [PATCH] Allow using whole row for power search Reviewed By: lblasa Differential Revision: D49827828 fbshipit-source-id: 93219146ba59074be305eb9a963550223a5abd18 --- .../src/ui/PowerSearch/PowerSearchConfig.tsx | 1 + .../src/ui/PowerSearch/PowerSearchTerm.tsx | 2 +- .../data-table/DataTableWithPowerSearch.tsx | 37 +++++++++++++------ .../DataTableWithPowerSearchManager.tsx | 4 +- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchConfig.tsx b/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchConfig.tsx index 1455aa35e..f2775d0b1 100644 --- a/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchConfig.tsx +++ b/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchConfig.tsx @@ -84,6 +84,7 @@ export type FieldConfig = { key: string; label: string; operators: {[key: string]: OperatorConfig}; + useWholeRow?: boolean; }; export type PowerSearchConfig = { diff --git a/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchTerm.tsx b/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchTerm.tsx index d2a7404cc..bef2230bf 100644 --- a/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchTerm.tsx +++ b/desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchTerm.tsx @@ -20,7 +20,7 @@ import {PowerSearchStringSetTerm} from './PowerSearchStringSetTerm'; import {PowerSearchStringTerm} from './PowerSearchStringTerm'; export type IncompleteSearchExpressionTerm = { - field: {key: string; label: string}; + field: {key: string; label: string; useWholeRow?: boolean}; operator: OperatorConfig; searchValue?: any; }; diff --git a/desktop/flipper-plugin/src/ui/data-table/DataTableWithPowerSearch.tsx b/desktop/flipper-plugin/src/ui/data-table/DataTableWithPowerSearch.tsx index 51129d7fb..8788b3cce 100644 --- a/desktop/flipper-plugin/src/ui/data-table/DataTableWithPowerSearch.tsx +++ b/desktop/flipper-plugin/src/ui/data-table/DataTableWithPowerSearch.tsx @@ -132,7 +132,10 @@ export type DataTableColumn = { visible?: boolean; inversed?: boolean; sortable?: boolean; - powerSearchConfig?: OperatorConfig[] | false; + powerSearchConfig?: + | OperatorConfig[] + | false + | {operators: OperatorConfig[]; useWholeRow?: boolean}; }; export interface TableRowRenderContext { @@ -248,22 +251,32 @@ export function DataTable( if (column.powerSearchConfig === false) { continue; } + + let useWholeRow = false; + let columnPowerSearchOperators: OperatorConfig[]; + // If no power search config provided we treat every input as a string + if (!column.powerSearchConfig) { + columnPowerSearchOperators = [ + dataTablePowerSearchOperators.string_contains(true), + dataTablePowerSearchOperators.string_not_contains(true), + dataTablePowerSearchOperators.string_matches_exactly(true), + dataTablePowerSearchOperators.string_not_matches_exactly(true), + ]; + } else if (Array.isArray(column.powerSearchConfig)) { + columnPowerSearchOperators = column.powerSearchConfig; + } else { + columnPowerSearchOperators = column.powerSearchConfig.operators; + useWholeRow = true; + } + const columnFieldConfig: FieldConfig = { label: column.title || column.key, key: column.key, - // If no power search config provided we treat every input as a string - operators: column.powerSearchConfig?.reduce((res, operatorConfig) => { + operators: columnPowerSearchOperators.reduce((res, operatorConfig) => { res[operatorConfig.key] = operatorConfig; return res; - }, {} as Record) ?? { - string_contains: dataTablePowerSearchOperators.string_contains(true), - string_not_contains: - dataTablePowerSearchOperators.string_not_contains(true), - string_matches_exactly: - dataTablePowerSearchOperators.string_matches_exactly(true), - string_not_matches_exactly: - dataTablePowerSearchOperators.string_not_matches_exactly(true), - }, + }, {} as Record), + useWholeRow, }; res.fields[column.key] = columnFieldConfig; } diff --git a/desktop/flipper-plugin/src/ui/data-table/DataTableWithPowerSearchManager.tsx b/desktop/flipper-plugin/src/ui/data-table/DataTableWithPowerSearchManager.tsx index f65a9ad65..76421bf86 100644 --- a/desktop/flipper-plugin/src/ui/data-table/DataTableWithPowerSearchManager.tsx +++ b/desktop/flipper-plugin/src/ui/data-table/DataTableWithPowerSearchManager.tsx @@ -480,7 +480,9 @@ export function computeDataTableFilter( return true; } return searchExpression.every((searchTerm) => { - const value = getValueAtPath(item, searchTerm.field.key); + const value = searchTerm.field.useWholeRow + ? item + : getValueAtPath(item, searchTerm.field.key); if (!value) { console.warn( 'computeDataTableFilter -> value at searchTerm.field.key is not recognized',