|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import React, { useEffect, useState } from 'react'; |
| 9 | +import { |
| 10 | + EuiPopover, |
| 11 | + EuiFormRow, |
| 12 | + EuiSwitch, |
| 13 | + EuiButtonEmpty, |
| 14 | + EuiPopoverTitle, |
| 15 | + EuiText, |
| 16 | + EuiSpacer, |
| 17 | + EuiLink, |
| 18 | + EuiButtonIcon, |
| 19 | +} from '@elastic/eui'; |
| 20 | +import { FormattedMessage } from '@kbn/i18n/react'; |
| 21 | +import { i18n } from '@kbn/i18n'; |
| 22 | +import { useKibana } from '../../../../../../../../../src/plugins/kibana_react/public'; |
| 23 | +import { euiStyled } from '../../../../../../../../../src/plugins/kibana_react/common'; |
| 24 | +import { useUrlParams } from '../../../../../hooks'; |
| 25 | +import { |
| 26 | + CHANGE_SEARCH_BAR_SYNTAX, |
| 27 | + CHANGE_SEARCH_BAR_SYNTAX_SIMPLE, |
| 28 | + SYNTAX_OPTIONS_LABEL, |
| 29 | +} from '../translations'; |
| 30 | + |
| 31 | +const BoxesVerticalIcon = euiStyled(EuiButtonIcon)` |
| 32 | + padding: 10px 8px 0 8px; |
| 33 | + border-radius: 0; |
| 34 | + height: 38px; |
| 35 | + width: 32px; |
| 36 | + background-color: ${(props) => props.theme.eui.euiColorLightestShade}; |
| 37 | + padding-top: 8px; |
| 38 | + padding-bottom: 8px; |
| 39 | + cursor: pointer; |
| 40 | +`; |
| 41 | + |
| 42 | +interface Props { |
| 43 | + kqlSyntax: boolean; |
| 44 | + setKqlSyntax: (val: boolean) => void; |
| 45 | +} |
| 46 | + |
| 47 | +export const SearchType = ({ kqlSyntax, setKqlSyntax }: Props) => { |
| 48 | + const { |
| 49 | + services: { docLinks }, |
| 50 | + } = useKibana(); |
| 51 | + |
| 52 | + const [getUrlParams, updateUrlParams] = useUrlParams(); |
| 53 | + |
| 54 | + const { query, search } = getUrlParams(); |
| 55 | + |
| 56 | + const [isPopoverOpen, setIsPopoverOpen] = useState(false); |
| 57 | + |
| 58 | + const onButtonClick = () => setIsPopoverOpen((prevState) => !prevState); |
| 59 | + |
| 60 | + const closePopover = () => setIsPopoverOpen(false); |
| 61 | + |
| 62 | + useEffect(() => { |
| 63 | + if (kqlSyntax && query) { |
| 64 | + updateUrlParams({ query: '' }); |
| 65 | + } |
| 66 | + |
| 67 | + if (!kqlSyntax && search) { |
| 68 | + updateUrlParams({ search: '' }); |
| 69 | + } |
| 70 | + }, [kqlSyntax, query, search, updateUrlParams]); |
| 71 | + |
| 72 | + const button = kqlSyntax ? ( |
| 73 | + <EuiButtonEmpty |
| 74 | + data-test-subj="syntaxChangeToSimple" |
| 75 | + onClick={onButtonClick} |
| 76 | + aria-label={CHANGE_SEARCH_BAR_SYNTAX_SIMPLE} |
| 77 | + title={CHANGE_SEARCH_BAR_SYNTAX_SIMPLE} |
| 78 | + > |
| 79 | + KQL |
| 80 | + </EuiButtonEmpty> |
| 81 | + ) : ( |
| 82 | + <BoxesVerticalIcon |
| 83 | + color="text" |
| 84 | + iconType="boxesVertical" |
| 85 | + onClick={onButtonClick} |
| 86 | + data-test-subj="syntaxChangeToKql" |
| 87 | + aria-label={CHANGE_SEARCH_BAR_SYNTAX} |
| 88 | + title={CHANGE_SEARCH_BAR_SYNTAX} |
| 89 | + /> |
| 90 | + ); |
| 91 | + |
| 92 | + return ( |
| 93 | + <EuiPopover |
| 94 | + button={button} |
| 95 | + isOpen={isPopoverOpen} |
| 96 | + closePopover={closePopover} |
| 97 | + ownFocus={true} |
| 98 | + anchorPosition="downRight" |
| 99 | + > |
| 100 | + <div style={{ width: '360px' }}> |
| 101 | + <EuiPopoverTitle>{SYNTAX_OPTIONS_LABEL}</EuiPopoverTitle> |
| 102 | + <EuiText> |
| 103 | + <p> |
| 104 | + <KqlDescription href={docLinks!.links.query.kueryQuerySyntax} /> |
| 105 | + </p> |
| 106 | + </EuiText> |
| 107 | + <EuiSpacer /> |
| 108 | + <EuiFormRow label={KIBANA_QUERY_LANGUAGE} hasChildLabel={false}> |
| 109 | + <EuiSwitch |
| 110 | + name="switch" |
| 111 | + label={kqlSyntax ? 'On' : 'Off'} |
| 112 | + checked={kqlSyntax} |
| 113 | + onChange={() => setKqlSyntax(!kqlSyntax)} |
| 114 | + data-test-subj="toggleKqlSyntax" |
| 115 | + /> |
| 116 | + </EuiFormRow> |
| 117 | + </div> |
| 118 | + </EuiPopover> |
| 119 | + ); |
| 120 | +}; |
| 121 | + |
| 122 | +const KqlDescription = ({ href }: { href: string }) => { |
| 123 | + return ( |
| 124 | + <FormattedMessage |
| 125 | + id="xpack.uptime.queryBar.syntaxOptionsDescription" |
| 126 | + defaultMessage="The {docsLink} (KQL) offers a simplified query |
| 127 | + syntax and support for scripted fields. KQL also provides autocomplete if you have |
| 128 | + a Basic license or above. If you turn off KQL, Uptime |
| 129 | + uses simple wildcard search against {searchField} fields." |
| 130 | + values={{ |
| 131 | + docsLink: ( |
| 132 | + <EuiLink href={href} target="_blank" external> |
| 133 | + {KIBANA_QUERY_LANGUAGE} |
| 134 | + </EuiLink> |
| 135 | + ), |
| 136 | + searchField: <strong>Monitor Name, ID, Url</strong>, |
| 137 | + }} |
| 138 | + /> |
| 139 | + ); |
| 140 | +}; |
| 141 | + |
| 142 | +const KIBANA_QUERY_LANGUAGE = i18n.translate('xpack.uptime.query.queryBar.kqlFullLanguageName', { |
| 143 | + defaultMessage: 'Kibana Query Language', |
| 144 | +}); |
0 commit comments