Skip to content

Commit 5dfdb25

Browse files
Updated API-explorer page to use ResourceDataView
1 parent 90b613a commit 5dfdb25

File tree

5 files changed

+186
-119
lines changed

5 files changed

+186
-119
lines changed

frontend/packages/console-app/src/components/data-view/ResourceDataView.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,17 @@ export const ResourceDataView = <
134134
const basicFilters: React.ReactNode[] = [];
135135

136136
if (!hideNameLabelFilters) {
137-
basicFilters.push(<DataViewTextFilter key="name" filterId="name" title={t('public~Name')} />);
137+
basicFilters.push(
138+
<DataViewTextFilter
139+
key="name"
140+
filterId="name"
141+
title={t('public~Name')}
142+
placeholder={t('public~Filter by name')}
143+
/>,
144+
);
138145
}
139146

140-
if (!hideNameLabelFilters && !hideLabelFilter) {
147+
if (!hideNameLabelFilters && !hideLabelFilter && loaded) {
141148
basicFilters.push(
142149
<DataViewLabelFilter key="label" filterId="label" title={t('public~Label')} data={data} />,
143150
);
@@ -149,7 +156,7 @@ export const ResourceDataView = <
149156

150157
// Can't use data in the deps array as it will recompute the filters and will cause the selected category to reset
151158
// eslint-disable-next-line react-hooks/exhaustive-deps
152-
}, [additionalFilterNodes, t]);
159+
}, [additionalFilterNodes, t, loaded]);
153160

