"use client"; import { Loader2Icon, ScanSearchIcon } from "lucide-react"; import { useEffect, useState, useSyncExternalStore } from "react"; import { toast } from "sonner"; import { LocalTime } from "@/components/local-time"; 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"; import { NativeSelect } from "@/components/ui/native-select"; import { FREQUENCY_LABEL, SCAN_FREQUENCIES, type ScanFrequency } from "@/lib/folder-scan"; import type { ScanStatus } from "@/lib/settings"; const WEEKDAYS = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; const browserTz = () => Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC"; const noSubscribe = () => () => {}; /** When the worker looks for importable folders in the models folder, plus "Scan now". */ export function ScanSettings({ initial }: { initial: ScanStatus }) { const [status, setStatus] = useState(initial); const [every, setEvery] = useState(initial.schedule.every); const [time, setTime] = useState(initial.schedule.time); const [weekday, setWeekday] = useState(initial.schedule.weekday); const [monthDay, setMonthDay] = useState(initial.schedule.monthDay); const [busy, setBusy] = useState(false); // The browser's zone; the server renders the stored one. const tz = useSyncExternalStore(noSubscribe, browserTz, () => initial.schedule.tz); const s = status.schedule; const dirty = every !== s.every || time !== s.time || weekday !== s.weekday || monthDay !== s.monthDay || (every !== "2min" && every !== "manual" && tz !== s.tz); const timed = every === "daily" || every === "weekly" || every === "monthly"; // While a requested scan hasn't run yet, refresh until it has. useEffect(() => { if (!status.requested) return; const t = setInterval(async () => { const res = await fetch("/api/settings/downloads", { cache: "no-store" }).catch(() => null); if (res?.ok) setStatus((await res.json()).scan); }, 2_000); return () => clearInterval(t); }, [status.requested]); async function save() { setBusy(true); try { const res = await fetch("/api/settings/downloads", { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ scanSchedule: { every, time, weekday, monthDay, tz } }), }); const body = await res.json().catch(() => ({})); if (!res.ok) return toast.error(body.error?.message ?? `Could not save (${res.status})`); setStatus(body.scan); toast.success("Scan schedule saved"); } finally { setBusy(false); } } async function scanNow() { const res = await fetch("/api/local-folders/scan", { method: "POST" }); if (!res.ok) return toast.error(`Could not start a scan (${res.status})`); setStatus((cur) => ({ ...cur, requested: true })); } return ( Models folder scan Kaname looks through the models folder for folders that aren't in the library yet, so you can import them. It only lists files and their sizes (no hashing) and skips the models you already have; on a NAS, a rarer schedule lets the disks sleep.
setEvery(e.target.value as ScanFrequency)} className="w-64"> {SCAN_FREQUENCIES.map((f) => ( ))}
{every === "weekly" && (
setWeekday(Number(e.target.value))} className="w-36"> {WEEKDAYS.map((d, i) => ( ))}
)} {every === "monthly" && (
setMonthDay(Number(e.target.value))} className="w-24"> {Array.from({ length: 28 }, (_, i) => i + 1).map((d) => ( ))}
)} {timed && (
setTime(e.target.value || "03:00")} className="w-32" />
)} {every === "hourly" && (
setTime(`${time.slice(0, 2)}:${e.target.value}`)} className="w-24"> {Array.from({ length: 12 }, (_, i) => String(i * 5).padStart(2, "0")).map((m) => ( ))}
)}
{timed &&

Times are in your time zone ({tz}).

}
Last scan
{status.lastScanAt ? ( <> · {status.lastScanFound ?? 0} folder{status.lastScanFound === 1 ? "" : "s"} to import ) : ( "not yet" )}
Next scan
{status.nextScanAt ? : "only when you click Scan now"}
); }