2022-06-27 19:25:26 +02:00
|
|
|
import { Button, Group } from '@mantine/core';
|
|
|
|
|
import { showNotification, updateNotification } from '@mantine/notifications';
|
|
|
|
|
import {
|
|
|
|
|
IconCheck,
|
2022-07-06 18:08:39 +02:00
|
|
|
IconLicense,
|
2022-06-27 19:25:26 +02:00
|
|
|
IconPlayerPlay,
|
|
|
|
|
IconPlayerStop,
|
2022-07-06 18:08:39 +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
|
|
|
IconX,
|
|
|
|
|
} from '@tabler/icons';
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
import Dockerode from 'dockerode';
|
2022-07-06 18:08:39 +02:00
|
|
|
import addToHomarr from '../../tools/addToHomarr';
|
|
|
|
|
import { useConfig } from '../../tools/state';
|
2022-06-27 19:25:26 +02:00
|
|
|
|
2022-07-06 18:08:39 +02:00
|
|
|
function sendDockerCommand(action: string, containerId: string, containerName: string) {
|
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-06 18:08:39 +02:00
|
|
|
title: `${action}ing container ${containerName.substring(1)}`,
|
2022-06-27 23:38:54 +02:00
|
|
|
message: undefined,
|
2022-06-27 19:25:26 +02:00
|
|
|
autoClose: false,
|
|
|
|
|
disallowClose: true,
|
|
|
|
|
});
|
|
|
|
|
axios.get(`/api/docker/container/${containerId}?action=${action}`).then((res) => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
if (res.data.success === true) {
|
|
|
|
|
updateNotification({
|
2022-06-27 23:38:54 +02:00
|
|
|
id: containerId,
|
|
|
|
|
title: `Container ${containerName} ${action}ed`,
|
|
|
|
|
message: `Your container was successfully ${action}ed`,
|
2022-06-27 19:25:26 +02:00
|
|
|
icon: <IconCheck />,
|
|
|
|
|
autoClose: 2000,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (res.data.success === false) {
|
|
|
|
|
updateNotification({
|
2022-06-27 23:38:54 +02:00
|
|
|
id: containerId,
|
2022-06-27 19:25:26 +02:00
|
|
|
color: 'red',
|
2022-06-27 23:38:54 +02:00
|
|
|
title: 'There was an error with your container.',
|
|
|
|
|
message: undefined,
|
2022-06-27 19:25:26 +02:00
|
|
|
icon: <IconX />,
|
|
|
|
|
autoClose: 2000,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, 500);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-07-06 18:08:39 +02:00
|
|
|
const { config, setConfig } = useConfig();
|
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-06 18:08:39 +02:00
|
|
|
sendDockerCommand('restart', container.Id, container.Names[0].substring(1))
|
2022-06-27 23:38:54 +02:00
|
|
|
)
|
|
|
|
|
).then(() => reload())
|
|
|
|
|
}
|
|
|
|
|
variant="light"
|
2022-06-27 19:25:26 +02:00
|
|
|
color="orange"
|
|
|
|
|
radius="md"
|
|
|
|
|
>
|
|
|
|
|
Restart
|
|
|
|
|
</Button>
|
2022-06-27 23:38:54 +02:00
|
|
|
<Button
|
|
|
|
|
leftIcon={<IconPlayerStop />}
|
|
|
|
|
onClick={() =>
|
|
|
|
|
Promise.all(
|
2022-07-06 18:08:39 +02:00
|
|
|
selected.map((container) => {
|
|
|
|
|
if (container.State === 'stopped' || container.State === 'created' || container.State === 'exited') {
|
|
|
|
|
return showNotification({
|
|
|
|
|
id: container.Id,
|
|
|
|
|
title: `Failed to stop ${container.Names[0].substring(1)}`,
|
|
|
|
|
message: "You can't stop a stopped container",
|
|
|
|
|
autoClose: 1000,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return sendDockerCommand('stop', container.Id, container.Names[0].substring(1));
|
|
|
|
|
})
|
2022-06-27 23:38:54 +02:00
|
|
|
).then(() => reload())
|
|
|
|
|
}
|
|
|
|
|
variant="light"
|
|
|
|
|
color="red"
|
|
|
|
|
radius="md"
|
|
|
|
|
>
|
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) =>
|
|
|
|
|
sendDockerCommand('start', container.Id, container.Names[0].substring(1))
|
|
|
|
|
)
|
2022-06-27 23:38:54 +02:00
|
|
|
).then(() => reload())
|
|
|
|
|
}
|
|
|
|
|
variant="light"
|
|
|
|
|
color="green"
|
|
|
|
|
radius="md"
|
|
|
|
|
>
|
2022-06-27 19:25:26 +02:00
|
|
|
Start
|
|
|
|
|
</Button>
|
2022-07-06 18:08:39 +02:00
|
|
|
<Button leftIcon={<IconRefresh />} onClick={() => reload()} variant="light" radius="md">
|
2022-06-27 23:38:54 +02:00
|
|
|
Refresh data
|
|
|
|
|
</Button>
|
2022-07-06 18:08:39 +02:00
|
|
|
<Button
|
|
|
|
|
leftIcon={<IconPlus />}
|
|
|
|
|
color="indigo"
|
|
|
|
|
variant="light"
|
|
|
|
|
radius="md"
|
|
|
|
|
onClick={() =>
|
|
|
|
|
Promise.all(selected.map((container) => addToHomarr(container, config, setConfig))).then(
|
|
|
|
|
() => reload()
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
Add to Homarr
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
leftIcon={<IconTrash />}
|
|
|
|
|
color="red"
|
|
|
|
|
variant="light"
|
|
|
|
|
radius="md"
|
|
|
|
|
onClick={() =>
|
|
|
|
|
Promise.all(
|
|
|
|
|
selected.map((container) => {
|
|
|
|
|
if (container.State === 'running') {
|
|
|
|
|
return showNotification({
|
|
|
|
|
id: container.Id,
|
|
|
|
|
title: `Failed to delete ${container.Names[0].substring(1)}`,
|
|
|
|
|
message: "You can't delete a running container",
|
|
|
|
|
autoClose: 1000,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return sendDockerCommand('remove', container.Id, container.Names[0].substring(1));
|
|
|
|
|
})
|
|
|
|
|
).then(() => reload())
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
Remove
|
|
|
|
|
</Button>
|
2022-06-27 19:25:26 +02:00
|
|
|
</Group>
|
|
|
|
|
);
|
|
|
|
|
}
|