2022-09-02 13:01:56 +02:00
|
|
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
2022-11-22 09:56:40 +09:00
|
|
|
import { Button, Group } from '@mantine/core';
|
2022-06-27 19:25:26 +02:00
|
|
|
import { showNotification, updateNotification } from '@mantine/notifications';
|
|
|
|
|
import {
|
|
|
|
|
IconCheck,
|
|
|
|
|
IconPlayerPlay,
|
|
|
|
|
IconPlayerStop,
|
2022-09-02 13:01:56 +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-08-22 09:50:54 +02:00
|
|
|
import { useTranslation } from 'next-i18next';
|
2022-08-31 15:52:16 +02:00
|
|
|
import { useState } from 'react';
|
2022-08-27 01:16:18 +02:00
|
|
|
import { TFunction } from 'react-i18next';
|
2022-12-08 22:17:33 +01:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
2023-01-06 12:11:47 +09:00
|
|
|
import { useConfigContext } from '../../config/provider';
|
|
|
|
|
import { openContextModalGeneric } from '../../tools/mantineModalManagerExtensions';
|
2023-03-17 22:40:14 +01:00
|
|
|
import { MatchingImages, ServiceType, tryMatchPort } from '../../tools/types';
|
2023-01-06 12:11:47 +09:00
|
|
|
import { AppType } from '../../types/app';
|
2022-08-27 01:16:18 +02:00
|
|
|
|
|
|
|
|
let t: TFunction<'modules/docker', undefined>;
|
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-10-11 11:11:21 +09:00
|
|
|
title: `${t(`actions.${action}.start`)} ${containerName}`,
|
2022-06-27 23:38:54 +02:00
|
|
|
message: undefined,
|
2022-06-27 19:25:26 +02:00
|
|
|
autoClose: false,
|
2023-03-03 00:37:22 +09:00
|
|
|
withCloseButton: false,
|
2022-06-27 19:25:26 +02:00
|
|
|
});
|
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,
|
2022-10-11 11:11:21 +09:00
|
|
|
title: containerName,
|
|
|
|
|
message: `${t(`actions.${action}.end`)} ${containerName}`,
|
2022-07-22 18:08:32 +02:00
|
|
|
icon: <IconCheck />,
|
|
|
|
|
autoClose: 2000,
|
|
|
|
|
});
|
2022-07-22 16:19:56 +02:00
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
updateNotification({
|
|
|
|
|
id: containerId,
|
|
|
|
|
color: 'red',
|
2022-08-22 09:50:54 +02:00
|
|
|
title: t('errors.unknownError.title'),
|
2022-07-22 16:19:56 +02:00
|
|
|
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-08-27 01:16:18 +02:00
|
|
|
t = useTranslation('modules/docker').t;
|
2022-08-31 15:52:16 +02:00
|
|
|
const [isLoading, setisLoading] = useState(false);
|
2023-01-04 22:17:37 +09:00
|
|
|
const { name: configName, config } = useConfigContext();
|
|
|
|
|
const getLowestWrapper = () => config?.wrappers.sort((a, b) => a.position - b.position)[0];
|
2022-09-02 13:01:56 +02:00
|
|
|
|
2022-06-27 19:25:26 +02:00
|
|
|
return (
|
2022-11-22 09:56:40 +09:00
|
|
|
<Group spacing="xs">
|
2022-08-31 15:52:16 +02:00
|
|
|
<Button
|
|
|
|
|
leftIcon={<IconRefresh />}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setisLoading(true);
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
reload();
|
|
|
|
|
setisLoading(false);
|
|
|
|
|
}, 750);
|
|
|
|
|
}}
|
|
|
|
|
variant="light"
|
|
|
|
|
color="violet"
|
|
|
|
|
loading={isLoading}
|
|
|
|
|
radius="md"
|
|
|
|
|
>
|
|
|
|
|
{t('actionBar.refreshData.title')}
|
|
|
|
|
</Button>
|
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"
|
2022-08-23 19:57:03 +02:00
|
|
|
disabled={selected.length === 0}
|
2022-06-27 19:25:26 +02:00
|
|
|
>
|
2022-08-22 09:50:54 +02:00
|
|
|
{t('actionBar.restart.title')}
|
2022-06-27 19:25:26 +02:00
|
|
|
</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-08-22 09:50:54 +02:00
|
|
|
{t('actionBar.stop.title')}
|
2022-06-27 19:25:26 +02:00
|
|
|
</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-08-22 09:50:54 +02:00
|
|
|
{t('actionBar.start.title')}
|
2022-06-27 19:25:26 +02:00
|
|
|
</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
|
|
|
>
|
2022-08-22 09:50:54 +02:00
|
|
|
{t('actionBar.remove.title')}
|
2022-07-06 18:08:39 +02:00
|
|
|
</Button>
|
2022-09-02 13:01:56 +02:00
|
|
|
<Button
|
|
|
|
|
leftIcon={<IconPlus />}
|
|
|
|
|
color="indigo"
|
|
|
|
|
variant="light"
|
|
|
|
|
radius="md"
|
|
|
|
|
disabled={selected.length === 0 || selected.length > 1}
|
|
|
|
|
onClick={() => {
|
2023-01-04 22:17:37 +09:00
|
|
|
const app = tryMatchService(selected.at(0)!);
|
2023-01-19 08:23:44 +09:00
|
|
|
const containerUrl = `http://localhost:${selected[0].Ports[0]?.PublicPort ?? 0}`;
|
2023-01-04 22:17:37 +09:00
|
|
|
openContextModalGeneric<{ app: AppType; allowAppNamePropagation: boolean }>({
|
|
|
|
|
modal: 'editApp',
|
2023-01-19 08:15:59 +09:00
|
|
|
zIndex: 202,
|
2022-12-08 22:17:33 +01:00
|
|
|
innerProps: {
|
2023-01-04 22:17:37 +09:00
|
|
|
app: {
|
2022-12-08 22:17:33 +01:00
|
|
|
id: uuidv4(),
|
2023-01-04 22:17:37 +09:00
|
|
|
name: app.name ? app.name : selected[0].Names[0].substring(1),
|
2022-12-08 22:17:33 +01:00
|
|
|
url: containerUrl,
|
|
|
|
|
appearance: {
|
2023-01-04 22:17:37 +09:00
|
|
|
iconUrl: app.icon ? app.icon : '/imgs/logo/logo.png',
|
2022-12-08 22:17:33 +01:00
|
|
|
},
|
|
|
|
|
network: {
|
2023-01-04 21:51:25 +09:00
|
|
|
enabledStatusChecker: true,
|
2023-03-02 20:32:41 +09:00
|
|
|
statusCodes: ['200'],
|
2022-12-08 22:17:33 +01:00
|
|
|
},
|
|
|
|
|
behaviour: {
|
|
|
|
|
isOpeningNewTab: true,
|
2022-12-22 11:29:51 +09:00
|
|
|
externalUrl: '',
|
2022-12-08 22:17:33 +01:00
|
|
|
},
|
|
|
|
|
area: {
|
2023-01-04 22:17:37 +09:00
|
|
|
type: 'wrapper',
|
2022-12-23 17:17:57 +01:00
|
|
|
properties: {
|
2023-01-04 22:17:37 +09:00
|
|
|
id: getLowestWrapper()?.id ?? 'default',
|
2022-12-23 17:17:57 +01:00
|
|
|
},
|
2022-12-08 22:17:33 +01:00
|
|
|
},
|
2023-01-07 23:25:13 +01:00
|
|
|
shape: {},
|
2022-12-23 17:17:57 +01:00
|
|
|
integration: {
|
|
|
|
|
type: null,
|
|
|
|
|
properties: [],
|
|
|
|
|
},
|
2022-12-08 22:17:33 +01:00
|
|
|
},
|
2023-01-04 22:17:37 +09:00
|
|
|
allowAppNamePropagation: true,
|
2022-12-08 22:17:33 +01:00
|
|
|
},
|
2023-01-04 22:17:37 +09:00
|
|
|
size: 'xl',
|
2022-09-02 13:01:56 +02:00
|
|
|
});
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{t('actionBar.addToHomarr.title')}
|
|
|
|
|
</Button>
|
2022-06-27 19:25:26 +02:00
|
|
|
</Group>
|
|
|
|
|
);
|
|
|
|
|
}
|
2023-03-17 22:40:14 +01:00
|
|
|
|
2023-03-22 13:29:00 +01:00
|
|
|
/**
|
|
|
|
|
* @deprecated legacy code
|
|
|
|
|
*/
|
2023-03-17 22:40:14 +01:00
|
|
|
function tryMatchType(imageName: string): ServiceType {
|
|
|
|
|
const match = MatchingImages.find(({ image }) => imageName.includes(image));
|
|
|
|
|
if (match) {
|
|
|
|
|
return match.type;
|
|
|
|
|
}
|
|
|
|
|
// TODO: Remove this legacy code
|
|
|
|
|
return 'Other';
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-22 13:29:00 +01:00
|
|
|
/**
|
|
|
|
|
* @deprecated
|
|
|
|
|
* @param container the container to match
|
|
|
|
|
* @returns a new service
|
|
|
|
|
*/
|
2023-03-17 22:40:14 +01:00
|
|
|
const tryMatchService = (container: Dockerode.ContainerInfo | undefined) => {
|
|
|
|
|
if (container === undefined) return {};
|
|
|
|
|
const name = container.Names[0].substring(1);
|
|
|
|
|
const type = tryMatchType(container.Image);
|
|
|
|
|
const port = tryMatchPort(type.toLowerCase())?.value ?? container.Ports[0]?.PublicPort;
|
|
|
|
|
return {
|
|
|
|
|
name,
|
|
|
|
|
id: container.Id,
|
|
|
|
|
type: tryMatchType(container.Image),
|
|
|
|
|
url: `localhost${port ? `:${port}` : ''}`,
|
|
|
|
|
icon: `https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/png/${name
|
|
|
|
|
.replace(/\s+/g, '-')
|
|
|
|
|
.toLowerCase()}.png`,
|
|
|
|
|
};
|
|
|
|
|
};
|