|
| 1 | +import { LinearGradient } from '@visx/gradient'; |
| 2 | +import { ParentSize } from '@visx/responsive'; |
| 3 | +import { AreaSeries, type AxisScale, Tooltip, XYChart } from '@visx/xychart'; |
| 4 | +import { useMemo } from 'react'; |
| 5 | +import { Text, View } from 'react-native'; |
| 6 | +import useSWR from 'swr'; |
| 7 | + |
| 8 | +import { Label } from '~/common/styleguide'; |
| 9 | +import ThreeDotsLoader from '~/components/Package/ThreeDotsLoader'; |
| 10 | +import { TimeRange } from '~/util/datetime'; |
| 11 | +import tw from '~/util/tailwind'; |
| 12 | + |
| 13 | +type DataMap = Record<string, number>; |
| 14 | +type Point = { date: Date; value: number }; |
| 15 | + |
| 16 | +type Props = { |
| 17 | + packageName: string; |
| 18 | + height?: number; |
| 19 | +}; |
| 20 | + |
| 21 | +const COLOR = '#2e9ab8'; |
| 22 | +const DATE_FORMAT = { |
| 23 | + month: 'short' as const, |
| 24 | + day: '2-digit' as const, |
| 25 | + year: '2-digit' as const, |
| 26 | +}; |
| 27 | + |
| 28 | +export default function DownloadsChart({ packageName, height = 48 }: Props) { |
| 29 | + const { data } = useSWR( |
| 30 | + `/api/proxy/npm-stat?name=${packageName}`, |
| 31 | + (url: string) => fetch(url).then(res => res.json()), |
| 32 | + { |
| 33 | + dedupingInterval: TimeRange.HOUR * 1000, |
| 34 | + revalidateOnFocus: false, |
| 35 | + } |
| 36 | + ); |
| 37 | + |
| 38 | + const series = useMemo( |
| 39 | + () => (data && Object.keys(data).length ? mapData(data[packageName]) : null), |
| 40 | + [data, packageName] |
| 41 | + ); |
| 42 | + |
| 43 | + const yDomain = useMemo(() => { |
| 44 | + if (!series?.length) { |
| 45 | + return undefined; |
| 46 | + } |
| 47 | + |
| 48 | + const max = series.reduce((acc, p) => Math.max(acc, p.value), 0); |
| 49 | + const startPadding = Math.max(1, max * 0.15); |
| 50 | + |
| 51 | + return [0, max + startPadding]; |
| 52 | + }, [series]); |
| 53 | + |
| 54 | + return ( |
| 55 | + <ParentSize> |
| 56 | + {({ width }) => { |
| 57 | + if (data && !Object.keys(data).length) { |
| 58 | + return ( |
| 59 | + <View style={tw`h-full justify-center items-center`}> |
| 60 | + <Label style={tw`text-secondary font-light`}>Cannot fetch download data</Label> |
| 61 | + </View> |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + if (!width || !data || !series) { |
| 66 | + return ( |
| 67 | + <View style={tw`h-full justify-center items-center`}> |
| 68 | + <ThreeDotsLoader /> |
| 69 | + </View> |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + return ( |
| 74 | + <XYChart |
| 75 | + width={width} |
| 76 | + height={height + 4} |
| 77 | + xScale={{ type: 'time' }} |
| 78 | + yScale={{ type: 'linear', nice: true, domain: yDomain }} |
| 79 | + margin={{ top: 0, right: 0, bottom: 0, left: 0 }}> |
| 80 | + <LinearGradient |
| 81 | + id="area-gradient" |
| 82 | + from={COLOR} |
| 83 | + to={COLOR} |
| 84 | + fromOpacity={tw.prefixMatch('dark') ? 0.3 : 0.5} |
| 85 | + toOpacity={0} |
| 86 | + /> |
| 87 | + <AreaSeries<AxisScale, AxisScale, Point> |
| 88 | + dataKey="area" |
| 89 | + data={series} |
| 90 | + xAccessor={(p: Point) => p.date.getTime()} |
| 91 | + yAccessor={(p: Point) => p.value} |
| 92 | + fill="url(#area-gradient)" |
| 93 | + /> |
| 94 | + <Tooltip<Point> |
| 95 | + showVerticalCrosshair |
| 96 | + verticalCrosshairStyle={{ |
| 97 | + stroke: COLOR, |
| 98 | + strokeWidth: 0.5, |
| 99 | + strokeDasharray: '4 1', |
| 100 | + }} |
| 101 | + offsetLeft={8} |
| 102 | + offsetTop={10} |
| 103 | + detectBounds |
| 104 | + unstyled |
| 105 | + applyPositionStyle |
| 106 | + renderTooltip={({ tooltipData }) => { |
| 107 | + const data = tooltipData?.nearestDatum?.datum; |
| 108 | + if (!data) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + return ( |
| 112 | + <Text |
| 113 | + style={tw`flex flex-col text-xs bg-black text-white font-sans font-light px-2.5 py-1.5 rounded dark:border dark:border-default`}> |
| 114 | + <span style={tw`text-palette-gray3 dark:text-secondary`}> |
| 115 | + {data.date.toLocaleDateString('en-US', DATE_FORMAT)} |
| 116 | + ' |
| 117 | + </span> |
| 118 | + <span>{data.value.toLocaleString()}</span> |
| 119 | + </Text> |
| 120 | + ); |
| 121 | + }} |
| 122 | + /> |
| 123 | + </XYChart> |
| 124 | + ); |
| 125 | + }} |
| 126 | + </ParentSize> |
| 127 | + ); |
| 128 | +} |
| 129 | + |
| 130 | +function mapData(dataMap: DataMap): Point[] { |
| 131 | + return Object.entries(dataMap).map(([date, value]) => ({ |
| 132 | + date: new Date(date + 'T00:00:00Z'), |
| 133 | + value, |
| 134 | + })); |
| 135 | +} |
0 commit comments