Skip to content

Commit

Permalink
Feature number-count animation for DORA-cards
Browse files Browse the repository at this point in the history
* chore: downgrade axios version

* feat: add count-up hook

* feat: add the count-up animation in Dora Cards

* refactor: update code as per review comment

* fix: linting issue
  • Loading branch information
xyfer17 authored Oct 30, 2024
1 parent 2b7ec16 commit e625e8e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
NoDataImg
} from '@/content/DoraMetrics/DoraCards/sharedComponents';
import { useAuth } from '@/hooks/useAuth';
import { useCountUp } from '@/hooks/useCountUp';
import { useDoraMetricsGraph } from '@/hooks/useDoraMetricsGraph';
import {
useStateDateConfig,
Expand Down Expand Up @@ -80,6 +81,12 @@ export const ChangeFailureRateCard = () => {
.incident_count
);

const changeFailureRateCount = useCountUp(
changeFailureRateProps.count || 0,
1500, // animation duration
2 // decimal place
);

const series = useMemo(
() => [
{
Expand Down Expand Up @@ -195,7 +202,7 @@ export const ChangeFailureRateCard = () => {
lineHeight={1}
>
{changeFailureRateProps.count ? (
`${Number(changeFailureRateProps.count.toFixed(2))}%`
`${Number(changeFailureRateCount.toFixed(2))}%`
) : (
<NoIncidentsLabel
deploymentsCount={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
} from '@/content/DoraMetrics/DoraCards/sharedComponents';
import { usePropsForChangeTimeCard } from '@/content/DoraMetrics/DoraCards/sharedHooks';
import { useAuth } from '@/hooks/useAuth';
import { useCountUp } from '@/hooks/useCountUp';
import { useSelector } from '@/store';
import { ChangeTimeModes } from '@/types/resources';
import { merge } from '@/utils/datatype';
Expand Down Expand Up @@ -100,6 +101,8 @@ export const ChangeTimeCard = () => {
[activeModeProps.backgroundColor, mergedLeadTimeTrends]
);

const leadTimeDuration = useCountUp(activeModeProps.count || 0);

return (
<CardRoot
onClick={() => {
Expand Down Expand Up @@ -293,7 +296,7 @@ export const ChangeTimeCard = () => {
color={activeModeProps.color}
sx={{ fontSize: '3em' }}
>
{getDurationString(activeModeProps.count) || 0}
{getDurationString(leadTimeDuration) || 0}
</Line>
{Boolean(activeModeProps.count || prevChangeTime) && (
<DoraMetricsComparisonPill
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
CardRoot,
NoDataImg
} from '@/content/DoraMetrics/DoraCards/sharedComponents';
import { useCountUp } from '@/hooks/useCountUp';
import { useDoraMetricsGraph } from '@/hooks/useDoraMetricsGraph';
import { getDurationString } from '@/utils/date';

Expand Down Expand Up @@ -76,6 +77,8 @@ export const MeanTimeToRestoreCard = () => {
]
);

const meanTimeToRestoreCount = useCountUp(meanTimeToRestoreProps.count || 0);

const { addPage } = useOverlayPage();

return (
Expand Down Expand Up @@ -168,7 +171,7 @@ export const MeanTimeToRestoreCard = () => {
lineHeight={1}
>
{meanTimeToRestoreProps.count ? (
getDurationString(meanTimeToRestoreProps.count)
getDurationString(meanTimeToRestoreCount)
) : (
<NoIncidentsLabel />
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
NoDataImg
} from '@/content/DoraMetrics/DoraCards/sharedComponents';
import { useAuth } from '@/hooks/useAuth';
import { useCountUp } from '@/hooks/useCountUp';
import {
useCurrentDateRangeLabel,
useStateDateConfig
Expand Down Expand Up @@ -55,6 +56,10 @@ export const WeeklyDeliveryVolumeCard = () => {
const dateRangeLabel = useCurrentDateRangeLabel();
const deploymentFrequencyProps = useAvgIntervalBasedDeploymentFrequency();

const deploymentFrequencyCount = useCountUp(
deploymentFrequencyProps.count || 0
);

const { addPage } = useOverlayPage();
const deploymentsConfigured = true;
const isCodeProviderIntegrationEnabled = integrationSet.has(
Expand Down Expand Up @@ -205,7 +210,7 @@ export const WeeklyDeliveryVolumeCard = () => {
sx={{ fontSize: '3em' }}
>
{deploymentFrequencyProps.count ? (
`${deploymentFrequencyProps.count}`
`${deploymentFrequencyCount}`
) : (
<Line>No Deployments</Line>
)}
Expand Down
33 changes: 33 additions & 0 deletions web-server/src/hooks/useCountUp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useState, useEffect } from 'react';

const FRAME_DURATION_MS = 16; // Average frame duration for 60fps

export const useCountUp = (
targetValue: number,
duration: number = 1500,
decimalPlaces: number = 0
): number => {
const [count, setCount] = useState<number>(0);

useEffect(() => {
let start = 0;
const increment = targetValue / (duration / FRAME_DURATION_MS);

const animateCount = () => {
start += increment;

if (start >= targetValue) {
setCount(parseFloat(targetValue.toFixed(decimalPlaces)));
} else {
setCount(parseFloat(start.toFixed(decimalPlaces)));
requestAnimationFrame(animateCount);
}
};

animateCount();

return () => {};
}, [targetValue, duration, decimalPlaces]);

return count;
};

0 comments on commit e625e8e

Please sign in to comment.