|
| 1 | +import { useTranslation } from 'react-i18next'; |
| 2 | +import { |
| 3 | + AnalyticalTable, |
| 4 | + AnalyticalTableColumnDefinition, |
| 5 | + AnalyticalTableScaleWidthMode, |
| 6 | + Title, |
| 7 | + MultiComboBox, |
| 8 | + MultiComboBoxItem, |
| 9 | +} from '@ui5/webcomponents-react'; |
| 10 | +import useResource from '../../lib/api/useApiResource'; |
| 11 | +import '@ui5/webcomponents-icons/dist/sys-enter-2'; |
| 12 | +import '@ui5/webcomponents-icons/dist/sys-cancel-2'; |
| 13 | +import { ListNamespaces } from '../../lib/api/types/k8s/listNamespaces'; |
| 14 | +import { useEffect, useState } from 'react'; |
| 15 | +import { resourcesInterval } from '../../lib/shared/constants'; |
| 16 | +import { InstalationsRequest } from '../../lib/api/types/landscaper/listInstallations'; |
| 17 | + |
| 18 | +export function Landscapers() { |
| 19 | + const { t } = useTranslation(); |
| 20 | + |
| 21 | + // Namespaces z API |
| 22 | + const { data: namespaces, error: namespacesError } = useResource( |
| 23 | + ListNamespaces, |
| 24 | + { |
| 25 | + refreshInterval: resourcesInterval, |
| 26 | + }, |
| 27 | + ); |
| 28 | + |
| 29 | + const [selectedNamespaces, setSelectedNamespaces] = useState<string[]>([]); |
| 30 | + const [installations, setInstallations] = useState<any[]>([]); |
| 31 | + const [loading, setLoading] = useState(false); |
| 32 | + |
| 33 | + // Handler wyboru namespace’ów |
| 34 | + const handleSelectionChange = (e: CustomEvent) => { |
| 35 | + const selectedItems = Array.from(e.detail.items || []); |
| 36 | + const selectedValues = selectedItems.map((item: any) => item.text); |
| 37 | + setSelectedNamespaces(selectedValues); |
| 38 | + }; |
| 39 | + |
| 40 | + // Fetch installations, gdy zmienią się namespace’y |
| 41 | + useEffect(() => { |
| 42 | + const fetchInstallations = async () => { |
| 43 | + if (selectedNamespaces.length === 0) { |
| 44 | + setInstallations([]); |
| 45 | + return; |
| 46 | + } |
| 47 | + setLoading(true); |
| 48 | + try { |
| 49 | + const paths = selectedNamespaces |
| 50 | + .map((ns) => InstalationsRequest(ns).path) |
| 51 | + .filter((p): p is string => p !== null && p !== undefined); |
| 52 | + |
| 53 | + const allResponses = await Promise.all( |
| 54 | + paths.map((path) => fetch(path).then((res) => res.json())), |
| 55 | + ); |
| 56 | + |
| 57 | + const allItems = allResponses.flatMap((res) => res.items || []); |
| 58 | + setInstallations(allItems); |
| 59 | + } catch (error) { |
| 60 | + console.error(error); |
| 61 | + setInstallations([]); |
| 62 | + } finally { |
| 63 | + setLoading(false); |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + fetchInstallations(); |
| 68 | + }, [selectedNamespaces]); |
| 69 | + |
| 70 | + // Definicja kolumn tabeli |
| 71 | + const columns: AnalyticalTableColumnDefinition[] = [ |
| 72 | + { |
| 73 | + Header: t('Namespace'), |
| 74 | + accessor: 'metadata.namespace', |
| 75 | + }, |
| 76 | + { |
| 77 | + Header: t('Name'), |
| 78 | + accessor: 'metadata.name', |
| 79 | + }, |
| 80 | + { |
| 81 | + Header: t('Phase'), |
| 82 | + accessor: 'status.phase', |
| 83 | + }, |
| 84 | + { |
| 85 | + Header: t('Created At'), |
| 86 | + accessor: 'metadata.creationTimestamp', |
| 87 | + }, |
| 88 | + ]; |
| 89 | + |
| 90 | + return ( |
| 91 | + <> |
| 92 | + <Title level="H4">{t('Providers.headerProviders')}</Title> |
| 93 | + |
| 94 | + {namespaces && ( |
| 95 | + <MultiComboBox |
| 96 | + placeholder={t('Select namespace')} |
| 97 | + style={{ marginBottom: '1rem', maxWidth: '400px' }} |
| 98 | + onSelectionChange={handleSelectionChange} |
| 99 | + > |
| 100 | + {namespaces.map((ns) => ( |
| 101 | + <MultiComboBoxItem key={ns.metadata.name} text={ns.metadata.name} /> |
| 102 | + ))} |
| 103 | + </MultiComboBox> |
| 104 | + )} |
| 105 | + |
| 106 | + <AnalyticalTable |
| 107 | + columns={columns} |
| 108 | + data={installations} |
| 109 | + minRows={1} |
| 110 | + loading={loading} |
| 111 | + scaleWidthMode={AnalyticalTableScaleWidthMode.Smart} |
| 112 | + filterable |
| 113 | + retainColumnWidth |
| 114 | + reactTableOptions={{ |
| 115 | + autoResetHiddenColumns: false, |
| 116 | + autoResetPage: false, |
| 117 | + autoResetExpanded: false, |
| 118 | + autoResetGroupBy: false, |
| 119 | + autoResetSelectedRows: false, |
| 120 | + autoResetSortBy: false, |
| 121 | + autoResetFilters: false, |
| 122 | + autoResetRowState: false, |
| 123 | + autoResetResize: false, |
| 124 | + }} |
| 125 | + /> |
| 126 | + </> |
| 127 | + ); |
| 128 | +} |
0 commit comments