Files
Homarr/src/modules/Docker/ContainerActionBar.tsx

242 lines
6.8 KiB
TypeScript
Raw Normal View History

2022-09-02 13:01:56 +02:00
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { Button, Group } from '@mantine/core';
import { showNotification, updateNotification } from '@mantine/notifications';
import {
IconCheck,
IconPlayerPlay,
IconPlayerStop,
2022-09-02 13:01:56 +02:00
IconPlus,
IconRefresh,
IconRotateClockwise,
IconTrash,
} from '@tabler/icons';
import axios from 'axios';
import Dockerode from 'dockerode';
2022-08-22 09:50:54 +02:00
import { useTranslation } from 'next-i18next';
import { useState } from 'react';
import { TFunction } from 'react-i18next';
import { v4 as uuidv4 } from 'uuid';
2023-01-06 12:11:47 +09:00
import { useConfigContext } from '../../config/provider';
import { openContextModalGeneric } from '../../tools/mantineModalManagerExtensions';
import { MatchingImages, ServiceType, tryMatchPort } from '../../tools/types';
2023-01-06 12:11:47 +09:00
import { AppType } from '../../types/app';
let t: TFunction<'modules/docker', undefined>;
function sendDockerCommand(
action: string,
containerId: string,
containerName: string,
reload: () => void
) {
showNotification({
id: containerId,
loading: true,
title: `${t(`actions.${action}.start`)} ${containerName}`,
message: undefined,
autoClose: false,
2023-03-03 00:37:22 +09:00
withCloseButton: false,
});
axios
.get(`/api/docker/container/${containerId}?action=${action}`)
.then((res) => {
updateNotification({
id: containerId,
title: containerName,
message: `${t(`actions.${action}.end`)} ${containerName}`,
icon: <IconCheck />,
autoClose: 2000,
});
})
.catch((err) => {
updateNotification({
id: containerId,
color: 'red',
2022-08-22 09:50:54 +02:00
title: t('errors.unknownError.title'),
message: err.response.data.reason,
autoClose: 2000,
});
})
.finally(() => {
reload();
});
}
export interface ContainerActionBarProps {
selected: Dockerode.ContainerInfo[];
reload: () => void;
}
export default function ContainerActionBar({ selected, reload }: ContainerActionBarProps) {
t = useTranslation('modules/docker').t;
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
return (
<Group spacing="xs">
<Button
leftIcon={<IconRefresh />}
onClick={() => {
setisLoading(true);
setTimeout(() => {
reload();
setisLoading(false);
}, 750);
}}
variant="light"
color="violet"
loading={isLoading}
radius="md"
>
{t('actionBar.refreshData.title')}
</Button>
<Button
leftIcon={<IconRotateClockwise />}
onClick={() =>
Promise.all(
selected.map((container) =>
sendDockerCommand('restart', container.Id, container.Names[0].substring(1), reload)
)
)
}
variant="light"
color="orange"
radius="md"
disabled={selected.length === 0}
>
2022-08-22 09:50:54 +02:00
{t('actionBar.restart.title')}
</Button>
<Button
leftIcon={<IconPlayerStop />}
onClick={() =>
Promise.all(
selected.map((container) =>
sendDockerCommand('stop', container.Id, container.Names[0].substring(1), reload)
)
)
}
variant="light"
color="red"
radius="md"
disabled={selected.length === 0}
>
2022-08-22 09:50:54 +02:00
{t('actionBar.stop.title')}
</Button>
<Button
leftIcon={<IconPlayerPlay />}
onClick={() =>
Promise.all(
selected.map((container) =>
sendDockerCommand('start', container.Id, container.Names[0].substring(1), reload)
)
)
}
variant="light"
color="green"
radius="md"
disabled={selected.length === 0}
>
2022-08-22 09:50:54 +02:00
{t('actionBar.start.title')}
</Button>
<Button
leftIcon={<IconTrash />}
color="red"
variant="light"
radius="md"
onClick={() =>
Promise.all(
selected.map((container) =>
sendDockerCommand('remove', container.Id, container.Names[0].substring(1), reload)
)
)
}
disabled={selected.length === 0}
>
2022-08-22 09:50:54 +02:00
{t('actionBar.remove.title')}
</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)!);
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',
zIndex: 202,
innerProps: {
2023-01-04 22:17:37 +09:00
app: {
id: uuidv4(),
2023-01-04 22:17:37 +09:00
name: app.name ? app.name : selected[0].Names[0].substring(1),
url: containerUrl,
appearance: {
2023-01-04 22:17:37 +09:00
iconUrl: app.icon ? app.icon : '/imgs/logo/logo.png',
},
network: {
2023-01-04 21:51:25 +09:00
enabledStatusChecker: true,
2023-03-02 20:32:41 +09:00
statusCodes: ['200'],
},
behaviour: {
isOpeningNewTab: true,
2022-12-22 11:29:51 +09:00
externalUrl: '',
},
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
},
},
2023-01-07 23:25:13 +01:00
shape: {},
2022-12-23 17:17:57 +01:00
integration: {
type: null,
properties: [],
},
},
2023-01-04 22:17:37 +09:00
allowAppNamePropagation: true,
},
2023-01-04 22:17:37 +09:00
size: 'xl',
2022-09-02 13:01:56 +02:00
});
}}
>
{t('actionBar.addToHomarr.title')}
</Button>
</Group>
);
}
2023-03-22 13:29:00 +01:00
/**
* @deprecated legacy code
*/
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
*/
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`,
};
};