2022-06-27 19:25:26 +02:00
|
|
|
import { Button, Group } from '@mantine/core';
|
|
|
|
|
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,
|
|
|
|
|
IconX,
|
|
|
|
|
} from '@tabler/icons';
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
import Dockerode from 'dockerode';
|
|
|
|
|
|
|
|
|
|
function sendNotification(action: string, containerId: string, containerName: string) {
|
|
|
|
|
showNotification({
|
2022-06-27 23:38:54 +02:00
|
|
|
id: containerId,
|
2022-06-27 19:25:26 +02:00
|
|
|
loading: true,
|
|
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
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-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) =>
|
|
|
|
|
sendNotification('restart', container.Id, container.Names[0])
|
|
|
|
|
)
|
|
|
|
|
).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(
|
|
|
|
|
selected.map((container) => sendNotification('stop', container.Id, container.Names[0]))
|
|
|
|
|
).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(
|
|
|
|
|
selected.map((container) => sendNotification('start', container.Id, container.Names[0]))
|
|
|
|
|
).then(() => reload())
|
|
|
|
|
}
|
|
|
|
|
variant="light"
|
|
|
|
|
color="green"
|
|
|
|
|
radius="md"
|
|
|
|
|
>
|
2022-06-27 19:25:26 +02:00
|
|
|
Start
|
|
|
|
|
</Button>
|
2022-06-27 23:38:54 +02:00
|
|
|
<Button leftIcon={<IconRefresh />} onClick={() => reload()} variant="light">
|
|
|
|
|
Refresh data
|
|
|
|
|
</Button>
|
2022-06-27 19:25:26 +02:00
|
|
|
</Group>
|
|
|
|
|
);
|
|
|
|
|
}
|