/* * 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 * as React from 'react'; import { EuiCallOut, EuiCodeBlock, EuiFlexGroup, EuiFlexItem, EuiFlyout, EuiFlyoutHeader, EuiFlyoutBody, EuiLoadingSpinner, EuiPortal, // EuiPortal is a temporary requirement to use EuiFlyout with "ownFocus" EuiText, EuiTextColor, EuiTitle, } from '@elastic/eui'; import { FormattedMessage } from '@osd/i18n/react'; interface Props { fetchExample: () => Promise; onClose: () => void; } interface State { isLoading: boolean; hasPrivilegeToRead: boolean; data: any[] | null; } /** * React component for displaying the example data associated with the Telemetry opt-in banner. */ export class OptInExampleFlyout extends React.PureComponent { private _isMounted: boolean = false; public readonly state: State = { data: null, isLoading: true, hasPrivilegeToRead: false, }; async componentDidMount() { this._isMounted = true; try { const { fetchExample } = this.props; const clusters = await fetchExample(); if (!this._isMounted) return; this.setState({ data: Array.isArray(clusters) ? clusters : null, isLoading: false, hasPrivilegeToRead: true, }); } catch (err) { if (!this._isMounted) return; this.setState({ isLoading: false, hasPrivilegeToRead: err.status !== 403, }); } } componentWillUnmount() { this._isMounted = false; } renderBody({ data, isLoading, hasPrivilegeToRead }: State) { if (isLoading) { return ( ); } if (!hasPrivilegeToRead) { return ( } color="danger" iconType="cross" > ); } if (data === null) { return ( } color="danger" iconType="cross" > ); } return {JSON.stringify(data, null, 2)}; } render() { return (

{this.renderBody(this.state)}
); } }