Skip to content

Commit dbc4538

Browse files
committed
format quota and don't display negative values
1 parent 9e125ec commit dbc4538

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/components/Quota.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import { styled } from '@mui/material/styles';
1010
import AuthContext from 'context/authContext';
1111
import { useContext } from 'react';
1212

13-
import { formatBytes } from '../utils/utils';
13+
import { formatUsedQuotaInGB } from '../utils/utils';
1414

1515
export const Quota = () => {
1616
const { user } = useContext(AuthContext);
1717
const quota = user?.quota || 0;
1818
const usedQuota = user?.usedQuota || 0;
1919

20-
const percentage = ((usedQuota / quota) * 100).toFixed(1);
20+
const percentage = (usedQuota / quota) * 100;
21+
const displayedUsedQuota = percentage > 0 ? Math.max(0.1, Math.round(percentage * 10) / 10).toFixed(1) : "0.0";
2122

2223
const BorderLinearProgress = styled(LinearProgress)(() => ({
2324
height: 8,
@@ -60,7 +61,7 @@ export const Quota = () => {
6061
Quota
6162
</Typography>
6263
<Typography color="text.primary" fontWeight="600" variant="h4">
63-
{percentage}%
64+
{displayedUsedQuota}%
6465
</Typography>
6566
</Stack>
6667
</Stack>
@@ -73,7 +74,7 @@ export const Quota = () => {
7374
/>
7475
</Box>
7576
<Typography color="text.secondary" gutterBottom sx={{ mt: 3 }}>
76-
{formatBytes(usedQuota, 2)} GB of {formatBytes(quota, 2)} GB used
77+
{formatUsedQuotaInGB(usedQuota, 2)} GB of {formatUsedQuotaInGB(quota, 2)} GB used
7778
</Typography>
7879
</Box>
7980
) : (

src/utils/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,12 @@ export const generateImageHash = async (file: File) => {
7979
const base64String = hexToBase64(hashValue);
8080
return base64String;
8181
};
82+
83+
export const formatUsedQuotaInGB = (usedQuota: number, decimals = 2) => {
84+
if (usedQuota <= 0) return "0.0";
85+
86+
const rawGB = formatBytes(usedQuota, decimals);
87+
const gbValue = parseFloat(rawGB);
88+
89+
return gbValue < 0.1 ? "0.1" : `${rawGB}`;
90+
}

0 commit comments

Comments
 (0)