2022-08-23 20:00:46 +02:00
|
|
|
import { ActionIcon, Drawer, Text, Tooltip } from '@mantine/core';
|
2022-06-20 09:00:42 +02:00
|
|
|
import axios from 'axios';
|
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
import Docker from 'dockerode';
|
2022-07-21 11:43:43 +02:00
|
|
|
import { IconBrandDocker, IconX } from '@tabler/icons';
|
|
|
|
|
import { showNotification } from '@mantine/notifications';
|
2022-08-22 09:50:54 +02:00
|
|
|
import { useTranslation } from 'next-i18next';
|
2022-08-18 21:46:46 +02:00
|
|
|
|
2022-06-27 19:25:26 +02:00
|
|
|
import ContainerActionBar from './ContainerActionBar';
|
2022-07-06 18:08:39 +02:00
|
|
|
import DockerTable from './DockerTable';
|
2022-07-23 22:22:55 +02:00
|
|
|
import { useConfig } from '../../tools/state';
|
|
|
|
|
import { IModule } from '../ModuleTypes';
|
2022-06-20 09:00:42 +02:00
|
|
|
|
2022-07-21 11:43:43 +02:00
|
|
|
export const DockerModule: IModule = {
|
|
|
|
|
title: 'Docker',
|
|
|
|
|
icon: IconBrandDocker,
|
|
|
|
|
component: DockerMenuButton,
|
2022-08-25 11:07:25 +02:00
|
|
|
id: 'docker',
|
2022-07-21 11:43:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default function DockerMenuButton(props: any) {
|
2022-06-20 09:00:42 +02:00
|
|
|
const [opened, setOpened] = useState(false);
|
2022-06-27 19:25:26 +02:00
|
|
|
const [containers, setContainers] = useState<Docker.ContainerInfo[]>([]);
|
|
|
|
|
const [selection, setSelection] = useState<Docker.ContainerInfo[]>([]);
|
2022-07-21 11:43:43 +02:00
|
|
|
const { config } = useConfig();
|
2022-08-25 11:07:25 +02:00
|
|
|
const moduleEnabled = config.modules?.[DockerModule.id]?.enabled ?? false;
|
2022-07-21 11:43:43 +02:00
|
|
|
|
2022-08-25 11:07:25 +02:00
|
|
|
const { t } = useTranslation('modules/docker');
|
2022-07-21 11:43:43 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
reload();
|
2022-07-22 22:36:53 +02:00
|
|
|
}, [config.modules]);
|
2022-07-06 18:08:39 +02:00
|
|
|
|
2022-06-27 23:38:54 +02:00
|
|
|
function reload() {
|
2022-07-22 22:36:53 +02:00
|
|
|
if (!moduleEnabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-07-06 18:08:39 +02:00
|
|
|
setTimeout(() => {
|
2022-07-21 11:43:43 +02:00
|
|
|
axios
|
|
|
|
|
.get('/api/docker/containers')
|
|
|
|
|
.then((res) => {
|
|
|
|
|
setContainers(res.data);
|
|
|
|
|
setSelection([]);
|
|
|
|
|
})
|
2022-08-23 20:00:46 +02:00
|
|
|
.catch(() => {
|
|
|
|
|
// Remove containers from the list
|
|
|
|
|
setContainers([]);
|
2022-07-21 11:43:43 +02:00
|
|
|
// Send an Error notification
|
|
|
|
|
showNotification({
|
|
|
|
|
autoClose: 1500,
|
2022-08-22 09:50:54 +02:00
|
|
|
title: <Text>{t('errors.integrationFailed.title')}</Text>,
|
2022-07-21 11:43:43 +02:00
|
|
|
color: 'red',
|
|
|
|
|
icon: <IconX />,
|
2022-08-22 09:50:54 +02:00
|
|
|
message: t('errors.integrationFailed.message'),
|
2022-08-23 20:00:46 +02:00
|
|
|
});
|
2022-09-02 13:01:56 +02:00
|
|
|
});
|
2022-07-06 18:08:39 +02:00
|
|
|
}, 300);
|
2022-06-27 23:38:54 +02:00
|
|
|
}
|
2022-08-25 11:07:25 +02:00
|
|
|
const exists = config.modules?.[DockerModule.id]?.enabled ?? false;
|
2022-07-21 11:43:43 +02:00
|
|
|
if (!exists) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2022-07-20 14:21:11 +02:00
|
|
|
// Check if the user has at least one container
|
|
|
|
|
if (containers.length < 1) return null;
|
2022-06-20 09:00:42 +02:00
|
|
|
return (
|
|
|
|
|
<>
|
2022-07-26 00:51:55 +02:00
|
|
|
<Drawer
|
|
|
|
|
opened={opened}
|
|
|
|
|
onClose={() => setOpened(false)}
|
|
|
|
|
padding="xl"
|
|
|
|
|
size="full"
|
|
|
|
|
title={<ContainerActionBar selected={selection} reload={reload} />}
|
|
|
|
|
>
|
|
|
|
|
<DockerTable containers={containers} selection={selection} setSelection={setSelection} />
|
2022-06-20 09:00:42 +02:00
|
|
|
</Drawer>
|
2022-08-22 09:50:54 +02:00
|
|
|
<Tooltip label={t('actionIcon.tooltip')}>
|
2022-06-20 09:00:42 +02:00
|
|
|
<ActionIcon
|
|
|
|
|
variant="default"
|
|
|
|
|
radius="md"
|
|
|
|
|
size="xl"
|
|
|
|
|
color="blue"
|
|
|
|
|
onClick={() => setOpened(true)}
|
|
|
|
|
>
|
|
|
|
|
<IconBrandDocker />
|
|
|
|
|
</ActionIcon>
|
2022-07-26 01:21:04 +02:00
|
|
|
</Tooltip>
|
2022-06-20 09:00:42 +02:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|