Skip to content

Commit

Permalink
Massaged groups into allOperators
Browse files Browse the repository at this point in the history
  • Loading branch information
m-rgba committed Jan 10, 2025
1 parent 3b3739a commit 3723c91
Showing 1 changed file with 50 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<OperatorGroup, string> = {
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<string, string> = allOperators.reduce(
(acc, operator) => {
acc[operator.value] = operator.label;
Expand All @@ -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) {
Expand Down Expand Up @@ -184,10 +221,12 @@ export const getOperatorOptions = (field: string): SelectOperatorOption[] => {
{
value: '(string): equals',
label: 'equals',
group: 'string'
},
{
value: '(string): in',
label: 'in',
group: 'string'
},
];
}
Expand All @@ -196,10 +235,12 @@ export const getOperatorOptions = (field: string): SelectOperatorOption[] => {
{
value: '(date): after',
label: 'after',
group: 'date'
},
{
value: '(date): before',
label: 'before',
group: 'date'
},
];
}
Expand All @@ -208,10 +249,12 @@ export const getOperatorOptions = (field: string): SelectOperatorOption[] => {
{
value: 'is',
label: 'is',
group: 'boolean'
},
{
value: 'is not',
label: 'is not',
group: 'boolean'
},
];
}
Expand All @@ -220,6 +263,7 @@ export const getOperatorOptions = (field: string): SelectOperatorOption[] => {
{
value: '(string): equals',
label: 'equals',
group: 'string'
},
];
}
Expand Down Expand Up @@ -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},
];
}

0 comments on commit 3723c91

Please sign in to comment.