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): roll the metric tile values with RollingNumber

spacesdrivefa2e780e27a4
diff --git a/frontend/src/components/MetricBox.tsx b/frontend/src/components/MetricBox.tsx
index 664a39f..4096bdd 100644
--- a/frontend/src/components/MetricBox.tsx
+++ b/frontend/src/components/MetricBox.tsx
@@ -3,6 +3,9 @@ import { TrendingUp, TrendingDown } from 'lucide-react';
import { Skeleton } from '@/components/ui/skeleton';
import { cn } from '@/lib/utils';
import { trendTone } from '@/lib/library';
+import Num from './Num';
+import RollingNumber from './RollingNumber';
+import { RollingValueContext } from './rollingValue';
export interface MetricBoxProps {
icon?: React.ReactNode;
@@ -28,6 +31,13 @@ function textOf(node: React.ReactNode): string | undefined {
return typeof node === 'string' ? node : undefined;
}
+// a number or a formatted string rolls to its new value; a number also offers its exact value, as Num does
+function shownValue(value: React.ReactNode): React.ReactNode {
+ if (typeof value === 'number') return <Num value={value} />;
+ if (typeof value === 'string') return <RollingNumber text={value} />;
+ return value;
+}
+
// Quiet metric tile: neutral icon, large tabular value, label and one line of context.
export default function MetricBox({
icon,
@@ -101,7 +111,7 @@ export default function MetricBox({
small ? 'text-page-title' : 'text-metric',
)}
>
- {value}
+ <RollingValueContext.Provider value>{shownValue(value)}</RollingValueContext.Provider>
</p>
{(period || detail) && (
<p className="truncate text-caption text-ink-faint" title={context || undefined}>
diff --git a/frontend/src/components/Num.tsx b/frontend/src/components/Num.tsx
index 3685aa7..bcacaaa 100644
--- a/frontend/src/components/Num.tsx
+++ b/frontend/src/components/Num.tsx
@@ -1,6 +1,8 @@
import { fmtNum, fmtNumFull } from '@/utils/formatters';
import type { NumberLike } from '@/utils/formatters';
import { cn } from '@/lib/utils';
+import RollingNumber from './RollingNumber';
+import { useRollingValue } from './rollingValue';
export interface NumProps {
value: NumberLike;
@@ -10,14 +12,16 @@ export interface NumProps {
// a rounded figure that still offers the exact one: on hover for the mouse, and read aloud in full
export default function Num({ value, suffix = '', className }: NumProps) {
+ const rolling = useRollingValue();
const short = fmtNum(value);
const full = fmtNumFull(value);
+ const shown = rolling ? <RollingNumber text={`${short}${suffix}`} /> : `${short}${suffix}`;
if (short === full) {
- return <span className={cn('tabular-nums', className)}>{short}{suffix}</span>;
+ return <span className={cn('tabular-nums', className)}>{shown}</span>;
}
return (
<data value={String(Number(value ?? 0))} title={`${full}${suffix}`} className={cn('tabular-nums', className)}>
- <span aria-hidden="true">{short}{suffix}</span>
+ <span aria-hidden="true">{shown}</span>
<span className="sr-only">{full}{suffix}</span>
</data>
);
diff --git a/frontend/src/components/rollingValue.ts b/frontend/src/components/rollingValue.ts
new file mode 100644
index 0000000..c3295e2
--- /dev/null
+++ b/frontend/src/components/rollingValue.ts
@@ -0,0 +1,8 @@
+import { createContext, useContext } from 'react';
+
+// true inside a metric tile, where a changed number rolls to its new value; tables and lists keep theirs still
+export const RollingValueContext = createContext(false);
+
+export function useRollingValue(): boolean {
+ return useContext(RollingValueContext);
+}
diff --git a/frontend/src/features/analytics/overview/MetricTabsList.tsx b/frontend/src/features/analytics/overview/MetricTabsList.tsx
index 54e3ee1..6ed0dec 100644
--- a/frontend/src/features/analytics/overview/MetricTabsList.tsx
+++ b/frontend/src/features/analytics/overview/MetricTabsList.tsx
@@ -1,4 +1,6 @@
import { TabsList, TabsTrigger } from '@/components/ui/tabs';
+import Num from '@/components/Num';
+import { RollingValueContext } from '@/components/rollingValue';
import type { MetricTab } from './overviewLabels.ts';
export interface MetricTabsListProps {
@@ -19,7 +21,9 @@ export default function MetricTabsList({ tabs }: MetricTabsListProps) {
className="h-auto min-w-0 flex-col items-start justify-start gap-1 rounded-card border border-line bg-surface-raised p-4 text-left after:hidden hover:border-line-strong/40 data-active:border-brand data-active:bg-brand-soft/40"
>
<span className="text-small font-medium text-ink-muted">{tab.label}</span>
- <span className="font-heading text-page-title font-semibold text-ink tabular-nums">{tab.figure}</span>
+ <span className="font-heading text-page-title font-semibold text-ink tabular-nums">
+ <RollingValueContext.Provider value><Num value={tab.figure} /></RollingValueContext.Provider>
+ </span>
<span className="w-full truncate text-caption font-normal text-ink-faint" title={typeof tab.detail === 'string' ? tab.detail : undefined}>{tab.detail}</span>
</TabsTrigger>
))}
diff --git a/frontend/src/features/analytics/overview/overviewLabels.ts b/frontend/src/features/analytics/overview/overviewLabels.ts
index cbe0004..3738ef9 100644
--- a/frontend/src/features/analytics/overview/overviewLabels.ts
+++ b/frontend/src/features/analytics/overview/overviewLabels.ts
@@ -23,7 +23,8 @@ export interface MetricTabStats {
export interface MetricTab {
value: string;
label: string;
- figure: string | number;
+ // the exact figure; the tile rounds it and offers the full value
+ figure: number;
detail: string;
}
@@ -59,19 +60,19 @@ export function metricTabsOf(accounts: OverviewAccountFields[], stats: MetricTab
const { totals, cacheTotals, trackedStats, summary } = stats;
return [
{
- value: 'audience', label: 'Audience', figure: fmtNum(summary.totalAudience),
+ value: 'audience', label: 'Audience', figure: summary.totalAudience,
detail: detailOf([
partOf(totals.subscribers, 'subscribers'), partOf(totals.followers, 'followers'), partOf(totals.karma, 'karma'),
], 'No data yet'),
},
{
- value: 'views', label: 'Views', figure: fmtNum(summary.totalViews),
+ value: 'views', label: 'Views', figure: summary.totalViews,
detail: detailOf([
partOf(totals.views, 'YouTube'), partOf(cacheTotals.igViews, 'Instagram'), partOf(summary.trackedViewTotal, 'tracked'),
], 'No views recorded yet'),
},
{
- value: 'content', label: 'Content', figure: fmtNum(summary.totalContent),
+ value: 'content', label: 'Content', figure: summary.totalContent,
detail: detailOf([
partOf(totals.videos, 'videos'), partOf(totals.posts, 'Instagram'),
partOf(cacheTotals.rdPosts, 'Reddit'), partOf(trackedStats.total, 'tracked'),
@@ -79,11 +80,11 @@ export function metricTabsOf(accounts: OverviewAccountFields[], stats: MetricTab
},
{ value: 'accounts', label: 'Accounts', figure: accounts.length, detail: platformSummary(accounts) },
{
- value: 'comments', label: 'Comments', figure: fmtNum(summary.totalComments),
+ value: 'comments', label: 'Comments', figure: summary.totalComments,
detail: syncDetail(isSyncing, summary.totalComments),
},
{
- value: 'likes', label: 'Likes', figure: fmtNum(summary.totalLikes),
+ value: 'likes', label: 'Likes', figure: summary.totalLikes,
detail: syncDetail(isSyncing, summary.totalLikes),
},
];
diff --git a/frontend/tests/features/analytics/overview/overviewLabels.test.ts b/frontend/tests/features/analytics/overview/overviewLabels.test.ts
index 24b900e..fb5480d 100644
--- a/frontend/tests/features/analytics/overview/overviewLabels.test.ts
+++ b/frontend/tests/features/analytics/overview/overviewLabels.test.ts
@@ -36,7 +36,7 @@ test('metric tabs list present parts or a fallback', () => {
};
const tabs = metricTabsOf([channel, redditAccount], stats, false);
assert.deepEqual(tabs.map(tab => tab.value), ['audience', 'views', 'content', 'accounts', 'comments', 'likes']);
- assert.equal(tabs[0].figure, '1.5K');
+ assert.equal(tabs[0].figure, 1520);
assert.equal(tabs[0].detail, '1.5K subscribers, 20 karma');
assert.equal(tabs[1].detail, 'No views recorded yet');
assert.equal(tabs[2].detail, '3 videos, 2 tracked');