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

spacesdrive57bfed041ff0
diff --git a/frontend/src/components/ExactTooltip.tsx b/frontend/src/components/ExactTooltip.tsx
index 7f19af4..182f352 100644
--- a/frontend/src/components/ExactTooltip.tsx
+++ b/frontend/src/components/ExactTooltip.tsx
@@ -4,16 +4,17 @@ import { fmtNumFull } from '@/utils/formatters';
interface ValueRowProps {
name: string;
+ label?: string;
value: string;
color?: string;
}
-function ValueRow({ name, value, color }: ValueRowProps) {
+function ValueRow({ name, label, value, color }: ValueRowProps) {
const { config } = useChart();
return (
<div className="flex w-full items-center gap-2">
<span aria-hidden="true" className="size-2.5 shrink-0 rounded-full" style={{ background: color }} />
- <span className="flex-1 text-ink-muted">{config[name]?.label ?? name}</span>
+ <span className="flex-1 text-ink-muted">{label ?? config[name]?.label ?? name}</span>
<span className="font-mono font-medium text-ink tabular-nums">{value}</span>
</div>
);
@@ -22,15 +23,22 @@ function ValueRow({ name, value, color }: ValueRowProps) {
export type ExactTooltipProps = ComponentProps<typeof ChartTooltipContent> & {
// how one value reads; whole numbers in full by default
format?: (value: number) => string;
+ // names the value by its row when one series mixes kinds (subscribers next to karma)
+ labelOf?: (row: Record<string, unknown>) => string;
};
// Intent UI's tooltip with every value in full (1,204,553, not 1.2M), since the axis already shows the rounded scale
-export default function ExactTooltip({ format = fmtNumFull, ...props }: ExactTooltipProps) {
+export default function ExactTooltip({ format = fmtNumFull, labelOf, ...props }: ExactTooltipProps) {
return (
<ChartTooltipContent
{...props}
formatter={(value, name, item) => (
- <ValueRow name={String(name)} value={format(Number(value))} color={item.payload?.fill ?? item.color} />
+ <ValueRow
+ name={String(name)}
+ label={labelOf?.(item.payload ?? {})}
+ value={format(Number(value))}
+ color={item.payload?.fill ?? item.color}
+ />
)}
/>
);
diff --git a/frontend/src/features/analytics/overview/AudienceChart.tsx b/frontend/src/features/analytics/overview/AudienceChart.tsx
index 13d1776..746a6b7 100644
--- a/frontend/src/features/analytics/overview/AudienceChart.tsx
+++ b/frontend/src/features/analytics/overview/AudienceChart.tsx
@@ -1,18 +1,52 @@
-import RankingBarChart from '@/components/RankingBarChart';
-import { audienceRankingOf } from './overviewCharts.ts';
-import type { AudienceLegendItem, AudiencePoint } from './overviewCharts.ts';
+import type { DotItemDotProps } from 'recharts';
+import { AreaChart } from '@/components/ui/area-chart';
+import ChartFigure from '@/components/ChartFigure';
+import ExactTooltip from '@/components/ExactTooltip';
+import { fmtNum } from '../../../utils/formatters';
+import { AUDIENCE_KIND, audienceDescription, audienceDotColor } from './overviewCharts.ts';
+import type { AudienceLegendItem, AudiencePoint, AudiencePointType } from './overviewCharts.ts';
export interface AudienceChartProps {
data: AudiencePoint[];
legend: AudienceLegendItem[];
}
-// a ranking of separate accounts, so bars rather than a line; the legend names each colour in words
+const CONFIG = { value: { label: 'Audience', color: 'var(--chart-1)' } };
+
+function audienceKind(row: Record<string, unknown>): string {
+ return AUDIENCE_KIND[row.type as AudiencePointType] ?? 'Audience';
+}
+
+function renderAudienceDot({ cx, cy, payload, index }: DotItemDotProps) {
+ return (
+ <circle
+ key={`dot-${index}`}
+ cx={cx}
+ cy={cy}
+ r={4.5}
+ fill={audienceDotColor(payload.type)}
+ stroke="var(--surface-raised)"
+ strokeWidth={2}
+ />
+ );
+}
+
export default function AudienceChart({ data, legend }: AudienceChartProps) {
return (
- <div className="flex flex-col gap-3">
- <RankingBarChart rows={audienceRankingOf(data)} label="Audience" title="Audience by account" kindLabel="Counts" />
- <ul aria-label="Colour key" className="flex flex-wrap gap-x-4 gap-y-1.5 px-2">
+ <>
+ <ChartFigure label="Audience by account" description={audienceDescription(data)}>
+ <AreaChart
+ data={data.map(point => ({ ...point }))}
+ dataKey="name"
+ config={CONFIG}
+ containerHeight={260}
+ legend={false}
+ tooltip={<ExactTooltip labelOf={audienceKind} />}
+ valueFormatter={fmtNum}
+ areaProps={{ type: 'monotone', dot: renderAudienceDot, activeDot: { r: 6 } }}
+ />
+ </ChartFigure>
+ <ul className="mt-3 flex flex-wrap gap-x-4 gap-y-1.5">
{legend.map(({ color, label }) => (
<li key={label} className="flex items-center gap-1.5 text-caption text-ink-muted">
<span aria-hidden="true" className="size-2.5 shrink-0 rounded-full" style={{ background: color }} />
@@ -20,6 +54,6 @@ export default function AudienceChart({ data, legend }: AudienceChartProps) {
</li>
))}
</ul>
- </div>
+ </>
);
}
diff --git a/frontend/src/features/analytics/overview/CommentsTab.tsx b/frontend/src/features/analytics/overview/CommentsTab.tsx
index 6864800..adb3392 100644
--- a/frontend/src/features/analytics/overview/CommentsTab.tsx
+++ b/frontend/src/features/analytics/overview/CommentsTab.tsx
@@ -3,10 +3,11 @@ import { Button } from '@/components/ui/button';
import SurfaceBox from '@/components/SurfaceBox';
import MetricBox from '@/components/MetricBox';
import Num from '@/components/Num';
-import RankingBarChart from '@/components/RankingBarChart';
+import MetricAreaChart from '@/components/MetricAreaChart';
import { EmptyState } from '@/components/StateViews';
+import { SERIES, pointsOf, rankingSummary } from '@/lib/chart';
import { METRIC_ICONS } from '@/lib/metricIcons';
-import { fmtNum, fmtPercent } from '../../../utils/formatters';
+import { fmtNum, fmtNumFull, fmtPercent } from '../../../utils/formatters';
import { CATEGORY_SERIES } from './overviewCharts.ts';
import { pluralWord } from './overviewLabels.ts';
import type { OverviewStats } from './useOverviewStats';
@@ -20,8 +21,6 @@ export interface CommentsTabProps {
export default function CommentsTab({ stats }: CommentsTabProps) {
const { summary, charts } = stats;
const { totalComments, totalContent } = summary;
- const byContent = charts.commentByContent.map(point => ({ name: point.name, value: point.comments }));
- const byCategory = charts.commentByCategory.map(point => ({ name: point.name, value: point.comments }));
return (
<>
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2 xl:grid-cols-4">
@@ -43,10 +42,26 @@ export default function CommentsTab({ stats }: CommentsTabProps) {
) : (
<div className="grid grid-cols-1 gap-4 lg:grid-cols-2">
<SurfaceBox title="Comments by tracked content" subheader="Top 12 tracked items ranked by comments">
- <RankingBarChart rows={byContent} label="Comments" title="Comments by tracked content" emptyMessage="Track content to compare comment volume" />
+ <MetricAreaChart
+ data={charts.commentByContent}
+ dataKey="comments"
+ label="Comments"
+ title="Comments by tracked content"
+ description={rankingSummary(pointsOf(charts.commentByContent, 'name', 'comments'), fmtNumFull, 'comments')}
+ color={SERIES.neutral}
+ emptyMessage="Track content to compare comment volume"
+ />
</SurfaceBox>
<SurfaceBox title="Comments by category" subheader="Total comments across every item in each category">
- <RankingBarChart rows={byCategory} label="Comments" title="Comments by category" color={CATEGORY_SERIES} emptyMessage="Assign categories to tracked content to see this breakdown" />
+ <MetricAreaChart
+ data={charts.commentByCategory}
+ dataKey="comments"
+ label="Comments"
+ title="Comments by category"
+ description={rankingSummary(pointsOf(charts.commentByCategory, 'name', 'comments'), fmtNumFull, 'comments')}
+ color={CATEGORY_SERIES}
+ emptyMessage="Assign categories to tracked content to see this breakdown"
+ />
</SurfaceBox>
</div>
)}
diff --git a/frontend/src/features/analytics/overview/LikesTab.tsx b/frontend/src/features/analytics/overview/LikesTab.tsx
index 3ac0166..b00edfa 100644
--- a/frontend/src/features/analytics/overview/LikesTab.tsx
+++ b/frontend/src/features/analytics/overview/LikesTab.tsx
@@ -3,11 +3,12 @@ import { Button } from '@/components/ui/button';
import SurfaceBox from '@/components/SurfaceBox';
import MetricBox from '@/components/MetricBox';
import Num from '@/components/Num';
-import RankingBarChart from '@/components/RankingBarChart';
+import MetricAreaChart from '@/components/MetricAreaChart';
import { EmptyState } from '@/components/StateViews';
import { PLATFORM_META } from '@/lib/accounts';
+import { SERIES, pointsOf, rankingSummary } from '@/lib/chart';
import { METRIC_ICONS } from '@/lib/metricIcons';
-import { fmtNum, fmtPercent } from '../../../utils/formatters';
+import { fmtNum, fmtNumFull, fmtPercent } from '../../../utils/formatters';
import type { OverviewStats } from './useOverviewStats';
const { likes: Likes, karma: Karma, videos: Videos, instagram: Instagram } = METRIC_ICONS;
@@ -24,8 +25,6 @@ export interface LikesTabProps {
export default function LikesTab({ stats }: LikesTabProps) {
const { totals, cacheTotals, trackedStats, summary, charts } = stats;
const { totalLikes } = summary;
- const karmaRows = charts.karmaByAccount.map(point => ({ name: point.name, value: point.karma }));
- const engagementRows = charts.engagementByContent.map(point => ({ name: point.name, value: point.engagement }));
return (
<>
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2 xl:grid-cols-4">
@@ -47,10 +46,26 @@ export default function LikesTab({ stats }: LikesTabProps) {
) : (
<div className="grid grid-cols-1 gap-4 lg:grid-cols-2">
<SurfaceBox title="Karma by Reddit account" subheader="Post karma earned by each connected account">
- <RankingBarChart rows={karmaRows} label="Post karma" title="Karma by Reddit account" color={PLATFORM_META.reddit.series} emptyMessage="Connect a Reddit account to see karma" />
+ <MetricAreaChart
+ data={charts.karmaByAccount}
+ dataKey="karma"
+ label="Post karma"
+ title="Karma by Reddit account"
+ description={rankingSummary(pointsOf(charts.karmaByAccount, 'name', 'karma'), fmtNumFull, 'post karma')}
+ color={PLATFORM_META.reddit.series}
+ emptyMessage="Connect a Reddit account to see karma"
+ />
</SurfaceBox>
- <SurfaceBox title="Likes and karma by tracked content" subheader="YouTube likes and Reddit karma for the top 12 tracked items">
- <RankingBarChart rows={engagementRows} label="Likes or karma" title="Likes and karma by tracked content" emptyMessage="Track content to compare engagement" />
+ <SurfaceBox title="Engagement by tracked content" subheader="YouTube likes and Reddit karma for the top 12 tracked items">
+ <MetricAreaChart
+ data={charts.engagementByContent}
+ dataKey="engagement"
+ label="Likes or karma"
+ title="Engagement by tracked content"
+ description={rankingSummary(pointsOf(charts.engagementByContent, 'name', 'engagement'), fmtNumFull, 'likes or karma')}
+ color={SERIES.neutral}
+ emptyMessage="Track content to compare engagement"
+ />
</SurfaceBox>
</div>
)}
diff --git a/frontend/src/features/analytics/overview/ViewsTab.tsx b/frontend/src/features/analytics/overview/ViewsTab.tsx
index 6641eeb..06b92c6 100644
--- a/frontend/src/features/analytics/overview/ViewsTab.tsx
+++ b/frontend/src/features/analytics/overview/ViewsTab.tsx
@@ -1,9 +1,10 @@
import SurfaceBox from '@/components/SurfaceBox';
import MetricBox from '@/components/MetricBox';
import Num from '@/components/Num';
-import RankingBarChart from '@/components/RankingBarChart';
+import MetricAreaChart from '@/components/MetricAreaChart';
+import { pointsOf, rankingSummary } from '@/lib/chart';
import { METRIC_ICONS } from '@/lib/metricIcons';
-import { fmtNum } from '../../../utils/formatters';
+import { fmtNum, fmtNumFull } from '../../../utils/formatters';
import { CATEGORY_SERIES } from './overviewCharts.ts';
import { countLabel, igTypeSummary } from './overviewLabels.ts';
import InstagramViewsTable from './InstagramViewsTable';
@@ -24,7 +25,6 @@ function instagramViewsSubtitle(stats: OverviewStats): string {
export default function ViewsTab({ stats, isSyncing }: ViewsTabProps) {
const { groups, totals, cacheTotals, trackedStats, summary } = stats;
- const categoryRows = stats.charts.categoryViews.map(point => ({ name: point.name, value: point.views }));
return (
<>
<div className="grid grid-cols-1 gap-3 sm:grid-cols-2 xl:grid-cols-4">
@@ -38,7 +38,15 @@ export default function ViewsTab({ stats, isSyncing }: ViewsTabProps) {
{stats.categoryStats.length > 0 && (
<SurfaceBox title="Views by category" subheader="YouTube video views plus Reddit post views entered by hand">
- <RankingBarChart rows={categoryRows} label="Views" title="Views by category" color={CATEGORY_SERIES} emptyMessage="No views recorded for these categories yet" />
+ <MetricAreaChart
+ data={stats.charts.categoryViews}
+ dataKey="views"
+ label="Views"
+ title="Views by category"
+ description={rankingSummary(pointsOf(stats.charts.categoryViews, 'name', 'views'), fmtNumFull, 'views')}
+ color={CATEGORY_SERIES}
+ emptyMessage="No views recorded for these categories yet"
+ />
</SurfaceBox>
)}
diff --git a/frontend/src/features/analytics/overview/overviewCharts.ts b/frontend/src/features/analytics/overview/overviewCharts.ts
index 419206c..36b941f 100644
--- a/frontend/src/features/analytics/overview/overviewCharts.ts
+++ b/frontend/src/features/analytics/overview/overviewCharts.ts
@@ -1,5 +1,6 @@
import { PLATFORM_META } from '../../../lib/accounts.ts';
import { SERIES } from '../../../lib/chart.ts';
+import { fmtNumFull } from '../../../utils/formatters.ts';
import type { TrackedMetrics } from '../../../utils/trackedContent.ts';
import type { Platform } from '../../../../../shared/types/domain.ts';
import type { OverviewAccountFields } from './overviewStats.ts';
@@ -76,23 +77,17 @@ export function audienceLegendOf(groups: AccountGroups, categoryStats: CategoryC
].filter(Boolean) as AudienceLegendItem[];
}
-const AUDIENCE_KIND: Record<AudiencePointType, string> = {
+export const AUDIENCE_KIND: Record<AudiencePointType, string> = {
youtube: 'YouTube subscribers',
instagram: 'Instagram followers',
reddit: 'Reddit karma',
category: 'Category karma',
};
-export interface AudienceRankingRow {
- name: string;
- value: number;
- kind: string;
- color: string;
-}
-
-// each bar says what it counts, because subscribers, followers and karma share one axis
-export function audienceRankingOf(points: AudiencePoint[]): AudienceRankingRow[] {
- return points.map(point => ({ name: point.name, value: point.value, kind: AUDIENCE_KIND[point.type], color: audienceDotColor(point.type) }));
+// subscribers, followers and karma share one line, so each point is read with what it counts
+export function audienceDescription(points: AudiencePoint[]): string {
+ if (points.length === 0) return 'No data yet.';
+ return points.map(point => `${point.name}, ${AUDIENCE_KIND[point.type]}: ${fmtNumFull(point.value)}.`).join(' ');
}
export function audienceDotColor(type: AudiencePointType): string {
diff --git a/frontend/src/lib/chart.ts b/frontend/src/lib/chart.ts
index ff937bb..9d3db0c 100644
--- a/frontend/src/lib/chart.ts
+++ b/frontend/src/lib/chart.ts
@@ -38,6 +38,11 @@ export interface ChartPoint {
value: number;
}
+// the rows a chart draws as name and value pairs, for the description read in place of the plot
+export function pointsOf<T extends object>(rows: readonly T[], nameKey: keyof T, valueKey: keyof T): ChartPoint[] {
+ return rows.map(row => ({ name: String(row[nameKey] ?? ''), value: Number(row[valueKey]) || 0 }));
+}
+
export function rankingSummary(points: readonly ChartPoint[], formatValue: (value: number) => string, unit: string): string {
if (points.length === 0) return 'No data yet.';
const sorted = [...points].sort((a, b) => b.value - a.value);
diff --git a/frontend/tests/features/analytics/overview/overviewCharts.test.ts b/frontend/tests/features/analytics/overview/overviewCharts.test.ts
index 02b4dc6..34aaf05 100644
--- a/frontend/tests/features/analytics/overview/overviewCharts.test.ts
+++ b/frontend/tests/features/analytics/overview/overviewCharts.test.ts
@@ -1,7 +1,7 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
- CATEGORY_SERIES, audienceChartDataOf, audienceLegendOf, audienceDotColor, commentByContentOf, engagementByContentOf,
+ CATEGORY_SERIES, audienceChartDataOf, audienceDescription, audienceLegendOf, audienceDotColor, commentByContentOf, engagementByContentOf,
categoryViewDataOf, commentByCategoryOf, karmaByAccountOf,
} from '../../../../src/features/analytics/overview/overviewCharts.ts';
@@ -22,6 +22,12 @@ test('audience chart shortens names and keeps category karma only', () => {
]);
});
+test('the audience description names what each point counts, in full', () => {
+ const points = [{ name: 'Kitchen', value: 12345, type: 'youtube' as const }, { name: 'Tech', value: 40, type: 'category' as const }];
+ assert.equal(audienceDescription(points), 'Kitchen, YouTube subscribers: 12,345. Tech, Category karma: 40.');
+ assert.equal(audienceDescription([]), 'No data yet.');
+});
+
test('legend lists only platforms with data', () => {
const labels = audienceLegendOf({ youtube: [], instagram: [{}], reddit: [] }, []).map(item => item.label);
assert.deepEqual(labels, ['Instagram followers']);
diff --git a/frontend/tests/lib/chart.test.ts b/frontend/tests/lib/chart.test.ts
new file mode 100644
index 0000000..0dceddb
--- /dev/null
+++ b/frontend/tests/lib/chart.test.ts
@@ -0,0 +1,21 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { pointsOf, rankingSummary, trendSummary } from '../../src/lib/chart.ts';
+
+const format = (value: number) => String(value);
+
+test('chart rows become name and value pairs, with a missing value read as zero', () => {
+ const rows = [{ day: 'Mon', count: 3 }, { day: 'Tue', count: Number.NaN }];
+ assert.deepEqual(pointsOf(rows, 'day', 'count'), [{ name: 'Mon', value: 3 }, { name: 'Tue', value: 0 }]);
+});
+
+test('a ranking names the highest and the lowest item', () => {
+ const points = [{ name: 'A', value: 2 }, { name: 'B', value: 9 }, { name: 'C', value: 5 }];
+ assert.equal(rankingSummary(points, format, 'views'), 'Highest: B, 9 views. Lowest: A, 2 views.');
+ assert.equal(rankingSummary([], format, 'views'), 'No data yet.');
+});
+
+test('a trend reads from the first point to the last with its peak', () => {
+ const points = [{ name: 'Jan', value: 4 }, { name: 'Feb', value: 10 }, { name: 'Mar', value: 6 }];
+ assert.equal(trendSummary(points, format, 'posts'), 'From Jan to Mar: 4 to 6 posts. Peak 10 in Feb.');
+});