|
| 1 | +import { useSession } from "@/hooks" |
| 2 | +import { trpc } from "@/trpc" |
| 3 | +import { |
| 4 | + CardContent, |
| 5 | + CardDescription, |
| 6 | + CardTitle, |
| 7 | + CardHeader, |
| 8 | + ChartContainer, |
| 9 | + ChartTooltip, |
| 10 | + ChartTooltipContent, |
| 11 | + ChartConfig, |
| 12 | + CardFooter, |
| 13 | + Card, |
| 14 | + Skeleton, |
| 15 | +} from "@repo/ui" |
| 16 | +import dayjs from "dayjs" |
| 17 | +import { ArrowDown, TrendingUp } from "lucide-react" |
| 18 | +import { Area, AreaChart, CartesianGrid, XAxis } from "recharts" |
| 19 | + |
| 20 | +const chartConfig = { |
| 21 | + totalSubscribers: { |
| 22 | + label: "Total Subscribers", |
| 23 | + color: "hsl(var(--chart-1))", |
| 24 | + }, |
| 25 | +} satisfies ChartConfig |
| 26 | + |
| 27 | +export const SubscriberGrowthChart = () => { |
| 28 | + const { organization } = useSession() |
| 29 | + |
| 30 | + const { data: analytics, isLoading: analyticsLoading } = |
| 31 | + trpc.stats.getStats.useQuery( |
| 32 | + { |
| 33 | + organizationId: organization?.id ?? "", |
| 34 | + }, |
| 35 | + { |
| 36 | + enabled: !!organization?.id, |
| 37 | + } |
| 38 | + ) |
| 39 | + |
| 40 | + const { data: dashboard, isLoading: dashboardLoading } = |
| 41 | + trpc.dashboard.getStats.useQuery( |
| 42 | + { |
| 43 | + organizationId: organization?.id ?? "", |
| 44 | + }, |
| 45 | + { |
| 46 | + enabled: !!organization?.id, |
| 47 | + } |
| 48 | + ) |
| 49 | + |
| 50 | + const isLoading = analyticsLoading || dashboardLoading |
| 51 | + |
| 52 | + return ( |
| 53 | + <Card hoverEffect className="col-span-4"> |
| 54 | + <CardHeader> |
| 55 | + <CardTitle>Subscriber Growth</CardTitle> |
| 56 | + <CardDescription> |
| 57 | + New subscribers over time{" "} |
| 58 | + <span className="text-xs text-muted-foreground">(Daily)</span> |
| 59 | + </CardDescription> |
| 60 | + </CardHeader> |
| 61 | + <CardContent> |
| 62 | + {isLoading ? ( |
| 63 | + <Skeleton className="h-[200px] w-full" /> |
| 64 | + ) : ( |
| 65 | + <ChartContainer config={chartConfig}> |
| 66 | + <AreaChart |
| 67 | + accessibilityLayer |
| 68 | + data={dashboard?.subscriberGrowth || []} |
| 69 | + margin={{ |
| 70 | + left: 12, |
| 71 | + right: 12, |
| 72 | + }} |
| 73 | + > |
| 74 | + <CartesianGrid vertical={false} /> |
| 75 | + <XAxis |
| 76 | + dataKey="date" |
| 77 | + tickLine={false} |
| 78 | + axisLine={false} |
| 79 | + tickMargin={8} |
| 80 | + tickFormatter={(value) => dayjs(value).format("DD MMM")} |
| 81 | + /> |
| 82 | + <ChartTooltip |
| 83 | + cursor={false} |
| 84 | + content={<ChartTooltipContent indicator="dot" />} |
| 85 | + /> |
| 86 | + <Area |
| 87 | + dataKey="count" |
| 88 | + type="natural" |
| 89 | + fill="var(--color-totalSubscribers)" |
| 90 | + fillOpacity={0.4} |
| 91 | + stroke="var(--color-totalSubscribers)" |
| 92 | + stackId="a" |
| 93 | + /> |
| 94 | + </AreaChart> |
| 95 | + </ChartContainer> |
| 96 | + )} |
| 97 | + </CardContent> |
| 98 | + <CardFooter> |
| 99 | + {isLoading ? ( |
| 100 | + <Skeleton className="h-10 w-full" /> |
| 101 | + ) : ( |
| 102 | + <div className="flex w-full items-start gap-2 text-sm"> |
| 103 | + <div className="grid gap-2"> |
| 104 | + <div className="flex items-center gap-2 font-medium leading-none"> |
| 105 | + {(analytics?.subscribers?.newThisMonth || 0) >= 0 ? ( |
| 106 | + <> |
| 107 | + Trending up by {analytics?.subscribers.newThisMonth}% this |
| 108 | + month <TrendingUp className="h-4 w-4" /> |
| 109 | + </> |
| 110 | + ) : ( |
| 111 | + <> |
| 112 | + Trending down by{" "} |
| 113 | + {Math.abs(analytics?.subscribers.newThisMonth || 0)}% this |
| 114 | + month <ArrowDown className="h-4 w-4" /> |
| 115 | + </> |
| 116 | + )} |
| 117 | + </div> |
| 118 | + <div className="flex items-center gap-2 leading-none text-muted-foreground"> |
| 119 | + Last 30 days |
| 120 | + </div> |
| 121 | + </div> |
| 122 | + </div> |
| 123 | + )} |
| 124 | + </CardFooter> |
| 125 | + </Card> |
| 126 | + ) |
| 127 | +} |
0 commit comments