import React, { useState } from 'react'; import { css, cx } from '@emotion/css'; import { gt, satisfies } from 'semver'; import { config } from '@grafana/runtime'; import { Button, HorizontalGroup, Icon, LinkButton, useStyles2 } from '@grafana/ui'; import { AppEvents, GrafanaTheme2 } from '@grafana/data'; import { Metadata, Plugin } from '../types'; import { api } from '../api'; // This isn't exported in the sdk yet // @ts-ignore import appEvents from 'grafana/app/core/app_events'; import { isGrafanaAdmin } from '../helpers'; interface Props { localPlugin?: Metadata; remotePlugin: Plugin; } export const InstallControls = ({ localPlugin, remotePlugin }: Props) => { const [loading, setLoading] = useState(false); const [isInstalled, setIsInstalled] = useState(Boolean(localPlugin)); const [shouldUpdate, setShouldUpdate] = useState( remotePlugin?.version && localPlugin?.info.version && gt(remotePlugin?.version!, localPlugin?.info.version!) ); const [hasInstalledPanel, setHasInstalledPanel] = useState(false); const isExternallyManaged = config.pluginAdminExternalManageEnabled; const externalManageLink = getExternalManageLink(remotePlugin); const styles = useStyles2(getStyles); const onInstall = async () => { setLoading(true); try { await api.installPlugin(remotePlugin.slug, remotePlugin.version); appEvents.emit(AppEvents.alertSuccess, [`Installed ${remotePlugin?.name}`]); setLoading(false); setIsInstalled(true); setHasInstalledPanel(remotePlugin.typeCode === 'panel'); } catch (error) { setLoading(false); } }; const onUninstall = async () => { setLoading(true); try { await api.uninstallPlugin(remotePlugin.slug); appEvents.emit(AppEvents.alertSuccess, [`Uninstalled ${remotePlugin?.name}`]); setLoading(false); setIsInstalled(false); } catch (error) { setLoading(false); } }; const onUpdate = async () => { setLoading(true); try { await api.installPlugin(remotePlugin.slug, remotePlugin.version); appEvents.emit(AppEvents.alertSuccess, [`Updated ${remotePlugin?.name}`]); setLoading(false); setShouldUpdate(false); } catch (error) { setLoading(false); } }; const grafanaDependency = remotePlugin?.json?.dependencies?.grafanaDependency; const unsupportedGrafanaVersion = grafanaDependency ? !satisfies(config.buildInfo.version, grafanaDependency, { // needed for when running against master includePrerelease: true, }) : false; const isDevelopmentBuild = Boolean(localPlugin?.dev); const isEnterprise = remotePlugin?.status === 'enterprise'; const hasPermission = isGrafanaAdmin(); if (isEnterprise) { return (