"use client"; import { useCallback, useEffect, useState } from "react"; import { api, type AdminShortUrl, type ShortUrlStats } from "@/lib/api"; import { usePagedList } from "@/lib/use-paged-list"; import { ListShell, EmptyRow } from "@/components/list-shell"; import { Table, THead, TR, TH, TD } from "@/components/ui/table"; import { fmtDate } from "@/lib/utils"; import { Clamp } from "@/components/ui/clamp"; export default function ShortUrlsPage() { // ── Stats ──────────────────────────────────────────────────────────────── const [stats, setStats] = useState(null); const [from, setFrom] = useState(""); const [to, setTo] = useState(""); useEffect(() => { // Fetch once both custom dates are set, or once both are cleared (fixed // windows only). A half-filled range waits for the second date. const ready = (!!from && !!to) || (!from && !to); if (!ready) return; let cancelled = false; api .shortUrlStats(from || undefined, to || undefined) .then((s) => { if (!cancelled) setStats(s); }) .catch(() => { /* leave the previous stats in place on error */ }); return () => { cancelled = true; }; }, [from, to]); // ── Paginated list ───────────────────────────────────────────────────────── const [q, setQ] = useState(""); const fetcher = useCallback( (limit: number, offset: number) => api.shortUrls(q, limit, offset), [q], ); const list = usePagedList(fetcher, [q]); return (

Short URL stats

{/* Custom date range (inclusive). Clearing both returns to the fixed windows below. */}
{(from || to) && ( )}
{stats?.range && ( )}
{list.items.length === 0 && !list.loading ? ( ) : ( list.items.map((s) => ( )) )}
Code Target Type Clicks Status Created
{s.code} {s.long_url} {s.content_type || "—"} {s.click_count.toLocaleString()} ({s.human_click_count.toLocaleString()} human) {s.disabled ? "Disabled" : "Active"} {fmtDate(s.created_at)}
); } function StatCard({ label, created, clicks, highlight, }: { label: string; created?: number; clicks?: number; highlight?: boolean; }) { return (
{label}
{(created ?? 0).toLocaleString()}
created
{(clicks ?? 0).toLocaleString()}
clicks
); }