|
| 1 | +import * as React from 'react'; |
| 2 | +import { sortable } from '@patternfly/react-table'; |
| 3 | +import * as classNames from 'classnames'; |
| 4 | +import { useTranslation } from 'react-i18next'; |
| 5 | +import { |
| 6 | + ListPageBody, |
| 7 | + ListPageCreate, |
| 8 | + ListPageFilter, |
| 9 | + ListPageHeader, |
| 10 | + RowProps, |
| 11 | + TableColumn, |
| 12 | + useListPageFilter, |
| 13 | + VirtualizedTable, |
| 14 | +} from '@console/dynamic-plugin-sdk/src/lib-core'; |
| 15 | +import { TableData } from '@console/internal/components/factory'; |
| 16 | +import { useActiveColumns } from '@console/internal/components/factory/Table/active-columns-hook'; |
| 17 | +import { Kebab, ResourceLink } from '@console/internal/components/utils'; |
| 18 | +import { useK8sWatchResource } from '@console/internal/components/utils/k8s-watch-hook'; |
| 19 | +import { VolumeGroupSnapshotClassModel } from '@console/internal/models'; |
| 20 | +import { |
| 21 | + referenceFor, |
| 22 | + referenceForModel, |
| 23 | + Selector, |
| 24 | + VolumeGroupSnapshotClassKind, |
| 25 | +} from '@console/internal/module/k8s'; |
| 26 | +import { getAnnotations, LazyActionMenu } from '@console/shared'; |
| 27 | + |
| 28 | +const tableColumnInfo = [ |
| 29 | + { id: 'name' }, |
| 30 | + { className: classNames('pf-m-hidden', 'pf-m-visible-on-md'), id: 'driver' }, |
| 31 | + { className: classNames('pf-m-hidden', 'pf-m-visible-on-md'), id: 'deletionPolicy' }, |
| 32 | + { className: Kebab.columnClass, id: '' }, |
| 33 | +]; |
| 34 | + |
| 35 | +const defaultSnapshotClassAnnotation: string = 'snapshot.storage.kubernetes.io/is-default-class'; |
| 36 | +export const isDefaultSnapshotClass = (VolumeGroupSnapshotClass: VolumeGroupSnapshotClassKind) => |
| 37 | + getAnnotations(VolumeGroupSnapshotClass, { defaultSnapshotClassAnnotation: 'false' })[ |
| 38 | + defaultSnapshotClassAnnotation |
| 39 | + ] === 'true'; |
| 40 | + |
| 41 | +const Row: React.FC<RowProps<VolumeGroupSnapshotClassKind>> = ({ obj }) => { |
| 42 | + const { name } = obj?.metadata || {}; |
| 43 | + const { deletionPolicy, driver } = obj || {}; |
| 44 | + const { t } = useTranslation(); |
| 45 | + const resourceKind = referenceFor(obj); |
| 46 | + const context = { [resourceKind]: obj }; |
| 47 | + |
| 48 | + return ( |
| 49 | + <> |
| 50 | + <TableData {...tableColumnInfo[0]}> |
| 51 | + <ResourceLink name={name} kind={referenceForModel(VolumeGroupSnapshotClassModel)}> |
| 52 | + {isDefaultSnapshotClass(obj) && ( |
| 53 | + <span className="small text-muted co-resource-item__help-text"> |
| 54 | + {t('public~Default')} |
| 55 | + </span> |
| 56 | + )} |
| 57 | + </ResourceLink> |
| 58 | + </TableData> |
| 59 | + <TableData {...tableColumnInfo[1]}>{driver}</TableData> |
| 60 | + <TableData {...tableColumnInfo[2]}>{deletionPolicy}</TableData> |
| 61 | + <TableData {...tableColumnInfo[3]}> |
| 62 | + <LazyActionMenu context={context} /> |
| 63 | + </TableData> |
| 64 | + </> |
| 65 | + ); |
| 66 | +}; |
| 67 | + |
| 68 | +const VolumeGroupSnapshotClassTable: React.FC<VolumeGroupSnapshotClassTableProps> = (props) => { |
| 69 | + const { t } = useTranslation(); |
| 70 | + const getTableColumns = (): TableColumn<VolumeGroupSnapshotClassKind>[] => [ |
| 71 | + { |
| 72 | + title: t('console-app~Name'), |
| 73 | + sort: 'metadata.name', |
| 74 | + transforms: [sortable], |
| 75 | + id: tableColumnInfo[0].id, |
| 76 | + }, |
| 77 | + { |
| 78 | + title: t('console-app~Driver'), |
| 79 | + sort: 'driver', |
| 80 | + transforms: [sortable], |
| 81 | + props: { className: tableColumnInfo[1].className }, |
| 82 | + id: tableColumnInfo[1].id, |
| 83 | + }, |
| 84 | + { |
| 85 | + title: t('console-app~Deletion policy'), |
| 86 | + sort: 'deletionPolicy', |
| 87 | + transforms: [sortable], |
| 88 | + props: { className: tableColumnInfo[2].className }, |
| 89 | + id: tableColumnInfo[2].id, |
| 90 | + }, |
| 91 | + { |
| 92 | + title: '', |
| 93 | + props: { className: tableColumnInfo[3].className }, |
| 94 | + id: tableColumnInfo[3].id, |
| 95 | + }, |
| 96 | + ]; |
| 97 | + const [columns] = useActiveColumns({ columns: getTableColumns() }); |
| 98 | + |
| 99 | + return ( |
| 100 | + <VirtualizedTable<VolumeGroupSnapshotClassKind> |
| 101 | + {...props} |
| 102 | + aria-label={t('console-app~VolumeGroupSnapshotClasses')} |
| 103 | + label={t('console-app~VolumeGroupSnapshotClasses')} |
| 104 | + columns={columns} |
| 105 | + Row={Row} |
| 106 | + /> |
| 107 | + ); |
| 108 | +}; |
| 109 | + |
| 110 | +const VolumeGroupSnapshotClassPage: React.FC<VolumeGroupSnapshotClassPageProps> = ({ |
| 111 | + canCreate = true, |
| 112 | + showTitle = true, |
| 113 | + namespace, |
| 114 | + selector, |
| 115 | +}) => { |
| 116 | + const { t } = useTranslation(); |
| 117 | + const [resources, loaded, loadError] = useK8sWatchResource<VolumeGroupSnapshotClassKind[]>({ |
| 118 | + groupVersionKind: { |
| 119 | + group: VolumeGroupSnapshotClassModel.apiGroup, |
| 120 | + kind: VolumeGroupSnapshotClassModel.kind, |
| 121 | + version: VolumeGroupSnapshotClassModel.apiVersion, |
| 122 | + }, |
| 123 | + isList: true, |
| 124 | + namespaced: true, |
| 125 | + namespace, |
| 126 | + selector, |
| 127 | + }); |
| 128 | + const [data, filteredData, onFilterChange] = useListPageFilter(resources); |
| 129 | + const resourceKind = referenceForModel(VolumeGroupSnapshotClassModel); |
| 130 | + |
| 131 | + return ( |
| 132 | + <> |
| 133 | + <ListPageHeader |
| 134 | + title={showTitle ? t(VolumeGroupSnapshotClassModel.labelPluralKey) : undefined} |
| 135 | + > |
| 136 | + {canCreate && ( |
| 137 | + <ListPageCreate groupVersionKind={resourceKind}> |
| 138 | + {t('console-app~Create VolumeGroupSnapshotClass')} |
| 139 | + </ListPageCreate> |
| 140 | + )} |
| 141 | + </ListPageHeader> |
| 142 | + <ListPageBody> |
| 143 | + <ListPageFilter data={data} loaded={loaded} onFilterChange={onFilterChange} /> |
| 144 | + <VolumeGroupSnapshotClassTable |
| 145 | + unfilteredData={resources} |
| 146 | + data={filteredData} |
| 147 | + loaded={loaded} |
| 148 | + loadError={loadError} |
| 149 | + /> |
| 150 | + </ListPageBody> |
| 151 | + </> |
| 152 | + ); |
| 153 | +}; |
| 154 | + |
| 155 | +type VolumeGroupSnapshotClassPageProps = { |
| 156 | + namespace?: string; |
| 157 | + canCreate?: boolean; |
| 158 | + showTitle?: boolean; |
| 159 | + selector?: Selector; |
| 160 | +}; |
| 161 | + |
| 162 | +type VolumeGroupSnapshotClassTableProps = { |
| 163 | + data: VolumeGroupSnapshotClassKind[]; |
| 164 | + unfilteredData: VolumeGroupSnapshotClassKind[]; |
| 165 | + loaded: boolean; |
| 166 | + loadError: any; |
| 167 | +}; |
| 168 | +export default VolumeGroupSnapshotClassPage; |
0 commit comments