import React, { PureComponent } from 'react'; import { AppEvents, SelectableValue } from '@grafana/data'; import { selectors as e2eSelectors } from '@grafana/e2e-selectors'; import { Alert, ClipboardButton, Field, FieldSet, Icon, Input, RadioButtonGroup, Switch } from '@grafana/ui'; import config from 'app/core/config'; import { appEvents } from 'app/core/core'; import { ShareModalTabProps } from './types'; import { buildImageUrl, buildShareUrl } from './utils'; const themeOptions: Array> = [ { label: 'Current', value: 'current' }, { label: 'Dark', value: 'dark' }, { label: 'Light', value: 'light' }, ]; export interface Props extends ShareModalTabProps {} export interface State { useCurrentTimeRange: boolean; useShortUrl: boolean; selectedTheme: string; shareUrl: string; imageUrl: string; } export class ShareLink extends PureComponent { constructor(props: Props) { super(props); this.state = { useCurrentTimeRange: true, useShortUrl: false, selectedTheme: 'current', shareUrl: '', imageUrl: '', }; } componentDidMount() { this.buildUrl(); } componentDidUpdate(prevProps: Props, prevState: State) { const { useCurrentTimeRange, useShortUrl, selectedTheme } = this.state; if ( prevState.useCurrentTimeRange !== useCurrentTimeRange || prevState.selectedTheme !== selectedTheme || prevState.useShortUrl !== useShortUrl ) { this.buildUrl(); } } buildUrl = async () => { const { panel, dashboard } = this.props; const { useCurrentTimeRange, useShortUrl, selectedTheme } = this.state; const shareUrl = await buildShareUrl(useCurrentTimeRange, selectedTheme, panel, useShortUrl); const imageUrl = buildImageUrl(useCurrentTimeRange, dashboard.uid, selectedTheme, panel); this.setState({ shareUrl, imageUrl }); }; onUseCurrentTimeRangeChange = () => { this.setState({ useCurrentTimeRange: !this.state.useCurrentTimeRange }); }; onUrlShorten = () => { this.setState({ useShortUrl: !this.state.useShortUrl }); }; onThemeChange = (value: string) => { this.setState({ selectedTheme: value }); }; onShareUrlCopy = () => { appEvents.emit(AppEvents.alertSuccess, ['Content copied to clipboard']); }; getShareUrl = () => { return this.state.shareUrl; }; render() { const { panel, dashboard } = this.props; const isRelativeTime = dashboard ? dashboard.time.to === 'now' : false; const { useCurrentTimeRange, useShortUrl, selectedTheme, shareUrl, imageUrl } = this.state; const selectors = e2eSelectors.pages.SharePanelModal; const isDashboardSaved = Boolean(dashboard.id); return ( <>

Create a direct link to this dashboard or panel, customized with the options below.

Copy } />
{panel && config.rendererAvailable && ( <> {isDashboardSaved && (
Direct link rendered image
)} {!isDashboardSaved && ( To render a panel image, you must save the dashboard first. )} )} {panel && !config.rendererAvailable && ( <>To render a panel image, you must install the Grafana image renderer plugin . Please contact your Grafana administrator to install the plugin. )} ); } }