Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: location details chart #62

Merged
merged 1 commit into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,22 @@ import { useTheme } from '../../../../../../shared/defguard-ui/hooks/theme/useTh
import { LocationStats } from '../../../../types';
import { LocationUsageChartType } from './types';

type ChartBoxSpacing = {
top?: number;
bottom?: number;
left?: number;
right?: number;
};

interface LocationUsageProps {
data: LocationStats[];
type: LocationUsageChartType;
hideX?: boolean;
barSize?: number;
barGap?: number;
heightX?: number;
margin?: ChartBoxSpacing;
padding?: ChartBoxSpacing;
}

const parseStatsForCharts = (data: LocationStats[]): LocationStats[] => {
Expand Down Expand Up @@ -47,11 +56,33 @@ export const LocationUsageChart = ({
barGap = 2,
heightX = 50,
type,
margin,
padding,
}: LocationUsageProps) => {
const [totalUpload, totalDownload] = useMemo(() => totalUploadDownload(data), [data]);
const getFormattedData = useMemo(() => parseStatsForCharts(data), [data]);
const { colors } = useTheme();

const getMargin = useMemo((): ChartBoxSpacing => {
const defaultMargin: ChartBoxSpacing = {
top: 0,
left: 0,
right: 0,
bottom: 0,
};
return margin ?? defaultMargin;
}, [margin]);

const getPadding = useMemo((): ChartBoxSpacing => {
const defaultPadding: ChartBoxSpacing = {
bottom: 0,
right: 0,
left: 0,
top: 0,
};
return padding ?? defaultPadding;
}, [padding]);

if (!data.length) return null;

return (
Expand All @@ -67,7 +98,7 @@ export const LocationUsageChart = ({
width={size.width}
height={size.height}
data={getFormattedData}
margin={{ bottom: 0, left: 0, right: 0, top: 0 }}
margin={getMargin}
barSize={barSize}
barGap={barGap}
>
Expand All @@ -80,7 +111,7 @@ export const LocationUsageChart = ({
axisLine={{ stroke: colors.surfaceDefaultModal }}
tickLine={{ stroke: colors.surfaceDefaultModal }}
hide={hideX}
padding={{ left: 0, right: 0 }}
padding={getPadding}
tick={{
fontSize: 12,
color: '#222',
Expand Down Expand Up @@ -110,7 +141,7 @@ export const LocationUsageChart = ({
width={size.width}
height={size.height}
data={getFormattedData}
margin={{ bottom: 0, left: 0, right: 0, top: 0 }}
margin={getMargin}
>
<XAxis
dataKey="collected_at"
Expand All @@ -121,7 +152,7 @@ export const LocationUsageChart = ({
axisLine={{ stroke: colors.surfaceDefaultModal }}
tickLine={{ stroke: colors.surfaceDefaultModal }}
hide={hideX}
padding={{ left: 0, right: 0 }}
padding={getPadding}
tick={{
fontSize: 12,
color: '#222',
Expand All @@ -130,15 +161,25 @@ export const LocationUsageChart = ({
}}
tickFormatter={formatXTick}
domain={['dataMin', 'dataMax']}
interval={'preserveEnd'}
interval={'equidistantPreserveStart'}
/>
<YAxis
hide={true}
domain={['dataMin', 'dataMax']}
padding={{ top: 0, bottom: 0 }}
/>
<Line dataKey="download" stroke={colors.surfaceMainPrimary} />
<Line dataKey="upload" stroke={colors.textAlert} />
<Line
dataKey="download"
stroke={colors.surfaceMainPrimary}
strokeWidth={1}
dot={false}
/>
<Line
dataKey="upload"
stroke={colors.textAlert}
strokeWidth={1}
dot={false}
/>
</LineChart>
)}
</AutoSizer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ export const LocationsDetailView = ({ locations }: Props) => {
)}
{locationStats && locationStats.length ? (
<LocationUsageChart
barSize={4}
data={locationStats}
type={LocationUsageChartType.LINE}
margin={{ left: 20, right: 20 }}
/>
) : null}
{connectionHistory && connectionHistory.length ? (
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"baseUrl": "./",
"target": "ESNext",
"types": ["vite/client"],
"useDefineForClassFields": true,
Expand Down