|
| 1 | + |
| 2 | +import { useTranslation } from 'react-i18next'; |
| 3 | +import { AnalyticalTable, AnalyticalTableColumnDefinition, AnalyticalTableScaleWidthMode, Icon, Title } from '@ui5/webcomponents-react'; |
| 4 | +import useResource from '../../lib/api/useApiResource'; |
| 5 | +import IllustratedError from '../Shared/IllustratedError'; |
| 6 | +import '@ui5/webcomponents-icons/dist/sys-enter-2'; |
| 7 | +import '@ui5/webcomponents-icons/dist/sys-cancel-2'; |
| 8 | +import { ProvidersListRequest } from '../../lib/api/types/crossplane/listProviders'; |
| 9 | +import { resourcesInterval } from '../../lib/shared/constants'; |
| 10 | +import { timeAgo } from '../../utils/i18n/timeAgo'; |
| 11 | +import { StatusCellProps } from '../../lib/shared/interfaces'; |
| 12 | + |
| 13 | +interface CellData<T> { |
| 14 | + cell: { |
| 15 | + value: T | null; // null for grouping rows |
| 16 | + row: { |
| 17 | + original?: ProvidersRow; // missing for grouping rows |
| 18 | + } |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +type ProvidersRow = { |
| 23 | + name: string |
| 24 | + version: string; |
| 25 | + healthy: boolean; |
| 26 | + healthyTransitionTime: string; |
| 27 | + installed: boolean; |
| 28 | + installedTransitionTime: string; |
| 29 | + created: string; |
| 30 | +} |
| 31 | + |
| 32 | +export function Providers() { |
| 33 | + const { t } = useTranslation(); |
| 34 | + |
| 35 | + let {data: providers, error, isLoading} = useResource(ProvidersListRequest, { |
| 36 | + refreshInterval: resourcesInterval |
| 37 | + }); |
| 38 | + |
| 39 | + const columns: AnalyticalTableColumnDefinition[] = [ |
| 40 | + { |
| 41 | + Header: t('Providers.tableHeaderName'), |
| 42 | + accessor: 'name', |
| 43 | + }, |
| 44 | + { |
| 45 | + Header: t('Providers.tableHeaderVersion'), |
| 46 | + accessor: 'version', |
| 47 | + }, |
| 48 | + { |
| 49 | + Header: t('Providers.tableHeaderInstalled'), |
| 50 | + accessor: 'installed', |
| 51 | + Cell: (cellData: CellData<ProvidersRow['installed']>) => cellData.cell.row.original?.installed != null ? <ResourceStatusCell value={cellData.cell.row.original?.installed} transitionTime={cellData.cell.row.original?.installedTransitionTime} /> : null |
| 52 | + }, |
| 53 | + { |
| 54 | + Header: t('Providers.tableHeaderHealthy'), |
| 55 | + accessor: 'healthy', |
| 56 | + Cell: (cellData: CellData<ProvidersRow['healthy']>) => cellData.cell.row.original?.installed != null ? <ResourceStatusCell value={cellData.cell.row.original?.healthy} transitionTime={cellData.cell.row.original?.healthyTransitionTime} /> : null |
| 57 | + }, |
| 58 | + { |
| 59 | + Header: t('Providers.tableHeaderCreated'), |
| 60 | + accessor: 'created', |
| 61 | + }, |
| 62 | + ]; |
| 63 | + |
| 64 | + const rows: ProvidersRow[] = |
| 65 | + providers?.items?.map((item) => { |
| 66 | + const installed = item.status.conditions?.find((condition) => condition.type === 'Installed'); |
| 67 | + const healthy = item.status.conditions?.find((condition) => condition.type === 'Healthy'); |
| 68 | + |
| 69 | + return { |
| 70 | + name: item.metadata.name, |
| 71 | + created: timeAgo.format(new Date(item.metadata.creationTimestamp)), |
| 72 | + installed: installed?.status === "True", |
| 73 | + installedTransitionTime: installed?.lastTransitionTime ?? "", |
| 74 | + healthy: healthy?.status === "True", |
| 75 | + healthyTransitionTime: healthy?.lastTransitionTime ?? "", |
| 76 | + version: item.spec.package.match(/\d+(\.\d+)+/g)?.toString() ?? "", |
| 77 | + } |
| 78 | + }) |
| 79 | + ?? []; |
| 80 | + |
| 81 | + return ( |
| 82 | + <> |
| 83 | + <Title level='H4'>{t('Providers.headerProviders')}</Title> |
| 84 | + |
| 85 | + {error && <IllustratedError error={error}/>} |
| 86 | + |
| 87 | + {!error && |
| 88 | + <AnalyticalTable |
| 89 | + columns={columns} |
| 90 | + data={rows} |
| 91 | + minRows={1} |
| 92 | + scaleWidthMode={AnalyticalTableScaleWidthMode.Smart} |
| 93 | + loading={isLoading} |
| 94 | + filterable |
| 95 | + // Prevent the table from resetting when the data changes |
| 96 | + retainColumnWidth |
| 97 | + reactTableOptions={{ |
| 98 | + autoResetHiddenColumns: false, |
| 99 | + autoResetPage: false, |
| 100 | + autoResetExpanded: false, |
| 101 | + autoResetGroupBy: false, |
| 102 | + autoResetSelectedRows: false, |
| 103 | + autoResetSortBy: false, |
| 104 | + autoResetFilters: false, |
| 105 | + autoResetRowState: false, |
| 106 | + autoResetResize: false |
| 107 | + }} |
| 108 | + /> |
| 109 | + } |
| 110 | + </> |
| 111 | + ) |
| 112 | +} |
| 113 | + |
| 114 | +function ResourceStatusCell({ value, transitionTime }: StatusCellProps) { |
| 115 | + return <Icon |
| 116 | + design={value ? 'Positive' : 'Negative'} |
| 117 | + name={value ? 'sys-enter-2' : 'sys-cancel-2'} |
| 118 | + showTooltip={true} |
| 119 | + accessibleName={timeAgo.format(new Date(transitionTime))} |
| 120 | + /> |
| 121 | +} |
0 commit comments