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

141 lines
4.1 KiB
TypeScript
Raw Normal View History

2022-12-17 00:28:46 +09:00
import { createStyles, Group, Title } from '@mantine/core';
2022-12-10 22:14:31 +01:00
import { useQuery } from '@tanstack/react-query';
import axios from 'axios';
import { useTranslation } from 'next-i18next';
2022-12-17 00:28:46 +09:00
import { DashDotCompactNetwork, DashDotInfo } from './DashDotCompactNetwork';
2022-12-10 22:14:31 +01:00
import { BaseTileProps } from '../type';
import { DashDotGraph } from './DashDotGraph';
import { DashDotIntegrationType } from '../../../../types/integration';
import { IntegrationsMenu } from '../Integrations/IntegrationsMenu';
2022-12-14 10:32:16 +01:00
import { useConfigContext } from '../../../../config/provider';
import { HomarrCardWrapper } from '../HomarrCardWrapper';
2022-12-17 00:28:46 +09:00
import { DashDotCompactStorage } from './DashDotCompactStorage';
2022-12-10 22:14:31 +01:00
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;
2022-12-14 10:32:16 +01:00
const { data: info } = useDashDotInfo();
2022-12-10 22:14:31 +01:00
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 = (
<Title order={3} mb="xs">
{t('card.title')}
</Title>
);
const menu = (
<IntegrationsMenu<'dashDot'>
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 (
2022-12-14 10:32:16 +01:00
<HomarrCardWrapper className={className}>
2022-12-10 22:14:31 +01:00
{menu}
<div>
{heading}
<p>{t('card.errors.noService')}</p>
</div>
2022-12-14 10:32:16 +01:00
</HomarrCardWrapper>
2022-12-10 22:14:31 +01:00
);
}
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 (
2022-12-14 10:32:16 +01:00
<HomarrCardWrapper className={className}>
2022-12-10 22:14:31 +01:00
{menu}
{heading}
{!info && <p>{t('card.errors.noInformation')}</p>}
{info && (
<div className={classes.graphsContainer}>
<Group position="apart" w="100%">
2022-12-14 10:32:16 +01:00
{isCompactStorageVisible && <DashDotCompactStorage info={info} />}
2022-12-10 22:14:31 +01:00
{isCompactNetworkVisible && <DashDotCompactNetwork info={info} />}
</Group>
<Group position="center" w="100%" className={classes.graphsWrapper}>
{displayedGraphs?.map((graph) => (
<DashDotGraph
key={graph.id}
graph={graph}
dashDotUrl={dashDotUrl}
isCompact={isCompact}
/>
))}
</Group>
</div>
)}
2022-12-14 10:32:16 +01:00
</HomarrCardWrapper>
2022-12-10 22:14:31 +01:00
);
};
2022-12-14 10:32:16 +01:00
const useDashDotInfo = () => {
const { name: configName, config } = useConfigContext();
2022-12-10 22:14:31 +01:00
return useQuery({
queryKey: [
'dashdot/info',
{
2022-12-14 10:32:16 +01:00
configName,
url: config?.integrations.dashDot?.properties.url,
2022-12-10 22:14:31 +01:00
},
],
2022-12-14 10:32:16 +01:00
queryFn: () => fetchDashDotInfo(configName),
2022-12-10 22:14:31 +01:00
});
};
2022-12-14 10:32:16 +01:00
const fetchDashDotInfo = async (configName: string | undefined) => {
if (!configName) return {} as DashDotInfo;
2022-12-10 22:14:31 +01:00
return (await (
2022-12-14 10:32:16 +01:00
await axios.get('/api/modules/dashdot/info', { params: { configName } })
2022-12-10 22:14:31 +01:00
).data) as DashDotInfo;
};
export const useDashDotTileStyles = createStyles(() => ({
graphsContainer: {
display: 'flex',
flexDirection: 'row',
flexWrap: 'wrap',
rowGap: 10,
columnGap: 10,
},
graphsWrapper: {
2022-12-17 00:28:46 +09:00
'& > *:nth-child(odd):last-child': {
2022-12-10 22:14:31 +01:00
width: '100% !important',
maxWidth: '100% !important',
},
},
}));