|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import * as React from "react"; |
| 4 | +import { getDashboardAnalyticsOverview } from "@/backend/services/analytics.actions"; |
| 5 | +import { |
| 6 | + Card, |
| 7 | + CardContent, |
| 8 | + CardDescription, |
| 9 | + CardHeader, |
| 10 | + CardTitle, |
| 11 | +} from "@/components/ui/card"; |
| 12 | +import { |
| 13 | + ChartContainer, |
| 14 | + ChartLegend, |
| 15 | + ChartLegendContent, |
| 16 | + ChartTooltip, |
| 17 | + ChartTooltipContent, |
| 18 | + type ChartConfig, |
| 19 | +} from "@/components/ui/chart"; |
| 20 | +import { useTranslation } from "@/i18n/use-translation"; |
| 21 | +import { actionPromisify } from "@/lib/utils"; |
| 22 | +import { useQuery } from "@tanstack/react-query"; |
| 23 | +import { CartesianGrid, Line, LineChart, XAxis } from "recharts"; |
| 24 | +import { |
| 25 | + AnalyticsRangeControls, |
| 26 | + analyticsTimeRangeToQueryPayload, |
| 27 | + type AnalyticsTimeRange, |
| 28 | +} from "@/components/analytics/AnalyticsRangeControls"; |
| 29 | + |
| 30 | +const chartConfig = { |
| 31 | + unique_viewers: { |
| 32 | + label: "Unique viewers", |
| 33 | + color: "hsl(var(--chart-1))", |
| 34 | + }, |
| 35 | + impressions: { |
| 36 | + label: "Impressions", |
| 37 | + color: "hsl(var(--chart-2))", |
| 38 | + }, |
| 39 | +} satisfies ChartConfig; |
| 40 | + |
| 41 | +export default function DashboardAnalyticsOverview() { |
| 42 | + const { _t } = useTranslation(); |
| 43 | + const [timeRange, setTimeRange] = React.useState<AnalyticsTimeRange>({ |
| 44 | + kind: "preset", |
| 45 | + days: 30, |
| 46 | + }); |
| 47 | + |
| 48 | + const rangePayload = analyticsTimeRangeToQueryPayload(timeRange); |
| 49 | + |
| 50 | + const query = useQuery({ |
| 51 | + queryKey: ["dashboard-analytics-overview", rangePayload], |
| 52 | + queryFn: () => |
| 53 | + actionPromisify(getDashboardAnalyticsOverview({ ...rangePayload })), |
| 54 | + }); |
| 55 | + |
| 56 | + if (query.isPending) { |
| 57 | + return ( |
| 58 | + <section id="dashboard-analytics" className="mb-10 space-y-4 scroll-mt-4"> |
| 59 | + <div className="h-8 w-48 animate-pulse rounded-md bg-muted" /> |
| 60 | + <div className="h-24 animate-pulse rounded-lg bg-muted" /> |
| 61 | + <div className="h-[300px] animate-pulse rounded-lg bg-muted" /> |
| 62 | + </section> |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + if (query.isError) { |
| 67 | + return ( |
| 68 | + <section id="dashboard-analytics" className="mb-10 scroll-mt-4"> |
| 69 | + <p className="text-sm text-destructive"> |
| 70 | + {query.error instanceof Error ? query.error.message : String(query.error)} |
| 71 | + </p> |
| 72 | + </section> |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + const data = query.data; |
| 77 | + const chartData = data.series.map((row) => ({ |
| 78 | + ...row, |
| 79 | + dateLabel: row.date.slice(5), |
| 80 | + })); |
| 81 | + |
| 82 | + const hasTrackedResources = |
| 83 | + data.publishedArticleCount > 0 || data.publicGistCount > 0; |
| 84 | + |
| 85 | + return ( |
| 86 | + <section id="dashboard-analytics" className="mb-10 scroll-mt-4"> |
| 87 | + <div className="mb-4 flex flex-col gap-1 sm:flex-row sm:items-end sm:justify-between"> |
| 88 | + <div> |
| 89 | + <h2 className="text-lg font-semibold tracking-tight"> |
| 90 | + {_t("Reach & views")} |
| 91 | + </h2> |
| 92 | + <p className="text-sm text-muted-foreground"> |
| 93 | + {_t("Across your published articles and public gists")} ( |
| 94 | + {data.publishedArticleCount}{" "} |
| 95 | + {data.publishedArticleCount === 1 ? _t("article") : _t("articles")} |
| 96 | + {", "} |
| 97 | + {data.publicGistCount}{" "} |
| 98 | + {data.publicGistCount === 1 ? _t("gist") : _t("gists")}).{" "} |
| 99 | + <span className="text-foreground/80"> |
| 100 | + {_t("Use Article analytics on each published row for a single post.")} |
| 101 | + </span> |
| 102 | + </p> |
| 103 | + </div> |
| 104 | + <AnalyticsRangeControls value={timeRange} onChange={setTimeRange} /> |
| 105 | + </div> |
| 106 | + |
| 107 | + {!data.clickhouseConfigured ? ( |
| 108 | + <p className="mb-4 rounded-md border border-amber-500/40 bg-amber-500/10 px-3 py-2 text-sm text-amber-950 dark:text-amber-100"> |
| 109 | + {_t( |
| 110 | + "View analytics are not configured (ClickHouse env missing). Totals below show reactions and bookmarks from the database only.", |
| 111 | + )} |
| 112 | + </p> |
| 113 | + ) : null} |
| 114 | + |
| 115 | + {data.clickhouseConfigured && !hasTrackedResources ? ( |
| 116 | + <p className="mb-4 rounded-md border border-border bg-muted/40 px-3 py-2 text-sm text-muted-foreground"> |
| 117 | + {_t( |
| 118 | + "Publish an article or create a public gist to start collecting view analytics.", |
| 119 | + )} |
| 120 | + </p> |
| 121 | + ) : null} |
| 122 | + |
| 123 | + <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4"> |
| 124 | + <Card className="rounded-lg border shadow-none"> |
| 125 | + <CardHeader className="pb-2"> |
| 126 | + <CardDescription>{_t("Unique viewers")}</CardDescription> |
| 127 | + <CardTitle className="text-2xl tabular-nums"> |
| 128 | + {data.totals.unique_viewers} |
| 129 | + </CardTitle> |
| 130 | + </CardHeader> |
| 131 | + </Card> |
| 132 | + <Card className="rounded-lg border shadow-none"> |
| 133 | + <CardHeader className="pb-2"> |
| 134 | + <CardDescription>{_t("Impressions")}</CardDescription> |
| 135 | + <CardTitle className="text-2xl tabular-nums"> |
| 136 | + {data.totals.impressions} |
| 137 | + </CardTitle> |
| 138 | + </CardHeader> |
| 139 | + </Card> |
| 140 | + <Card className="rounded-lg border shadow-none"> |
| 141 | + <CardHeader className="pb-2"> |
| 142 | + <CardDescription>{_t("Reactions")}</CardDescription> |
| 143 | + <CardTitle className="text-2xl tabular-nums">{data.reactions}</CardTitle> |
| 144 | + </CardHeader> |
| 145 | + </Card> |
| 146 | + <Card className="rounded-lg border shadow-none"> |
| 147 | + <CardHeader className="pb-2"> |
| 148 | + <CardDescription>{_t("Bookmarks")}</CardDescription> |
| 149 | + <CardTitle className="text-2xl tabular-nums">{data.bookmarks}</CardTitle> |
| 150 | + </CardHeader> |
| 151 | + </Card> |
| 152 | + </div> |
| 153 | + |
| 154 | + <Card className="mt-4 rounded-lg border shadow-none"> |
| 155 | + <CardHeader> |
| 156 | + <CardTitle className="text-base">{_t("Views over time")}</CardTitle> |
| 157 | + <CardDescription> |
| 158 | + {_t("Stacked across everything you track in this dashboard")} |
| 159 | + </CardDescription> |
| 160 | + </CardHeader> |
| 161 | + <CardContent> |
| 162 | + {!data.clickhouseConfigured || !hasTrackedResources ? ( |
| 163 | + <p className="text-sm text-muted-foreground"> |
| 164 | + {_t("Chart appears when view tracking is available for your content.")} |
| 165 | + </p> |
| 166 | + ) : chartData.length === 0 ? ( |
| 167 | + <p className="text-sm text-muted-foreground"> |
| 168 | + {_t("No view data in this range yet.")} |
| 169 | + </p> |
| 170 | + ) : ( |
| 171 | + <ChartContainer config={chartConfig} className="h-[min(320px,50vh)] w-full"> |
| 172 | + <LineChart |
| 173 | + accessibilityLayer |
| 174 | + data={chartData} |
| 175 | + margin={{ left: 8, right: 8, top: 8, bottom: 0 }} |
| 176 | + > |
| 177 | + <CartesianGrid vertical={false} /> |
| 178 | + <XAxis |
| 179 | + dataKey="dateLabel" |
| 180 | + tickLine={false} |
| 181 | + axisLine={false} |
| 182 | + tickMargin={8} |
| 183 | + /> |
| 184 | + <ChartTooltip cursor={false} content={<ChartTooltipContent />} /> |
| 185 | + <ChartLegend content={<ChartLegendContent />} /> |
| 186 | + <Line |
| 187 | + type="monotone" |
| 188 | + dataKey="unique_viewers" |
| 189 | + stroke="var(--color-unique_viewers)" |
| 190 | + strokeWidth={2} |
| 191 | + dot={false} |
| 192 | + /> |
| 193 | + <Line |
| 194 | + type="monotone" |
| 195 | + dataKey="impressions" |
| 196 | + stroke="var(--color-impressions)" |
| 197 | + strokeWidth={2} |
| 198 | + dot={false} |
| 199 | + /> |
| 200 | + </LineChart> |
| 201 | + </ChartContainer> |
| 202 | + )} |
| 203 | + </CardContent> |
| 204 | + </Card> |
| 205 | + </section> |
| 206 | + ); |
| 207 | +} |
0 commit comments