2022-12-17 00:28:46 +09:00
|
|
|
import {
|
|
|
|
|
Badge,
|
2023-03-29 12:55:03 +02:00
|
|
|
Checkbox,
|
2022-12-17 00:28:46 +09:00
|
|
|
createStyles,
|
2023-03-29 12:55:03 +02:00
|
|
|
Group,
|
2022-12-17 00:28:46 +09:00
|
|
|
ScrollArea,
|
2023-03-29 12:55:03 +02:00
|
|
|
Table,
|
2023-01-23 23:56:38 +09:00
|
|
|
Text,
|
2023-03-29 12:55:03 +02:00
|
|
|
TextInput,
|
2022-12-17 00:28:46 +09:00
|
|
|
} from '@mantine/core';
|
2022-11-22 09:56:40 +09:00
|
|
|
import { useElementSize } from '@mantine/hooks';
|
2023-05-15 17:40:59 +09:00
|
|
|
import { IconSearch } from '@tabler/icons-react';
|
2022-07-06 18:08:39 +02:00
|
|
|
import Dockerode from 'dockerode';
|
2022-08-27 01:16:18 +02:00
|
|
|
import { useTranslation } from 'next-i18next';
|
2022-07-22 16:19:56 +02:00
|
|
|
import { useEffect, useState } from 'react';
|
2023-03-03 00:37:22 +09:00
|
|
|
import { MIN_WIDTH_MOBILE } from '../../constants/constants';
|
2022-07-06 18:08:39 +02:00
|
|
|
import ContainerState from './ContainerState';
|
|
|
|
|
|
|
|
|
|
const useStyles = createStyles((theme) => ({
|
|
|
|
|
rowSelected: {
|
|
|
|
|
backgroundColor:
|
|
|
|
|
theme.colorScheme === 'dark'
|
|
|
|
|
? theme.fn.rgba(theme.colors[theme.primaryColor][7], 0.2)
|
|
|
|
|
: theme.colors[theme.primaryColor][0],
|
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
export default function DockerTable({
|
|
|
|
|
containers,
|
|
|
|
|
selection,
|
|
|
|
|
setSelection,
|
|
|
|
|
}: {
|
|
|
|
|
setSelection: any;
|
|
|
|
|
containers: Dockerode.ContainerInfo[];
|
|
|
|
|
selection: Dockerode.ContainerInfo[];
|
|
|
|
|
}) {
|
2022-07-22 13:17:48 +02:00
|
|
|
const [usedContainers, setContainers] = useState<Dockerode.ContainerInfo[]>(containers);
|
2022-07-06 18:08:39 +02:00
|
|
|
const { classes, cx } = useStyles();
|
2022-07-22 13:17:48 +02:00
|
|
|
const [search, setSearch] = useState('');
|
2022-11-22 09:56:40 +09:00
|
|
|
const { ref, width, height } = useElementSize();
|
2022-07-22 13:17:48 +02:00
|
|
|
|
2022-08-25 11:07:25 +02:00
|
|
|
const { t } = useTranslation('modules/docker');
|
2022-08-22 09:50:54 +02:00
|
|
|
|
2022-07-22 16:19:56 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
setContainers(containers);
|
|
|
|
|
}, [containers]);
|
|
|
|
|
|
2022-07-22 13:17:48 +02:00
|
|
|
const handleSearchChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
const { value } = event.currentTarget;
|
|
|
|
|
setSearch(value);
|
|
|
|
|
setContainers(filterContainers(containers, value));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function filterContainers(data: Dockerode.ContainerInfo[], search: string) {
|
|
|
|
|
const query = search.toLowerCase().trim();
|
|
|
|
|
return data.filter((item) =>
|
|
|
|
|
item.Names.some((name) => name.toLowerCase().includes(query) || item.Image.includes(query))
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-07-06 18:08:39 +02:00
|
|
|
|
|
|
|
|
const toggleRow = (container: Dockerode.ContainerInfo) =>
|
|
|
|
|
setSelection((current: Dockerode.ContainerInfo[]) =>
|
|
|
|
|
current.includes(container) ? current.filter((c) => c !== container) : [...current, container]
|
|
|
|
|
);
|
|
|
|
|
const toggleAll = () =>
|
|
|
|
|
setSelection((current: any) =>
|
2022-07-22 13:17:48 +02:00
|
|
|
current.length === usedContainers.length ? [] : usedContainers.map((c) => c)
|
2022-07-06 18:08:39 +02:00
|
|
|
);
|
|
|
|
|
|
2022-07-22 13:17:48 +02:00
|
|
|
const rows = usedContainers.map((element) => {
|
2022-07-06 18:08:39 +02:00
|
|
|
const selected = selection.includes(element);
|
|
|
|
|
return (
|
|
|
|
|
<tr key={element.Id} className={cx({ [classes.rowSelected]: selected })}>
|
|
|
|
|
<td>
|
|
|
|
|
<Checkbox
|
|
|
|
|
checked={selection.includes(element)}
|
|
|
|
|
onChange={() => toggleRow(element)}
|
|
|
|
|
transitionDuration={0}
|
|
|
|
|
/>
|
|
|
|
|
</td>
|
2023-01-23 23:56:38 +09:00
|
|
|
<td>
|
|
|
|
|
<Text size="lg" weight={600}>
|
|
|
|
|
{element.Names[0].replace('/', '')}
|
|
|
|
|
</Text>
|
|
|
|
|
</td>
|
|
|
|
|
{width > MIN_WIDTH_MOBILE && (
|
|
|
|
|
<td>
|
|
|
|
|
<Text size="lg">{element.Image}</Text>
|
|
|
|
|
</td>
|
|
|
|
|
)}
|
2022-11-22 18:15:37 +09:00
|
|
|
{width > MIN_WIDTH_MOBILE && (
|
2022-11-22 09:56:40 +09:00
|
|
|
<td>
|
|
|
|
|
<Group>
|
|
|
|
|
{element.Ports.sort((a, b) => a.PrivatePort - b.PrivatePort)
|
|
|
|
|
// Remove duplicates with filter function
|
|
|
|
|
.filter(
|
|
|
|
|
(port, index, self) =>
|
|
|
|
|
index === self.findIndex((t) => t.PrivatePort === port.PrivatePort)
|
|
|
|
|
)
|
|
|
|
|
.slice(-3)
|
|
|
|
|
.map((port) => (
|
|
|
|
|
<Badge key={port.PrivatePort} variant="outline">
|
|
|
|
|
{port.PrivatePort}:{port.PublicPort}
|
|
|
|
|
</Badge>
|
|
|
|
|
))}
|
|
|
|
|
{element.Ports.length > 3 && (
|
|
|
|
|
<Badge variant="filled">
|
|
|
|
|
{t('table.body.portCollapse', { ports: element.Ports.length - 3 })}
|
2022-07-06 18:08:39 +02:00
|
|
|
</Badge>
|
2022-11-22 09:56:40 +09:00
|
|
|
)}
|
|
|
|
|
</Group>
|
|
|
|
|
</td>
|
2022-11-22 18:15:37 +09:00
|
|
|
)}
|
2022-07-06 18:08:39 +02:00
|
|
|
<td>
|
|
|
|
|
<ContainerState state={element.State} />
|
|
|
|
|
</td>
|
|
|
|
|
</tr>
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
2023-01-31 18:06:49 +01:00
|
|
|
<ScrollArea style={{ height: '100%' }} offsetScrollbars>
|
2022-07-22 13:12:52 +02:00
|
|
|
<TextInput
|
2022-08-22 09:50:54 +02:00
|
|
|
placeholder={t('search.placeholder')}
|
2023-01-23 23:56:38 +09:00
|
|
|
mr="md"
|
2022-07-22 13:12:52 +02:00
|
|
|
icon={<IconSearch size={14} />}
|
|
|
|
|
value={search}
|
2023-01-23 23:56:38 +09:00
|
|
|
autoFocus
|
2022-07-22 13:12:52 +02:00
|
|
|
onChange={handleSearchChange}
|
|
|
|
|
/>
|
2022-11-22 09:56:40 +09:00
|
|
|
<Table ref={ref} captionSide="bottom" highlightOnHover verticalSpacing="sm">
|
2022-07-22 13:17:48 +02:00
|
|
|
<thead>
|
|
|
|
|
<tr>
|
|
|
|
|
<th style={{ width: 40 }}>
|
|
|
|
|
<Checkbox
|
|
|
|
|
onChange={toggleAll}
|
2022-08-23 19:57:03 +02:00
|
|
|
checked={selection.length === usedContainers.length && selection.length > 0}
|
2022-07-22 13:17:48 +02:00
|
|
|
indeterminate={selection.length > 0 && selection.length !== usedContainers.length}
|
|
|
|
|
transitionDuration={0}
|
2022-08-23 19:57:03 +02:00
|
|
|
disabled={usedContainers.length === 0}
|
2022-07-22 13:17:48 +02:00
|
|
|
/>
|
|
|
|
|
</th>
|
2022-08-22 09:50:54 +02:00
|
|
|
<th>{t('table.header.name')}</th>
|
2022-11-22 18:15:37 +09:00
|
|
|
{width > MIN_WIDTH_MOBILE ? <th>{t('table.header.image')}</th> : null}
|
|
|
|
|
{width > MIN_WIDTH_MOBILE ? <th>{t('table.header.ports')}</th> : null}
|
2022-08-22 09:50:54 +02:00
|
|
|
<th>{t('table.header.state')}</th>
|
2022-07-22 13:17:48 +02:00
|
|
|
</tr>
|
|
|
|
|
</thead>
|
|
|
|
|
<tbody>{rows}</tbody>
|
|
|
|
|
</Table>
|
2022-07-22 13:12:52 +02:00
|
|
|
</ScrollArea>
|
2022-07-06 18:08:39 +02:00
|
|
|
);
|
|
|
|
|
}
|