2022-07-20 14:09:47 +02:00
|
|
|
import { Button, Group, Modal, Title } from '@mantine/core';
|
2022-07-20 14:08:56 +02:00
|
|
|
import { useBooleanToggle } from '@mantine/hooks';
|
2022-06-27 19:25:26 +02:00
|
|
|
import { showNotification, updateNotification } from '@mantine/notifications';
|
|
|
|
|
import {
|
|
|
|
|
IconCheck,
|
|
|
|
|
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
|
|
|
} from '@tabler/icons';
|
|
|
|
|
import axios from 'axios';
|
|
|
|
|
import Dockerode from 'dockerode';
|
2022-07-23 22:22:55 +02:00
|
|
|
import { tryMatchService } from '../../tools/addToHomarr';
|
|
|
|
|
import { AddAppShelfItemForm } from '../../components/AppShelf/AddAppShelfItem';
|
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-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-07-20 14:08:56 +02:00
|
|
|
const [opened, setOpened] = useBooleanToggle(false);
|
2022-06-27 19:25:26 +02:00
|
|
|
return (
|
|
|
|
|
<Group>
|
2022-07-20 14:08:56 +02:00
|
|
|
<Modal
|
|
|
|
|
size="xl"
|
|
|
|
|
radius="md"
|
|
|
|
|
opened={opened}
|
|
|
|
|
onClose={() => setOpened(false)}
|
|
|
|
|
title="Add service"
|
|
|
|
|
>
|
|
|
|
|
<AddAppShelfItemForm
|
|
|
|
|
setOpened={setOpened}
|
|
|
|
|
{...tryMatchService(selected.at(0))}
|
|
|
|
|
message="Add service to homarr"
|
|
|
|
|
/>
|
|
|
|
|
</Modal>
|
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"
|
|
|
|
|
>
|
|
|
|
|
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-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-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"
|
2022-07-20 14:08:56 +02:00
|
|
|
onClick={() => {
|
|
|
|
|
if (selected.length !== 1) {
|
|
|
|
|
showNotification({
|
|
|
|
|
autoClose: 5000,
|
2022-07-22 18:08:32 +02:00
|
|
|
title: <Title order={5}>Please only add one service at a time!</Title>,
|
2022-07-20 14:08:56 +02:00
|
|
|
color: 'red',
|
|
|
|
|
message: undefined,
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
setOpened(true);
|
|
|
|
|
}
|
|
|
|
|
}}
|
2022-07-06 18:08:39 +02:00
|
|
|
>
|
|
|
|
|
Add to Homarr
|
|
|
|
|
</Button>
|
|
|
|
|
<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
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
Remove
|
|
|
|
|
</Button>
|
2022-06-27 19:25:26 +02:00
|
|
|
</Group>
|
|
|
|
|
);
|
|
|
|
|
}
|