/* * 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 React, { Component } from 'react'; import { EuiIcon, EuiContextMenuPanel, EuiContextMenuItem, EuiPopover } from '@elastic/eui'; import { FormattedMessage } from '@osd/i18n/react'; import { i18n } from '@osd/i18n'; interface Props { getCurl: () => Promise; getDocumentation: () => Promise; autoIndent: (ev?: React.MouseEvent) => void; addNotification?: (opts: { title: string }) => void; } interface State { isPopoverOpen: boolean; curlCode: string; } export class ConsoleMenu extends Component { constructor(props: Props) { super(props); this.state = { curlCode: '', isPopoverOpen: false, }; } mouseEnter = () => { if (this.state.isPopoverOpen) return; this.props.getCurl().then((text) => { this.setState({ curlCode: text }); }); }; async copyAsCurl() { await this.copyText(this.state.curlCode); const { addNotification } = this.props; if (addNotification) { addNotification({ title: i18n.translate('console.consoleMenu.copyAsCurlMessage', { defaultMessage: 'Request copied as cURL', }), }); } } async copyText(text: string) { if (window.navigator?.clipboard) { /** * Browser Compatibility Chart * * Chrome: 66 * Edge: 79 * Firefox: 63 * Opera: 53 * Internet Explorer: Not supported * */ await window.navigator.clipboard.writeText(text); } } onButtonClick = () => { this.setState((prevState) => ({ isPopoverOpen: !prevState.isPopoverOpen, })); }; closePopover = () => { this.setState({ isPopoverOpen: false, }); }; openDocs = async () => { this.closePopover(); const documentation = await this.props.getDocumentation(); if (!documentation) { return; } window.open(documentation, '_blank'); }; // Using `any` here per this issue: https://github.com/elastic/eui/issues/2265 autoIndent: any = (event: React.MouseEvent) => { this.closePopover(); this.props.autoIndent(event); }; render() { const button = ( ); const items = [ { this.closePopover(); this.copyAsCurl(); }} > , { this.openDocs(); }} > , , ]; return ( ); } }