|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +import { useEffect, useRef, useMemo } from 'react'; |
| 20 | +import { Select } from 'src/components'; |
| 21 | +import { styled, t, useTheme } from '@superset-ui/core'; |
| 22 | +import { SQLEditor } from 'src/components/AsyncAceEditor'; |
| 23 | +import sqlKeywords from 'src/SqlLab/utils/sqlKeywords'; |
| 24 | +import { getColumnKeywords } from 'src/explore/controlUtils/getColumnKeywords'; |
| 25 | +import AdhocFilter from 'src/explore/components/controls/FilterControl/AdhocFilter'; |
| 26 | +import { OptionSortType } from 'src/explore/types'; |
| 27 | +import { ColumnMeta } from '@superset-ui/chart-controls'; |
| 28 | +import { Clauses, ExpressionTypes } from '../types'; |
| 29 | + |
| 30 | +const StyledSelect = styled(Select)` |
| 31 | + ${({ theme }) => ` |
| 32 | + width: ${theme.gridUnit * 30}px; |
| 33 | + marginRight: ${theme.gridUnit}px; |
| 34 | + `} |
| 35 | +`; |
| 36 | + |
| 37 | +export default function AdhocFilterEditPopoverSqlTabContent({ |
| 38 | + adhocFilter, |
| 39 | + onChange, |
| 40 | + options, |
| 41 | + height, |
| 42 | +}: { |
| 43 | + adhocFilter: AdhocFilter; |
| 44 | + onChange: (filter: AdhocFilter) => void; |
| 45 | + options: OptionSortType[]; |
| 46 | + height: number; |
| 47 | +}) { |
| 48 | + const aceEditorRef = useRef(null); |
| 49 | + const theme = useTheme(); |
| 50 | + |
| 51 | + useEffect(() => { |
| 52 | + // @ts-ignore |
| 53 | + aceEditorRef?.current?.editor.resize(); |
| 54 | + }, [adhocFilter]); |
| 55 | + |
| 56 | + const onSqlExpressionClauseChange = (clause: string) => { |
| 57 | + onChange( |
| 58 | + adhocFilter.duplicateWith({ |
| 59 | + clause, |
| 60 | + expressionType: ExpressionTypes.Sql, |
| 61 | + }), |
| 62 | + ); |
| 63 | + }; |
| 64 | + |
| 65 | + const onSqlExpressionChange = (sqlExpression: string) => { |
| 66 | + onChange( |
| 67 | + adhocFilter.duplicateWith({ |
| 68 | + sqlExpression, |
| 69 | + expressionType: ExpressionTypes.Sql, |
| 70 | + }), |
| 71 | + ); |
| 72 | + }; |
| 73 | + |
| 74 | + const keywords = useMemo( |
| 75 | + () => |
| 76 | + sqlKeywords.concat( |
| 77 | + getColumnKeywords( |
| 78 | + options.filter( |
| 79 | + (option): option is ColumnMeta => |
| 80 | + typeof option === 'object' && |
| 81 | + option !== null && |
| 82 | + 'column_name' in option && |
| 83 | + typeof option.column_name === 'string' && |
| 84 | + 'type' in option, |
| 85 | + ), |
| 86 | + ), |
| 87 | + ), |
| 88 | + [sqlKeywords], |
| 89 | + ); |
| 90 | + |
| 91 | + const selectOptions = useMemo( |
| 92 | + () => |
| 93 | + Object.values(Clauses).map(clause => ({ |
| 94 | + label: clause, |
| 95 | + value: clause, |
| 96 | + })), |
| 97 | + [Clauses], |
| 98 | + ); |
| 99 | + |
| 100 | + return ( |
| 101 | + <span> |
| 102 | + <div className="filter-edit-clause-section"> |
| 103 | + <div> |
| 104 | + <StyledSelect |
| 105 | + options={selectOptions} |
| 106 | + ariaLabel={t('Select column')} |
| 107 | + placeholder={t('choose WHERE or HAVING...')} |
| 108 | + value={adhocFilter.clause} |
| 109 | + onChange={onSqlExpressionClauseChange} |
| 110 | + /> |
| 111 | + </div> |
| 112 | + <span className="filter-edit-clause-info"> |
| 113 | + <strong>WHERE</strong> {t('Filters by columns')} |
| 114 | + <br /> |
| 115 | + <strong>HAVING</strong> {t('Filters by metrics')} |
| 116 | + </span> |
| 117 | + </div> |
| 118 | + <div css={{ marginTop: theme.gridUnit * 4 }}> |
| 119 | + <SQLEditor |
| 120 | + ref={aceEditorRef} |
| 121 | + keywords={keywords} |
| 122 | + height={`${height - 130}px`} |
| 123 | + onChange={onSqlExpressionChange} |
| 124 | + width="100%" |
| 125 | + showGutter={false} |
| 126 | + value={adhocFilter.sqlExpression || adhocFilter.translateToSql()} |
| 127 | + editorProps={{ $blockScrolling: true }} |
| 128 | + enableLiveAutocompletion |
| 129 | + className="filter-sql-editor" |
| 130 | + wrapEnabled |
| 131 | + /> |
| 132 | + </div> |
| 133 | + </span> |
| 134 | + ); |
| 135 | +} |
0 commit comments