mirror of
https://github.com/portainer/portainer.git
synced 2025-08-02 20:35:25 +02:00
refactor(docker/swarm): migrate nodes table to react [EE-4672] (#10184)
This commit is contained in:
parent
fbdbd277f7
commit
bf85a8861d
18 changed files with 210 additions and 255 deletions
|
@ -9,7 +9,7 @@ import { mergeOptions } from '@@/datatables/extend-options/mergeOptions';
|
|||
import { withMeta } from '@@/datatables/extend-options/withMeta';
|
||||
import { withControlledSelected } from '@@/datatables/extend-options/withControlledSelected';
|
||||
|
||||
import { useColumns } from './columns';
|
||||
import { useColumns } from './useColumns';
|
||||
|
||||
const tableKey = 'macvlan-nodes-selector';
|
||||
const store = createPersistedStore(tableKey);
|
||||
|
@ -41,7 +41,7 @@ export function MacvlanNodesSelector({
|
|||
settingsManager={tableState}
|
||||
extendTableOptions={mergeOptions(
|
||||
withMeta({
|
||||
table: 'macvlan-nodes',
|
||||
table: 'nodes',
|
||||
haveAccessToNode,
|
||||
}),
|
||||
withControlledSelected(
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
import _ from 'lodash';
|
||||
|
||||
import { columnHelper } from './column-helper';
|
||||
import { name } from './name';
|
||||
import { status } from './status';
|
||||
|
||||
export function useColumns(isIpColumnVisible: boolean) {
|
||||
return _.compact([
|
||||
name,
|
||||
columnHelper.accessor('Role', {}),
|
||||
columnHelper.accessor('EngineVersion', {
|
||||
header: 'Engine',
|
||||
}),
|
||||
isIpColumnVisible &&
|
||||
columnHelper.accessor('Addr', {
|
||||
header: 'IP Address',
|
||||
}),
|
||||
status,
|
||||
]);
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
import { useMemo } from 'react';
|
||||
import _ from 'lodash';
|
||||
|
||||
import {
|
||||
engine,
|
||||
ip,
|
||||
role,
|
||||
name,
|
||||
status,
|
||||
} from '@/react/docker/swarm/SwarmView/NodesDatatable/columns';
|
||||
|
||||
export function useColumns(isIpColumnVisible: boolean) {
|
||||
return useMemo(
|
||||
() => _.compact([name, role, engine, isIpColumnVisible && ip, status]),
|
||||
[isIpColumnVisible]
|
||||
);
|
||||
}
|
|
@ -0,0 +1,70 @@
|
|||
import { Trello } from 'lucide-react';
|
||||
|
||||
import { NodeViewModel } from '@/docker/models/node';
|
||||
|
||||
import { Datatable, TableSettingsMenu } from '@@/datatables';
|
||||
import {
|
||||
BasicTableSettings,
|
||||
RefreshableTableSettings,
|
||||
createPersistedStore,
|
||||
refreshableSettings,
|
||||
} from '@@/datatables/types';
|
||||
import { useTableState } from '@@/datatables/useTableState';
|
||||
import { useRepeater } from '@@/datatables/useRepeater';
|
||||
import { TableSettingsMenuAutoRefresh } from '@@/datatables/TableSettingsMenuAutoRefresh';
|
||||
import { withMeta } from '@@/datatables/extend-options/withMeta';
|
||||
|
||||
import { useColumns } from './columns';
|
||||
|
||||
const tableKey = 'nodes';
|
||||
|
||||
interface TableSettings extends BasicTableSettings, RefreshableTableSettings {}
|
||||
|
||||
const store = createPersistedStore<TableSettings>(
|
||||
tableKey,
|
||||
undefined,
|
||||
(set) => ({
|
||||
...refreshableSettings(set),
|
||||
})
|
||||
);
|
||||
|
||||
export function NodesDatatable({
|
||||
dataset,
|
||||
isIpColumnVisible,
|
||||
haveAccessToNode,
|
||||
onRefresh,
|
||||
}: {
|
||||
dataset?: Array<NodeViewModel>;
|
||||
isIpColumnVisible: boolean;
|
||||
haveAccessToNode: boolean;
|
||||
onRefresh(): Promise<void>;
|
||||
}) {
|
||||
const columns = useColumns(isIpColumnVisible);
|
||||
const tableState = useTableState(store, tableKey);
|
||||
useRepeater(tableState.autoRefreshRate, onRefresh);
|
||||
|
||||
return (
|
||||
<Datatable<NodeViewModel>
|
||||
disableSelect
|
||||
title="Nodes"
|
||||
titleIcon={Trello}
|
||||
columns={columns}
|
||||
dataset={dataset || []}
|
||||
isLoading={!dataset}
|
||||
emptyContentLabel="No node available"
|
||||
settingsManager={tableState}
|
||||
extendTableOptions={withMeta({
|
||||
table: 'nodes',
|
||||
haveAccessToNode,
|
||||
})}
|
||||
renderTableSettings={() => (
|
||||
<TableSettingsMenu>
|
||||
<TableSettingsMenuAutoRefresh
|
||||
value={tableState.autoRefreshRate}
|
||||
onChange={(value) => tableState.setAutoRefreshRate(value)}
|
||||
/>
|
||||
</TableSettingsMenu>
|
||||
)}
|
||||
/>
|
||||
);
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
import clsx from 'clsx';
|
||||
|
||||
import { NodeViewModel } from '@/docker/models/node';
|
||||
|
||||
import { columnHelper } from './column-helper';
|
||||
|
||||
export const availability = columnHelper.accessor('Availability', {
|
||||
header: 'Availability',
|
||||
cell({ getValue }) {
|
||||
const value = getValue();
|
||||
return (
|
||||
<span className={clsx('label', `label-${badgeClass(value)}`)}>
|
||||
{value}
|
||||
</span>
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
export function badgeClass(text: NodeViewModel['Availability']) {
|
||||
if (text === 'pause') {
|
||||
return 'warning';
|
||||
}
|
||||
|
||||
if (text === 'drain') {
|
||||
return 'danger';
|
||||
}
|
||||
|
||||
return 'success';
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
import _ from 'lodash';
|
||||
import { useMemo } from 'react';
|
||||
|
||||
import { humanize } from '@/portainer/filters/filters';
|
||||
|
||||
import { columnHelper } from './column-helper';
|
||||
import { name } from './name';
|
||||
import { status } from './status';
|
||||
import { availability } from './availability';
|
||||
|
||||
export { name, status };
|
||||
|
||||
export const role = columnHelper.accessor('Role', {});
|
||||
|
||||
export const engine = columnHelper.accessor('EngineVersion', {
|
||||
header: 'Engine',
|
||||
});
|
||||
|
||||
export const ip = columnHelper.accessor('Addr', {
|
||||
header: 'IP Address',
|
||||
});
|
||||
|
||||
export const cpu = columnHelper.accessor(
|
||||
(item) => (item.CPUs ? item.CPUs / 1000000000 : 0),
|
||||
{
|
||||
header: 'CPU',
|
||||
}
|
||||
);
|
||||
|
||||
export const memory = columnHelper.accessor('Memory', {
|
||||
header: 'Memory',
|
||||
cell({ getValue }) {
|
||||
const value = getValue();
|
||||
return humanize(value);
|
||||
},
|
||||
});
|
||||
|
||||
export function useColumns(isIpColumnVisible: boolean) {
|
||||
return useMemo(
|
||||
() =>
|
||||
_.compact([
|
||||
name,
|
||||
role,
|
||||
cpu,
|
||||
memory,
|
||||
engine,
|
||||
isIpColumnVisible && ip,
|
||||
status,
|
||||
availability,
|
||||
]),
|
||||
[isIpColumnVisible]
|
||||
);
|
||||
}
|
|
@ -5,7 +5,7 @@ import { nodeStatusBadge } from '@/docker/filters/utils';
|
|||
import { columnHelper } from './column-helper';
|
||||
|
||||
export const status = columnHelper.accessor('Status', {
|
||||
cell: ({ getValue }) => {
|
||||
cell({ getValue }) {
|
||||
const value = getValue();
|
||||
return (
|
||||
<span className={clsx('label', `label-${nodeStatusBadge(value)}`)}>
|
1
app/react/docker/swarm/SwarmView/NodesDatatable/index.ts
Normal file
1
app/react/docker/swarm/SwarmView/NodesDatatable/index.ts
Normal file
|
@ -0,0 +1 @@
|
|||
export { NodesDatatable } from './NodesDatatable';
|
|
@ -3,12 +3,12 @@ import { TableMeta as BaseTableMeta } from '@tanstack/react-table';
|
|||
import { NodeViewModel } from '@/docker/models/node';
|
||||
|
||||
export type TableMeta = BaseTableMeta<NodeViewModel> & {
|
||||
table: 'macvlan-nodes';
|
||||
table: 'nodes';
|
||||
haveAccessToNode: boolean;
|
||||
};
|
||||
|
||||
export function isTableMeta(
|
||||
meta?: BaseTableMeta<NodeViewModel>
|
||||
): meta is TableMeta {
|
||||
return !!meta && meta.table === 'macvlan-nodes';
|
||||
return !!meta && meta.table === 'nodes';
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue