"use client"; import { useState } from "react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; const MB = 1_000_000; /** Download speed limit in MB/s; empty means no limit. Applies to running downloads within seconds. */ export function SpeedLimit({ initial }: { initial: number | null }) { const [saved, setSaved] = useState(initial); const [text, setText] = useState(initial ? String(initial / MB) : ""); const [busy, setBusy] = useState(false); const value = text.trim() === "" ? null : Number(text.replace(",", ".")); const invalid = value != null && !(value >= 0.1); const dirty = value !== (saved == null ? null : saved / MB); async function save(next: number | null) { setBusy(true); try { const res = await fetch("/api/settings/downloads", { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ maxBytesPerSecond: next == null ? null : Math.round(next * MB) }), }); const body = await res.json().catch(() => ({})); if (!res.ok) return toast.error(body.error?.message ?? `Could not save (${res.status})`); setSaved(body.maxBytesPerSecond); setText(body.maxBytesPerSecond ? String(body.maxBytesPerSecond / MB) : ""); toast.success(body.maxBytesPerSecond ? `Downloads limited to ${body.maxBytesPerSecond / MB} MB/s` : "Download speed is no longer limited"); } finally { setBusy(false); } } return ( Speed limit Caps the total download speed from Hugging Face, e.g. to leave bandwidth for others. It also applies to downloads that are already running. Leave empty for no limit.
{ e.preventDefault(); if (!invalid) void save(value); }} >
setText(e.target.value.replace(/[^\d.,]/g, ""))} inputMode="decimal" placeholder="No limit" className="w-40" aria-invalid={invalid} />
{saved != null && ( )}

{invalid ? "At least 0.1 MB/s." : `1 MB/s ≈ 8 Mbit/s. hf has no speed option of its own, so Kaname pauses it whenever it gets ahead; short bursts above the limit are normal, the average holds.`}

); }