Open History to see the last commit
1import { useMemo } from "react";
2import { View } from "react-native";
3
4import type { DailyTotals } from "@t3tools/shared/usageMerge";
5
6import { buildChartDays, type UsageChartMetric } from "./usageChartData";
7import { useProviderColors } from "./usageProviders";
8
9export interface UsageDailyChartProps {
10 readonly days: readonly string[];
11 readonly daily: readonly DailyTotals[];
12 readonly metric: UsageChartMetric;
13 readonly height: number;
14}
15
16export function UsageDailyChart({ days, daily, metric, height }: UsageDailyChartProps) {
17 const colors = useProviderColors();
18 const chartDays = useMemo(() => buildChartDays(days, daily, metric), [days, daily, metric]);
19 const max = chartDays.reduce((peak, day) => Math.max(peak, day.total), 0);
20
21 return (
22 <View style={{ height }} className="flex-row items-end gap-px">
23 {/* column-reverse stacks the bottom-first provider values upward
24 without reversing the array (Hermes lacks Array#toReversed). */}
25 {chartDays.map((day) => (
26 <View key={day.day} className="h-full flex-1 flex-col-reverse overflow-hidden rounded-sm">
27 {day.values.map((entry) => (
28 <View
29 key={entry.provider}
30 style={{
31 height: max === 0 ? 0 : (entry.value / max) * height,
32 backgroundColor: colors[entry.provider],
33 }}
34 />
35 ))}
36 </View>
37 ))}
38 </View>
39 );
40}