Measured against a live instance: 47 failed jobs against 33 imported. Three independent causes, all of which ended the request. The Soulseek server filters some artist names. "Michael Jackson Billie Jean" and bare "Michael Jackson" both return zero responses; "Billie Jean" returns 1083 files. The Weeknd, Kanye West and Frank Ocean behave the same way. Since pick_and_enqueue sent exactly one query of `artist title`, every request for a filtered artist failed reporting "searched 0 files" -- 28 of 29 occurrences. search_queries() now walks a ladder, loosest last, ending at the bare title, and escalates only when a search underdelivers so the common case still costs one search. Dropping the artist from the query does not mean ignoring it: match_ratio() looks for it in the folder path, where an organised share puts it. A rejected transfer ended the request. pick_and_enqueue kept a running maximum and discarded every runner-up, so "Transfer rejected: File not shared." -- a per-file condition where candidate two almost always works -- was terminal, as were timeouts and slskd's 500 on an unreachable peer. The ranked list is now persisted on the job row, ordered to put a different peer next, and try_next_candidate() advances through it. A job fails only once the list is exhausted, and then reports every peer it tried and what each one said. norm() destroyed every non-ASCII character, so "Beyonce" never matched Beyonce, nor Bjork Bjork, nor Dont Don't. It now folds accents and drops apostrophes while leaving & as a separator. Also: availability outranks format in score(), because a FLAC behind 5000 queued users used to beat an immediately available MP3; transfers that stall or that slskd forgets are abandoned instead of sitting in `downloading` forever; searches are deleted only once finished, since deleting one mid-flight makes slskd throw an optimistic-concurrency error and lose the record; a failed search POST is retried rather than failing the job on one blip. The UI moves to a sidebar shell -- rail at tablet, bottom bar on phones -- with neutral chrome and the sampled artwork colour confined to hero areas rather than repainting the whole interface. Adds URL routing, a primitive set under components/ui, and a request sheet showing the attempt ladder. Fixes hover:bg-glass-hi, which was never a valid class, so track rows and the search field had no hover or focus state at all; a reduced-motion path that could strand elements at opacity 0; the login page's hardcoded palette; and the missing 404 and error boundaries. tests/test_acquisition.py covers this path against a faked slskd. It had none. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
254 lines
8.8 KiB
TypeScript
254 lines
8.8 KiB
TypeScript
"use client";
|
|
|
|
import * as React from "react";
|
|
import { AnimatePresence, motion } from "framer-motion";
|
|
import { Search, X, Loader2 } from "lucide-react";
|
|
import { api, type Suggestions } from "@/lib/api";
|
|
import { cn } from "@/lib/utils";
|
|
|
|
type Item = { kind: "track" | "album" | "artist"; label: string; sub: string; cover: string | null; meta?: string; run: () => void };
|
|
|
|
export function SearchBar({
|
|
initial,
|
|
busy,
|
|
onSearch,
|
|
onAlbum,
|
|
onArtist,
|
|
}: {
|
|
/** Seeds the field from the URL, so a shared link shows what it searched. */
|
|
initial?: string;
|
|
busy?: boolean;
|
|
onSearch: (q: string) => void;
|
|
onAlbum: (id: number) => void;
|
|
onArtist: (id: number) => void;
|
|
}) {
|
|
const [q, setQ] = React.useState(initial ?? "");
|
|
// Follows the URL on Back/Forward, but never fights what is being typed:
|
|
// only a genuinely different incoming query replaces the field.
|
|
const seeded = React.useRef(initial);
|
|
React.useEffect(() => {
|
|
if (initial !== undefined && initial !== seeded.current) {
|
|
seeded.current = initial;
|
|
typed.current = false;
|
|
setQ(initial);
|
|
setOpen(false);
|
|
}
|
|
}, [initial]);
|
|
const [items, setItems] = React.useState<Item[]>([]);
|
|
const [open, setOpen] = React.useState(false);
|
|
// A value seeded from the URL must not pop the suggestion list open over the
|
|
// results that link was for. Only typing counts as asking for suggestions.
|
|
const typed = React.useRef(false);
|
|
const [sel, setSel] = React.useState(-1);
|
|
const inputRef = React.useRef<HTMLInputElement>(null);
|
|
|
|
// Debounced so a fast typist makes one upstream call, not eight. The
|
|
// AbortController matters more than the delay: without it a slow response
|
|
// to "rad" can land after "radiohead" and repopulate the stale list.
|
|
React.useEffect(() => {
|
|
const v = q.trim();
|
|
if (v.length < 2) {
|
|
setItems([]);
|
|
setOpen(false);
|
|
return;
|
|
}
|
|
const ac = new AbortController();
|
|
const t = setTimeout(async () => {
|
|
try {
|
|
const d: Suggestions = await api.suggest(v, ac.signal);
|
|
const next: Item[] = [
|
|
...d.tracks.map((t) => ({
|
|
kind: "track" as const,
|
|
label: t.title,
|
|
sub: t.artist,
|
|
cover: t.cover,
|
|
run: () => {
|
|
const nq = `${t.artist} ${t.title}`;
|
|
setQ(nq);
|
|
setOpen(false);
|
|
onSearch(nq);
|
|
},
|
|
})),
|
|
...d.albums.map((a) => ({
|
|
kind: "album" as const,
|
|
label: a.title,
|
|
sub: a.artist,
|
|
cover: a.cover,
|
|
meta: a.nb_tracks ? `${a.nb_tracks} tracks` : "",
|
|
run: () => {
|
|
setOpen(false);
|
|
onAlbum(a.album_id);
|
|
},
|
|
})),
|
|
...d.artists.map((a) => ({
|
|
kind: "artist" as const,
|
|
label: a.name,
|
|
sub: a.nb_album ? `${a.nb_album} releases` : "Artist",
|
|
cover: a.cover,
|
|
run: () => {
|
|
setOpen(false);
|
|
onArtist(a.artist_id);
|
|
},
|
|
})),
|
|
];
|
|
setItems(next);
|
|
setSel(-1);
|
|
setOpen(typed.current && next.length > 0);
|
|
} catch {
|
|
/* aborted or offline — leave the previous list alone */
|
|
}
|
|
}, 180);
|
|
return () => {
|
|
clearTimeout(t);
|
|
ac.abort();
|
|
};
|
|
}, [q, onSearch, onAlbum, onArtist]);
|
|
|
|
// "/" focuses search, the way every music app does.
|
|
React.useEffect(() => {
|
|
const onKey = (e: KeyboardEvent) => {
|
|
const tag = (document.activeElement as HTMLElement)?.tagName;
|
|
if (e.key === "/" && !/^(INPUT|TEXTAREA)$/.test(tag)) {
|
|
e.preventDefault();
|
|
inputRef.current?.focus();
|
|
}
|
|
};
|
|
document.addEventListener("keydown", onKey);
|
|
return () => document.removeEventListener("keydown", onKey);
|
|
}, []);
|
|
|
|
const key = (e: React.KeyboardEvent) => {
|
|
if (e.key === "ArrowDown") {
|
|
e.preventDefault();
|
|
setSel((s) => (items.length ? (s + 1) % items.length : -1));
|
|
} else if (e.key === "ArrowUp") {
|
|
e.preventDefault();
|
|
setSel((s) => (items.length ? (s - 1 + items.length) % items.length : -1));
|
|
} else if (e.key === "Escape") {
|
|
setOpen(false);
|
|
} else if (e.key === "Enter") {
|
|
e.preventDefault();
|
|
if (sel >= 0 && items[sel]) items[sel].run();
|
|
else {
|
|
setOpen(false);
|
|
onSearch(q.trim());
|
|
}
|
|
}
|
|
};
|
|
|
|
const groups: { title: string; kind: Item["kind"] }[] = [
|
|
{ title: "Songs", kind: "track" },
|
|
{ title: "Albums", kind: "album" },
|
|
{ title: "Artists", kind: "artist" },
|
|
];
|
|
|
|
return (
|
|
<div className="relative z-50">
|
|
<div
|
|
className={cn(
|
|
"card flex items-center gap-3 h-[50px] px-3.5 rounded-[20px]",
|
|
"transition-all duration-250 ease-out",
|
|
"focus-within:border-accent-line focus-within:bg-[var(--surface-2)]",
|
|
"focus-within:shadow-[0_0_0_4px_var(--color-accent-soft)]",
|
|
)}
|
|
>
|
|
<Search className="w-[18px] h-[18px] shrink-0 text-dim" />
|
|
<input
|
|
ref={inputRef}
|
|
type="search"
|
|
value={q}
|
|
onChange={(e) => {
|
|
typed.current = true;
|
|
setQ(e.target.value);
|
|
}}
|
|
onKeyDown={key}
|
|
onFocus={() => items.length && setOpen(true)}
|
|
onBlur={() => setTimeout(() => setOpen(false), 120)}
|
|
placeholder="Search songs, albums, artists…"
|
|
aria-label="Search"
|
|
autoComplete="off"
|
|
spellCheck={false}
|
|
className="flex-1 min-w-0 bg-transparent border-0 outline-none text-base
|
|
font-medium tracking-[-0.1px] text-ink placeholder:text-dim
|
|
placeholder:font-normal"
|
|
/>
|
|
{busy && <Loader2 className="w-4 h-4 shrink-0 text-accent animate-spin" />}
|
|
{q && !busy && (
|
|
<button
|
|
onClick={() => {
|
|
setQ("");
|
|
inputRef.current?.focus();
|
|
}}
|
|
aria-label="Clear search"
|
|
className="w-6.5 h-6.5 shrink-0 grid place-items-center rounded-full
|
|
card text-dim hover:text-ink cursor-pointer"
|
|
>
|
|
<X className="w-3.5 h-3.5" />
|
|
</button>
|
|
)}
|
|
</div>
|
|
|
|
<AnimatePresence>
|
|
{open && items.length > 0 && (
|
|
<motion.div
|
|
initial={{ opacity: 0, y: -8, scale: 0.985 }}
|
|
animate={{ opacity: 1, y: 0, scale: 1 }}
|
|
exit={{ opacity: 0, y: -8, scale: 0.985 }}
|
|
transition={{ duration: 0.18, ease: [0.22, 0.9, 0.28, 1] }}
|
|
className="sheet absolute top-[calc(100%+8px)] left-0 right-0 z-60 p-1.5
|
|
rounded-[20px] max-h-[min(64vh,460px)] overflow-y-auto no-scrollbar"
|
|
>
|
|
{groups.map((g) => {
|
|
const rows = items.filter((i) => i.kind === g.kind);
|
|
if (!rows.length) return null;
|
|
return (
|
|
<div key={g.kind}>
|
|
<div className="px-2.5 pt-2 pb-1 text-caption font-bold uppercase tracking-[0.9px] text-dim">
|
|
{g.title}
|
|
</div>
|
|
{rows.map((it) => {
|
|
const idx = items.indexOf(it);
|
|
return (
|
|
<div
|
|
key={`${g.kind}-${idx}`}
|
|
role="option"
|
|
aria-selected={idx === sel}
|
|
onMouseDown={(e) => {
|
|
e.preventDefault();
|
|
it.run();
|
|
}}
|
|
onMouseEnter={() => setSel(idx)}
|
|
className={cn(
|
|
"flex items-center gap-3 px-2.5 py-2 rounded-xl cursor-pointer transition-colors",
|
|
idx === sel && "bg-accent-soft",
|
|
)}
|
|
>
|
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
|
<img
|
|
src={it.cover ?? ""}
|
|
alt=""
|
|
loading="lazy"
|
|
className={cn(
|
|
"w-[38px] h-[38px] shrink-0 object-cover bg-faint",
|
|
it.kind === "artist" ? "rounded-full" : "rounded-lg",
|
|
)}
|
|
onError={(e) => (e.currentTarget.style.visibility = "hidden")}
|
|
/>
|
|
<div className="flex-1 min-w-0">
|
|
<div className="text-sm font-medium clamp1">{it.label}</div>
|
|
<div className="text-xs text-dim clamp1">{it.sub}</div>
|
|
</div>
|
|
{it.meta && <div className="shrink-0 text-caption text-dim">{it.meta}</div>}
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
);
|
|
})}
|
|
</motion.div>
|
|
)}
|
|
</AnimatePresence>
|
|
</div>
|
|
);
|
|
}
|