Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

feat(frontend): restore the Reddit charts with Intent UI

spacesdrivea950d548f016
diff --git a/frontend/src/features/analytics/reddit/RedditKarmaCharts.tsx b/frontend/src/features/analytics/reddit/RedditKarmaCharts.tsx
index 439d187..76f915b 100644
--- a/frontend/src/features/analytics/reddit/RedditKarmaCharts.tsx
+++ b/frontend/src/features/analytics/reddit/RedditKarmaCharts.tsx
@@ -1,27 +1,17 @@
-import {
- XAxis, YAxis, CartesianGrid, Tooltip as ReTooltip, AreaChart, Area, PieChart, Pie, Cell, Legend,
-} from 'recharts';
-import type { TooltipValueType } from 'recharts';
+import { AreaChart } from '@/components/ui/area-chart';
import ChartPanel from '@/components/ChartPanel';
+import DonutChart from '@/components/DonutChart';
+import ExactTooltip from '@/components/ExactTooltip';
import { PLATFORM_META } from '@/lib/accounts';
-import { axisProps, gridProps, legendStyle, rankingSummary, tooltipStyle, trendSummary } from '@/lib/chart';
+import { pointsOf, rankingSummary, trendSummary } from '@/lib/chart';
import { fmtNum, fmtNumFull } from '../../../utils/formatters';
-import type { NumberLike } from '../../../utils/formatters';
import type { RedditMonthStats } from '@shared/types/reddit.ts';
import type { RedditMediaSlice } from './redditAnalyticsData.ts';
// karma keeps Reddit's series colour here and on the Overview, so one colour means one thing
const KARMA = PLATFORM_META.reddit.series;
-const full = (value: unknown) => fmtNumFull(value as NumberLike);
-
-function formatKarmaTooltip(value: TooltipValueType | undefined) {
- return [fmtNumFull(value as number), 'Karma'];
-}
-
-function formatPostsTooltip(value: TooltipValueType | undefined) {
- return [fmtNumFull(value as number), 'Posts'];
-}
+const BY_MONTH = { score: { label: 'Karma', color: KARMA } };
export interface RedditKarmaChartsProps {
monthlyBreakdown: RedditMonthStats[];
@@ -29,58 +19,29 @@ export interface RedditKarmaChartsProps {
}
export default function RedditKarmaCharts({ monthlyBreakdown, mediaTypeData }: RedditKarmaChartsProps) {
- const karmaByMonth = monthlyBreakdown.map(month => ({ name: month.month, value: month.score }));
return (
<div className="grid grid-cols-1 gap-4 xl:grid-cols-3">
<ChartPanel
- title="Karma by month"
- subheader="Karma earned by the posts submitted in each month"
+ title="Karma over time"
height={240}
className="xl:col-span-2"
- summary={trendSummary(karmaByMonth, fmtNum, 'karma')}
- table={{
- caption: 'Karma by month',
- columns: [
- { key: 'month', label: 'Month' },
- { key: 'posts', label: 'Posts', numeric: true, format: full },
- { key: 'score', label: 'Karma', numeric: true, format: full },
- ],
- rows: monthlyBreakdown,
- }}
+ description={trendSummary(pointsOf(monthlyBreakdown, 'month', 'score'), fmtNumFull, 'karma')}
>
- <AreaChart data={monthlyBreakdown} margin={{ top: 4, right: 4, left: 0, bottom: 0 }}>
- <defs>
- <linearGradient id="redditGrad" x1="0" y1="0" x2="0" y2="1">
- <stop offset="5%" stopColor={KARMA} stopOpacity={0.2} />
- <stop offset="95%" stopColor={KARMA} stopOpacity={0} />
- </linearGradient>
- </defs>
- <CartesianGrid {...gridProps} vertical={false} />
- <XAxis dataKey="month" {...axisProps} />
- <YAxis {...axisProps} tickFormatter={fmtNum} width={48} />
- <ReTooltip contentStyle={tooltipStyle} cursor={{ stroke: 'var(--line-strong)' }} formatter={formatKarmaTooltip} />
- <Area type="monotone" dataKey="score" name="Karma" stroke={KARMA} strokeWidth={2} fill="url(#redditGrad)" dot={{ fill: KARMA, r: 3, strokeWidth: 0 }} />
- </AreaChart>
+ <AreaChart
+ data={monthlyBreakdown.map(month => ({ ...month }))}
+ dataKey="month"
+ config={BY_MONTH}
+ containerHeight={240}
+ legend={false}
+ tooltip={<ExactTooltip />}
+ valueFormatter={fmtNum}
+ areaProps={{ type: 'monotone', dot: { fill: KARMA, r: 3, strokeWidth: 0 } }}
+ />
</ChartPanel>
{mediaTypeData.length > 0 && (
- <ChartPanel
- title="Post types"
- height={240}
- summary={rankingSummary(mediaTypeData, fmtNum, 'posts')}
- table={{
- caption: 'Post types',
- columns: [{ key: 'name', label: 'Type' }, { key: 'value', label: 'Posts', numeric: true, format: full }],
- rows: mediaTypeData,
- }}
- >
- <PieChart>
- <Pie data={mediaTypeData} dataKey="value" nameKey="name" cx="50%" cy="44%" outerRadius={80} innerRadius={48} paddingAngle={2} strokeWidth={0}>
- {mediaTypeData.map(entry => <Cell key={entry.name} fill={entry.color} />)}
- </Pie>
- <ReTooltip contentStyle={tooltipStyle} formatter={formatPostsTooltip} />
- <Legend iconType="circle" iconSize={8} wrapperStyle={{ ...legendStyle, paddingTop: 8 }} />
- </PieChart>
+ <ChartPanel title="Post types" height={240} description={rankingSummary(mediaTypeData, fmtNumFull, 'posts')}>
+ <DonutChart data={mediaTypeData} height={240} cy="44%" innerRadius={48} outerRadius={80} />
</ChartPanel>
)}
</div>
diff --git a/frontend/src/features/analytics/reddit/RedditTimingCharts.tsx b/frontend/src/features/analytics/reddit/RedditTimingCharts.tsx
index f89e02d..4a66f84 100644
--- a/frontend/src/features/analytics/reddit/RedditTimingCharts.tsx
+++ b/frontend/src/features/analytics/reddit/RedditTimingCharts.tsx
@@ -1,14 +1,19 @@
-import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip as ReTooltip } from 'recharts';
+import { BarChart } from '@/components/ui/bar-chart';
import ChartPanel from '@/components/ChartPanel';
+import ExactTooltip from '@/components/ExactTooltip';
import { PLATFORM_META } from '@/lib/accounts';
-import { axisProps, axisTick, gridProps, rankingSummary, tooltipCursor, tooltipStyle } from '@/lib/chart';
+import { SERIES, pointsOf, rankingSummary } from '@/lib/chart';
import { fmtNum, fmtNumFull } from '../../../utils/formatters';
-import type { NumberLike } from '../../../utils/formatters';
import type { RedditAnalytics } from '@shared/types/reddit.ts';
const KARMA = PLATFORM_META.reddit.series;
-const full = (value: unknown) => fmtNumFull(value as NumberLike);
+const BY_DAY = {
+ posts: { label: 'Posts', color: SERIES.neutral },
+ avgScore: { label: 'Average karma', color: KARMA },
+};
+
+const BY_HOUR = { avgScore: { label: 'Average karma', color: KARMA } };
function formatHour(hour: number) {
return `${hour}h`;
@@ -18,71 +23,50 @@ function bestLabel(prefix: string, value: string | undefined) {
return value ? `${prefix}: ${value}` : undefined;
}
-interface TimingPayload {
- posts?: number;
-}
-
-// one unit per chart: the bars are average karma, and the post count behind each bar is in the tooltip and table
-function formatKarmaWithPosts(value: unknown, _name: unknown, item: { payload?: unknown }) {
- const posts = (item.payload as TimingPayload | undefined)?.posts;
- return [`${fmtNumFull(value as NumberLike)}${posts !== undefined ? ` (${posts} posts)` : ''}`, 'Average karma'];
-}
-
export interface RedditTimingChartsProps {
analytics: Partial<RedditAnalytics>;
}
export default function RedditTimingCharts({ analytics }: RedditTimingChartsProps) {
- const byDay = analytics.postsByDayOfWeek || [];
- const byHour = analytics.postsByHour || [];
+ const byDay = (analytics.postsByDayOfWeek || []).map(day => ({ ...day }));
+ // the tooltip heads each bar with its category, which has to be text
+ const byHour = (analytics.postsByHour || []).map(hour => ({ ...hour, hourName: formatHour(hour.hour) }));
return (
<div className="grid grid-cols-1 gap-4 xl:grid-cols-2">
<ChartPanel
- title="Average karma by day posted"
+ title="Posts by day of week"
subheader={bestLabel('Best day', analytics.bestPostingDay)}
height={220}
- summary={rankingSummary(byDay.map(day => ({ name: day.day, value: day.avgScore })), fmtNum, 'karma per post')}
- table={{
- caption: 'Average karma by day posted',
- columns: [
- { key: 'day', label: 'Day' },
- { key: 'posts', label: 'Posts', numeric: true, format: full },
- { key: 'avgScore', label: 'Average karma', numeric: true, format: full },
- ],
- rows: byDay,
- }}
+ description={rankingSummary(pointsOf(byDay, 'day', 'avgScore'), fmtNumFull, 'karma per post')}
>
- <BarChart data={byDay} margin={{ top: 4, right: 4, left: 0, bottom: 0 }}>
- <CartesianGrid {...gridProps} vertical={false} />
- <XAxis dataKey="shortDay" {...axisProps} />
- <YAxis {...axisProps} tickFormatter={fmtNum} width={36} />
- <ReTooltip contentStyle={tooltipStyle} cursor={tooltipCursor} formatter={formatKarmaWithPosts} />
- <Bar dataKey="avgScore" name="Average karma" fill={KARMA} radius={[3, 3, 0, 0]} />
- </BarChart>
+ <BarChart
+ data={byDay}
+ dataKey="shortDay"
+ config={BY_DAY}
+ containerHeight={220}
+ tooltip={<ExactTooltip />}
+ valueFormatter={fmtNum}
+ yAxisProps={{ width: 36 }}
+ />
</ChartPanel>
<ChartPanel
- title="Average karma by hour posted"
+ title="Average karma by hour"
subheader={bestLabel('Best hour', analytics.bestPostingHour)}
height={220}
- summary={rankingSummary(byHour.map(hour => ({ name: `${hour.hour}:00`, value: hour.avgScore })), fmtNum, 'karma per post')}
- table={{
- caption: 'Average karma by hour posted',
- columns: [
- { key: 'hour', label: 'Hour', format: value => `${value}:00` },
- { key: 'posts', label: 'Posts', numeric: true, format: full },
- { key: 'avgScore', label: 'Average karma', numeric: true, format: full },
- ],
- rows: byHour,
- }}
+ description={rankingSummary(pointsOf(byHour, 'hourName', 'avgScore'), fmtNumFull, 'karma per post')}
>
- <BarChart data={byHour} margin={{ top: 4, right: 4, left: 0, bottom: 0 }}>
- <CartesianGrid {...gridProps} vertical={false} />
- <XAxis dataKey="hour" tickFormatter={formatHour} tick={{ ...axisTick, fontSize: 10 }} axisLine={false} tickLine={false} interval={3} />
- <YAxis {...axisProps} tickFormatter={fmtNum} width={36} />
- <ReTooltip contentStyle={tooltipStyle} cursor={tooltipCursor} formatter={formatKarmaWithPosts} />
- <Bar dataKey="avgScore" name="Average karma" fill={KARMA} radius={[3, 3, 0, 0]} />
- </BarChart>
+ <BarChart
+ data={byHour}
+ dataKey="hourName"
+ config={BY_HOUR}
+ containerHeight={220}
+ legend={false}
+ tooltip={<ExactTooltip />}
+ valueFormatter={fmtNum}
+ xAxisProps={{ interval: 3 }}
+ yAxisProps={{ width: 36 }}
+ />
</ChartPanel>
</div>
);