mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* adding some structure to combine the local and remote into one type. * feat(catalog): map local and remote responses to catalog plugin * feat(catalog): render CatalogPlugins in list * refactor(catalog): update usePluginsByFilter to work with new data structure * refactor(catalog): move helper functions into helpers file. delete redundent usePlugins hook * feat(catalog): create CatalogPluginDetails and pass to PluginDetails * feat(catalog): update types and components for plugin installation * chore(catalog): comment so not to forget to move code out of api layer * fix(catalog): make sure all filter shows gcom and installed * fix(catalog): fix up getCatalogPlugin logic for only locally available plugins * refactor(catalog): create getCatalogPluginDetails helper. Move usage to hook * revert(catalog): put back small logos in PluginList * revert(catalog): put back small logo for PluginDetails page * fix(catalog): prevent useDebounce from triggering when SearchField mounts * chore(catalog): add coment explaining reason for usedebouncewithoutfirstrender * refactor(catalog): replace reduce with filter to remove duplicate array of all plugins * refactor(catalog): update types for useDebounceWithoutFirstRender * chore(catalog): remove commented out import Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>
176 lines
5.3 KiB
TypeScript
176 lines
5.3 KiB
TypeScript
import React, { useState } from 'react';
|
|
import { css, cx } from '@emotion/css';
|
|
import { satisfies } from 'semver';
|
|
|
|
import { config } from '@grafana/runtime';
|
|
import { Button, HorizontalGroup, Icon, LinkButton, useStyles2 } from '@grafana/ui';
|
|
import { AppEvents, GrafanaTheme2 } from '@grafana/data';
|
|
|
|
import appEvents from 'app/core/app_events';
|
|
import { CatalogPluginDetails } from '../types';
|
|
import { api } from '../api';
|
|
import { isGrafanaAdmin } from '../helpers';
|
|
|
|
interface Props {
|
|
plugin: CatalogPluginDetails;
|
|
}
|
|
|
|
export const InstallControls = ({ plugin }: Props) => {
|
|
const [loading, setLoading] = useState(false);
|
|
const [isInstalled, setIsInstalled] = useState(plugin.isInstalled || false);
|
|
const [shouldUpdate, setShouldUpdate] = useState(plugin.hasUpdate || false);
|
|
const [hasInstalledPanel, setHasInstalledPanel] = useState(false);
|
|
const isExternallyManaged = config.pluginAdminExternalManageEnabled;
|
|
const externalManageLink = getExternalManageLink(plugin);
|
|
|
|
const styles = useStyles2(getStyles);
|
|
|
|
if (!plugin) {
|
|
return null;
|
|
}
|
|
|
|
const onInstall = async () => {
|
|
setLoading(true);
|
|
try {
|
|
await api.installPlugin(plugin.id, plugin.version);
|
|
appEvents.emit(AppEvents.alertSuccess, [`Installed ${plugin.name}`]);
|
|
setLoading(false);
|
|
setIsInstalled(true);
|
|
setHasInstalledPanel(plugin.type === 'panel');
|
|
} catch (error) {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const onUninstall = async () => {
|
|
setLoading(true);
|
|
try {
|
|
await api.uninstallPlugin(plugin.id);
|
|
appEvents.emit(AppEvents.alertSuccess, [`Uninstalled ${plugin.name}`]);
|
|
setLoading(false);
|
|
setIsInstalled(false);
|
|
} catch (error) {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const onUpdate = async () => {
|
|
setLoading(true);
|
|
try {
|
|
await api.installPlugin(plugin.id, plugin.version);
|
|
appEvents.emit(AppEvents.alertSuccess, [`Updated ${plugin.name}`]);
|
|
setLoading(false);
|
|
setShouldUpdate(false);
|
|
} catch (error) {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const grafanaDependency = plugin.grafanaDependency;
|
|
const unsupportedGrafanaVersion = grafanaDependency
|
|
? !satisfies(config.buildInfo.version, grafanaDependency, {
|
|
// needed for when running against master
|
|
includePrerelease: true,
|
|
})
|
|
: false;
|
|
|
|
const isDevelopmentBuild = Boolean(plugin.isDev);
|
|
const isEnterprise = plugin.isEnterprise;
|
|
const isCore = plugin.isCore;
|
|
const hasPermission = isGrafanaAdmin();
|
|
|
|
if (isCore) {
|
|
return null;
|
|
}
|
|
|
|
if (isEnterprise && !config.licenseInfo?.hasValidLicense) {
|
|
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">
|
|
{shouldUpdate &&
|
|
(isExternallyManaged ? (
|
|
<LinkButton href={externalManageLink} target="_blank" rel="noopener noreferrer">
|
|
{'Update via grafana.com'}
|
|
</LinkButton>
|
|
) : (
|
|
<Button disabled={loading || !hasPermission} onClick={onUpdate}>
|
|
{loading ? 'Updating' : 'Update'}
|
|
</Button>
|
|
))}
|
|
|
|
{isExternallyManaged ? (
|
|
<LinkButton variant="destructive" href={externalManageLink} target="_blank" rel="noopener noreferrer">
|
|
{'Uninstall via grafana.com'}
|
|
</LinkButton>
|
|
) : (
|
|
<>
|
|
<Button variant="destructive" disabled={loading || !hasPermission} onClick={onUninstall}>
|
|
{loading && !shouldUpdate ? 'Uninstalling' : 'Uninstall'}
|
|
</Button>
|
|
{hasInstalledPanel && (
|
|
<div className={cx(styles.message, styles.messageMargin)}>
|
|
Please refresh your browser window before using this plugin.
|
|
</div>
|
|
)}
|
|
{!hasPermission && <div className={styles.message}>You need admin privileges to manage this plugin.</div>}
|
|
</>
|
|
)}
|
|
</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">
|
|
{isExternallyManaged ? (
|
|
<LinkButton href={externalManageLink} target="_blank" rel="noopener noreferrer">
|
|
{'Install via grafana.com'}
|
|
</LinkButton>
|
|
) : (
|
|
<>
|
|
<Button disabled={loading || !hasPermission} onClick={onInstall}>
|
|
{loading ? 'Installing' : 'Install'}
|
|
</Button>
|
|
{!hasPermission && <div className={styles.message}>You need admin privileges to install this plugin.</div>}
|
|
</>
|
|
)}
|
|
</HorizontalGroup>
|
|
);
|
|
};
|
|
|
|
function getExternalManageLink(plugin: CatalogPluginDetails): string {
|
|
return `https://grafana.com/grafana/plugins/${plugin.id}`;
|
|
}
|
|
|
|
export const getStyles = (theme: GrafanaTheme2) => {
|
|
return {
|
|
message: css`
|
|
color: ${theme.colors.text.secondary};
|
|
`,
|
|
messageMargin: css`
|
|
margin-left: ${theme.spacing()};
|
|
`,
|
|
};
|
|
};
|