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

Bug 1840543: Use undefined as default value for width hook #5642

Merged
merged 2 commits into from Jun 2, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions frontend/public/components/graphs/gauge.tsx
Expand Up @@ -9,7 +9,7 @@ import classNames from 'classnames';
import { PrometheusGraph, PrometheusGraphLink } from './prometheus-graph';
import { usePrometheusPoll } from './prometheus-poll-hook';
import { PrometheusEndpoint } from './helpers';
import { humanizePercentage, Humanize } from '../utils';
import { useRefWidth, humanizePercentage, Humanize } from '../utils';
import { getInstantVectorStats } from './utils';
import { DataPoint } from '.';

Expand All @@ -32,13 +32,24 @@ export const GaugeChart: React.FC<GaugeChartProps> = ({
secondaryTitle = usedLabel,
className,
}) => {
const [ref, width] = useRefWidth();
const ready = !error && !loading;
const status = loading ? 'Loading' : error;
const labels = ({ datum: { x, y } }) => (x ? `${x} ${usedLabel}` : `${y} ${remainderLabel}`);
return (
<PrometheusGraph className={classNames('graph-wrapper--title-center', className)} title={title}>
<PrometheusGraph
className={classNames('graph-wrapper--title-center graph-wrapper--gauge', className)}
ref={ref}
title={title}
>
<PrometheusGraphLink query={query}>
<ChartDonutThreshold data={thresholds} height={160} padding={0} width={160} y="value">
<ChartDonutThreshold
data={thresholds}
height={width} // Changes the scale of the graph, not actual width and height
padding={0}
width={width}
y="value"
>
<ChartDonutUtilization
labels={labels}
data={ready ? data : { y: 0 }}
Expand Down
6 changes: 3 additions & 3 deletions frontend/public/components/utils/ref-width-hook.ts
Expand Up @@ -2,12 +2,12 @@ import { useEffect, useRef, useState } from 'react';

export const useRefWidth = () => {
const ref = useRef<HTMLDivElement>(null);
const [width, setWidth] = useState(0);
const [width, setWidth] = useState<number>();

const clientWidth = ref?.current?.clientWidth ?? 0;
const clientWidth = ref?.current?.clientWidth;

useEffect(() => {
const handleResize = () => setWidth(ref?.current?.clientWidth ?? 0);
const handleResize = () => setWidth(ref?.current?.clientWidth);
window.addEventListener('resize', handleResize);
window.addEventListener('sidebar_toggle', handleResize);
return () => {
Expand Down