Replace react-spring with motion, fix breaking changes in react-window/date-fns-tz/pure-rand.

This commit is contained in:
Mike Cao committed 2026-04-18 15:41:25 -07:00
1 parent df29c16c00
commit cc829c8f6f
9 files changed
+132 -110

No files matched your search

+2 -2
View File
@@ -1,3 +1,3 @@
import { type AnimatedComponent, animated } from '@react-spring/web';
import { motion } from 'motion/react';
export const AnimatedDiv: AnimatedComponent<any> = animated.div;
export const AnimatedDiv = motion.div;
+5 -5
View File
@@ -1,4 +1,4 @@
import { formatInTimeZone, utcToZonedTime, zonedTimeToUtc } from 'date-fns-tz';
import { formatInTimeZone, fromZonedTime, toZonedTime } from 'date-fns-tz';
import { TIMEZONE_CONFIG, TIMEZONE_LEGACY } from '@/lib/constants';
import { getTimezone } from '@/lib/date';
import { setItem } from '@/lib/storage';
@@ -61,19 +61,19 @@ export function useTimezone() {
};
const toUtc = (date: Date | string | number) => {
return zonedTimeToUtc(date, timezone);
return fromZonedTime(date, timezone);
};
const fromUtc = (date: Date | string | number) => {
return utcToZonedTime(date, timezone);
return toZonedTime(date, timezone);
};
const localToUtc = (date: Date | string | number) => {
return zonedTimeToUtc(date, localTimeZone);
return fromZonedTime(date, localTimeZone);
};
const localFromUtc = (date: Date | string | number) => {
return utcToZonedTime(date, localTimeZone);
return toZonedTime(date, localTimeZone);
};
const canonicalizeTimezone = (timezone: string): string => {
+29 -22
View File
@@ -1,7 +1,7 @@
import { config, useSpring } from '@react-spring/web';
import { Column, Grid, Row, Text } from '@umami/react-zen';
import type { ReactNode } from 'react';
import { FixedSizeList } from 'react-window';
import { useSpring, useTransform } from 'motion/react';
import { type ReactNode, useEffect } from 'react';
import { List, type RowComponentProps } from 'react-window';
import { AnimatedDiv } from '@/components/common/AnimatedDiv';
import { Empty } from '@/components/common/Empty';
import { useMessages, useMobile } from '@/components/hooks';
@@ -62,7 +62,7 @@ export function ListTable({
);
};
const ListTableRow = ({ index, style }) => {
const ListTableRow = ({ index, style }: RowComponentProps) => {
return <div style={style}>{getRow(data[index], index)}</div>;
};
@@ -82,14 +82,14 @@ export function ListTable({
<Column gap="1">
{data?.length === 0 && <Empty />}
{virtualize && data.length > 0 ? (
<FixedSizeList
width="100%"
height={itemCount * ITEM_SIZE}
itemCount={data.length}
itemSize={ITEM_SIZE}
>
{ListTableRow}
</FixedSizeList>
<List
style={{ width: '100%', height: itemCount * ITEM_SIZE }}
defaultHeight={itemCount * ITEM_SIZE}
rowCount={data.length}
rowHeight={ITEM_SIZE}
rowComponent={ListTableRow}
rowProps={{}}
/>
) : (
data.map(getRow)
)}
@@ -108,12 +108,21 @@ const AnimatedRow = ({
formatCount,
isPhone,
}) => {
const props = useSpring({
width: percent,
y: !Number.isNaN(value) ? value : 0,
from: { width: 0, y: 0 },
config: animate ? config.default : { duration: 0 },
});
const y = !Number.isNaN(value) ? value : 0;
const ySpring = useSpring(0, { stiffness: 170, damping: 26 });
const widthSpring = useSpring(0, { stiffness: 170, damping: 26 });
const yText = useTransform(ySpring, n => (formatCount ? formatCount(n) : formatLongNumber(n)));
const widthText = useTransform(widthSpring, n => `${n?.toFixed?.(0)}%`);
useEffect(() => {
if (animate) {
ySpring.set(y);
widthSpring.set(percent);
} else {
ySpring.jump(y);
widthSpring.jump(percent);
}
}, [y, percent, animate, ySpring, widthSpring]);
return (
<Grid
@@ -136,9 +145,7 @@ const AnimatedRow = ({
>
{change}
<Text weight="bold">
<AnimatedDiv title={props?.y as any}>
{formatCount ? props.y?.to(formatCount) : props.y?.to(formatLongNumber)}
</AnimatedDiv>
<AnimatedDiv title={String(value)}>{yText}</AnimatedDiv>
</Text>
</Row>
{showPercentage && (
@@ -151,7 +158,7 @@ const AnimatedRow = ({
color="muted"
paddingLeft="3"
>
<AnimatedDiv>{props.width.to(n => `${n?.toFixed?.(0)}%`)}</AnimatedDiv>
<AnimatedDiv>{widthText}</AnimatedDiv>
</Row>
)}
</Grid>
+18 -6
View File
@@ -1,6 +1,6 @@
import { useSpring } from '@react-spring/web';
import { Button, Column, Icon, Row, Text, Tooltip, TooltipTrigger } from '@umami/react-zen';
import type { ReactNode } from 'react';
import { useSpring, useTransform } from 'motion/react';
import { type ReactNode, useEffect } from 'react';
import { AnimatedDiv } from '@/components/common/AnimatedDiv';
import { Info } from '@/components/icons';
import { ChangeLabel } from '@/components/metrics/ChangeLabel';
@@ -30,8 +30,20 @@ export const MetricCard = ({
}: MetricCardProps) => {
const diff = value - change;
const pct = diff !== 0 ? ((value - diff) / diff) * 100 : value !== 0 ? 100 : 0;
const props = useSpring({ x: Number(value) || 0, from: { x: 0 } });
const changeProps = useSpring({ x: Number(pct) || 0, from: { x: 0 } });
const x = Number(value) || 0;
const p = Number(pct) || 0;
const xSpring = useSpring(0, { stiffness: 170, damping: 26 });
const pctSpring = useSpring(0, { stiffness: 170, damping: 26 });
const valueText = useTransform(xSpring, n => formatValue(n));
const pctText = useTransform(pctSpring, n => `${Math.abs(~~n)}%`);
useEffect(() => {
xSpring.set(x);
}, [x, xSpring]);
useEffect(() => {
pctSpring.set(p);
}, [p, pctSpring]);
return (
<Column
@@ -61,11 +73,11 @@ export const MetricCard = ({
</Row>
)}
<Text size="4xl" weight="bold" wrap="nowrap">
<AnimatedDiv title={value?.toString()}>{props?.x?.to(x => formatValue(x))}</AnimatedDiv>
<AnimatedDiv title={value?.toString()}>{valueText}</AnimatedDiv>
</Text>
{showChange && (
<ChangeLabel value={change} title={formatValue(change)} reverseColors={reverseColors}>
<AnimatedDiv>{changeProps?.x?.to(x => `${Math.abs(~~x)}%`)}</AnimatedDiv>
<AnimatedDiv>{pctText}</AnimatedDiv>
</ChangeLabel>
)}
</Column>
+11 -7
View File
@@ -1,6 +1,6 @@
import { config, useSpring } from '@react-spring/web';
import { Column, Text } from '@umami/react-zen';
import { useRef } from 'react';
import { useSpring, useTransform } from 'motion/react';
import { useEffect, useRef } from 'react';
import { AnimatedDiv } from '@/components/common/AnimatedDiv';
import { Badge } from '@/components/common/Badge';
import { useMessages } from '@/components/hooks';
@@ -45,10 +45,14 @@ export const PerformanceCard = ({
const metricChanged = prevMetricRef.current !== metric;
if (metricChanged) prevMetricRef.current = metric;
const spring = useSpring({
value: !Number.isNaN(value) ? value : 0,
config: metricChanged ? { duration: 0 } : config.default,
});
const target = !Number.isNaN(value) ? value : 0;
const spring = useSpring(target, { stiffness: 170, damping: 26 });
const display = useTransform(spring, n => formatValue(n));
useEffect(() => {
if (metricChanged) spring.jump(target);
else spring.set(target);
}, [target, metricChanged, spring]);
return (
<Column
@@ -67,7 +71,7 @@ export const PerformanceCard = ({
{label}
</Text>
<Text size="4xl" weight="bold" wrap="nowrap">
<AnimatedDiv>{spring.value.to(n => formatValue(n))}</AnimatedDiv>
<AnimatedDiv>{display}</AnimatedDiv>
</Text>
<Badge variant={RATING_VARIANTS[rating]}>
{t(labels[rating === 'needs-improvement' ? 'needsImprovement' : rating])}