|
| 1 | +import { useCallback, useEffect, useState } from "react"; |
| 2 | +import { useI18n, type TFn, type TKey, type Locale } from "../i18n/shared"; |
| 3 | +import { EmptyState } from "../ui"; |
| 4 | +import { IconRefresh } from "../icons"; |
| 5 | +import { formatBytes } from "../format-bytes"; |
| 6 | + |
| 7 | +interface StorageLargestEntry { |
| 8 | + path: string; |
| 9 | + bytes: number; |
| 10 | +} |
| 11 | + |
| 12 | +interface StorageBucket { |
| 13 | + key: string; |
| 14 | + label: string; |
| 15 | + bytes: number; |
| 16 | + fileCount: number; |
| 17 | + oldest?: number; |
| 18 | + newest?: number; |
| 19 | + largest?: StorageLargestEntry[]; |
| 20 | + rows?: number | null; |
| 21 | +} |
| 22 | + |
| 23 | +interface StorageReport { |
| 24 | + codexHome: string; |
| 25 | + generatedAt: number; |
| 26 | + total: { bytes: number; fileCount: number }; |
| 27 | + buckets: StorageBucket[]; |
| 28 | + error?: string; |
| 29 | +} |
| 30 | + |
| 31 | +// Known scanner bucket keys → localized labels; unknown future keys fall back to the API label. |
| 32 | +const BUCKET_TKEYS: Record<string, TKey> = { |
| 33 | + sessions: "storage.bucket.sessions", |
| 34 | + archived_sessions: "storage.bucket.archived_sessions", |
| 35 | + logs_db: "storage.bucket.logs_db", |
| 36 | + state_db: "storage.bucket.state_db", |
| 37 | + attachments: "storage.bucket.attachments", |
| 38 | + deletion_manifests: "storage.bucket.deletion_manifests", |
| 39 | + other: "storage.bucket.other", |
| 40 | +}; |
| 41 | + |
| 42 | +function bucketLabel(bucket: StorageBucket, t: TFn): string { |
| 43 | + const tkey = BUCKET_TKEYS[bucket.key]; |
| 44 | + return tkey ? t(tkey) : bucket.label; |
| 45 | +} |
| 46 | + |
| 47 | +function formatDate(ms: number | undefined, locale: Locale): string { |
| 48 | + return ms === undefined ? "—" : new Date(ms).toLocaleDateString(locale); |
| 49 | +} |
| 50 | + |
| 51 | +function BucketsTable({ buckets, locale, t }: { buckets: StorageBucket[]; locale: Locale; t: TFn }) { |
| 52 | + return ( |
| 53 | + <section className="panel" style={{ marginTop: 16 }} aria-labelledby="storage-buckets-title"> |
| 54 | + <h3 id="storage-buckets-title" className="panel-title">{t("storage.section.buckets")}</h3> |
| 55 | + <div className="tbl-wrap"> |
| 56 | + <table className="tbl"> |
| 57 | + <thead> |
| 58 | + <tr> |
| 59 | + <th>{t("storage.col.bucket")}</th> |
| 60 | + <th className="num">{t("storage.col.size")}</th> |
| 61 | + <th className="num">{t("storage.col.files")}</th> |
| 62 | + <th>{t("storage.col.oldest")}</th> |
| 63 | + <th>{t("storage.col.newest")}</th> |
| 64 | + <th className="num">{t("storage.col.rows")}</th> |
| 65 | + </tr> |
| 66 | + </thead> |
| 67 | + <tbody> |
| 68 | + {buckets.map(bucket => ( |
| 69 | + <tr key={bucket.key}> |
| 70 | + <td>{bucketLabel(bucket, t)}</td> |
| 71 | + <td className="num mono">{formatBytes(bucket.bytes, locale)}</td> |
| 72 | + <td className="num">{bucket.fileCount}</td> |
| 73 | + <td className="muted">{formatDate(bucket.oldest, locale)}</td> |
| 74 | + <td className="muted">{formatDate(bucket.newest, locale)}</td> |
| 75 | + <td className="num mono"> |
| 76 | + {bucket.rows === undefined ? "—" : bucket.rows === null ? t("storage.rows.unknown") : bucket.rows.toLocaleString(locale)} |
| 77 | + </td> |
| 78 | + </tr> |
| 79 | + ))} |
| 80 | + </tbody> |
| 81 | + </table> |
| 82 | + </div> |
| 83 | + </section> |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +function LargestFilesPanel({ buckets, locale, t }: { buckets: StorageBucket[]; locale: Locale; t: TFn }) { |
| 88 | + const withLargest = buckets.filter(bucket => (bucket.largest?.length ?? 0) > 0); |
| 89 | + if (withLargest.length === 0) return null; |
| 90 | + return ( |
| 91 | + <section className="panel" style={{ marginTop: 16 }} aria-labelledby="storage-largest-title"> |
| 92 | + <h3 id="storage-largest-title" className="panel-title">{t("storage.section.largest")}</h3> |
| 93 | + {withLargest.map(bucket => ( |
| 94 | + <details key={bucket.key} style={{ marginTop: 8 }}> |
| 95 | + <summary>{bucketLabel(bucket, t)}</summary> |
| 96 | + <div className="tbl-wrap" style={{ marginTop: 8 }}> |
| 97 | + <table className="tbl"> |
| 98 | + <tbody> |
| 99 | + {bucket.largest!.map(entry => ( |
| 100 | + <tr key={entry.path}> |
| 101 | + <td className="mono">{entry.path}</td> |
| 102 | + <td className="num mono">{formatBytes(entry.bytes, locale)}</td> |
| 103 | + </tr> |
| 104 | + ))} |
| 105 | + </tbody> |
| 106 | + </table> |
| 107 | + </div> |
| 108 | + </details> |
| 109 | + ))} |
| 110 | + </section> |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +export default function Storage({ apiBase }: { apiBase: string }) { |
| 115 | + const { t, locale } = useI18n(); |
| 116 | + const [data, setData] = useState<StorageReport | null>(null); |
| 117 | + const [loading, setLoading] = useState(true); |
| 118 | + |
| 119 | + const fetchStorage = useCallback(async (signal?: AbortSignal) => { |
| 120 | + setLoading(true); |
| 121 | + try { |
| 122 | + const res = await fetch(`${apiBase}/api/storage`, { signal }); |
| 123 | + if (!res.ok) throw new Error("fetch failed"); |
| 124 | + const json = await res.json() as StorageReport; |
| 125 | + if (signal?.aborted) return; |
| 126 | + setData(json); |
| 127 | + } catch { |
| 128 | + if (signal?.aborted) return; |
| 129 | + setData(null); |
| 130 | + } finally { |
| 131 | + if (!signal?.aborted) setLoading(false); |
| 132 | + } |
| 133 | + }, [apiBase]); |
| 134 | + |
| 135 | + useEffect(() => { |
| 136 | + const controller = new AbortController(); |
| 137 | + // Deferred a tick (same pattern as Usage.tsx) so the effect never sets state synchronously. |
| 138 | + const timeout = window.setTimeout(() => { |
| 139 | + void fetchStorage(controller.signal); |
| 140 | + }, 0); |
| 141 | + return () => { |
| 142 | + window.clearTimeout(timeout); |
| 143 | + controller.abort(); |
| 144 | + }; |
| 145 | + }, [fetchStorage]); |
| 146 | + |
| 147 | + const failed = !loading && (!data || data.error !== undefined); |
| 148 | + const empty = !loading && !failed && data!.total.fileCount === 0; |
| 149 | + |
| 150 | + return ( |
| 151 | + <> |
| 152 | + <div className="page-head"> |
| 153 | + <h2 id="storage-page-title">{t("storage.title")}</h2> |
| 154 | + <button type="button" className="btn btn-ghost btn-sm" disabled={loading} onClick={() => void fetchStorage()}> |
| 155 | + <IconRefresh /> {t("storage.refresh")} |
| 156 | + </button> |
| 157 | + </div> |
| 158 | + <p className="page-sub">{t("storage.subtitle")}</p> |
| 159 | + |
| 160 | + {loading && !data ? ( |
| 161 | + <EmptyState title={t("storage.loading")} /> |
| 162 | + ) : failed ? ( |
| 163 | + <EmptyState title={t("storage.error")} /> |
| 164 | + ) : empty ? ( |
| 165 | + <EmptyState title={t("storage.empty")} /> |
| 166 | + ) : ( |
| 167 | + <> |
| 168 | + <div className="usage-cards"> |
| 169 | + <div className="stat"><div className="muted">{t("storage.card.total")}</div><div className="stat-value">{formatBytes(data!.total.bytes, locale)}</div></div> |
| 170 | + <div className="stat"><div className="muted">{t("storage.card.files")}</div><div className="stat-value">{data!.total.fileCount.toLocaleString(locale)}</div></div> |
| 171 | + <div className="stat"><div className="muted">{t("storage.card.home")}</div><div className="stat-value mono" style={{ fontSize: "var(--text-body)", wordBreak: "break-all" }}>{data!.codexHome}</div></div> |
| 172 | + </div> |
| 173 | + <BucketsTable buckets={data!.buckets} locale={locale} t={t} /> |
| 174 | + <LargestFilesPanel buckets={data!.buckets} locale={locale} t={t} /> |
| 175 | + </> |
| 176 | + )} |
| 177 | + </> |
| 178 | + ); |
| 179 | +} |
0 commit comments