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.

refactor(frontend): remove the chart summary, table and ranking bars

spacesdrive2a036e6cc920
diff --git a/frontend/src/components/ChartFigure.tsx b/frontend/src/components/ChartFigure.tsx
index ce33033..675d744 100644
--- a/frontend/src/components/ChartFigure.tsx
+++ b/frontend/src/components/ChartFigure.tsx
@@ -1,86 +1,21 @@
-import React, { useId, useState } from 'react';
-import { Table as TableIcon, LineChart } from 'lucide-react';
-import { Button } from '@/components/ui/button';
-import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
-import { cn } from '@/lib/utils';
-
-export interface ChartTableColumn {
- key: string;
- label: string;
- numeric?: boolean;
- format?: (value: unknown) => string;
-}
-
-export interface ChartTableSpec {
- caption: string;
- columns: ChartTableColumn[];
- rows: readonly object[];
-}
+import React, { useId } from 'react';
export interface ChartFigureProps {
// the chart's name for screen readers, usually its title
label?: string;
- // the takeaway in words, computed from the data and read in place of the plot
+ // the takeaway in words, computed from the data
description?: string;
- summary?: string;
- table?: ChartTableSpec;
- children: React.ReactNode;
className?: string;
+ children: React.ReactNode;
}
-function cellText(column: ChartTableColumn, row: object): string {
- const value = (row as Record<string, unknown>)[column.key];
- if (column.format) return column.format(value);
- return value == null ? '' : String(value);
-}
-
-// a chart with its takeaway in words and its numbers as a table, for anyone who cannot read the plot
-export default function ChartFigure({ label, description, summary, table, children, className }: ChartFigureProps) {
- const [showTable, setShowTable] = useState(false);
- const summaryId = useId();
- if (!summary && !table) {
- return (
- <figure aria-label={label} aria-describedby={description ? summaryId : undefined} className={className}>
- {description && <p id={summaryId} className="sr-only">{description}</p>}
- {children}
- </figure>
- );
- }
+// a plot is a picture to a screen reader, so the figure carries its name and a hidden description of what it shows
+export default function ChartFigure({ label, description, className, children }: ChartFigureProps) {
+ const descriptionId = useId();
return (
- <figure className={cn('flex flex-col gap-2', className)} aria-describedby={summary ? summaryId : undefined}>
- {showTable && table ? (
- <Table aria-label={table.caption} containerClassName="max-h-80 overflow-y-auto">
- <TableHeader>
- <TableRow>
- {table.columns.map(column => (
- <TableHead key={column.key} className={cn(column.numeric && 'text-right')}>{column.label}</TableHead>
- ))}
- </TableRow>
- </TableHeader>
- <TableBody>
- {table.rows.map((row, index) => (
- <TableRow key={index}>
- {table.columns.map(column => (
- <TableCell key={column.key} className={cn(column.numeric && 'text-right')}>{cellText(column, row)}</TableCell>
- ))}
- </TableRow>
- ))}
- </TableBody>
- </Table>
- ) : (
- children
- )}
- {(summary || table) && (
- <figcaption className="flex flex-wrap items-center justify-between gap-x-4 gap-y-1 px-2">
- {summary ? <p id={summaryId} className="max-w-prose text-small text-ink-muted">{summary}</p> : <span />}
- {table && (
- <Button variant="ghost" size="sm" onClick={() => setShowTable(shown => !shown)} aria-pressed={showTable}>
- {showTable ? <LineChart data-icon="inline-start" /> : <TableIcon data-icon="inline-start" />}
- {showTable ? 'Show chart' : 'Show table'}
- </Button>
- )}
- </figcaption>
- )}
+ <figure aria-label={label} aria-describedby={description ? descriptionId : undefined} className={className}>
+ {description && <p id={descriptionId} className="sr-only">{description}</p>}
+ {children}
</figure>
);
}
diff --git a/frontend/src/components/ChartPanel.tsx b/frontend/src/components/ChartPanel.tsx
index 695f782..23211dd 100644
--- a/frontend/src/components/ChartPanel.tsx
+++ b/frontend/src/components/ChartPanel.tsx
@@ -1,40 +1,27 @@
import React from 'react';
-import { ResponsiveContainer } from 'recharts';
import SurfaceBox from './SurfaceBox';
import ChartFigure from './ChartFigure';
-import type { ChartTableSpec } from './ChartFigure';
-// Titled chart container. Pass `empty` to show a quiet message instead of an empty plot, `summary` for the takeaway
-// in words and `table` for the same numbers as a table.
+// Titled chart container. Pass `empty` to show a quiet message instead of an empty plot.
export interface ChartPanelProps {
title?: React.ReactNode;
subheader?: React.ReactNode;
// read by screen readers in place of the plot
description?: string;
- summary?: string;
- table?: ChartTableSpec;
height?: number;
empty?: boolean;
emptyMessage?: React.ReactNode;
className?: string;
- children: React.ReactElement<{ title?: string; desc?: string }>;
+ children: React.ReactNode;
}
-export default function ChartPanel({ title, subheader, description, summary, table, height = 280, empty = false, emptyMessage = 'Not enough data yet', className, children }: ChartPanelProps) {
- // the svg itself is a keyboard stop (recharts' accessibility layer), so it needs a name of its own
- const plot = typeof title === 'string' ? React.cloneElement(children, { title, desc: summary }) : children;
+export default function ChartPanel({ title, subheader, description, height = 280, empty = false, emptyMessage = 'Not enough data yet', className, children }: ChartPanelProps) {
return (
<SurfaceBox title={title} subheader={subheader} className={className} contentClassName="px-3 pt-4 pb-3">
{empty ? (
<p className="flex items-center justify-center text-small text-ink-faint" style={{ height }}>{emptyMessage}</p>
- ) : !summary && !table ? (
- <ChartFigure label={typeof title === 'string' ? title : undefined} description={description}>{children}</ChartFigure>
) : (
- <ChartFigure summary={summary} table={table}>
- <ResponsiveContainer width="100%" height={height} debounce={200}>
- {plot}
- </ResponsiveContainer>
- </ChartFigure>
+ <ChartFigure label={typeof title === 'string' ? title : undefined} description={description}>{children}</ChartFigure>
)}
</SurfaceBox>
);
diff --git a/frontend/src/components/RankingBarChart.tsx b/frontend/src/components/RankingBarChart.tsx
deleted file mode 100644
index d922282..0000000
--- a/frontend/src/components/RankingBarChart.tsx
+++ /dev/null
@@ -1,64 +0,0 @@
-import React from 'react';
-import { Bar, BarChart, CartesianGrid, Cell, ResponsiveContainer, Tooltip as ReTooltip, XAxis, YAxis } from 'recharts';
-import { fmtNum, fmtNumFull } from '../utils/formatters';
-import type { NumberLike } from '../utils/formatters';
-import { axisProps, gridProps, rankingSummary, tooltipCursor, tooltipStyle } from '@/lib/chart';
-import ChartFigure from './ChartFigure';
-
-export interface RankingRow {
- name: string;
- value: number;
- // a second label per bar (for example the platform or unit), shown in the tooltip and the table
- kind?: string;
- color?: string;
-}
-
-// separate things ranked by one number: horizontal bars, largest first, because a line would suggest a trend
-export interface RankingBarChartProps {
- rows: readonly RankingRow[];
- label: string;
- title: string;
- kindLabel?: string;
- color?: string;
- emptyMessage?: React.ReactNode;
-}
-
-const ROW_HEIGHT = 32;
-const MIN_HEIGHT = 120;
-
-export default function RankingBarChart({ rows, label, title, kindLabel, color = 'var(--chart-2)', emptyMessage = 'No data to display' }: RankingBarChartProps) {
- if (rows.length === 0) {
- return <p className="py-8 text-center text-small text-ink-faint">{emptyMessage}</p>;
- }
- const sorted = [...rows].sort((a, b) => b.value - a.value);
- const summary = rankingSummary(sorted, fmtNum, label.toLowerCase());
- const height = Math.max(MIN_HEIGHT, sorted.length * ROW_HEIGHT + 24);
- const columns = [
- { key: 'name', label: 'Name' },
- ...(kindLabel ? [{ key: 'kind', label: kindLabel }] : []),
- { key: 'value', label, numeric: true, format: (value: unknown) => fmtNumFull(value as NumberLike) },
- ];
-
- return (
- <ChartFigure summary={summary} table={{ caption: title, columns, rows: sorted }}>
- <ResponsiveContainer width="100%" height={height} debounce={200}>
- <BarChart data={sorted} layout="vertical" margin={{ top: 4, right: 16, left: 0, bottom: 4 }} title={title} desc={summary}>
- <CartesianGrid {...gridProps} horizontal={false} />
- <XAxis type="number" {...axisProps} tickFormatter={fmtNum} allowDecimals={false} />
- <YAxis type="category" dataKey="name" {...axisProps} width={120} interval={0} />
- <ReTooltip
- contentStyle={tooltipStyle}
- cursor={tooltipCursor}
- formatter={(value, _name, item) => {
- const kind = (item.payload as RankingRow).kind;
- return [fmtNumFull(value as NumberLike), kind ? `${label} (${kind})` : label];
- }}
- />
- <Bar dataKey="value" name={label} fill={color} radius={[0, 4, 4, 0]} maxBarSize={20}>
- {sorted.map((row, index) => <Cell key={index} fill={row.color ?? color} />)}
- </Bar>
- </BarChart>
- </ResponsiveContainer>
- </ChartFigure>
- );
-}
diff --git a/frontend/src/lib/chart.ts b/frontend/src/lib/chart.ts
index 9d3db0c..ad0d628 100644
--- a/frontend/src/lib/chart.ts
+++ b/frontend/src/lib/chart.ts
@@ -12,26 +12,6 @@ export const SERIES_ORDER = [SERIES.primary, SERIES.neutral, SERIES.warm, SERIES
export const seriesAt = (index: number): string => SERIES_ORDER[index % SERIES_ORDER.length];
-export const axisTick = { fill: 'var(--ink-faint)', fontSize: 11, fontFamily: 'var(--font-sans)' };
-
-export const axisProps = { tick: axisTick, axisLine: false, tickLine: false };
-
-export const gridProps = { strokeDasharray: '3 3', stroke: 'var(--divider)' };
-
-export const tooltipStyle = {
- background: 'var(--surface-overlay)',
- border: '1px solid var(--line)',
- borderRadius: 10,
- boxShadow: 'var(--shadow-md)',
- color: 'var(--ink)',
- fontSize: 12,
- padding: '8px 10px',
-};
-
-export const tooltipCursor = { fill: 'var(--surface-hover)' };
-
-export const legendStyle = { fontSize: 12, color: 'var(--ink-muted)' };
-
// a figure is a category name and its value; the summary is what a sighted reader takes from the plot at a glance
export interface ChartPoint {
name: string;