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