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

Converted CircularProgressbar.tsx to functional component #2624

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -7,7 +7,7 @@ exports[`MetricCard renders as expected when passCount is zero 1`] = `
<div
className="MetricCard--CircularProgressbarWrapper"
>
<CircularProgressbar
<Memo(CircularProgressbar)
backgroundHue={0}
decorationHue={0}
maxValue={255}
Expand Down Expand Up @@ -83,7 +83,7 @@ exports[`MetricCard renders as expected with details 1`] = `
<div
className="MetricCard--CircularProgressbarWrapper"
>
<CircularProgressbar
<Memo(CircularProgressbar)
decorationHue={120}
maxValue={363}
text="29.7%"
Expand Down Expand Up @@ -200,7 +200,7 @@ exports[`MetricCard renders as expected without details 1`] = `
<div
className="MetricCard--CircularProgressbarWrapper"
>
<CircularProgressbar
<Memo(CircularProgressbar)
decorationHue={120}
maxValue={363}
text="29.7%"
Expand Down Expand Up @@ -275,7 +275,7 @@ exports[`MetricCard renders as expected without details 2`] = `
<div
className="MetricCard--CircularProgressbarWrapper"
>
<CircularProgressbar
<Memo(CircularProgressbar)
decorationHue={120}
maxValue={363}
text="29.7%"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exports[`ScoreCard renders as expected when score is below max 1`] = `
<div
className="ScoreCard--CircularProgressbarWrapper"
>
<CircularProgressbar
<Memo([object Object])
decorationHue={120}
maxValue={108}
text="38.9%"
Expand Down Expand Up @@ -42,7 +42,7 @@ exports[`ScoreCard renders as expected when score is max 1`] = `
<div
className="ScoreCard--CircularProgressbarWrapper"
>
<CircularProgressbar
<Memo([object Object])
decorationHue={120}
maxValue={108}
text="100.0%"
Expand Down Expand Up @@ -72,7 +72,7 @@ exports[`ScoreCard renders as expected when score is zero 1`] = `
<div
className="ScoreCard--CircularProgressbarWrapper"
>
<CircularProgressbar
<Memo([object Object])
backgroundHue={0}
decorationHue={120}
maxValue={108}
Expand Down
65 changes: 38 additions & 27 deletions packages/jaeger-ui/src/components/common/CircularProgressbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,51 @@ type TProps = {
value: number;
};

export default class CircularProgressbar extends React.PureComponent<TProps> {
render() {
const { backgroundHue, decorationHue = 0, maxValue, strokeWidth, text, value } = this.props;
function CircularProgressbar({
backgroundHue,
decorationHue = 0,
maxValue,
strokeWidth,
text,
value,
}: TProps) {
const { decorationColor, decorationBackgroundColor } = React.useMemo(() => {
const scale = (value / maxValue) ** (1 / 4);
const saturation = 20 + Math.ceil(scale * 80);
const light = 50 + Math.ceil((1 - scale) * 30);
const decorationColor = `hsl(${decorationHue}, ${saturation}%, ${light}%)`;

const backgroundScale = ((maxValue - value) / maxValue) ** (1 / 4);
const backgroundSaturation = 20 + Math.ceil(backgroundScale * 80);
const backgroundLight = 50 + Math.ceil((1 - backgroundScale) * 30);
const decorationBackgroundColor = `hsl(${backgroundHue}, ${backgroundSaturation}%, ${backgroundLight}%)`;

return (
<div data-testid="circular-progress-bar">
<CircularProgressbarImpl
styles={{
path: {
stroke: decorationColor,
strokeLinecap: 'butt',
},
text: {
fill: decorationColor,
},
trail: {
stroke: backgroundHue !== undefined ? decorationBackgroundColor : 'transparent',
strokeLinecap: 'butt',
},
}}
maxValue={maxValue}
strokeWidth={strokeWidth}
text={text}
value={value}
/>
</div>
);
}
return { decorationColor, decorationBackgroundColor };
}, [backgroundHue, decorationHue, maxValue, value]);

return (
<div data-testid="circular-progress-bar">
<CircularProgressbarImpl
styles={{
path: {
stroke: decorationColor,
strokeLinecap: 'butt',
},
text: {
fill: decorationColor,
},
trail: {
stroke: backgroundHue !== undefined ? decorationBackgroundColor : 'transparent',
strokeLinecap: 'butt',
},
}}
maxValue={maxValue}
strokeWidth={strokeWidth}
text={text}
value={value}
/>
</div>
);
}

export default React.memo(CircularProgressbar);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`extractDecorationFromState prefers operation specific decoration over service decoration 1`] = `
<CircularProgressbar
<Memo(CircularProgressbar)
backgroundHue={120}
decorationHue={0}
maxValue={108}
Expand All @@ -12,7 +12,7 @@ exports[`extractDecorationFromState prefers operation specific decoration over s
`;

exports[`extractDecorationFromState returns service decoration 1`] = `
<CircularProgressbar
<Memo(CircularProgressbar)
backgroundHue={120}
decorationHue={0}
maxValue={108}
Expand Down
Loading