Files
Homarr/src/widgets/dashDot/DashDotGraph.tsx

99 lines
2.4 KiB
TypeScript
Raw Normal View History

import { createStyles, Title, useMantineTheme } from '@mantine/core';
import { DashDotCompactNetwork, DashDotInfo } from './DashDotCompactNetwork';
import { DashDotCompactStorage } from './DashDotCompactStorage';
2022-12-10 22:14:31 +01:00
interface DashDotGraphProps {
graph: string;
graphHeight: number;
2022-12-10 22:14:31 +01:00
isCompact: boolean;
multiView: boolean;
2022-12-10 22:14:31 +01:00
dashDotUrl: string;
usePercentages: boolean;
info: DashDotInfo;
2022-12-10 22:14:31 +01:00
}
export const DashDotGraph = ({
graph,
graphHeight,
isCompact,
multiView,
dashDotUrl,
usePercentages,
info,
}: DashDotGraphProps) => {
2022-12-10 22:14:31 +01:00
const { classes } = useStyles();
return graph === 'storage' && isCompact ? (
<DashDotCompactStorage info={info} />
) : graph === 'network' && isCompact ? (
<DashDotCompactNetwork info={info} />
) : (
<div className={classes.graphContainer}>
2022-12-10 22:14:31 +01:00
<Title className={classes.graphTitle} order={4}>
{graph}
2022-12-10 22:14:31 +01:00
</Title>
<iframe
className={classes.iframe}
key={graph}
title={graph}
src={useIframeSrc(dashDotUrl, graph, multiView, usePercentages)}
style={{
height: `${graphHeight}px`,
}}
2022-12-10 22:14:31 +01:00
/>
</div>
2022-12-10 22:14:31 +01:00
);
};
const useIframeSrc = (
dashDotUrl: string,
graph: string,
multiView: boolean,
usePercentages: boolean
) => {
2022-12-10 22:14:31 +01:00
const { colorScheme, colors, radius } = useMantineTheme();
const surface = (colorScheme === 'dark' ? colors.dark[7] : colors.gray[0]).substring(1); // removes # from hex value
2022-12-18 21:21:23 +01:00
2022-12-10 22:14:31 +01:00
return (
`${dashDotUrl}` +
'?singleGraphMode=true' + // obsolete in newer versions
`&graph=${graph}` +
2022-12-10 22:14:31 +01:00
`&theme=${colorScheme}` +
`&surface=${surface}` +
'&gap=5' +
2022-12-10 22:14:31 +01:00
`&innerRadius=${radius.lg}` +
`&multiView=${multiView}` +
`&showPercentage=${usePercentages.toString()}` +
'&textOffset=16' +
'&textSize=12'
2022-12-10 22:14:31 +01:00
);
};
export const useStyles = createStyles((theme, _params, getRef) => ({
iframe: {
flex: '1 0 auto',
maxWidth: '100%',
width: '100%',
2022-12-10 22:14:31 +01:00
borderRadius: theme.radius.lg,
2022-12-30 15:28:30 +01:00
border: 'none',
colorScheme: 'light', // fixes white borders around iframe
2022-12-10 22:14:31 +01:00
},
graphTitle: {
ref: getRef('graphTitle'),
position: 'absolute',
right: 0,
bottom: 0,
2022-12-10 22:14:31 +01:00
opacity: 0,
transition: 'opacity .1s ease-in-out',
pointerEvents: 'none',
marginBottom: 12,
marginRight: 12,
2022-12-10 22:14:31 +01:00
},
graphContainer: {
2022-12-10 22:14:31 +01:00
position: 'relative',
[`&:hover .${getRef('graphTitle')}`]: {
opacity: 0.5,
},
},
}));