From 0bda6c28aea5b6ca49abcc75a0957fc6adf37782 Mon Sep 17 00:00:00 2001 From: Peter Makowski Date: Tue, 30 Jan 2024 16:51:33 +0100 Subject: [PATCH] chore: use formatBytes from react components --- package.json | 2 +- .../UploadTextArea/UploadTextArea.tsx | 4 +- .../AddLogicalVolume/AddLogicalVolume.tsx | 55 +++++--- .../AddPartition/AddPartition.tsx | 55 +++++--- .../StorageTable/PoolSelect/PoolSelect.tsx | 33 +++-- .../KVMResourceMeter/KVMResourceMeter.tsx | 3 +- .../StoragePopover/StoragePopover.tsx | 17 ++- .../StorageCards/StorageCards.tsx | 16 +-- .../StorageResources/StorageResources.tsx | 9 +- src/app/kvm/utils.ts | 3 +- .../StorageColumn/StorageColumn.tsx | 4 +- src/app/utils/formatBytes.test.ts | 119 +----------------- src/app/utils/formatBytes.ts | 74 ++--------- src/app/utils/formatSpeedUnits.ts | 4 +- src/app/utils/index.ts | 1 - yarn.lock | 21 +--- 16 files changed, 140 insertions(+), 280 deletions(-) diff --git a/package.json b/package.json index 683a20a72e7..d7ccfa40b3c 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,7 @@ "build-storybook": "storybook build" }, "dependencies": { - "@canonical/maas-react-components": "1.15.0", + "@canonical/maas-react-components": "1.19.1", "@canonical/macaroon-bakery": "1.3.2", "@canonical/react-components": "0.46.0", "@reduxjs/toolkit": "1.9.3", diff --git a/src/app/base/components/UploadTextArea/UploadTextArea.tsx b/src/app/base/components/UploadTextArea/UploadTextArea.tsx index 056813f61fd..5c306dc8aab 100644 --- a/src/app/base/components/UploadTextArea/UploadTextArea.tsx +++ b/src/app/base/components/UploadTextArea/UploadTextArea.tsx @@ -1,6 +1,7 @@ import type { ChangeEvent } from "react"; import { useState } from "react"; +import { formatBytes } from "@canonical/maas-react-components"; import type { TextareaProps } from "@canonical/react-components"; import { Icon, Textarea } from "@canonical/react-components"; import { useFormikContext } from "formik"; @@ -9,7 +10,6 @@ import { useDropzone } from "react-dropzone"; import FormikField from "@/app/base/components/FormikField"; import { useId } from "@/app/base/hooks/base"; import type { AnyObject } from "@/app/base/types"; -import { formatBytes } from "@/app/utils"; type Props = { label: string; @@ -53,7 +53,7 @@ export const UploadTextArea = ({ } const file = files[0]; if (file.size > maxSize) { - const byteSize = formatBytes(maxSize, "B"); + const byteSize = formatBytes({ value: maxSize, unit: "B" }); setFileErrors( `File cannot be larger than ${byteSize.value}${byteSize.unit}.` ); diff --git a/src/app/base/components/node/StorageTables/AvailableStorageTable/AddLogicalVolume/AddLogicalVolume.tsx b/src/app/base/components/node/StorageTables/AvailableStorageTable/AddLogicalVolume/AddLogicalVolume.tsx index 5abb41c2036..33a58faf357 100644 --- a/src/app/base/components/node/StorageTables/AvailableStorageTable/AddLogicalVolume/AddLogicalVolume.tsx +++ b/src/app/base/components/node/StorageTables/AvailableStorageTable/AddLogicalVolume/AddLogicalVolume.tsx @@ -1,3 +1,4 @@ +import { formatBytes } from "@canonical/maas-react-components"; import { useDispatch, useSelector } from "react-redux"; import * as Yup from "yup"; @@ -13,7 +14,6 @@ import type { MachineEventErrors } from "@/app/store/machine/types/base"; import { isMachineDetails } from "@/app/store/machine/utils"; import type { RootState } from "@/app/store/root/types"; import type { Disk } from "@/app/store/types/node"; -import { formatBytes } from "@/app/utils"; export type AddLogicalVolumeValues = { fstype?: string; @@ -47,16 +47,22 @@ const generateSchema = (availableSize: number) => .test("enoughSpace", "Not enough space", function test() { const values: AddLogicalVolumeValues = this.parent; const { size, unit } = values; - const sizeInBytes = formatBytes(size, unit, { - convertTo: "B", - roundFunc: "floor", - }).value; + const sizeInBytes = formatBytes( + { value: size, unit: unit }, + { + convertTo: "B", + roundFunc: "floor", + } + ).value; if (sizeInBytes < MIN_PARTITION_SIZE) { - const min = formatBytes(MIN_PARTITION_SIZE, "B", { - convertTo: unit, - roundFunc: "floor", - }).value; + const min = formatBytes( + { value: MIN_PARTITION_SIZE, unit: "B" }, + { + convertTo: unit, + roundFunc: "floor", + } + ).value; return this.createError({ message: `At least ${min}${unit} is required to add a logical volume`, path: "size", @@ -64,10 +70,13 @@ const generateSchema = (availableSize: number) => } if (sizeInBytes > availableSize) { - const max = formatBytes(availableSize, "B", { - convertTo: unit, - roundFunc: "floor", - }).value; + const max = formatBytes( + { value: availableSize, unit: "B" }, + { + convertTo: unit, + roundFunc: "floor", + } + ).value; return this.createError({ message: `Only ${max}${unit} available in this volume group`, path: "size", @@ -114,10 +123,13 @@ export const AddLogicalVolume = ({ mountOptions: "", mountPoint: "", name: initialName, - size: formatBytes(disk.available_size, "B", { - convertTo: "GB", - roundFunc: "floor", - }).value, + size: formatBytes( + { value: disk.available_size, unit: "B" }, + { + convertTo: "GB", + roundFunc: "floor", + } + ).value, tags: [], unit: "GB", }} @@ -131,9 +143,12 @@ export const AddLogicalVolume = ({ const { fstype, mountOptions, mountPoint, name, size, tags, unit } = values; // Convert size into bytes before dispatching action - const convertedSize = formatBytes(size, unit, { - convertTo: "B", - })?.value; + const convertedSize = formatBytes( + { value: size, unit: unit }, + { + convertTo: "B", + } + )?.value; const params = { name, size: convertedSize, diff --git a/src/app/base/components/node/StorageTables/AvailableStorageTable/AddPartition/AddPartition.tsx b/src/app/base/components/node/StorageTables/AvailableStorageTable/AddPartition/AddPartition.tsx index 8c8af23ca3d..94fb9d3aed5 100644 --- a/src/app/base/components/node/StorageTables/AvailableStorageTable/AddPartition/AddPartition.tsx +++ b/src/app/base/components/node/StorageTables/AvailableStorageTable/AddPartition/AddPartition.tsx @@ -1,3 +1,4 @@ +import { formatBytes } from "@canonical/maas-react-components"; import { useDispatch, useSelector } from "react-redux"; import * as Yup from "yup"; @@ -13,7 +14,6 @@ import type { MachineEventErrors } from "@/app/store/machine/types/base"; import { isMachineDetails } from "@/app/store/machine/utils"; import type { RootState } from "@/app/store/root/types"; import type { Disk } from "@/app/store/types/node"; -import { formatBytes } from "@/app/utils"; export type AddPartitionValues = { fstype?: string; @@ -43,16 +43,22 @@ const generateSchema = (availableSize: number) => .test("enoughSpace", "Not enough space", function test() { const values: AddPartitionValues = this.parent; const { partitionSize, unit } = values; - const sizeInBytes = formatBytes(partitionSize, unit, { - convertTo: "B", - roundFunc: "floor", - }).value; + const sizeInBytes = formatBytes( + { value: partitionSize, unit }, + { + convertTo: "B", + roundFunc: "floor", + } + ).value; if (sizeInBytes < MIN_PARTITION_SIZE) { - const min = formatBytes(MIN_PARTITION_SIZE, "B", { - convertTo: unit, - roundFunc: "floor", - }).value; + const min = formatBytes( + { value: MIN_PARTITION_SIZE, unit: "B" }, + { + convertTo: unit, + roundFunc: "floor", + } + ).value; return this.createError({ message: `At least ${min}${unit} is required to partition this disk`, path: "partitionSize", @@ -60,10 +66,13 @@ const generateSchema = (availableSize: number) => } if (sizeInBytes > availableSize) { - const max = formatBytes(availableSize, "B", { - convertTo: unit, - roundFunc: "floor", - }).value; + const max = formatBytes( + { value: availableSize, unit: "B" }, + { + convertTo: unit, + roundFunc: "floor", + } + ).value; return this.createError({ message: `Only ${max}${unit} available in this disk`, path: "partitionSize", @@ -107,10 +116,13 @@ export const AddPartition = ({ fstype: "", mountOptions: "", mountPoint: "", - partitionSize: formatBytes(disk.available_size, "B", { - convertTo: "GB", - roundFunc: "floor", - }).value, + partitionSize: formatBytes( + { value: disk.available_size, unit: "B" }, + { + convertTo: "GB", + roundFunc: "floor", + } + ).value, unit: "GB", }} onCancel={closeExpanded} @@ -124,9 +136,12 @@ export const AddPartition = ({ const { fstype, mountOptions, mountPoint, partitionSize, unit } = values; // Convert size into bytes before dispatching action - const size = formatBytes(partitionSize, unit, { - convertTo: "B", - })?.value; + const size = formatBytes( + { value: partitionSize, unit }, + { + convertTo: "B", + } + )?.value; const params = { blockId: disk.id, partitionSize: size, diff --git a/src/app/kvm/components/KVMForms/ComposeForm/StorageTable/PoolSelect/PoolSelect.tsx b/src/app/kvm/components/KVMForms/ComposeForm/StorageTable/PoolSelect/PoolSelect.tsx index 78684074916..920a4b85a95 100644 --- a/src/app/kvm/components/KVMForms/ComposeForm/StorageTable/PoolSelect/PoolSelect.tsx +++ b/src/app/kvm/components/KVMForms/ComposeForm/StorageTable/PoolSelect/PoolSelect.tsx @@ -1,4 +1,4 @@ -import { Meter } from "@canonical/maas-react-components"; +import { Meter, formatBytes } from "@canonical/maas-react-components"; import { ContextualMenu } from "@canonical/react-components"; import { useFormikContext } from "formik"; import { useSelector } from "react-redux"; @@ -10,7 +10,6 @@ import { getSortedPoolsArray } from "@/app/kvm/utils"; import podSelectors from "@/app/store/pod/selectors"; import type { Pod, PodDetails } from "@/app/store/pod/types"; import type { RootState } from "@/app/store/root/types"; -import { formatBytes } from "@/app/utils"; type RequestMap = { [location: string]: number }; @@ -23,10 +22,13 @@ type Props = { }; const byteDisplay = (bytes: number, roundDown = false): number => - formatBytes(bytes, "B", { - convertTo: "GB", - roundFunc: roundDown ? "floor" : "round", - }).value; + formatBytes( + { value: bytes, unit: "B" }, + { + convertTo: "GB", + roundFunc: roundDown ? "floor" : "round", + } + ).value; const generateDropdownContent = ( pod: PodDetails, @@ -68,19 +70,26 @@ const generateDropdownContent = ( // Convert requests into bytes const requested = requests[name] - ? formatBytes(requests[name], "GB", { convertTo: "B" }).value + ? formatBytes( + { value: requests[name], unit: "GB" }, + { convertTo: "B" } + ).value : 0; const pendingRequest = isSelected ? 0 - : formatBytes(disk.size, "GB", { convertTo: "B" }).value; + : formatBytes({ value: disk.size, unit: "GB" }, { convertTo: "B" }) + .value; // Free amount is the actual space available in the pool, less any // existing storage requests (including the current request). const freeBytes = pool.total - pool.allocated_tracked - pool.allocated_other; - const free = formatBytes(freeBytes - requested - pendingRequest, "B", { - convertTo: "B", - roundFunc: "floor", - }).value; + const free = formatBytes( + { value: freeBytes - requested - pendingRequest, unit: "B" }, + { + convertTo: "B", + roundFunc: "floor", + } + ).value; return (