/* * SPDX-License-Identifier: Apache-2.0 * * The OpenSearch Contributors require contributions made to * this file be licensed under the Apache-2.0 license or a * compatible open source license. * * Any modifications Copyright OpenSearch Contributors. See * GitHub history for details. */ /* * Licensed to Elasticsearch B.V. under one or more contributor * license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright * ownership. Elasticsearch B.V. licenses this file to you under * the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ import _ from 'lodash'; import React, { Fragment, useState } from 'react'; import { i18n } from '@osd/i18n'; import { FormattedMessage } from '@osd/i18n/react'; import { EuiButton, EuiButtonEmpty, EuiFieldNumber, EuiFormRow, EuiCheckboxGroup, EuiModal, EuiModalBody, EuiModalFooter, EuiModalHeader, EuiModalHeaderTitle, EuiSwitch, } from '@elastic/eui'; import { DevToolsSettings } from '../../services'; export type AutocompleteOptions = 'fields' | 'indices' | 'templates'; interface Props { onSaveSettings: (newSettings: DevToolsSettings) => void; onClose: () => void; refreshAutocompleteSettings: (selectedSettings: any) => void; settings: DevToolsSettings; } export function DevToolsSettingsModal(props: Props) { const [fontSize, setFontSize] = useState(props.settings.fontSize); const [wrapMode, setWrapMode] = useState(props.settings.wrapMode); const [fields, setFields] = useState(props.settings.autocomplete.fields); const [indices, setIndices] = useState(props.settings.autocomplete.indices); const [templates, setTemplates] = useState(props.settings.autocomplete.templates); const [polling, setPolling] = useState(props.settings.polling); const [tripleQuotes, setTripleQuotes] = useState(props.settings.tripleQuotes); const autoCompleteCheckboxes = [ { id: 'fields', label: i18n.translate('console.settingsPage.fieldsLabelText', { defaultMessage: 'Fields', }), stateSetter: setFields, }, { id: 'indices', label: i18n.translate('console.settingsPage.indicesAndAliasesLabelText', { defaultMessage: 'Indices & Aliases', }), stateSetter: setIndices, }, { id: 'templates', label: i18n.translate('console.settingsPage.templatesLabelText', { defaultMessage: 'Templates', }), stateSetter: setTemplates, }, ]; const checkboxIdToSelectedMap = { fields, indices, templates, }; const onAutocompleteChange = (optionId: AutocompleteOptions) => { const option = _.find(autoCompleteCheckboxes, (item) => item.id === optionId); if (option) { option.stateSetter(!checkboxIdToSelectedMap[optionId]); } }; function saveSettings() { props.onSaveSettings({ fontSize, wrapMode, autocomplete: { fields, indices, templates, }, polling, tripleQuotes, }); } // It only makes sense to show polling options if the user needs to fetch any data. const pollingFields = fields || indices || templates ? ( } helpText={ } > } onChange={(e) => setPolling(e.target.checked)} /> { // Only refresh the currently selected settings. props.refreshAutocompleteSettings({ fields, indices, templates, }); }} > ) : undefined; return ( } > { const val = parseInt(e.target.value, 10); if (!val) return; setFontSize(val); }} /> } onChange={(e) => setWrapMode(e.target.checked)} /> } > } onChange={(e) => setTripleQuotes(e.target.checked)} /> } > { const { stateSetter, ...rest } = opts; return rest; })} idToSelectedMap={checkboxIdToSelectedMap} onChange={(e: any) => { onAutocompleteChange(e as AutocompleteOptions); }} /> {pollingFields} ); }