Skip to content

Commit

Permalink
Fix rounding issue and use proper units (#2516)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkirsz committed Oct 31, 2022
1 parent 7f1bb8a commit 1565a6d
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions docs/components/pages/pack-home/PackBenchmarksGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function GraphBar({
.then(() => {
setFinished(true);
});
const timerAnimationRef = animate(0, duration / 1000, {
const timerAnimationRef = animate(0, duration, {
...transition,
ease: "linear",
onUpdate(value) {
Expand Down Expand Up @@ -203,14 +203,26 @@ function GraphBar({
className="pr-2"
transition={{ duration: 0.1 }}
>
<GraphTimer turbo={turbo} timer={pinTime ? duration / 1000 : timer} />
<GraphTimer
turbo={turbo}
timer={pinTime ? duration : timer}
duration={duration}
/>
</motion.div>
</div>
</div>
);
}

const GraphTimer = ({ turbo, timer }: { turbo: boolean; timer: number }) => {
const GraphTimer = ({
turbo,
timer,
duration,
}: {
turbo: boolean;
timer: number;
duration: number;
}) => {
return (
<div className={`flex flex-row gap-2 w-24 justify-end items-center z-10`}>
{turbo && (
Expand Down Expand Up @@ -238,11 +250,44 @@ const GraphTimer = ({ turbo, timer }: { turbo: boolean; timer: number }) => {
/>
</div>
)}
<p className="font-mono">{timer.toFixed(2)}s</p>
<p className="font-mono">
<Time value={timer} maxValue={duration} />
</p>
</div>
);
};

function roundTo(num: number, decimals: number) {
const factor = Math.pow(10, decimals);
return Math.round(num * factor) / factor;
}

const Time = ({
value,
maxValue,
}: {
value: number;
maxValue: number;
}): JSX.Element => {
let unitValue: string;
let unit: string;
if (maxValue < 1000) {
unitValue = Math.round(value).toFixed(0);
unit = "ms";
} else {
const roundedValue = roundTo(value / 1000, 1);
unitValue = roundedValue.toFixed(1);
unit = "s";
}

return (
<>
{unitValue}
{unit}
</>
);
};

function GraphLabel({
label,
turbo,
Expand Down

1 comment on commit 1565a6d

@vercel
Copy link

@vercel vercel bot commented on 1565a6d Oct 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.