import { Card, createStyles, Flex, Grid, Group, Stack, Title, useMantineTheme, } from '@mantine/core'; import { useQuery } from '@tanstack/react-query'; import axios from 'axios'; import { useTranslation } from 'next-i18next'; import { DashDotCompactNetwork } from './DashDotCompactNetwork'; import { DashDotCompactStorage } from './DashDotCompactStorage'; import { BaseTileProps } from '../type'; import { DashDotGraph } from './DashDotGraph'; import { DashDotIntegrationType } from '../../../../types/integration'; import { IntegrationsMenu } from '../Integrations/IntegrationsMenu'; interface DashDotTileProps extends BaseTileProps { module: DashDotIntegrationType | undefined; } export const DashDotTile = ({ module, className }: DashDotTileProps) => { const { classes } = useDashDotTileStyles(); const { t } = useTranslation('modules/dashdot'); const dashDotUrl = module?.properties.url; const { data: info } = useDashDotInfo(dashDotUrl); const graphs = module?.properties.graphs.map((g) => ({ id: g, name: t(`card.graphs.${g === 'ram' ? 'memory' : g}.title`), twoSpan: ['network', 'gpu'].includes(g), isMultiView: (g === 'cpu' && module.properties.isCpuMultiView) || (g === 'storage' && module.properties.isStorageMultiView), })); const heading = ( {t('card.title')} ); const menu = ( module={module} integration="dashDot" options={module?.properties} labels={{ isCpuMultiView: 'descriptor.settings.cpuMultiView.label', isStorageMultiView: 'descriptor.settings.storageMultiView.label', isCompactView: 'descriptor.settings.useCompactView.label', graphs: 'descriptor.settings.graphs.label', url: 'descriptor.settings.url.label', }} /> ); if (!dashDotUrl) { return ( {menu}
{heading}

{t('card.errors.noService')}

); } const isCompact = module?.properties.isCompactView ?? false; const isCompactStorageVisible = graphs?.some((g) => g.id === 'storage' && isCompact); const isCompactNetworkVisible = graphs?.some((g) => g.id === 'network' && isCompact); const displayedGraphs = graphs?.filter( (g) => !isCompact || !['network', 'storage'].includes(g.id) ); return ( {menu} {heading} {!info &&

{t('card.errors.noInformation')}

} {info && (
{isCompactStorageVisible && ( )} {isCompactNetworkVisible && } {displayedGraphs?.map((graph) => ( ))}
)}
); }; const useDashDotInfo = (dashDotUrl: string | undefined) => { return useQuery({ queryKey: [ 'dashdot/info', { dashDotUrl, }, ], queryFn: () => fetchDashDotInfo(dashDotUrl), }); }; const fetchDashDotInfo = async (targetUrl: string | undefined) => { if (!targetUrl) return {} as DashDotInfo; return (await ( await axios.get('/api/modules/dashdot', { params: { url: '/info', base: targetUrl } }) ).data) as DashDotInfo; }; export interface DashDotInfo { storage: { layout: { size: number }[]; }; network: { speedUp: number; speedDown: number; }; } export const useDashDotTileStyles = createStyles(() => ({ graphsContainer: { display: 'flex', flexDirection: 'row', flexWrap: 'wrap', rowGap: 10, columnGap: 10, }, graphsWrapper: { [`& > *:nth-child(odd):last-child`]: { width: '100% !important', maxWidth: '100% !important', }, }, }));