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

Feature number-count animation for DORA-cards #610

Merged
merged 6 commits into from
Oct 30, 2024
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 @@ -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;
};
Loading