2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
2021-02-24 08:17:40 +01:00
|
|
|
import React, { FC, useEffect, useState } from "react";
|
|
|
|
|
import { useTranslation } from "react-i18next";
|
2019-10-20 18:02:52 +02:00
|
|
|
import classNames from "classnames";
|
|
|
|
|
import styled from "styled-components";
|
|
|
|
|
import { Plugin } from "@scm-manager/ui-types";
|
2021-02-24 08:17:40 +01:00
|
|
|
import { Button, ButtonGroup, Checkbox, ErrorNotification, Modal, Notification } from "@scm-manager/ui-components";
|
2019-10-20 18:02:52 +02:00
|
|
|
import SuccessNotification from "./SuccessNotification";
|
2021-02-24 08:17:40 +01:00
|
|
|
import { useInstallPlugin, useUninstallPlugin, useUpdatePlugins } from "@scm-manager/ui-api";
|
|
|
|
|
import { PluginAction } from "../containers/PluginsOverview";
|
2019-08-20 15:50:11 +02:00
|
|
|
|
2021-02-24 08:17:40 +01:00
|
|
|
type Props = {
|
2019-10-19 16:38:07 +02:00
|
|
|
plugin: Plugin;
|
|
|
|
|
pluginAction: string;
|
|
|
|
|
onClose: () => void;
|
2019-08-21 09:59:28 +02:00
|
|
|
};
|
|
|
|
|
|
2019-10-09 16:17:02 +02:00
|
|
|
const ListParent = styled.div`
|
|
|
|
|
margin-right: 0;
|
2019-10-21 10:57:56 +02:00
|
|
|
min-width: ${props => (props.pluginAction === PluginAction.INSTALL ? "5.5em" : "10em")};
|
2019-10-09 16:17:02 +02:00
|
|
|
text-align: left;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const ListChild = styled.div`
|
|
|
|
|
flex-grow: 4;
|
|
|
|
|
`;
|
2019-08-20 17:19:57 +02:00
|
|
|
|
2021-02-24 08:17:40 +01:00
|
|
|
const PluginModal: FC<Props> = ({ onClose, pluginAction, plugin }) => {
|
|
|
|
|
const [t] = useTranslation("admin");
|
|
|
|
|
const [shouldRestart, setShouldRestart] = useState<boolean>(false);
|
|
|
|
|
const { isLoading: isInstalling, error: installError, install, isInstalled } = useInstallPlugin();
|
|
|
|
|
const { isLoading: isUninstalling, error: uninstallError, uninstall, isUninstalled } = useUninstallPlugin();
|
|
|
|
|
const { isLoading: isUpdating, error: updateError, update, isUpdated } = useUpdatePlugins();
|
|
|
|
|
const error = installError || uninstallError || updateError;
|
|
|
|
|
const loading = isInstalling || isUninstalling || isUpdating;
|
|
|
|
|
const isDone = isInstalled || isUninstalled || isUpdated;
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (isDone && !shouldRestart) {
|
|
|
|
|
onClose();
|
2019-09-16 17:55:07 +02:00
|
|
|
}
|
2021-02-24 08:17:40 +01:00
|
|
|
}, [isDone]);
|
2019-09-16 17:55:07 +02:00
|
|
|
|
2021-02-24 08:17:40 +01:00
|
|
|
const handlePluginAction = (e: Event) => {
|
2019-08-21 09:59:28 +02:00
|
|
|
e.preventDefault();
|
2021-02-24 08:17:40 +01:00
|
|
|
switch (pluginAction) {
|
|
|
|
|
case PluginAction.INSTALL:
|
|
|
|
|
install(plugin, { restart: shouldRestart });
|
|
|
|
|
break;
|
|
|
|
|
case PluginAction.UNINSTALL:
|
|
|
|
|
uninstall(plugin, { restart: shouldRestart });
|
|
|
|
|
break;
|
|
|
|
|
case PluginAction.UPDATE:
|
|
|
|
|
update(plugin, { restart: shouldRestart });
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new Error(`Unkown plugin action ${pluginAction}`);
|
|
|
|
|
}
|
2019-08-21 09:59:28 +02:00
|
|
|
};
|
|
|
|
|
|
2021-02-24 08:17:40 +01:00
|
|
|
const footer = () => {
|
2019-10-20 18:02:52 +02:00
|
|
|
let color = pluginAction === PluginAction.UNINSTALL ? "warning" : "primary";
|
2019-09-16 17:55:07 +02:00
|
|
|
let label = `plugins.modal.${pluginAction}`;
|
2021-02-24 08:17:40 +01:00
|
|
|
if (shouldRestart) {
|
2019-10-20 18:02:52 +02:00
|
|
|
color = "warning";
|
2019-09-16 17:55:07 +02:00
|
|
|
label = `plugins.modal.${pluginAction}AndRestart`;
|
2019-08-21 11:22:49 +02:00
|
|
|
}
|
2019-08-21 09:59:28 +02:00
|
|
|
return (
|
2019-08-21 11:22:49 +02:00
|
|
|
<ButtonGroup>
|
|
|
|
|
<Button
|
|
|
|
|
label={t(label)}
|
|
|
|
|
color={color}
|
2021-02-24 08:17:40 +01:00
|
|
|
action={handlePluginAction}
|
2019-08-21 11:22:49 +02:00
|
|
|
loading={loading}
|
2021-02-24 08:17:40 +01:00
|
|
|
disabled={!!error || isDone}
|
2019-08-21 11:22:49 +02:00
|
|
|
/>
|
2019-10-20 18:02:52 +02:00
|
|
|
<Button label={t("plugins.modal.abort")} action={onClose} />
|
2019-08-21 11:22:49 +02:00
|
|
|
</ButtonGroup>
|
2019-08-21 09:59:28 +02:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-24 08:17:40 +01:00
|
|
|
const renderDependencies = () => {
|
2019-08-20 15:50:11 +02:00
|
|
|
let dependencies = null;
|
|
|
|
|
if (plugin.dependencies && plugin.dependencies.length > 0) {
|
|
|
|
|
dependencies = (
|
2019-08-21 11:22:49 +02:00
|
|
|
<div className="media">
|
|
|
|
|
<Notification type="warning">
|
2019-10-20 18:02:52 +02:00
|
|
|
<strong>{t("plugins.modal.dependencyNotification")}</strong>
|
2019-10-09 16:17:02 +02:00
|
|
|
<ul>
|
2019-08-21 11:22:49 +02:00
|
|
|
{plugin.dependencies.map((dependency, index) => {
|
|
|
|
|
return <li key={index}>{dependency}</li>;
|
|
|
|
|
})}
|
|
|
|
|
</ul>
|
|
|
|
|
</Notification>
|
|
|
|
|
</div>
|
2019-08-20 15:50:11 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return dependencies;
|
2021-02-24 08:17:40 +01:00
|
|
|
};
|
2020-07-22 10:12:00 +02:00
|
|
|
|
2021-02-24 08:17:40 +01:00
|
|
|
const renderOptionalDependencies = () => {
|
2020-07-22 10:12:00 +02:00
|
|
|
let optionalDependencies = null;
|
|
|
|
|
if (plugin.optionalDependencies && plugin.optionalDependencies.length > 0) {
|
|
|
|
|
optionalDependencies = (
|
|
|
|
|
<div className="media">
|
|
|
|
|
<Notification type="warning">
|
|
|
|
|
<strong>{t("plugins.modal.optionalDependencyNotification")}</strong>
|
|
|
|
|
<ul>
|
|
|
|
|
{plugin.optionalDependencies.map((optionalDependency, index) => {
|
|
|
|
|
return <li key={index}>{optionalDependency}</li>;
|
|
|
|
|
})}
|
|
|
|
|
</ul>
|
|
|
|
|
</Notification>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return optionalDependencies;
|
2021-02-24 08:17:40 +01:00
|
|
|
};
|
2020-07-22 10:12:00 +02:00
|
|
|
|
2021-02-24 08:17:40 +01:00
|
|
|
const renderNotifications = () => {
|
2019-08-21 09:59:28 +02:00
|
|
|
if (error) {
|
|
|
|
|
return (
|
2019-08-21 11:22:49 +02:00
|
|
|
<div className="media">
|
2019-08-21 09:59:28 +02:00
|
|
|
<ErrorNotification error={error} />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2021-02-24 08:17:40 +01:00
|
|
|
} else if (isDone && shouldRestart) {
|
2019-08-21 11:22:49 +02:00
|
|
|
return (
|
|
|
|
|
<div className="media">
|
2020-03-19 10:06:54 +01:00
|
|
|
<SuccessNotification pluginAction={pluginAction} />
|
2019-08-21 11:22:49 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
2021-02-24 08:17:40 +01:00
|
|
|
} else if (shouldRestart) {
|
2019-08-21 11:22:49 +02:00
|
|
|
return (
|
|
|
|
|
<div className="media">
|
2019-10-21 10:57:56 +02:00
|
|
|
<Notification type="warning">{t("plugins.modal.restartNotification")}</Notification>
|
2019-08-21 11:22:49 +02:00
|
|
|
</div>
|
|
|
|
|
);
|
2019-08-21 09:59:28 +02:00
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-24 08:17:40 +01:00
|
|
|
const createRestartSectionContent = () => {
|
2020-03-24 15:01:39 +01:00
|
|
|
if (plugin._links[pluginAction + "WithRestart"]) {
|
|
|
|
|
return (
|
|
|
|
|
<Checkbox
|
2021-02-24 08:17:40 +01:00
|
|
|
checked={shouldRestart}
|
2020-03-24 15:01:39 +01:00
|
|
|
label={t("plugins.modal.restart")}
|
2021-02-24 08:17:40 +01:00
|
|
|
onChange={setShouldRestart}
|
2020-03-24 15:01:39 +01:00
|
|
|
disabled={false}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return <Notification type="warning">{t("plugins.modal.manualRestartRequired")}</Notification>;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2021-02-24 08:17:40 +01:00
|
|
|
const body = (
|
|
|
|
|
<>
|
|
|
|
|
<div className="media">
|
|
|
|
|
<div className="media-content">
|
|
|
|
|
<p>{plugin.description}</p>
|
2019-08-20 17:19:57 +02:00
|
|
|
</div>
|
2021-02-24 08:17:40 +01:00
|
|
|
</div>
|
|
|
|
|
<div className="media">
|
|
|
|
|
<div className="media-content">
|
|
|
|
|
<div className="field is-horizontal">
|
|
|
|
|
<ListParent className={classNames("field-label", "is-inline-flex")} pluginAction={pluginAction}>
|
|
|
|
|
{t("plugins.modal.author")}:
|
|
|
|
|
</ListParent>
|
|
|
|
|
<ListChild className={classNames("field-body", "is-inline-flex")}>{plugin.author}</ListChild>
|
|
|
|
|
</div>
|
|
|
|
|
{pluginAction === PluginAction.INSTALL && (
|
2019-08-20 17:19:57 +02:00
|
|
|
<div className="field is-horizontal">
|
2019-10-21 10:57:56 +02:00
|
|
|
<ListParent className={classNames("field-label", "is-inline-flex")} pluginAction={pluginAction}>
|
2021-02-24 08:17:40 +01:00
|
|
|
{t("plugins.modal.version")}:
|
2019-10-09 16:17:02 +02:00
|
|
|
</ListParent>
|
2021-02-24 08:17:40 +01:00
|
|
|
<ListChild className={classNames("field-body", "is-inline-flex")}>{plugin.version}</ListChild>
|
2019-08-20 17:19:57 +02:00
|
|
|
</div>
|
2021-02-24 08:17:40 +01:00
|
|
|
)}
|
|
|
|
|
{(pluginAction === PluginAction.UPDATE || pluginAction === PluginAction.UNINSTALL) && (
|
|
|
|
|
<div className="field is-horizontal">
|
|
|
|
|
<ListParent className={classNames("field-label", "is-inline-flex")}>
|
|
|
|
|
{t("plugins.modal.currentVersion")}:
|
|
|
|
|
</ListParent>
|
|
|
|
|
<ListChild className={classNames("field-body", "is-inline-flex")}>{plugin.version}</ListChild>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{pluginAction === PluginAction.UPDATE && (
|
|
|
|
|
<div className="field is-horizontal">
|
|
|
|
|
<ListParent className={classNames("field-label", "is-inline-flex")}>
|
|
|
|
|
{t("plugins.modal.newVersion")}:
|
|
|
|
|
</ListParent>
|
|
|
|
|
<ListChild className={classNames("field-body", "is-inline-flex")}>{plugin.newVersion}</ListChild>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
{renderDependencies()}
|
|
|
|
|
{renderOptionalDependencies()}
|
2019-08-20 17:19:57 +02:00
|
|
|
</div>
|
2021-02-24 08:17:40 +01:00
|
|
|
</div>
|
|
|
|
|
<div className="media">
|
|
|
|
|
<div className="media-content">{createRestartSectionContent()}</div>
|
|
|
|
|
</div>
|
|
|
|
|
{renderNotifications()}
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Modal
|
|
|
|
|
title={t(`plugins.modal.title.${pluginAction}`, {
|
|
|
|
|
name: plugin.displayName ? plugin.displayName : plugin.name
|
|
|
|
|
})}
|
|
|
|
|
closeFunction={onClose}
|
|
|
|
|
body={body}
|
|
|
|
|
footer={footer()}
|
|
|
|
|
active={true}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|
2019-08-20 15:50:11 +02:00
|
|
|
|
2021-02-24 08:17:40 +01:00
|
|
|
export default PluginModal;
|