|
| 1 | +import { useState } from "react"; |
| 2 | +import { useSuspenseQuery } from "@tanstack/react-query"; |
| 3 | +import { |
| 4 | + DonutChart, |
| 5 | + ChartProps, |
| 6 | + getColorFromToken, |
| 7 | + DataVizPalette, |
| 8 | +} from "@fluentui/react-charts-preview"; |
| 9 | +import { tokens } from "@fluentui/react-components"; |
| 10 | + |
| 11 | +import { format_size } from "../../utils/format"; |
| 12 | +import { urbackupServer } from "../../App"; |
| 13 | + |
| 14 | +const colors = Object.entries(DataVizPalette) |
| 15 | + .filter(([k]) => k.includes("color")) |
| 16 | + .map(([, v]) => getColorFromToken(v)); |
| 17 | + |
| 18 | +export function StorageAllocation() { |
| 19 | + const clientsStorageUsageResult = useSuspenseQuery({ |
| 20 | + queryKey: ["client-storage-usage"], |
| 21 | + queryFn: () => urbackupServer.getPiegraphData(), |
| 22 | + }); |
| 23 | + |
| 24 | + const clientsStorageUsage = clientsStorageUsageResult.data; |
| 25 | + |
| 26 | + const data: ChartProps = { |
| 27 | + chartTitle: "Storage Allocation", |
| 28 | + chartData: clientsStorageUsage |
| 29 | + .sort((a, b) => a.data - b.data) |
| 30 | + .map((d, i) => ({ |
| 31 | + legend: d.label, |
| 32 | + data: d.data, |
| 33 | + yAxisCalloutData: format_size(d.data), |
| 34 | + // Don't use colours from palette if no data, to make legends |
| 35 | + // easier to parse. |
| 36 | + color: d.data ? colors[i] : tokens.colorNeutralForegroundDisabled, |
| 37 | + })), |
| 38 | + }; |
| 39 | + |
| 40 | + const [showChart, setShowChart] = useState(false); |
| 41 | + |
| 42 | + const isStorageInUse = data.chartData?.some((d) => d.data !== 0); |
| 43 | + |
| 44 | + if (!isStorageInUse) { |
| 45 | + return <span>No storage in use by clients</span>; |
| 46 | + } |
| 47 | + |
| 48 | + return ( |
| 49 | + <div |
| 50 | + style={{ |
| 51 | + visibility: showChart ? "initial" : "hidden", |
| 52 | + }} |
| 53 | + > |
| 54 | + <DonutChart |
| 55 | + onResize={() => { |
| 56 | + /** |
| 57 | + * Required to set the chart visible after it resizes itself |
| 58 | + * on initial render according to container dimensions. Without the check, |
| 59 | + * the chart flashes into the correct size after initial render. |
| 60 | + */ |
| 61 | + if (!showChart) { |
| 62 | + setShowChart(true); |
| 63 | + } |
| 64 | + }} |
| 65 | + data={data} |
| 66 | + innerRadius={100} |
| 67 | + legendProps={{ |
| 68 | + enabledWrapLines: true, |
| 69 | + allowFocusOnLegends: true, |
| 70 | + styles: { |
| 71 | + rect: "donut-chart__legend-rect", |
| 72 | + }, |
| 73 | + }} |
| 74 | + hideLabels={false} |
| 75 | + showLabelsInPercent |
| 76 | + styles={{ |
| 77 | + chart: "donut-chart", |
| 78 | + legendContainer: "donut-chart__legend", |
| 79 | + }} |
| 80 | + /> |
| 81 | + </div> |
| 82 | + ); |
| 83 | +} |
0 commit comments