Files
Homarr/src/components/Dashboard/Tiles/DashDot/DashDotCompactStorage.tsx

82 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-12-10 22:14:31 +01:00
import { Group, Stack, Text } from '@mantine/core';
import { useQuery } from '@tanstack/react-query';
import axios from 'axios';
import { useTranslation } from 'next-i18next';
2022-12-14 10:32:16 +01:00
import { useConfigContext } from '../../../../config/provider';
2022-12-10 22:14:31 +01:00
import { bytes } from '../../../../tools/bytesHelper';
import { percentage } from '../../../../tools/percentage';
import { DashDotInfo } from './DashDotTile';
interface DashDotCompactStorageProps {
info: DashDotInfo;
}
2022-12-14 10:32:16 +01:00
export const DashDotCompactStorage = ({ info }: DashDotCompactStorageProps) => {
2022-12-10 22:14:31 +01:00
const { t } = useTranslation('modules/dashdot');
2022-12-14 10:32:16 +01:00
const { data: storageLoad } = useDashDotStorage();
2022-12-10 22:14:31 +01:00
const totalUsed = calculateTotalLayoutSize({
layout: storageLoad?.layout ?? [],
key: 'load',
});
const totalSize = calculateTotalLayoutSize({
2022-12-14 10:32:16 +01:00
layout: info?.storage?.layout ?? [],
2022-12-10 22:14:31 +01:00
key: 'size',
});
return (
<Group noWrap align="start" position="apart" w={'100%'} maw={'251px'}>
<Text weight={500}>{t('card.graphs.storage.label')}</Text>
<Stack align="end" spacing={0}>
<Text color="dimmed" size="xs">
{percentage(totalUsed, totalSize)}%
</Text>
<Text color="dimmed" size="xs">
{bytes.toString(totalUsed)} / {bytes.toString(totalSize)}
</Text>
</Stack>
</Group>
);
};
const calculateTotalLayoutSize = <TLayoutItem,>({
layout,
key,
}: CalculateTotalLayoutSizeProps<TLayoutItem>) => {
return layout.reduce((total, current) => {
return total + (current[key] as number);
}, 0);
};
interface CalculateTotalLayoutSizeProps<TLayoutItem> {
layout: TLayoutItem[];
key: keyof TLayoutItem;
}
2022-12-14 10:32:16 +01:00
const useDashDotStorage = () => {
const { name: configName, config } = useConfigContext();
return useQuery({
queryKey: [
'dashdot/storage',
{
configName,
url: config?.integrations.dashDot?.properties.url,
},
],
queryFn: () => fetchDashDotStorageLoad(configName),
});
};
const fetchDashDotStorageLoad = async (configName: string | undefined) => {
console.log('storage request: ' + configName);
if (!configName) return;
2022-12-10 22:14:31 +01:00
return (await (
2022-12-14 10:32:16 +01:00
await axios.get('/api/modules/dashdot/storage', { params: { configName } })
2022-12-10 22:14:31 +01:00
).data) as DashDotStorageLoad;
};
interface DashDotStorageLoad {
layout: { load: number }[];
}