Chat / frontend /src /components /stillwave /StorageAdmin.tsx
kishankrish19's picture
Deploy Stillwave fullstack clean
b0798af
Raw History Blame Contribute Delete
15.2 kB
"use client";
import { useEffect, useState } from "react";
import Link from "next/link";
import {
Cloud,
Plus,
ShieldCheck,
Check,
RefreshCw,
ArrowLeft,
} from "lucide-react";
import { api, apiRequest, User } from "@/lib/api/apiClient";
interface Account {
id: string;
label: string;
repo: string;
enabled: boolean;
priority: number;
health: string;
last_error?: string;
cooldown: number;
has_token: boolean;
}
interface Job {
id: string;
title: string;
status: string;
attempts: number;
next_at: number;
error?: string;
copies: { repo: string }[];
}
export default function StorageAdmin() {
const [user, setUser] = useState<User | null>(null),
[accounts, setAccounts] = useState<Account[]>([]),
[jobs, setJobs] = useState<Job[]>([]),
[error, setError] = useState(""),
[notice, setNotice] = useState(""),
[editing, setEditing] = useState<string | null>(null),
[show, setShow] = useState(false),
[label, setLabel] = useState(""),
[repo, setRepo] = useState(""),
[accessToken, setAccessToken] = useState(""),
[priority, setPriority] = useState(0),
[enabled, setEnabled] = useState(false),
[busy, setBusy] = useState("");
const load = () =>
apiRequest<{ accounts: Account[]; jobs: Job[] }>("/admin/storage")
.then((r) => {
setAccounts(r.accounts);
setJobs(r.jobs);
})
.catch((e) => setError(e.message));
useEffect(() => {
api
.getMe()
.then(setUser)
.catch((e) => setError(e.message));
void load();
const timer = setInterval(load, 10000);
return () => clearInterval(timer);
}, []);
function edit(a?: Account) {
setEditing(a?.id || null);
setLabel(a?.label || "");
setRepo(a?.repo || "");
setPriority(a?.priority || 0);
setEnabled(a?.enabled || false);
setAccessToken("");
setShow(true);
setError("");
}
async function save(e: React.FormEvent) {
e.preventDefault();
setBusy("save");
setError("");
try {
await apiRequest(
"/admin/storage/accounts" + (editing ? "/" + editing : ""),
{
method: editing ? "PATCH" : "POST",
body: JSON.stringify({
label,
repo,
priority,
enabled,
...(accessToken ? { token: accessToken } : {}),
}),
},
);
setAccessToken("");
setShow(false);
setNotice(
"Storage account saved. Tokens are encrypted and never returned to the browser.",
);
await load();
} catch (e) {
setError((e as Error).message);
} finally {
setBusy("");
}
}
return (
<div className="page-enter">
<Link href="/settings" className="text-link">
<ArrowLeft size={15} /> Settings
</Link>
<div className="page-title-row">
<div>
<span className="eyebrow">ADMINISTRATION</span>
<h1>
A safe home for every song<span className="title-period">.</span>
</h1>
<p>
Manage Hugging Face datasets, backup priorities, and upload
recovery.
</p>
</div>
{user?.account_type === "admin" && (
<button className="button" onClick={() => edit()}>
<Plus size={17} /> Add account
</button>
)}
</div>
{error && (
<div className="notice error" role="alert">
{error}
</div>
)}
{notice && (
<div className="notice" role="status">
{notice}
<button onClick={() => setNotice("")}>×</button>
</div>
)}
{user?.account_type === "admin" && (
<>
<div className="storage-overview">
<div>
<span className="eyebrow">CONNECTED ACCOUNTS</span>
<strong>{accounts.length}</strong>
</div>
<div>
<span className="eyebrow">VERIFIED BACKUPS</span>
<strong>
{jobs.filter((j) => j.status === "replicated").length}
</strong>
</div>
<div>
<span className="eyebrow">WAITING / RETRYING</span>
<strong>
{jobs.filter((j) => j.status === "pending").length}
</strong>
</div>
<div>
<span className="eyebrow">NEEDS ATTENTION</span>
<strong>
{jobs.filter((j) => j.status === "failed").length}
</strong>
</div>
</div>
<section className="section">
<div className="section-heading">
<div>
<span className="eyebrow">PRIMARY & FALLBACK</span>
<h2>Your storage accounts</h2>
</div>
</div>
{accounts.length ? (
accounts.map((a) => (
<div className="account-card" key={a.id}>
<div className="cloud-mark">
<Cloud size={25} />
</div>
<div className="account-details">
<h3>
{a.label}{" "}
<span
className={
"status-pill " +
(a.health === "healthy" ? "success" : "")
}
>
{a.enabled ? a.health : "Paused"}
</span>
</h3>
<p>{a.repo}</p>
<small>
Priority {a.priority} ·{" "}
{a.has_token
? "Write token securely stored"
: "Token needed"}
</small>
{a.last_error && (
<small className="error-text">
{a.last_error}
{a.health === "cooldown"
? " · Cooling down until " +
new Date(a.cooldown).toLocaleTimeString()
: ""}
</small>
)}
</div>
<div className="account-buttons">
<button
className="button secondary small"
disabled={busy === a.id}
onClick={async () => {
setBusy(a.id);
setError("");
try {
const r = await apiRequest<{ message: string }>(
"/admin/storage/accounts/" + a.id + "/test",
{ method: "POST" },
);
setNotice(r.message);
await load();
} catch (e) {
setError((e as Error).message);
} finally {
setBusy("");
}
}}
>
{busy === a.id ? "Checking…" : "Test connection"}
</button>
<button
className="button secondary small"
onClick={() => edit(a)}
>
Edit
</button>
<button
className="text-link"
onClick={async () => {
try {
await apiRequest("/admin/storage/accounts/" + a.id, {
method: "PATCH",
body: JSON.stringify({ enabled: !a.enabled }),
});
await load();
} catch (e) {
setError((e as Error).message);
}
}}
>
{a.enabled ? "Pause" : "Enable"}
</button>
</div>
</div>
))
) : (
<div className="empty-state">
<Cloud />
<h3>Connect your first dataset</h3>
<p>
Add an existing repository and its write token. Your local
music is safe while you set this up.
</p>
<button className="button" onClick={() => edit()}>
Add Hugging Face account
</button>
</div>
)}
</section>
<div className="quiet-card storage-explainer">
<ShieldCheck size={23} />
<div>
<h3>Local first. Verified before trusted.</h3>
<p>
Audio is encrypted before it leaves the server. We try enabled
accounts in priority order and verify the uploaded file.
Temporary failures retry with backoff; rate limits pause
retries. A bounded local cache keeps recent songs close;
verified backups can stream directly when a local copy is
evicted.
</p>
<p>
Adding a backup account provides another upload destination. It
does not automatically duplicate existing backups. Confirm your
storage allowance and permitted dataset use with Hugging Face.
</p>
</div>
</div>
<section className="section">
<div className="section-heading">
<h2>Backup activity</h2>
<button
className="icon-button"
aria-label="Refresh backup status"
onClick={() => void load()}
>
<RefreshCw size={17} />
</button>
</div>
<div className="import-list">
{jobs.slice(0, 100).map((j) => (
<div className="import-row" key={j.id}>
<div
className={
"file-icon " + (j.status === "replicated" ? "done" : "")
}
>
{j.status === "replicated" ? (
<Check size={18} />
) : (
<Cloud size={18} />
)}
</div>
<div className="import-info">
<strong>{j.title}</strong>
<span>
{j.status === "replicated"
? "Verified on " +
j.copies.map((c) => c.repo).join(", ")
: j.status === "pending"
? "Safely stored locally · awaiting backup"
: j.status === "running"
? "Uploading and verifying"
: "Backup needs attention"}{" "}
· {j.attempts} attempts
</span>
{j.error && <small className="error-text">{j.error}</small>}
</div>
{!["running", "replicated"].includes(j.status) && (
<button
className="button small secondary"
onClick={async () => {
try {
await apiRequest(
"/admin/storage/jobs/" + j.id + "/retry",
{ method: "POST" },
);
await load();
} catch (e) {
setError((e as Error).message);
}
}}
>
Retry
</button>
)}
</div>
))}
</div>
{!jobs.length && (
<p className="muted">
Backup jobs appear after music is imported.
</p>
)}
</section>
</>
)}
{show && (
<div className="modal-backdrop">
<form
className="modal-card"
role="dialog"
aria-modal="true"
aria-label="Configure storage account"
onSubmit={save}
>
<button
type="button"
className="close-button"
aria-label="Close"
onClick={() => {
setShow(false);
setAccessToken("");
}}
>
×
</button>
<span className="eyebrow">HUGGING FACE DATASET</span>
<h2>{editing ? "Update storage" : "Another place to land"}</h2>
<label className="field">
Account label
<input
required
maxLength={80}
placeholder="Primary library"
value={label}
onChange={(e) => setLabel(e.target.value)}
/>
</label>
<label className="field">
Dataset repository
<input
required
pattern="[\w.\-]+/[\w.\-]+"
placeholder="account/music-library"
value={repo}
onChange={(e) => setRepo(e.target.value)}
/>
</label>
<label className="field">
Write access token{editing && " (leave blank to keep current)"}
<input
type="password"
autoComplete="new-password"
required={!editing}
placeholder="hf_…"
value={accessToken}
onChange={(e) => setAccessToken(e.target.value)}
/>
</label>
<label className="field">
Priority (lower numbers are tried first)
<input
type="number"
min="0"
max="100"
value={priority}
onChange={(e) => setPriority(Number(e.target.value))}
/>
</label>
<label className="setting-row">
<span>Enable background uploads to this dataset</span>
<input
type="checkbox"
checked={enabled}
onChange={(e) => setEnabled(e.target.checked)}
/>
</label>
<p className="fine-print">
Use a token with write access to this existing dataset. Enabling
it starts pending background backups.
</p>
{error && (
<p className="error-text" role="alert">
{error}
</p>
)}
<button className="button" disabled={busy === "save"}>
{busy === "save" ? "Saving…" : "Save account"}
</button>
</form>
</div>
)}
</div>
);
}