import { useCallback, useEffect, useState } from "react"; import { Loader2, Trash2, X } from "lucide-react"; import { api, asUiError, type BuildCacheSummary } from "@/lib/api"; import { formatBytes } from "@/lib/utils"; import { Button } from "@/components/ui/button"; import { Card } from "@/components/ui/card"; /** What the module build cache takes, with removal of one module, all modules, or the downloaded sources. */ export function BuildCacheDialog(props: { serverId: string; locale: string; onClose: () => void }) { const ru = props.locale === "ru"; const [summary, setSummary] = useState(null); const [busy, setBusy] = useState(null); const [message, setMessage] = useState(null); const [error, setError] = useState(null); const load = useCallback(() => { void api .buildCacheSummary(props.serverId) .then(setSummary) .catch((e) => setError(asUiError(e).technical)); }, [props.serverId]); useEffect(load, [load]); async function clear(key: string, module: string | null, sources: boolean) { setBusy(key); setError(null); try { const freed = await api.buildCacheClear(props.serverId, module, sources); setMessage(ru ? `Освобождено ${formatBytes(freed)}` : `Freed ${formatBytes(freed)}`); load(); } catch (e) { setError(asUiError(e).technical); } finally { setBusy(null); } } const when = (secs: number) => secs > 0 ? new Date(secs * 1000).toLocaleDateString(ru ? "ru-RU" : undefined, { day: "numeric", month: "short" }) : "—"; return (

{ru ? "Кэш сборок" : "Build cache"}

{ru ? "Скомпилированные части модулей хранятся сжатыми и ускоряют повторную сборку. После обновления ядра старый кэш удаляется сам. Удаление ничего не ломает: следующая сборка модуля будет полной." : "Compiled parts of modules are kept compressed and speed up the next build. After a core update the old cache goes by itself. Removing it breaks nothing: the next build of a module is a full one."}

{!summary && !error && } {summary && ( <>
{summary.modules.length === 0 &&

{ru ? "Кэш пуст." : "The cache is empty."}

} {summary.modules.map((m) => (

{m.module}

{m.packed ? ru ? `сжато из ${formatBytes(m.unpacked_bytes)}` : `packed from ${formatBytes(m.unpacked_bytes)}` : ru ? "не сжато" : "not packed"} {" · "} {ru ? "сборка " : "built "} {when(m.last_used)} {!m.current_core && (ru ? " · для другого ядра" : " · for another core")}

{formatBytes(m.bytes)}
))}
{ru ? "Скачанные исходники модулей" : "Downloaded module sources"}: {formatBytes(summary.sources_bytes)}
{ru ? "Всего" : "Total"}: {formatBytes(summary.total_bytes)} {ru ? `, предел ${formatBytes(summary.limit_bytes)}` : `, limit ${formatBytes(summary.limit_bytes)}`}
)} {message &&

{message}

} {error &&

{error}

}
); }