2022-09-02 13:01:56 +02:00
|
|
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
2022-11-22 09:56:40 +09:00
|
|
|
import { Button, Group } from '@mantine/core';
|
|
|
|
|
import { closeModal, openModal } from '@mantine/modals';
|
2022-06-27 19:25:26 +02:00
|
|
|
import { showNotification, updateNotification } from '@mantine/notifications';
|
|
|
|
|
import {
|
|
|
|
|
IconCheck,
|
|
|
|
|
IconPlayerPlay,
|
|
|
|
|
IconPlayerStop,
|
2022-09-02 13:01:56 +02:00
|
|
|
IconPlus,
|
2022-06-27 23:38:54 +02:00
|
|
|
IconRefresh,
|
2022-06-27 19:25:26 +02:00
|
|
|
IconRotateClockwise,
|
2022-07-06 18:08:39 +02:00
|
|
|
IconTrash,
|
2022-06-27 19:25:26 +02:00
|
|
|
} from '@tabler/icons';
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
import Dockerode from 'dockerode';
|
2022-08-22 09:50:54 +02:00
|
|
|
import { useTranslation } from 'next-i18next';
|
2022-08-31 15:52:16 +02:00
|
|
|
import { useState } from 'react';
|
2022-08-27 01:16:18 +02:00
|
|
|
import { TFunction } from 'react-i18next';
|
2022-09-02 13:01:56 +02:00
|
|
|
import { AddAppShelfItemForm } from '../../components/AppShelf/AddAppShelfItem';
|
|
|
|
|
import { tryMatchService } from '../../tools/addToHomarr';
|
|
|
|
|
import { useConfig } from '../../tools/state';
|
2022-08-27 01:16:18 +02:00
|
|
|
|
|
|
|
|
let t: TFunction<'modules/docker', undefined>;
|
2022-06-27 19:25:26 +02:00
|
|
|
|
2022-07-22 18:08:32 +02:00
|
|
|
function sendDockerCommand(
|
|
|
|
|
action: string,
|
|
|
|
|
containerId: string,
|
|
|
|
|
containerName: string,
|
|
|
|
|
reload: () => void
|
|
|
|
|
) {
|
2022-06-27 19:25:26 +02:00
|
|
|
showNotification({
|
2022-06-27 23:38:54 +02:00
|
|
|
id: containerId,
|
2022-06-27 19:25:26 +02:00
|
|
|
loading: true,
|
2022-10-11 11:11:21 +09:00
|
|
|
title: `${t(`actions.${action}.start`)} ${containerName}`,
|
2022-06-27 23:38:54 +02:00
|
|
|
message: undefined,
|
2022-06-27 19:25:26 +02:00
|
|
|
autoClose: false,
|
|
|
|
|
disallowClose: true,
|
|
|
|
|
});
|
2022-07-22 16:19:56 +02:00
|
|
|
axios
|
|
|
|
|
.get(`/api/docker/container/${containerId}?action=${action}`)
|
|
|
|
|
.then((res) => {
|
2022-07-22 18:08:32 +02:00
|
|
|
updateNotification({
|
|
|
|
|
id: containerId,
|
2022-10-11 11:11:21 +09:00
|
|
|
title: containerName,
|
|
|
|
|
message: `${t(`actions.${action}.end`)} ${containerName}`,
|
2022-07-22 18:08:32 +02:00
|
|
|
icon: <IconCheck />,
|
|
|
|
|
autoClose: 2000,
|
|
|
|
|
});
|
2022-07-22 16:19:56 +02:00
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
updateNotification({
|
|
|
|
|
id: containerId,
|
|
|
|
|
color: 'red',
|
2022-08-22 09:50:54 +02:00
|
|
|
title: t('errors.unknownError.title'),
|
2022-07-22 16:19:56 +02:00
|
|
|
message: err.response.data.reason,
|
|
|
|
|
autoClose: 2000,
|
|
|
|
|
});
|
2022-07-22 18:08:32 +02:00
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
reload();
|
2022-07-22 16:19:56 +02:00
|
|
|
});
|
2022-06-27 19:25:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ContainerActionBarProps {
|
|
|
|
|
selected: Dockerode.ContainerInfo[];
|
2022-06-27 23:38:54 +02:00
|
|
|
reload: () => void;
|
2022-06-27 19:25:26 +02:00
|
|
|
}
|
|
|
|
|
|
2022-06-27 23:38:54 +02:00
|
|
|
export default function ContainerActionBar({ selected, reload }: ContainerActionBarProps) {
|
2022-08-27 01:16:18 +02:00
|
|
|
t = useTranslation('modules/docker').t;
|
2022-08-31 15:52:16 +02:00
|
|
|
const [isLoading, setisLoading] = useState(false);
|
2022-09-02 13:01:56 +02:00
|
|
|
const { config, setConfig } = useConfig();
|
|
|
|
|
|
2022-06-27 19:25:26 +02:00
|
|
|
return (
|
2022-11-22 09:56:40 +09:00
|
|
|
<Group spacing="xs">
|
2022-08-31 15:52:16 +02:00
|
|
|
<Button
|
|
|
|
|
leftIcon={<IconRefresh />}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setisLoading(true);
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
reload();
|
|
|
|
|
setisLoading(false);
|
|
|
|
|
}, 750);
|
|
|
|
|
}}
|
|
|
|
|
variant="light"
|
|
|
|
|
color="violet"
|
|
|
|
|
loading={isLoading}
|
|
|
|
|
radius="md"
|
|
|
|
|
>
|
|
|
|
|
{t('actionBar.refreshData.title')}
|
|
|
|
|
</Button>
|
2022-06-27 19:25:26 +02:00
|
|
|
<Button
|
|
|
|
|
leftIcon={<IconRotateClockwise />}
|
2022-06-27 23:38:54 +02:00
|
|
|
onClick={() =>
|
|
|
|
|
Promise.all(
|
|
|
|
|
selected.map((container) =>
|
2022-07-22 18:08:32 +02:00
|
|
|
sendDockerCommand('restart', container.Id, container.Names[0].substring(1), reload)
|
2022-06-27 23:38:54 +02:00
|
|
|
)
|
2022-07-22 16:19:56 +02:00
|
|
|
)
|
2022-06-27 23:38:54 +02:00
|
|
|
}
|
|
|
|
|
variant="light"
|
2022-06-27 19:25:26 +02:00
|
|
|
color="orange"
|
|
|
|
|
radius="md"
|
2022-08-23 19:57:03 +02:00
|
|
|
disabled={selected.length === 0}
|
2022-06-27 19:25:26 +02:00
|
|
|
>
|
2022-08-22 09:50:54 +02:00
|
|
|
{t('actionBar.restart.title')}
|
2022-06-27 19:25:26 +02:00
|
|
|
</Button>
|
2022-06-27 23:38:54 +02:00
|
|
|
<Button
|
|
|
|
|
leftIcon={<IconPlayerStop />}
|
|
|
|
|
onClick={() =>
|
|
|
|
|
Promise.all(
|
2022-07-22 16:19:56 +02:00
|
|
|
selected.map((container) =>
|
2022-07-22 18:08:32 +02:00
|
|
|
sendDockerCommand('stop', container.Id, container.Names[0].substring(1), reload)
|
2022-07-22 16:19:56 +02:00
|
|
|
)
|
2022-07-22 18:08:32 +02:00
|
|
|
)
|
2022-06-27 23:38:54 +02:00
|
|
|
}
|
|
|
|
|
variant="light"
|
|
|
|
|
color="red"
|
|
|
|
|
radius="md"
|
2022-08-23 19:57:03 +02:00
|
|
|
disabled={selected.length === 0}
|
2022-06-27 23:38:54 +02:00
|
|
|
>
|
2022-08-22 09:50:54 +02:00
|
|
|
{t('actionBar.stop.title')}
|
2022-06-27 19:25:26 +02:00
|
|
|
</Button>
|
2022-06-27 23:38:54 +02:00
|
|
|
<Button
|
|
|
|
|
leftIcon={<IconPlayerPlay />}
|
|
|
|
|
onClick={() =>
|
|
|
|
|
Promise.all(
|
2022-07-06 18:08:39 +02:00
|
|
|
selected.map((container) =>
|
2022-07-22 18:08:32 +02:00
|
|
|
sendDockerCommand('start', container.Id, container.Names[0].substring(1), reload)
|
2022-07-06 18:08:39 +02:00
|
|
|
)
|
2022-07-22 18:08:32 +02:00
|
|
|
)
|
2022-06-27 23:38:54 +02:00
|
|
|
}
|
|
|
|
|
variant="light"
|
|
|
|
|
color="green"
|
|
|
|
|
radius="md"
|
2022-08-23 19:57:03 +02:00
|
|
|
disabled={selected.length === 0}
|
2022-06-27 23:38:54 +02:00
|
|
|
>
|
2022-08-22 09:50:54 +02:00
|
|
|
{t('actionBar.start.title')}
|
2022-06-27 19:25:26 +02:00
|
|
|
</Button>
|
2022-07-06 18:08:39 +02:00
|
|
|
<Button
|
|
|
|
|
leftIcon={<IconTrash />}
|
|
|
|
|
color="red"
|
|
|
|
|
variant="light"
|
|
|
|
|
radius="md"
|
|
|
|
|
onClick={() =>
|
|
|
|
|
Promise.all(
|
2022-07-22 18:08:32 +02:00
|
|
|
selected.map((container) =>
|
|
|
|
|
sendDockerCommand('remove', container.Id, container.Names[0].substring(1), reload)
|
|
|
|
|
)
|
|
|
|
|
)
|
2022-07-06 18:08:39 +02:00
|
|
|
}
|
2022-08-23 19:57:03 +02:00
|
|
|
disabled={selected.length === 0}
|
2022-07-06 18:08:39 +02:00
|
|
|
>
|
2022-08-22 09:50:54 +02:00
|
|
|
{t('actionBar.remove.title')}
|
2022-07-06 18:08:39 +02:00
|
|
|
</Button>
|
2022-09-02 13:01:56 +02:00
|
|
|
<Button
|
|
|
|
|
leftIcon={<IconPlus />}
|
|
|
|
|
color="indigo"
|
|
|
|
|
variant="light"
|
|
|
|
|
radius="md"
|
|
|
|
|
disabled={selected.length === 0 || selected.length > 1}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
openModal({
|
|
|
|
|
size: 'xl',
|
|
|
|
|
modalId: selected.at(0)!.Id,
|
|
|
|
|
radius: 'md',
|
|
|
|
|
title: t('actionBar.addService.title'),
|
|
|
|
|
zIndex: 500,
|
|
|
|
|
children: (
|
|
|
|
|
<AddAppShelfItemForm
|
|
|
|
|
setConfig={setConfig}
|
|
|
|
|
config={config}
|
|
|
|
|
setOpened={() => closeModal(selected.at(0)!.Id)}
|
|
|
|
|
message={t('actionBar.addService.message')}
|
|
|
|
|
{...tryMatchService(selected.at(0)!)}
|
|
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{t('actionBar.addToHomarr.title')}
|
|
|
|
|
</Button>
|
2022-06-27 19:25:26 +02:00
|
|
|
</Group>
|
|
|
|
|
);
|
|
|
|
|
}
|