|
| 1 | +import React, { useRef } from 'react'; |
| 2 | + |
| 3 | +import { useDispatch } from '@/hooks/useStatelessReducer'; |
| 4 | +import { IconButton } from '../../IconButton'; |
| 5 | +import { useQuery } from '../ElasticsearchQueryContext'; |
| 6 | +import { QueryEditorRow } from '../QueryEditorRow'; |
| 7 | + |
| 8 | +import { QueryFilter } from '@/types'; |
| 9 | +import { InlineSegmentGroup, Input, Segment, SegmentAsync, Tooltip } from '@grafana/ui'; |
| 10 | +import { |
| 11 | + addFilter, |
| 12 | + removeFilter, |
| 13 | + toggleFilterVisibility, |
| 14 | + changeFilterField, |
| 15 | + changeFilterOperation, |
| 16 | + changeFilterValue, |
| 17 | +} from '@/components/QueryEditor/FilterEditor/state/actions'; |
| 18 | +import { segmentStyles } from '@/components/QueryEditor/styles'; |
| 19 | +import { useFields } from '@/hooks/useFields'; |
| 20 | +import { newFilterId } from '@/utils/uid'; |
| 21 | +import { filterOperations } from '@/queryDef'; |
| 22 | +import { hasWhiteSpace, isSet } from '@/utils'; |
| 23 | + |
| 24 | +interface FilterEditorProps { |
| 25 | + onSubmit: () => void; |
| 26 | +} |
| 27 | + |
| 28 | +function filterErrors(filter: QueryFilter): string[] { |
| 29 | + const errors: string[] = []; |
| 30 | + |
| 31 | + if (!isSet(filter.filter.key)) { |
| 32 | + errors.push('Field is not set'); |
| 33 | + } |
| 34 | + |
| 35 | + if (!isSet(filter.filter.operator)) { |
| 36 | + errors.push('Operator is not set'); |
| 37 | + } |
| 38 | + |
| 39 | + if (!['exists', 'not exists'].includes(filter.filter.operator) && !isSet(filter.filter.value)) { |
| 40 | + errors.push('Value is not set'); |
| 41 | + } |
| 42 | + |
| 43 | + if (['term', 'not term'].includes(filter.filter.operator) && filter.filter.value && hasWhiteSpace(filter.filter.value)) { |
| 44 | + errors.push('Term cannot have whitespace in value'); |
| 45 | + } |
| 46 | + |
| 47 | + return errors; |
| 48 | +} |
| 49 | + |
| 50 | +export const FilterEditor = ({ onSubmit }: FilterEditorProps) => { |
| 51 | + const dispatch = useDispatch(); |
| 52 | + const { filters } = useQuery(); |
| 53 | + |
| 54 | + return ( |
| 55 | + <> |
| 56 | + {filters?.map((filter, index) => { |
| 57 | + const errors = filterErrors(filter) |
| 58 | + return ( |
| 59 | + <QueryEditorRow |
| 60 | + key={`${filter.id}`} |
| 61 | + label={errors.length > 0 ? ( |
| 62 | + <Tooltip content={errors.join('; ')}> |
| 63 | + <span style={{color: "gray"}}>Filter</span> |
| 64 | + </Tooltip> |
| 65 | + ): 'Filter'} |
| 66 | + hidden={filter.hide} |
| 67 | + onHideClick={() => { |
| 68 | + dispatch(toggleFilterVisibility(filter.id)); |
| 69 | + onSubmit(); |
| 70 | + }} |
| 71 | + onRemoveClick={() => { |
| 72 | + dispatch(removeFilter(filter.id)); |
| 73 | + onSubmit(); |
| 74 | + }} |
| 75 | + > |
| 76 | + <FilterEditorRow value={filter} onSubmit={onSubmit} /> |
| 77 | + |
| 78 | + {index === 0 && <IconButton |
| 79 | + label="add" |
| 80 | + iconName="plus" |
| 81 | + style={{marginLeft: '4px'}} |
| 82 | + onClick={() => dispatch(addFilter(newFilterId()))} |
| 83 | + />} |
| 84 | + </QueryEditorRow> |
| 85 | + ) |
| 86 | + })} |
| 87 | + </> |
| 88 | + ); |
| 89 | +}; |
| 90 | + |
| 91 | +interface FilterEditorRowProps { |
| 92 | + value: QueryFilter; |
| 93 | + onSubmit: () => void; |
| 94 | +} |
| 95 | + |
| 96 | +export const FilterEditorRow = ({ value, onSubmit }: FilterEditorRowProps) => { |
| 97 | + const dispatch = useDispatch(); |
| 98 | + const getFields = useFields('filters', 'startsWith'); |
| 99 | + const valueInputRef = useRef<HTMLInputElement>(null); |
| 100 | + |
| 101 | + return ( |
| 102 | + <> |
| 103 | + <InlineSegmentGroup> |
| 104 | + <SegmentAsync |
| 105 | + allowCustomValue={true} |
| 106 | + className={segmentStyles} |
| 107 | + loadOptions={getFields} |
| 108 | + reloadOptionsOnChange={true} |
| 109 | + onChange={(e) => { |
| 110 | + dispatch(changeFilterField({ id: value.id, field: e.value ?? '' })); |
| 111 | + if (['exists', 'not exists'].includes(value.filter.operator) || isSet(value.filter.value)) { |
| 112 | + onSubmit(); |
| 113 | + } |
| 114 | + // Auto focus the value input when a field is selected |
| 115 | + setTimeout(() => valueInputRef.current?.focus(), 100); |
| 116 | + }} |
| 117 | + placeholder="Select Field" |
| 118 | + value={value.filter.key} |
| 119 | + /> |
| 120 | + <div style={{ whiteSpace: 'nowrap' }}> |
| 121 | + <Segment |
| 122 | + value={filterOperations.find((op) => op.value === value.filter.operator)} |
| 123 | + options={filterOperations} |
| 124 | + onChange={(e) => { |
| 125 | + let op = e.value ?? filterOperations[0].value; |
| 126 | + dispatch(changeFilterOperation({ id: value.id, op: op })); |
| 127 | + if (['exists', 'not exists'].includes(op) || isSet(value.filter.value)) { |
| 128 | + onSubmit(); |
| 129 | + } |
| 130 | + }} |
| 131 | + /> |
| 132 | + </div> |
| 133 | + {!['exists', 'not exists'].includes(value.filter.operator) && ( |
| 134 | + <Input |
| 135 | + ref={valueInputRef} |
| 136 | + placeholder="Value" |
| 137 | + value={value.filter.value} |
| 138 | + onChange={(e) => dispatch(changeFilterValue({ id: value.id, value: e.currentTarget.value }))} |
| 139 | + onKeyUp={(e) => { |
| 140 | + if (e.key === 'Enter') { |
| 141 | + onSubmit(); |
| 142 | + } |
| 143 | + }} |
| 144 | + /> |
| 145 | + )} |
| 146 | + </InlineSegmentGroup> |
| 147 | + </> |
| 148 | + ); |
| 149 | +}; |
0 commit comments