🏗️ Migrate dashdot storage to tRPC

This commit is contained in:
Meier Lukas
2023-06-10 13:26:33 +02:00
parent 05e01286d4
commit b1adcf673f
4 changed files with 30 additions and 31 deletions

View File

@@ -1,20 +1,18 @@
import { Group, Stack, Text } from '@mantine/core';
import { useQuery } from '@tanstack/react-query';
import axios from 'axios';
import { useTranslation } from 'next-i18next';
import { useConfigContext } from '../../config/provider';
import { api } from '~/utils/api';
import { bytes } from '../../tools/bytesHelper';
import { percentage } from '../../tools/shared/math/percentage.tool';
import { DashDotInfo } from './DashDotCompactNetwork';
interface DashDotCompactStorageProps {
info: DashDotInfo;
widgetId: string;
url: string;
}
export const DashDotCompactStorage = ({ info, widgetId }: DashDotCompactStorageProps) => {
export const DashDotCompactStorage = ({ info, url }: DashDotCompactStorageProps) => {
const { t } = useTranslation('modules/dashdot');
const { data: storageLoad } = useDashDotStorage(widgetId);
const { data: storageLoad } = useDashDotStorage(url);
const totalUsed = calculateTotalLayoutSize({
layout: storageLoad ?? [],
@@ -55,25 +53,7 @@ interface CalculateTotalLayoutSizeProps<TLayoutItem> {
key?: keyof TLayoutItem;
}
const useDashDotStorage = (widgetId: string) => {
const { name: configName, config } = useConfigContext();
return useQuery({
queryKey: [
'dashdot/storage',
{
configName,
url: config?.widgets.find((x) => x.type === 'dashdot')?.properties.url,
widgetId,
},
],
queryFn: () => fetchDashDotStorageLoad(configName, widgetId),
const useDashDotStorage = (url: string) =>
api.dashDot.storage.useQuery({
url,
});
};
async function fetchDashDotStorageLoad(configName: string | undefined, widgetId: string) {
if (!configName) throw new Error('configName is undefined');
return (await (
await axios.get('/api/modules/dashdot/storage', { params: { configName, widgetId } })
).data) as number[];
}