Files
transmission/web/StatsView.tsx
T
pastilhasandClaude Opus 5 864dd16355 transmission, extracted from the platform into its own repository
The sixth plugin out, immediately after photos. A sidecar, a proxy router and
29 web files; no db/ directory, because there are no tables.

── The one thing that is not self-contained ──

The connection lives in `service_connections`, a CORE table, reached through
getServiceConnection/saveServiceConnection/getServiceCredentials. This plugin
declares no schema.

Deliberate: that table is the app store's shared credential store, keyed on
(user_id, service), already encrypted at rest and already read by
app-store/effects.ts. A private copy of the same three columns would be a
second place the same fact could live, and the runbook is explicit that a move
plus a data-format change gives any failure two causes.

The cost, stated rather than hidden: this plugin depends on a core facility it
does not own, and would break if that table changed shape. Photos, by contrast,
owns photos_config outright.

── 38 palette classes paid down, mapped by meaning ──

  blue     downloading / download speed  -> info
  cyan     checking (verifying)          -> busy
  emerald  seeding / complete            -> success
  amber    queued / stalled / warnings   -> warning

Collapsing blue and cyan into one tone would have lost a real distinction in
the status dot, so `checking` went to `busy` — in-progress work, which is what
that tone is for.

── A near-miss worth recording ──

The first copy globbed *.tsx and *.ts and silently missed detail/ and dialogs/
— ten components. tsgo caught it as ten "cannot find module" errors. Worth
stating because it is quiet in the other direction: had those been imported
lazily, the typecheck would have passed and the panels would have broken at
runtime. Check the directory tree, not the file glob.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-18 19:19:30 +00:00

115 lines
4.8 KiB
TypeScript

import type { ReactNode } from 'react';
import type { StatsBucket } from './shared';
import { ArrowDownToLine, ArrowUpFromLine, HardDrive, Loader2 } from 'lucide-react';
import { formatDuration, formatRatio, formatSize, formatSpeed } from './format';
import { useFreeSpace, useSession, useSessionStats, useTorrents } from './useTransmissionData';
// Session statistics. Two columns because Transmission keeps two counters and conflating them is a classic
// misreading — "this session" resets when the daemon restarts, "all time" never does.
export const StatsView = () => {
const { stats, isLoading } = useSessionStats();
const { session } = useSession();
const { torrents } = useTorrents();
const downloadDir = session?.['download-dir'];
const { bytes: freeBytes } = useFreeSpace(downloadDir);
if (isLoading && !stats) {
return (
<div className="flex h-full items-center justify-center text-sm text-muted-foreground">
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Loading statistics…
</div>
);
}
if (!stats) {
return <div className="flex h-full items-center justify-center text-sm text-muted-foreground">No statistics.</div>;
}
const totalSize = torrents.reduce((sum, t) => sum + (t.totalSize ?? 0), 0);
const onDisk = torrents.reduce((sum, t) => sum + (t.haveValid ?? 0), 0);
return (
<div className="h-full overflow-y-auto p-4">
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
<Tile
icon={<ArrowDownToLine className="h-4 w-4 text-info" />}
label="Download"
value={formatSpeed(stats.downloadSpeed) || '0 B/s'}
/>
<Tile
icon={<ArrowUpFromLine className="h-4 w-4 text-success" />}
label="Upload"
value={formatSpeed(stats.uploadSpeed) || '0 B/s'}
/>
<Tile label="Torrents" value={String(stats.torrentCount)} hint={`${stats.activeTorrentCount} active`} />
<Tile
icon={<HardDrive className="h-4 w-4 text-muted-foreground" />}
label="Free space"
value={freeBytes == null ? '—' : formatSize(freeBytes)}
hint={downloadDir}
/>
</div>
<div className="mt-4 grid gap-4 lg:grid-cols-2">
<Bucket title="This session" bucket={stats['current-stats']} />
<Bucket title="All time" bucket={stats['cumulative-stats']} />
</div>
<section className="mt-4 rounded-xl border border-border p-4">
<h3 className="mb-3 text-[11px] font-semibold uppercase tracking-wide text-muted-foreground">Local library</h3>
<dl className="grid gap-x-6 gap-y-1.5 text-xs sm:grid-cols-2">
<Row label="Torrents" value={String(torrents.length)} />
<Row label="Paused" value={String(stats.pausedTorrentCount)} />
<Row label="Total size" value={formatSize(totalSize)} />
{/* haveValid, not totalSize: an unfinished or partially-wanted torrent occupies less than it
claims, and the difference is the whole reason to look. */}
<Row label="Verified on disk" value={formatSize(onDisk)} />
</dl>
</section>
</div>
);
};
const Bucket = ({ title, bucket }: { title: string; bucket: StatsBucket }) => (
<section className="rounded-xl border border-border p-4">
<h3 className="mb-3 text-[11px] font-semibold uppercase tracking-wide text-muted-foreground">{title}</h3>
<dl className="grid gap-x-6 gap-y-1.5 text-xs sm:grid-cols-2">
<Row label="Downloaded" value={formatSize(bucket.downloadedBytes)} />
<Row label="Uploaded" value={formatSize(bucket.uploadedBytes)} />
<Row
label="Ratio"
value={formatRatio(bucket.downloadedBytes > 0 ? bucket.uploadedBytes / bucket.downloadedBytes : -1)}
/>
<Row label="Torrents added" value={bucket.filesAdded.toLocaleString()} />
<Row label="Running time" value={formatDuration(bucket.secondsActive)} />
<Row label="Sessions" value={bucket.sessionCount.toLocaleString()} />
</dl>
</section>
);
type TileProps = { label: string; value: string; hint?: string; icon?: ReactNode };
const Tile = ({ label, value, hint, icon }: TileProps) => (
<div className="rounded-xl border border-border p-4">
<div className="flex items-center gap-1.5 text-[11px] font-medium uppercase tracking-wide text-muted-foreground">
{icon}
{label}
</div>
<div className="mt-1 text-xl font-semibold tabular-nums">{value}</div>
{hint && (
<div className="mt-0.5 truncate text-xs text-muted-foreground" title={hint}>
{hint}
</div>
)}
</div>
);
const Row = ({ label, value }: { label: string; value: string }) => (
<div className="flex items-baseline gap-3">
<dt className="w-32 shrink-0 text-muted-foreground">{label}</dt>
<dd className="tabular-nums">{value}</dd>
</div>
);