| 1 |
import React from "react"; |
| 2 |
// PatternFly |
| 3 |
import { |
| 4 |
Button, |
| 5 |
Grid, |
| 6 |
GridItem, |
| 7 |
PageSection, |
| 8 |
ToolbarItemVariant, |
| 9 |
} from "@patternfly/react-core"; |
| 10 |
import TableLayout from "src/components/layouts/TableLayout"; |
| 11 |
import { Td, Th, Tr } from "@patternfly/react-table"; |
| 12 |
// components |
| 13 |
import { ToolbarItem } from "src/components/layouts/ToolbarLayout"; |
| 14 |
import PageLayout from "src/components/layouts/PageLayout"; |
| 15 |
import HelpTextWithIconLayout from "src/components/layouts/HelpTextWithIconLayout"; |
| 16 |
import SkeletonOnTableLayout from "src/components/layouts/Skeleton/SkeletonOnTableLayout"; |
| 17 |
// Redux |
| 18 |
import { useAppDispatch } from "src/store/hooks"; |
| 19 |
// Hooks |
| 20 |
import { addAlert } from "src/store/Global/alerts-slice"; |
| 21 |
// RPC |
| 22 |
import { useSubidStatsQuery } from "src/services/rpcSubIds"; |
| 23 |
|
| 24 |
interface SubidStats { |
| 25 |
assigned_subids: number; |
| 26 |
baseid: number; |
| 27 |
dna_remaining: number; |
| 28 |
rangesize: number; |
| 29 |
remaining_subids: number; |
| 30 |
} |
| 31 |
|
| 32 |
const SubIdsStatistics = () => { |
| 33 |
const dispatch = useAppDispatch(); |
| 34 |
|
| 35 |
// States |
| 36 |
const [subidStats, setSubidStats] = React.useState<SubidStats>({ |
| 37 |
assigned_subids: 0, |
| 38 |
baseid: 0, |
| 39 |
dna_remaining: 0, |
| 40 |
rangesize: 0, |
| 41 |
remaining_subids: 0, |
| 42 |
}); |
| 43 |
const [showTableRows, setShowTableRows] = React.useState<boolean>(false); |
| 44 |
|
| 45 |
// API call |
| 46 |
const subidStatsResponse = useSubidStatsQuery(); |
| 47 |
const { data, isLoading, error } = subidStatsResponse; |
| 48 |
|
| 49 |
React.useEffect(() => { |
| 50 |
setShowTableRows(!isLoading); |
| 51 |
|
| 52 |
if (!isLoading && error) { |
| 53 |
dispatch( |
| 54 |
addAlert({ |
| 55 |
name: "Error fetching data", |
| 56 |
title: JSON.stringify(error, null, 2), |
| 57 |
variant: "danger", |
| 58 |
}) |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
if (!isLoading && data) { |
| 63 |
const subidStats = data.result.result; |
| 64 |
setSubidStats(subidStats as unknown as SubidStats); |
| 65 |
} |
| 66 |
}, [data, isLoading]); |
| 67 |
|
| 68 |
// On refresh |
| 69 |
const onRefresh = () => { |
| 70 |
setShowTableRows(false); |
| 71 |
subidStatsResponse.refetch().then(() => { |
| 72 |
setShowTableRows(true); |
| 73 |
}); |
| 74 |
}; |
| 75 |
|
| 76 |
// List of Toolbar items |
| 77 |
const toolbarItems: ToolbarItem[] = [ |
| 78 |
{ |
| 79 |
key: 0, |
| 80 |
element: ( |
| 81 |
<Button |
| 82 |
variant="secondary" |
| 83 |
data-cy="subids-statistics-button-refresh" |
| 84 |
onClick={onRefresh} |
| 85 |
isDisabled={!showTableRows} |
| 86 |
> |
| 87 |
Refresh |
| 88 |
</Button> |
| 89 |
), |
| 90 |
}, |
| 91 |
{ |
| 92 |
key: 1, |
| 93 |
toolbarItemVariant: ToolbarItemVariant.separator, |
| 94 |
}, |
| 95 |
{ |
| 96 |
key: 2, |
| 97 |
element: <HelpTextWithIconLayout textContent="Help" />, |
| 98 |
}, |
| 99 |
]; |
| 100 |
|
| 101 |
// Table |
| 102 |
// - Data |
| 103 |
const headerData = [ |
| 104 |
"Base ID", |
| 105 |
"Assigned Subordinate IDs", |
| 106 |
"Remaining Subordinate IDs", |
| 107 |
"Range Size", |
| 108 |
"DNA Remaining", |
| 109 |
]; |
| 110 |
|
| 111 |
// - Header |
| 112 |
const header = ( |
| 113 |
<Tr> |
| 114 |
{headerData.map((entry, idx) => ( |
| 115 |
<Th modifier="wrap" key={idx}> |
| 116 |
{entry} |
| 117 |
</Th> |
| 118 |
))} |
| 119 |
</Tr> |
| 120 |
); |
| 121 |
|
| 122 |
// - Body |
| 123 |
const body = ( |
| 124 |
<Tr> |
| 125 |
<Td dataLabel="Base ID">{subidStats.baseid}</Td> |
| 126 |
<Td dataLabel="Assigned Subordinate IDs">{subidStats.assigned_subids}</Td> |
| 127 |
<Td dataLabel="Remaining Subordinate IDs"> |
| 128 |
{subidStats.remaining_subids} |
| 129 |
</Td> |
| 130 |
<Td dataLabel="Range Size">{subidStats.rangesize}</Td> |
| 131 |
<Td dataLabel="DNA Remaining">{subidStats.dna_remaining}</Td> |
| 132 |
</Tr> |
| 133 |
); |
| 134 |
|
| 135 |
const skeleton = ( |
| 136 |
<SkeletonOnTableLayout |
| 137 |
rows={1} |
| 138 |
colSpan={5} |
| 139 |
screenreaderText={"Loading table rows"} |
| 140 |
/> |
| 141 |
); |
| 142 |
|
| 143 |
return ( |
| 144 |
<PageLayout |
| 145 |
title="Subordinate ID Statistics" |
| 146 |
pathname="subordinate-id-statistics" |
| 147 |
hasAlerts={true} |
| 148 |
toolbarItems={toolbarItems} |
| 149 |
> |
| 150 |
<PageSection hasBodyWrapper={false}> |
| 151 |
<Grid hasGutter> |
| 152 |
<GridItem span={12}> |
| 153 |
<TableLayout |
| 154 |
ariaLabel={"subordinate id statistics table"} |
| 155 |
variant="compact" |
| 156 |
hasBorders={true} |
| 157 |
classes={"pf-v6-u-mt-md"} |
| 158 |
tableId={"subid-stats-table"} |
| 159 |
tableHeader={header} |
| 160 |
tableBody={!showTableRows ? skeleton : body} |
| 161 |
isStickyHeader={false} |
| 162 |
/> |
| 163 |
</GridItem> |
| 164 |
</Grid> |
| 165 |
</PageSection> |
| 166 |
</PageLayout> |
| 167 |
); |
| 168 |
}; |
| 169 |
|
| 170 |
export default SubIdsStatistics; |