154161
return mock ? (
155162
<EmptyBox label={label} />

frontend/packages/console-app/src/components/data-view/useResourceDataViewData.tsx

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -76,42 +76,35 @@ export const useResourceDataViewData = <
7676

7777
const dataViewColumns = React.useMemo<ResourceDataViewColumn<TData>[]>(
7878
() =>
79-
activeColumns.map(
80-
(
81-
{ id, title, sort, props: { classes, isStickyColumn, stickyMinWidth, modifier } },
82-
index,
83-
) => {
84-
const headerProps: ThProps = {
85-
className: classes,
86-
isStickyColumn,
87-
stickyMinWidth,
88-
modifier,
79+
activeColumns.map(({ id, title, sort, props }, index) => {
80+
const headerProps: ThProps = {
81+
...props,
82+
dataLabel: title,
83+
};
84+
85+
if (sort) {
86+
headerProps.sort = {
87+
columnIndex: index,
88+
sortBy: {
89+
index: 0,
90+
direction: SortByDirection.asc,
91+
defaultDirection: SortByDirection.asc,
92+
},
8993
};
90-
91-
if (sort) {
92-
headerProps.sort = {
93-
columnIndex: index,
94-
sortBy: {
95-
index: 0,
96-
direction: SortByDirection.asc,
97-
defaultDirection: SortByDirection.asc,
98-
},
99-
};
100-
}
101-
102-
return {
103-
id,
104-
title,
105-
sortFunction: sort,
106-
props: headerProps,
107-
cell: title ? (
108-
<span>{title}</span>
109-
) : (
110-
<span className="pf-v6-u-screen-reader">{t('public~Actions')}</span>
111-
),
112-
};
113-
},
114-
),
94+
}
95+
96+
return {
97+
id,
98+
title,
99+
sortFunction: sort,
100+
props: headerProps,
101+
cell: title ? (
102+
<span>{title}</span>
103+
) : (
104+
<span className="pf-v6-u-screen-reader">{t('public~Actions')}</span>
105+
),
106+
};
107+
}),
115108
[activeColumns, t],
116109
);
117110

Lines changed: 146 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import * as React from 'react';
12
import { useCallback } from 'react';
2-
import i18next from 'i18next';
33
import { useTranslation } from 'react-i18next';
4-
import { sortable } from '@patternfly/react-table';
54
import {
65
Button,
76
DescriptionList,
@@ -14,11 +13,21 @@ import {
1413
import { PencilAltIcon } from '@patternfly/react-icons/dist/esm/icons/pencil-alt-icon';
1514

1615
import PaneBody from '@console/shared/src/components/layout/PaneBody';
17-
import { referenceForModel, K8sResourceKind } from '../module/k8s';
18-
import { ListPage, DetailsPage, Table, TableData, RowFunctionArgs } from './factory';
16+
import { referenceForModel, K8sResourceKind, TableColumn } from '../module/k8s';
17+
import { ListPage, DetailsPage } from './factory';
18+
import {
19+
actionsCellProps,
20+
cellIsStickyProps,
21+
getNameCellProps,
22+
initialFiltersDefault,
23+
ResourceDataView,
24+
} from '@console/app/src/components/data-view/ResourceDataView';
25+
import { GetDataViewRows } from '@console/app/src/components/data-view/types';
1926
import { SectionHeading, LabelList, navFactory, ResourceLink, Selector, pluralize } from './utils';
2027
import { useConfigureCountModal } from './modals/configure-count-modal';
2128
import { AlertmanagerModel } from '../models';
29+
import { LoadingBox } from './utils/status-box';
30+
import { LazyActionMenu } from '@console/shared';
2231

2332
const Details: React.FCC<DetailsProps> = (props) => {
2433
const alertManager = props.obj;
@@ -97,93 +106,143 @@ const Details: React.FCC<DetailsProps> = (props) => {
97106
};
98107

99108
const { details, editYaml } = navFactory;
109+
const kind = referenceForModel(AlertmanagerModel);
100110

101111
export const AlertManagersDetailsPage = (props) => (
102112
<DetailsPage {...props} pages={[details(Details), editYaml()]} />
103113
);
104114

105-
const tableColumnClasses = [
106-
'',
107-
'',
108-
'pf-m-hidden pf-m-visible-on-md pf-v6-u-w-25-on-md',
109-
'pf-m-hidden pf-m-visible-on-lg',
110-
'pf-m-hidden pf-m-visible-on-lg pf-v6-u-w-25-on-lg',
115+
const tableColumnInfo = [
116+
{ id: 'name' },
117+
{ id: 'namespace' },
118+
{ id: 'labels' },
119+
{ id: 'version' },
120+
{ id: 'nodeSelector' },
121+
{ id: 'actions' },
111122
];
112123

113-
const AlertManagerTableRow: React.FC<RowFunctionArgs<K8sResourceKind>> = ({
114-
obj: alertManager,
115-
}) => {
116-
const { metadata, spec } = alertManager;
117-
return (
118-
<>
119-
<TableData className={tableColumnClasses[0]}>
120-
<ResourceLink
121-
kind={referenceForModel(AlertmanagerModel)}
122-
name={metadata.name}
123-
namespace={metadata.namespace}
124-
title={metadata.uid}
125-
/>
126-
</TableData>
127-
<TableData className={tableColumnClasses[1]}>
128-
<ResourceLink kind="Namespace" name={metadata.namespace} title={metadata.namespace} />
129-
</TableData>
130-
<TableData className={tableColumnClasses[2]}>
131-
<LabelList kind={AlertmanagerModel.kind} labels={metadata.labels} />
132-
</TableData>
133-
<TableData className={tableColumnClasses[3]}>{spec.version}</TableData>
134-
<TableData className={tableColumnClasses[4]}>
135-
<Selector selector={spec.nodeSelector} kind="Node" />
136-
</TableData>
137-
</>
138-
);
139-
};
124+
const getDataViewRows: GetDataViewRows<K8sResourceKind, undefined> = (data, columns) => {
125+
return data.map(({ obj: alertManager }) => {
126+
const { metadata, spec } = alertManager;
127+
const resourceKind = referenceForModel(AlertmanagerModel);
128+
const context = { [resourceKind]: alertManager };
140129

141-
const AlertManagerTableHeader = () => {
142-
return [
143-
{
144-
title: i18next.t('public~Name'),
145-
sortField: 'metadata.name',
146-
transforms: [sortable],
147-
props: { className: tableColumnClasses[0] },
148-
},
149-
{
150-
title: i18next.t('public~Namespace'),
151-
sortField: 'metadata.namespace',
152-
transforms: [sortable],
153-
props: { className: tableColumnClasses[1] },
154-
},
155-
{
156-
title: i18next.t('public~Labels'),
157-
sortField: 'metadata.labels',
158-
transforms: [sortable],
159-
props: { className: tableColumnClasses[2] },
160-
},
161-
{
162-
title: i18next.t('public~Version'),
163-
sortField: 'spec.version',
164-
transforms: [sortable],
165-
props: { className: tableColumnClasses[3] },
166-
},
167-
{
168-
title: i18next.t('public~Node selector'),
169-
sortField: 'spec.nodeSelector',
170-
transforms: [sortable],
171-
props: { className: tableColumnClasses[4] },
172-
},
173-
];
130+
const rowCells = {
131+
[tableColumnInfo[0].id]: {
132+
cell: (
133+
<ResourceLink
134+
kind={referenceForModel(AlertmanagerModel)}
135+
name={metadata.name}
136+
namespace={metadata.namespace}
137+
title={metadata.uid}
138+
/>
139+
),
140+
props: getNameCellProps(metadata.name),
141+
},
142+
[tableColumnInfo[1].id]: {
143+
cell: (
144+
<ResourceLink kind="Namespace" name={metadata.namespace} title={metadata.namespace} />
145+
),
146+
},
147+
[tableColumnInfo[2].id]: {
148+
cell: <LabelList kind={AlertmanagerModel.kind} labels={metadata.labels} />,
149+
},
150+
[tableColumnInfo[3].id]: {
151+
cell: spec.version || '-',
152+
},
153+
[tableColumnInfo[4].id]: {
154+
cell: <Selector selector={spec.nodeSelector} kind="Node" />,
155+
},
156+
[tableColumnInfo[5].id]: {
157+
cell: <LazyActionMenu context={context} />,
158+
props: actionsCellProps,
159+
},
160+
};
161+
162+
return columns.map(({ id }) => {
163+
const cell = rowCells[id]?.cell || '-';
164+
return {
165+
id,
166+
props: rowCells[id]?.props,
167+
cell,
168+
};
169+
});
170+
});
174171
};
175-
AlertManagerTableHeader.displayName = 'AlertManagerTableHeader';
176172

177-
const AlertManagersList = (props) => {
173+
const useAlertManagerColumns = (): TableColumn<K8sResourceKind>[] => {
178174
const { t } = useTranslation();
175+
const columns = React.useMemo(() => {
176+
return [
177+
{
178+
title: t('public~Name'),
179+
id: tableColumnInfo[0].id,
180+
sort: 'metadata.name',
181+
props: {
182+
...cellIsStickyProps,
183+
modifier: 'nowrap',
184+
},
185+
},
186+
{
187+
title: t('public~Namespace'),
188+
id: tableColumnInfo[1].id,
189+
sort: 'metadata.namespace',
190+
props: {
191+
modifier: 'nowrap',
192+
},
193+
},
194+
{
195+
title: t('public~Labels'),
196+
id: tableColumnInfo[2].id,
197+
sort: 'metadata.labels',
198+
props: {
199+
modifier: 'nowrap',
200+
},
201+
},
202+
{
203+
title: t('public~Version'),
204+
id: tableColumnInfo[3].id,
205+
sort: 'spec.version',
206+
props: {
207+
modifier: 'nowrap',
208+
},
209+
},
210+
{
211+
title: t('public~Node selector'),
212+
id: tableColumnInfo[4].id,
213+
sort: 'spec.nodeSelector',
214+
props: {
215+
modifier: 'nowrap',
216+
},
217+
},
218+
{
219+
title: '',
220+
id: tableColumnInfo[5].id,
221+
props: {
222+
...cellIsStickyProps,
223+
},
224+
},
225+
];
226+
}, [t]);
227+
return columns;
228+
};
229+
230+
const AlertManagersList: React.FCC<AlertManagersListProps> = ({ data, loaded, ...props }) => {
231+
const columns = useAlertManagerColumns();
232+
179233
return (
180-
<Table
181-
{...props}
182-
aria-label={t('public~Alertmanagers')}
183-
Header={AlertManagerTableHeader}
184-
Row={AlertManagerTableRow}
185-
virtualize
186-
/>
234+
<React.Suspense fallback={<LoadingBox />}>
235+
<ResourceDataView
236+
{...props}
237+
label={AlertmanagerModel.labelPlural}
238+
data={data}
239+
loaded={loaded}
240+
columns={columns}
241+
initialFilters={initialFiltersDefault}
242+
getDataViewRows={getDataViewRows}
243+
hideColumnManagement={true}
244+
/>
245+
</React.Suspense>
187246
);
188247
};
189248

@@ -192,10 +251,17 @@ export const AlertManagersPage = (props) => (
192251
{...props}
193252
ListComponent={AlertManagersList}
194253
canCreate={false}
195-
kind={referenceForModel(AlertmanagerModel)}
254+
kind={kind}
255+
omitFilterToolbar={true}
196256
/>
197257
);
198258

199259
type DetailsProps = {
200260
obj: K8sResourceKind;
201261
};
262+
263+
type AlertManagersListProps = {
264+
data: K8sResourceKind[];
265+
loaded: boolean;
266+
[key: string]: any;
267+
};

frontend/public/components/factory/list-page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ export const ListPageWrapper: React.FC<ListPageWrapperProps> = (props) => {
147147
hideLabelFilter={hideLabelFilter}
148148
columnLayout={columnLayout}
149149
uniqueFilterName={name}
150+
omitFilterToolbar={omitFilterToolbar}
150151
{...props}
151152
/>
152153
);

frontend/public/locales/en/public.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"Labels": "Labels",
1717
"Version": "Version",
1818
"Node selector": "Node selector",
19-
"Alertmanagers": "Alertmanagers",
2019
"API resources": "API resources",
2120
"true": "true",
2221
"false": "false",
@@ -1556,6 +1555,7 @@
15561555
"ServiceMonitors": "ServiceMonitors",
15571556
"PodMonitor": "PodMonitor",
15581557
"PodMonitors": "PodMonitors",
1558+
"Alertmanagers": "Alertmanagers",
15591559
"Service": "Service",
15601560
"Services": "Services",
15611561
"DaemonSet": "DaemonSet",

0 commit comments

Comments
 (0)