2022-08-23 19:57:03 +02:00
|
|
|
import { Button, Group, Modal } from '@mantine/core';
|
2022-06-27 19:25:26 +02:00
|
|
|
import { showNotification, updateNotification } from '@mantine/notifications';
|
|
|
|
|
import {
|
|
|
|
|
IconCheck,
|
|
|
|
|
IconPlayerPlay,
|
|
|
|
|
IconPlayerStop,
|
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-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-07-22 18:08:32 +02:00
|
|
|
title: `${action}ing container ${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,
|
|
|
|
|
title: `Container ${containerName} ${action}ed`,
|
|
|
|
|
message: `Your container was successfully ${action}ed`,
|
|
|
|
|
icon: <IconCheck />,
|
|
|
|
|
autoClose: 2000,
|
|
|
|
|
});
|
2022-07-22 16:19:56 +02:00
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
updateNotification({
|
|
|
|
|
id: containerId,
|
|
|
|
|
color: 'red',
|
|
|
|
|
title: 'There was an error',
|
|
|
|
|
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-06-27 19:25:26 +02:00
|
|
|
return (
|
|
|
|
|
<Group>
|
|
|
|
|
<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
|
|
|
>
|
|
|
|
|
Restart
|
|
|
|
|
</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-06-27 19:25:26 +02:00
|
|
|
Stop
|
|
|
|
|
</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-06-27 19:25:26 +02:00
|
|
|
Start
|
|
|
|
|
</Button>
|
2022-08-23 19:57:03 +02:00
|
|
|
<Button leftIcon={<IconRefresh />} onClick={() => reload()} variant="light" color="violet" radius="md">
|
2022-06-27 23:38:54 +02:00
|
|
|
Refresh data
|
|
|
|
|
</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
|
|
|
>
|
|
|
|
|
Remove
|
|
|
|
|
</Button>
|
2022-06-27 19:25:26 +02:00
|
|
|
</Group>
|
|
|
|
|
);
|
|
|
|
|
}
|