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 Instagram charts with Intent UI

spacesdrive61b772c4ee1f
diff --git a/frontend/src/features/analytics/instagram/InstagramEngagementCharts.tsx b/frontend/src/features/analytics/instagram/InstagramEngagementCharts.tsx
index a266c1f..b92f3dc 100644
--- a/frontend/src/features/analytics/instagram/InstagramEngagementCharts.tsx
+++ b/frontend/src/features/analytics/instagram/InstagramEngagementCharts.tsx
@@ -1,18 +1,30 @@
-import {
- AreaChart, Area, BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip as ReTooltip, PieChart, Pie, Cell, Legend,
-} from 'recharts';
+import { AreaChart } from '@/components/ui/area-chart';
+import { BarChart } from '@/components/ui/bar-chart';
import ChartPanel from '@/components/ChartPanel';
-import { SERIES, axisProps, gridProps, legendStyle, rankingSummary, tooltipCursor, tooltipStyle, trendSummary } from '@/lib/chart';
-import type { TooltipValueType } from 'recharts';
+import DonutChart from '@/components/DonutChart';
+import ExactTooltip from '@/components/ExactTooltip';
+import { SERIES, pointsOf, rankingSummary, trendSummary } from '@/lib/chart';
import { fmtNum, fmtNumFull } from '../../../utils/formatters';
-import type { NumberLike } from '../../../utils/formatters';
import type { InstagramCharts } from './instagramAnalyticsData.ts';
-function formatItems(value: TooltipValueType | undefined, name: string | number | undefined) {
- return [`${value} items`, name];
-}
+const TIMELINE = {
+ likes: { label: 'Likes', color: SERIES.primary },
+ comments: { label: 'Comments', color: SERIES.neutral },
+};
+
+const BY_TYPE = {
+ avgLikes: { label: 'Average likes', color: SERIES.primary },
+ avgComments: { label: 'Average comments', color: SERIES.neutral },
+};
+
+const BY_DAY = {
+ avgEngagement: { label: 'Average engagement', color: SERIES.warm },
+ posts: { label: 'Posts', color: SERIES.neutral },
+};
-const full = (value: unknown) => fmtNumFull(value as NumberLike);
+function formatItems(value: number): string {
+ return `${fmtNumFull(value)} items`;
+}
export interface InstagramEngagementChartsProps {
charts: InstagramCharts;
@@ -20,121 +32,64 @@ export interface InstagramEngagementChartsProps {
export default function InstagramEngagementCharts({ charts }: InstagramEngagementChartsProps) {
const { timeline, contentMix, typeRows, dayOfWeek } = charts;
- const likesTrend = timeline.map(point => ({ name: point.name, value: point.likes }));
- const bestDays = dayOfWeek.map(day => ({ name: day.name, value: day.avgEngagement }));
- const biggestType = rankingSummary(contentMix, fmtNum, 'items');
return (
<>
<div className="grid grid-cols-1 gap-4 xl:grid-cols-3">
<ChartPanel
- title="Likes and comments by month"
+ title="Engagement over time"
className="xl:col-span-2"
empty={timeline.length === 0}
emptyMessage="Not enough posts to chart a timeline"
- summary={trendSummary(likesTrend, fmtNum, 'likes')}
- table={{
- caption: 'Likes and comments by month',
- columns: [
- { key: 'name', label: 'Month' },
- { key: 'posts', label: 'Posts', numeric: true, format: full },
- { key: 'likes', label: 'Likes', numeric: true, format: full },
- { key: 'comments', label: 'Comments', numeric: true, format: full },
- ],
- rows: timeline,
- }}
+ description={trendSummary(pointsOf(timeline, 'name', 'likes'), fmtNumFull, 'likes')}
>
- <AreaChart data={timeline}>
- <defs>
- <linearGradient id="igLikesGrad" x1="0" y1="0" x2="0" y2="1">
- <stop offset="5%" stopColor={SERIES.primary} stopOpacity={0.2} />
- <stop offset="95%" stopColor={SERIES.primary} stopOpacity={0} />
- </linearGradient>
- </defs>
- <CartesianGrid {...gridProps} vertical={false} />
- <XAxis dataKey="name" {...axisProps} />
- <YAxis {...axisProps} width={44} tickFormatter={fmtNum} />
- <ReTooltip contentStyle={tooltipStyle} cursor={{ stroke: 'var(--line-strong)' }} formatter={value => fmtNumFull(value as NumberLike)} />
- <Area type="monotone" dataKey="likes" name="Likes" stroke={SERIES.primary} strokeWidth={2} fill="url(#igLikesGrad)" />
- <Area type="monotone" dataKey="comments" name="Comments" stroke={SERIES.neutral} strokeWidth={2} strokeDasharray="5 3" fill="transparent" />
- <Legend wrapperStyle={legendStyle} />
- </AreaChart>
+ <AreaChart
+ data={timeline.map(point => ({ ...point }))}
+ dataKey="name"
+ config={TIMELINE}
+ containerHeight={280}
+ tooltip={<ExactTooltip />}
+ valueFormatter={fmtNum}
+ yAxisProps={{ width: 44 }}
+ areaProps={{ type: 'monotone' }}
+ />
</ChartPanel>
- <ChartPanel
- title="Content type mix"
- empty={contentMix.length === 0}
- summary={biggestType}
- table={{
- caption: 'Content type mix',
- columns: [{ key: 'name', label: 'Type' }, { key: 'value', label: 'Items', numeric: true, format: full }],
- rows: contentMix,
- }}
- >
- <PieChart>
- <Pie data={contentMix} dataKey="value" nameKey="name" cx="50%" cy="44%" outerRadius={80} innerRadius={48} paddingAngle={2} strokeWidth={0}>
- {contentMix.map(entry => <Cell key={entry.name} fill={entry.color} />)}
- </Pie>
- <ReTooltip contentStyle={tooltipStyle} formatter={formatItems} />
- <Legend iconType="circle" iconSize={8} wrapperStyle={{ ...legendStyle, paddingTop: 8 }} />
- </PieChart>
+ <ChartPanel title="Content type mix" empty={contentMix.length === 0} description={rankingSummary(contentMix, fmtNumFull, 'items')}>
+ <DonutChart data={contentMix} height={280} cy="44%" innerRadius={48} outerRadius={80} format={formatItems} />
</ChartPanel>
</div>
<div className="grid grid-cols-1 gap-4 md:grid-cols-2">
<ChartPanel
- title="Average likes and comments by content type"
+ title="Performance by content type"
height={260}
empty={typeRows.length === 0}
- summary={rankingSummary(typeRows.map(row => ({ name: row.name, value: row.avgLikes })), fmtNum, 'likes per item on average')}
- table={{
- caption: 'Average likes and comments by content type',
- columns: [
- { key: 'name', label: 'Type' },
- { key: 'avgLikes', label: 'Average likes', numeric: true, format: full },
- { key: 'avgComments', label: 'Average comments', numeric: true, format: full },
- ],
- rows: typeRows,
- }}
+ description={rankingSummary(pointsOf(typeRows, 'name', 'avgLikes'), fmtNumFull, 'likes per item on average')}
>
- <BarChart data={typeRows}>
- <CartesianGrid {...gridProps} vertical={false} />
- <XAxis dataKey="name" {...axisProps} />
- <YAxis {...axisProps} width={44} tickFormatter={fmtNum} />
- <ReTooltip contentStyle={tooltipStyle} cursor={tooltipCursor} formatter={value => fmtNumFull(value as NumberLike)} />
- <Bar dataKey="avgLikes" name="Average likes" fill={SERIES.primary} radius={[4, 4, 0, 0]} />
- <Bar dataKey="avgComments" name="Average comments" fill={SERIES.neutral} radius={[4, 4, 0, 0]} />
- <Legend wrapperStyle={legendStyle} />
- </BarChart>
+ <BarChart
+ data={typeRows.map(row => ({ ...row }))}
+ dataKey="name"
+ config={BY_TYPE}
+ containerHeight={260}
+ tooltip={<ExactTooltip />}
+ valueFormatter={fmtNum}
+ yAxisProps={{ width: 44 }}
+ />
</ChartPanel>
<ChartPanel
- title="Interactions per post by day"
- subheader="Average likes and comments a post got, by the day it went out"
+ title="Best days to post"
height={260}
empty={dayOfWeek.length === 0}
- summary={rankingSummary(bestDays, fmtNum, 'interactions per post')}
- table={{
- caption: 'Interactions per post by day',
- columns: [
- { key: 'name', label: 'Day' },
- { key: 'avgEngagement', label: 'Interactions per post', numeric: true, format: full },
- { key: 'posts', label: 'Posts', numeric: true, format: full },
- ],
- rows: dayOfWeek,
- }}
+ description={rankingSummary(pointsOf(dayOfWeek, 'name', 'avgEngagement'), fmtNumFull, 'interactions per post on average')}
>
- <BarChart data={dayOfWeek}>
- <CartesianGrid {...gridProps} vertical={false} />
- <XAxis dataKey="name" {...axisProps} />
- <YAxis {...axisProps} width={44} tickFormatter={fmtNum} />
- <ReTooltip
- contentStyle={tooltipStyle}
- cursor={tooltipCursor}
- formatter={(value, _name, item) => [
- `${fmtNumFull(value as NumberLike)} (${(item.payload as { posts: number }).posts} posts)`,
- 'Interactions per post',
- ]}
- />
- <Bar dataKey="avgEngagement" name="Interactions per post" fill={SERIES.warm} radius={[4, 4, 0, 0]} />
- </BarChart>
+ <BarChart
+ data={dayOfWeek.map(day => ({ ...day }))}
+ dataKey="name"
+ config={BY_DAY}
+ containerHeight={260}
+ tooltip={<ExactTooltip />}
+ valueFormatter={fmtNum}
+ yAxisProps={{ width: 44 }}
+ />
</ChartPanel>
</div>
</>
diff --git a/frontend/src/features/analytics/instagram/InstagramPostCharts.tsx b/frontend/src/features/analytics/instagram/InstagramPostCharts.tsx
index 7edbb08..26e11b8 100644
--- a/frontend/src/features/analytics/instagram/InstagramPostCharts.tsx
+++ b/frontend/src/features/analytics/instagram/InstagramPostCharts.tsx
@@ -1,11 +1,18 @@
-import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip as ReTooltip, Legend } from 'recharts';
+import { BarChart } from '@/components/ui/bar-chart';
import ChartPanel from '@/components/ChartPanel';
-import { SERIES, axisProps, gridProps, legendStyle, rankingSummary, tooltipCursor, tooltipStyle, trendSummary } from '@/lib/chart';
+import ExactTooltip from '@/components/ExactTooltip';
+import { SERIES, pointsOf, rankingSummary, trendSummary } from '@/lib/chart';
import { fmtNum, fmtNumFull } from '../../../utils/formatters';
-import type { NumberLike } from '../../../utils/formatters';
import type { InstagramCharts } from './instagramAnalyticsData.ts';
-const full = (value: unknown) => fmtNumFull(value as NumberLike);
+const TOP_POSTS = {
+ likes: { label: 'Likes', color: SERIES.primary },
+ comments: { label: 'Comments', color: SERIES.neutral },
+};
+
+const BY_CAPTION = { avgEngagement: { label: 'Average engagement', color: SERIES.warm } };
+
+const BY_MONTH = { posts: { label: 'Posts', color: SERIES.neutral } };
export interface InstagramPostChartsProps {
charts: InstagramCharts;
@@ -17,74 +24,51 @@ export default function InstagramPostCharts({ charts }: InstagramPostChartsProps
<>
<div className="grid grid-cols-1 gap-4 md:grid-cols-2">
<ChartPanel
- title="Top posts by likes and comments"
- subheader="Numbered as in the top posts table"
+ title="Top posts by engagement"
height={260}
empty={topPosts.length === 0}
- summary={rankingSummary(topPosts.map(post => ({ name: `Post ${post.name}`, value: post.likes })), fmtNum, 'likes')}
- table={{
- caption: 'Top posts by likes and comments',
- columns: [
- { key: 'name', label: 'Post' },
- { key: 'likes', label: 'Likes', numeric: true, format: full },
- { key: 'comments', label: 'Comments', numeric: true, format: full },
- ],
- rows: topPosts,
- }}
+ description={rankingSummary(topPosts.map(post => ({ name: `Post ${post.name}`, value: post.likes })), fmtNumFull, 'likes')}
>
- <BarChart data={topPosts}>
- <CartesianGrid {...gridProps} vertical={false} />
- <XAxis dataKey="name" {...axisProps} />
- <YAxis {...axisProps} width={44} tickFormatter={fmtNum} />
- <ReTooltip contentStyle={tooltipStyle} cursor={tooltipCursor} formatter={value => fmtNumFull(value as NumberLike)} />
- <Bar dataKey="likes" name="Likes" fill={SERIES.primary} radius={[4, 4, 0, 0]} />
- <Bar dataKey="comments" name="Comments" fill={SERIES.neutral} radius={[4, 4, 0, 0]} />
- <Legend wrapperStyle={legendStyle} />
- </BarChart>
+ <BarChart
+ data={topPosts.map(post => ({ ...post }))}
+ dataKey="name"
+ config={TOP_POSTS}
+ containerHeight={260}
+ tooltip={<ExactTooltip />}
+ valueFormatter={fmtNum}
+ yAxisProps={{ width: 44 }}
+ />
</ChartPanel>
<ChartPanel
- title="Interactions by caption length"
- subheader="Average likes and comments a post got, by how long its caption is"
+ title="Caption length and engagement"
height={260}
empty={caption.length === 0}
- summary={rankingSummary(caption.map(row => ({ name: row.label, value: row.avgEngagement })), fmtNum, 'interactions per post')}
- table={{
- caption: 'Interactions by caption length',
- columns: [
- { key: 'label', label: 'Caption length' },
- { key: 'avgEngagement', label: 'Interactions per post', numeric: true, format: full },
- ],
- rows: caption,
- }}
+ description={rankingSummary(pointsOf(caption, 'label', 'avgEngagement'), fmtNumFull, 'interactions per post on average')}
>
- <BarChart data={caption}>
- <CartesianGrid {...gridProps} vertical={false} />
- <XAxis dataKey="label" {...axisProps} />
- <YAxis {...axisProps} width={44} tickFormatter={fmtNum} />
- <ReTooltip contentStyle={tooltipStyle} cursor={tooltipCursor} formatter={value => fmtNumFull(value as NumberLike)} />
- <Bar dataKey="avgEngagement" name="Interactions per post" fill={SERIES.warm} radius={[4, 4, 0, 0]} />
- </BarChart>
+ <BarChart
+ data={caption.map(row => ({ ...row }))}
+ dataKey="label"
+ config={BY_CAPTION}
+ containerHeight={260}
+ legend={false}
+ tooltip={<ExactTooltip />}
+ valueFormatter={fmtNum}
+ yAxisProps={{ width: 44 }}
+ />
</ChartPanel>
</div>
{timeline.length > 1 && (
- <ChartPanel
- title="Posts per month"
- height={220}
- summary={trendSummary(timeline.map(point => ({ name: point.name, value: point.posts })), fmtNum, 'posts')}
- table={{
- caption: 'Posts per month',
- columns: [{ key: 'name', label: 'Month' }, { key: 'posts', label: 'Posts', numeric: true, format: full }],
- rows: timeline,
- }}
- >
- <BarChart data={timeline}>
- <CartesianGrid {...gridProps} vertical={false} />
- <XAxis dataKey="name" {...axisProps} />
- <YAxis {...axisProps} width={44} allowDecimals={false} />
- <ReTooltip contentStyle={tooltipStyle} cursor={tooltipCursor} />
- <Bar dataKey="posts" name="Posts" fill={SERIES.neutral} radius={[4, 4, 0, 0]} />
- </BarChart>
+ <ChartPanel title="Posts per month" height={220} description={trendSummary(pointsOf(timeline, 'name', 'posts'), fmtNumFull, 'posts')}>
+ <BarChart
+ data={timeline.map(point => ({ ...point }))}
+ dataKey="name"
+ config={BY_MONTH}
+ containerHeight={220}
+ legend={false}
+ tooltip={<ExactTooltip />}
+ yAxisProps={{ width: 44, allowDecimals: false }}
+ />
</ChartPanel>
)}
</>