Skip to content

Commit

Permalink
Simplify search term renderer matching
Browse files Browse the repository at this point in the history
Summary: Each search term renderer handles its "editing" state internally now. As a result, we can simplify switch-case

Reviewed By: antonk52

Differential Revision: D49454252

fbshipit-source-id: 297ce73f53772b0112ed338c6aac0583f3807ec8
  • Loading branch information
aigoncharov authored and facebook-github-bot committed Sep 20, 2023
1 parent e031032 commit f3bff73
Showing 1 changed file with 125 additions and 249 deletions.
374 changes: 125 additions & 249 deletions desktop/flipper-plugin/src/ui/PowerSearch/PowerSearchTerm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@

import {CloseOutlined} from '@ant-design/icons';
import {Button, Space} from 'antd';
import dayjs from 'dayjs';
import * as React from 'react';
import {
DATE_ONLY_FORMAT,
DATE_TIME_FORMAT,
PowerSearchAbsoluteDateTerm,
} from './PowerSearchAbsoluteDateTerm';
import {FieldConfig, OperatorConfig} from './PowerSearchConfig';
import {PowerSearchAbsoluteDateTerm} from './PowerSearchAbsoluteDateTerm';
import {OperatorConfig} from './PowerSearchConfig';
import {PowerSearchEnumSetTerm} from './PowerSearchEnumSetTerm';
import {PowerSearchEnumTerm} from './PowerSearchEnumTerm';
import {PowerSearchFloatTerm} from './PowerSearchFloatTerm';
Expand All @@ -42,250 +37,131 @@ export const PowerSearchTerm: React.FC<PowerSearchTermProps> = ({
onCancel,
onFinalize,
}) => {
const hasValue = searchTerm.searchValue != null;

let searchValueComponent: React.ReactNode = null;
if (!hasValue) {
switch (searchTerm.operator.valueType) {
case 'STRING': {
searchValueComponent = (
<PowerSearchStringTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'STRING_SET': {
searchValueComponent = (
<PowerSearchStringSetTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
/>
);
break;
}
case 'INTEGER': {
searchValueComponent = (
<PowerSearchIntegerTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'FLOAT': {
searchValueComponent = (
<PowerSearchFloatTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'NO_VALUE': {
// Nothing needs to be done. It should never be the case.
searchValueComponent = null;
break;
}
case 'ENUM': {
searchValueComponent = (
<PowerSearchEnumTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
enumLabels={searchTerm.operator.enumLabels}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'ENUM_SET': {
searchValueComponent = (
<PowerSearchEnumSetTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
enumLabels={searchTerm.operator.enumLabels}
/>
);
break;
}
case 'ABSOLUTE_DATE': {
searchValueComponent = (
<PowerSearchAbsoluteDateTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
minValue={searchTerm.operator.minValue}
maxValue={searchTerm.operator.maxValue}
dateOnly={searchTerm.operator.dateOnly}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
default: {
console.error(
'PowerSearchTerm -> unknown operator.valueType',
searchTerm,
);
}
switch (searchTerm.operator.valueType) {
case 'STRING': {
searchValueComponent = (
<PowerSearchStringTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'STRING_SET': {
searchValueComponent = (
<PowerSearchStringSetTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'INTEGER': {
searchValueComponent = (
<PowerSearchIntegerTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'FLOAT': {
searchValueComponent = (
<PowerSearchFloatTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'NO_VALUE': {
// Nothing needs to be done. It should never be the case.
searchValueComponent = null;
break;
}
case 'ENUM': {
searchValueComponent = (
<PowerSearchEnumTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
enumLabels={searchTerm.operator.enumLabels}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'ENUM_SET': {
searchValueComponent = (
<PowerSearchEnumSetTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
enumLabels={searchTerm.operator.enumLabels}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'ABSOLUTE_DATE': {
searchValueComponent = (
<PowerSearchAbsoluteDateTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
minValue={searchTerm.operator.minValue}
maxValue={searchTerm.operator.maxValue}
dateOnly={searchTerm.operator.dateOnly}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
} else {
switch (searchTerm.operator.valueType) {
case 'STRING': {
searchValueComponent = (
<PowerSearchStringTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'INTEGER': {
searchValueComponent = (
<PowerSearchIntegerTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'FLOAT': {
searchValueComponent = (
<PowerSearchFloatTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'ENUM': {
searchValueComponent = (
<PowerSearchEnumTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
enumLabels={searchTerm.operator.enumLabels}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'ENUM_SET': {
searchValueComponent = (
<PowerSearchEnumSetTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
enumLabels={searchTerm.operator.enumLabels}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'STRING_SET': {
searchValueComponent = (
<PowerSearchStringSetTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'ABSOLUTE_DATE': {
searchValueComponent = (
<PowerSearchAbsoluteDateTerm
onCancel={onCancel}
onChange={(newValue) => {
onFinalize({
...searchTerm,
searchValue: newValue,
});
}}
minValue={searchTerm.operator.minValue}
maxValue={searchTerm.operator.maxValue}
dateOnly={searchTerm.operator.dateOnly}
defaultValue={searchTerm.searchValue}
/>
);
break;
}
case 'NO_VALUE': {
searchValueComponent = null;
break;
}
default: {
searchValueComponent = <Button>{searchTerm.searchValue}</Button>;
}
default: {
// Compilation is going to fail if switch-case is not exhaustive (i.e. we did not cover all possible cases)
const exhaustiveCheck: never = searchTerm.operator;
console.error(
'PowerSearchTerm -> unknown operator.valueType',
searchTerm,
exhaustiveCheck,
);
}
}

Expand Down

0 comments on commit f3bff73

Please sign in to comment.