From 3723c91676b49f8136a692850bf999a86528414f Mon Sep 17 00:00:00 2001 From: Martin Mark Date: Fri, 10 Jan 2025 17:50:03 -0500 Subject: [PATCH] Massaged groups into `allOperators` --- .../Home/Browse3/filters/common.ts | 105 +++++++++--------- 1 file changed, 50 insertions(+), 55 deletions(-) diff --git a/weave-js/src/components/PagePanelComponents/Home/Browse3/filters/common.ts b/weave-js/src/components/PagePanelComponents/Home/Browse3/filters/common.ts index 46cbc6867c39..a834c566231f 100644 --- a/weave-js/src/components/PagePanelComponents/Home/Browse3/filters/common.ts +++ b/weave-js/src/components/PagePanelComponents/Home/Browse3/filters/common.ts @@ -75,64 +75,106 @@ export const getFieldType = (field: string): string => { return FIELD_TYPE[field] ?? 'text'; }; -const allOperators = [ +export type OperatorGroup = 'string' | 'number' | 'boolean' | 'date' | 'any'; +export type SelectOperatorOption = { + label: string; + value: string; + group: OperatorGroup; +}; + +const allOperators: SelectOperatorOption[] = [ { value: '(string): contains', label: 'contains', + group: 'string', }, { value: '(string): equals', label: 'equals', + group: 'string', }, { value: '(string): in', label: 'in', + group: 'string', }, { value: '(number): =', label: '=', + group: 'number', }, { value: '(number): !=', label: '≠', + group: 'number', }, { value: '(number): <', label: '<', + group: 'number', }, { value: '(number): <=', label: '≤', + group: 'number', }, { value: '(number): >', label: '>', + group: 'number', }, { value: '(number): >=', label: '≥', + group: 'number', }, { value: '(bool): is', label: 'is', + group: 'boolean', }, { value: '(date): after', label: 'after', + group: 'date', }, { value: '(date): before', label: 'before', + group: 'date', }, { value: '(any): isEmpty', label: 'is empty', + group: 'any', }, { value: '(any): isNotEmpty', label: 'is not empty', + group: 'any', }, ]; + +// Display labels +const group_labels: Record = { + string: 'Text', + number: 'Number', + boolean: 'Boolean', + date: 'Date', + any: 'Other' +}; + +export function getGroupedOperatorOptions(field: string): OperatorGroupedOption[] { + // Get operators / operator groups + const availableOperators = getOperatorOptions(field); + const groups = [...new Set(availableOperators.map(op => op.group))]; + // Create grouped options + return groups.map(group => ({ + label: group_labels[group], + options: availableOperators.filter(op => op.group === group) + })); +} + const operatorLabels: Record = allOperators.reduce( (acc, operator) => { acc[operator.value] = operator.label; @@ -151,11 +193,6 @@ export const isNumericOperator = (operator: string) => { return operator.startsWith('(number):'); }; -export type SelectOperatorOption = { - value: string; - label: string; -}; - export const getOperatorLabel = (operatorValue: string): string => { const label = operatorLabels[operatorValue]; if (label) { @@ -184,10 +221,12 @@ export const getOperatorOptions = (field: string): SelectOperatorOption[] => { { value: '(string): equals', label: 'equals', + group: 'string' }, { value: '(string): in', label: 'in', + group: 'string' }, ]; } @@ -196,10 +235,12 @@ export const getOperatorOptions = (field: string): SelectOperatorOption[] => { { value: '(date): after', label: 'after', + group: 'date' }, { value: '(date): before', label: 'before', + group: 'date' }, ]; } @@ -208,10 +249,12 @@ export const getOperatorOptions = (field: string): SelectOperatorOption[] => { { value: 'is', label: 'is', + group: 'boolean' }, { value: 'is not', label: 'is not', + group: 'boolean' }, ]; } @@ -220,6 +263,7 @@ export const getOperatorOptions = (field: string): SelectOperatorOption[] => { { value: '(string): equals', label: 'equals', + group: 'string' }, ]; } @@ -317,52 +361,3 @@ export type OperatorGroupedOption = { label: string; options: SelectOperatorOption[]; }; - -/** - * Return grouped operators by type (string, number, bool, etc.). - * Customize the group labels & operators as needed. - */ -export function getGroupedOperatorOptions( - field: string -): OperatorGroupedOption[] { - const stringOperators: SelectOperatorOption[] = [ - {value: '(string): contains', label: 'contains'}, - {value: '(string): equals', label: 'equals'}, - {value: '(string): in', label: 'in'}, - ]; - const numberOperators: SelectOperatorOption[] = [ - {value: '(number): =', label: '='}, - {value: '(number): !=', label: '≠'}, - {value: '(number): <', label: '<'}, - {value: '(number): <=', label: '≤'}, - {value: '(number): >', label: '>'}, - {value: '(number): >=', label: '≥'}, - ]; - const dateOperators: SelectOperatorOption[] = [ - {value: '(date): after', label: 'after'}, - {value: '(date): before', label: 'before'}, - ]; - const boolOperators: SelectOperatorOption[] = [ - {value: '(bool): is', label: 'is'}, - ]; - const anyOperators: SelectOperatorOption[] = [ - {value: '(any): isEmpty', label: 'is empty'}, - {value: '(any): isNotEmpty', label: 'is not empty'}, - ]; - - // Select which groups/ops to show based on the field type - const fieldType = getFieldType(field); - if (fieldType === 'datetime') { - return [{label: 'Date Operators', options: dateOperators}]; - } else if (fieldType === 'user') { - return [{label: 'String Operators', options: stringOperators}]; - } - // Fallback: show all grouped - return [ - {label: 'Text', options: stringOperators}, - {label: 'Numeric', options: numberOperators}, - {label: 'Boolean', options: boolOperators}, - {label: 'Date', options: dateOperators}, - {label: 'Other', options: anyOperators}, - ]; -}