2021-05-12 14:07:37 -05:00
|
|
|
import React, { useState } from 'react';
|
2021-06-04 12:20:06 -05:00
|
|
|
import { css, cx } from '@emotion/css';
|
2021-05-12 14:07:37 -05:00
|
|
|
import { gt, satisfies } from 'semver';
|
|
|
|
|
|
|
|
import { config } from '@grafana/runtime';
|
2021-05-27 05:45:06 -05:00
|
|
|
import { Button, HorizontalGroup, Icon, LinkButton, useStyles2 } from '@grafana/ui';
|
2021-05-31 11:42:36 -05:00
|
|
|
import { AppEvents, GrafanaTheme2 } from '@grafana/data';
|
2021-05-12 14:07:37 -05:00
|
|
|
|
|
|
|
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';
|
2021-05-31 11:42:36 -05:00
|
|
|
import { isGrafanaAdmin } from '../helpers';
|
2021-05-12 14:07:37 -05:00
|
|
|
|
|
|
|
interface Props {
|
|
|
|
localPlugin?: Metadata;
|
|
|
|
remotePlugin: Plugin;
|
|
|
|
}
|
|
|
|
|
2021-05-20 03:42:26 -05:00
|
|
|
export const InstallControls = ({ localPlugin, remotePlugin }: Props) => {
|
2021-05-12 14:07:37 -05:00
|
|
|
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!)
|
|
|
|
);
|
2021-06-04 12:20:06 -05:00
|
|
|
const [hasInstalledPanel, setHasInstalledPanel] = useState(false);
|
2021-05-27 05:45:06 -05:00
|
|
|
const isExternallyManaged = config.pluginAdminExternalManageEnabled;
|
|
|
|
const externalManageLink = getExternalManageLink(remotePlugin);
|
2021-05-12 14:07:37 -05:00
|
|
|
|
|
|
|
const styles = useStyles2(getStyles);
|
|
|
|
|
|
|
|
const onInstall = async () => {
|
|
|
|
setLoading(true);
|
|
|
|
try {
|
2021-05-20 03:42:26 -05:00
|
|
|
await api.installPlugin(remotePlugin.slug, remotePlugin.version);
|
2021-05-12 14:07:37 -05:00
|
|
|
appEvents.emit(AppEvents.alertSuccess, [`Installed ${remotePlugin?.name}`]);
|
|
|
|
setLoading(false);
|
|
|
|
setIsInstalled(true);
|
2021-06-04 12:20:06 -05:00
|
|
|
setHasInstalledPanel(remotePlugin.typeCode === 'panel');
|
2021-05-12 14:07:37 -05:00
|
|
|
} catch (error) {
|
|
|
|
setLoading(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onUninstall = async () => {
|
|
|
|
setLoading(true);
|
|
|
|
try {
|
2021-05-20 03:42:26 -05:00
|
|
|
await api.uninstallPlugin(remotePlugin.slug);
|
2021-05-12 14:07:37 -05:00
|
|
|
appEvents.emit(AppEvents.alertSuccess, [`Uninstalled ${remotePlugin?.name}`]);
|
|
|
|
setLoading(false);
|
|
|
|
setIsInstalled(false);
|
|
|
|
} catch (error) {
|
|
|
|
setLoading(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onUpdate = async () => {
|
|
|
|
setLoading(true);
|
|
|
|
try {
|
2021-05-20 03:42:26 -05:00
|
|
|
await api.installPlugin(remotePlugin.slug, remotePlugin.version);
|
2021-05-12 14:07:37 -05:00
|
|
|
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';
|
2021-05-31 11:42:36 -05:00
|
|
|
const hasPermission = isGrafanaAdmin();
|
2021-05-12 14:07:37 -05:00
|
|
|
|
|
|
|
if (isEnterprise) {
|
|
|
|
return (
|
|
|
|
<div className={styles.message}>
|
|
|
|
Marketplace doesn't support installing Enterprise plugins yet. Stay tuned!
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDevelopmentBuild) {
|
|
|
|
return (
|
|
|
|
<div className={styles.message}>This is a development build of the plugin and can't be uninstalled.</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isInstalled) {
|
|
|
|
return (
|
|
|
|
<HorizontalGroup height="auto">
|
2021-05-27 05:45:06 -05:00
|
|
|
{shouldUpdate &&
|
|
|
|
(isExternallyManaged ? (
|
2021-05-31 11:42:36 -05:00
|
|
|
<LinkButton href={externalManageLink} target="_blank" rel="noopener noreferrer">
|
2021-05-27 05:45:06 -05:00
|
|
|
{'Update via grafana.com'}
|
|
|
|
</LinkButton>
|
|
|
|
) : (
|
|
|
|
<Button disabled={loading || !hasPermission} onClick={onUpdate}>
|
|
|
|
{loading ? 'Updating' : 'Update'}
|
|
|
|
</Button>
|
|
|
|
))}
|
|
|
|
|
|
|
|
{isExternallyManaged ? (
|
2021-05-31 11:42:36 -05:00
|
|
|
<LinkButton variant="destructive" href={externalManageLink} target="_blank" rel="noopener noreferrer">
|
2021-05-27 05:45:06 -05:00
|
|
|
{'Uninstall via grafana.com'}
|
|
|
|
</LinkButton>
|
|
|
|
) : (
|
2021-05-31 11:42:36 -05:00
|
|
|
<>
|
|
|
|
<Button variant="destructive" disabled={loading || !hasPermission} onClick={onUninstall}>
|
|
|
|
{loading && !shouldUpdate ? 'Uninstalling' : 'Uninstall'}
|
|
|
|
</Button>
|
2021-06-04 12:20:06 -05:00
|
|
|
{hasInstalledPanel && (
|
|
|
|
<div className={cx(styles.message, styles.messageMargin)}>
|
|
|
|
Please refresh your browser window before using this plugin.
|
|
|
|
</div>
|
|
|
|
)}
|
2021-05-31 11:42:36 -05:00
|
|
|
{!hasPermission && <div className={styles.message}>You need admin privileges to manage this plugin.</div>}
|
|
|
|
</>
|
2021-05-12 14:07:37 -05:00
|
|
|
)}
|
|
|
|
</HorizontalGroup>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unsupportedGrafanaVersion) {
|
|
|
|
return (
|
|
|
|
<div className={styles.message}>
|
|
|
|
<Icon name="exclamation-triangle" />
|
|
|
|
This plugin doesn't support your version of Grafana.
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<HorizontalGroup height="auto">
|
2021-05-27 05:45:06 -05:00
|
|
|
{isExternallyManaged ? (
|
2021-05-31 11:42:36 -05:00
|
|
|
<LinkButton href={externalManageLink} target="_blank" rel="noopener noreferrer">
|
2021-05-27 05:45:06 -05:00
|
|
|
{'Install via grafana.com'}
|
|
|
|
</LinkButton>
|
|
|
|
) : (
|
2021-05-31 11:42:36 -05:00
|
|
|
<>
|
|
|
|
<Button disabled={loading || !hasPermission} onClick={onInstall}>
|
|
|
|
{loading ? 'Installing' : 'Install'}
|
|
|
|
</Button>
|
|
|
|
{!hasPermission && <div className={styles.message}>You need admin privileges to install this plugin.</div>}
|
|
|
|
</>
|
2021-05-27 05:45:06 -05:00
|
|
|
)}
|
2021-05-12 14:07:37 -05:00
|
|
|
</HorizontalGroup>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-05-27 05:45:06 -05:00
|
|
|
function getExternalManageLink(plugin: Plugin): string {
|
|
|
|
return `https://grafana.com/grafana/plugins/${plugin.slug}`;
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:07:37 -05:00
|
|
|
export const getStyles = (theme: GrafanaTheme2) => {
|
|
|
|
return {
|
|
|
|
message: css`
|
|
|
|
color: ${theme.colors.text.secondary};
|
|
|
|
`,
|
2021-06-04 12:20:06 -05:00
|
|
|
messageMargin: css`
|
|
|
|
margin-left: ${theme.spacing()};
|
|
|
|
`,
|
2021-05-12 14:07:37 -05:00
|
|
|
readme: css`
|
|
|
|
margin: ${theme.spacing(3)} 0;
|
|
|
|
|
|
|
|
& img {
|
|
|
|
max-width: 100%;
|
|
|
|
}
|
|
|
|
|
|
|
|
h1,
|
|
|
|
h2,
|
|
|
|
h3 {
|
|
|
|
margin-top: ${theme.spacing(3)};
|
|
|
|
margin-bottom: ${theme.spacing(2)};
|
|
|
|
}
|
|
|
|
|
|
|
|
li {
|
|
|
|
margin-left: ${theme.spacing(2)};
|
|
|
|
& > p {
|
|
|
|
margin: ${theme.spacing()} 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
};
|
|
|
|
};
|