Files
karaoke-gen/frontend/components/ui/autocomplete-input.tsx
T
Andrew Beveridge ☄️andClaude Opus 4.6 186b3dd7a9 feat: song lookup autocomplete and community version detection (#451)
## Summary

- Add track autocomplete on the title field using karaoke-decide catalog
API (MusicBrainz + Spotify data) for canonical artist/title styling
- Add community karaoke version detection via karaokenerds.com scraping
— warns users when existing high-quality versions are available on
YouTube, saving credits
- Both features are optional and non-blocking — users can still type
freely and submit without selecting from autocomplete

## Changes

### Backend
- **`backend/services/catalog_proxy_service.py`** — HTTP proxy to
karaoke-decide API with 5-min in-memory TTL cache
- **`backend/services/karaokenerds_service.py`** — Karaokenerds.com HTML
scraper (ported from kjbox) for community version detection
- **`backend/api/routes/catalog.py`** — 3 new endpoints: `GET
/api/catalog/artists`, `GET /api/catalog/tracks`, `POST
/api/catalog/community-check` with per-user rate limiting (20 req/min)
- **`backend/config.py`** — Added `karaoke_decide_api_url` setting

### Frontend
- **`frontend/components/ui/autocomplete-input.tsx`** — Reusable
autocomplete input component (shadcn Command + Popover)
- **`frontend/components/job/CommunityVersionBanner.tsx`** — Green
dismissible banner showing community karaoke versions with YouTube links
- **`frontend/components/job/steps/SongInfoStep.tsx`** — Title field now
has autocomplete; selecting a track overwrites both artist and title
with canonical names
- **`frontend/lib/api.ts`** — Added catalog API client functions

### Tests
- 9 backend route tests (artist/track search, community check, rate
limiting)
- 9 karaokenerds service tests (HTML parsing, community detection,
YouTube URL cleanup)

## How it works

1. User types artist name in the plain text field
2. User starts typing in the title field → autocomplete dropdown shows
tracks from Spotify/MusicBrainz, filtered by artist
3. Selecting a track overwrites both fields with canonical names (e.g.,
"fleetwood mac" → "Fleetwood Mac")
4. When both fields are filled, a community check fires — if existing
karaoke versions exist on YouTube, a green banner appears with links

## Test plan

- [x] 18 backend tests pass (catalog routes + karaokenerds service)
- [x] Tested locally with dev server against real karaoke-decide API
- [x] Verified autocomplete shows results, selection overwrites both
fields
- [x] Verified community banner appears for known songs (e.g., Fleetwood
Mac - Dreams)
- [x] Verified graceful degradation when API is unavailable

@coderabbitai ignore

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 11:28:01 -05:00

232 lines
7.2 KiB
TypeScript

"use client"
import * as React from "react"
import { useRef, useState, useEffect, useCallback } from "react"
import { Command, CommandEmpty, CommandGroup, CommandItem, CommandList } from "@/components/ui/command"
import { Popover, PopoverContent, PopoverAnchor } from "@/components/ui/popover"
import { Loader2 } from "lucide-react"
import { cn } from "@/lib/utils"
export interface AutocompleteSuggestion {
/** Unique key for the suggestion */
key: string
/** Primary display text (will be set as the input value on select) */
label: string
/** Optional secondary text displayed below the label */
description?: string
/** Optional right-aligned text (e.g., popularity score) */
meta?: string
/** The full data object from the API */
data?: any
}
interface AutocompleteInputProps {
/** Current input value */
value: string
/** Called when the input value changes (typing) */
onChange: (value: string) => void
/** Called when a suggestion is selected */
onSelect?: (suggestion: AutocompleteSuggestion) => void
/** Async function to fetch suggestions for a query */
fetchSuggestions: (query: string) => Promise<AutocompleteSuggestion[]>
/** Minimum characters before fetching suggestions */
minChars?: number
/** Debounce delay in ms */
debounceMs?: number
/** Input placeholder */
placeholder?: string
/** Disable the input */
disabled?: boolean
/** HTML id for the input */
id?: string
/** data-testid for testing */
"data-testid"?: string
/** Inline style for the input element */
style?: React.CSSProperties
/** Additional className for the input */
className?: string
}
export function AutocompleteInput({
value,
onChange,
onSelect,
fetchSuggestions,
minChars = 2,
debounceMs = 300,
placeholder,
disabled,
id,
"data-testid": dataTestId,
style,
className,
}: AutocompleteInputProps) {
const [open, setOpen] = useState(false)
const [suggestions, setSuggestions] = useState<AutocompleteSuggestion[]>([])
const [isLoading, setIsLoading] = useState(false)
const inputRef = useRef<HTMLInputElement>(null)
const debounceRef = useRef<ReturnType<typeof setTimeout> | null>(null)
// Track whether the user just selected a suggestion to avoid re-fetching
const justSelectedRef = useRef(false)
const doFetch = useCallback(
async (query: string) => {
if (query.length < minChars) {
setSuggestions([])
setOpen(false)
return
}
setIsLoading(true)
try {
const results = await fetchSuggestions(query)
setSuggestions(results)
setOpen(results.length > 0)
} catch {
setSuggestions([])
setOpen(false)
} finally {
setIsLoading(false)
}
},
[fetchSuggestions, minChars]
)
// Debounced fetch on value change
useEffect(() => {
if (justSelectedRef.current) {
justSelectedRef.current = false
return
}
if (debounceRef.current) {
clearTimeout(debounceRef.current)
}
if (value.length < minChars) {
setSuggestions([])
setOpen(false)
return
}
debounceRef.current = setTimeout(() => {
doFetch(value)
}, debounceMs)
return () => {
if (debounceRef.current) {
clearTimeout(debounceRef.current)
}
}
}, [value, doFetch, debounceMs, minChars])
function handleSelect(suggestion: AutocompleteSuggestion) {
justSelectedRef.current = true
onChange(suggestion.label)
onSelect?.(suggestion)
setOpen(false)
setSuggestions([])
// Re-focus input after selection
inputRef.current?.focus()
}
function handleInputChange(e: React.ChangeEvent<HTMLInputElement>) {
onChange(e.target.value)
}
function handleKeyDown(e: React.KeyboardEvent<HTMLInputElement>) {
// Let cmdk handle arrow keys and enter when popover is open
if (open && (e.key === "ArrowDown" || e.key === "ArrowUp")) {
// Don't prevent - let the event bubble to Command
return
}
if (e.key === "Escape") {
setOpen(false)
}
}
return (
<Popover open={open} onOpenChange={setOpen}>
<PopoverAnchor asChild>
<div className="relative">
<input
ref={inputRef}
id={id}
data-testid={dataTestId}
type="text"
value={value}
onChange={handleInputChange}
onKeyDown={handleKeyDown}
onFocus={() => {
// Re-open if we have suggestions
if (suggestions.length > 0 && value.length >= minChars) {
setOpen(true)
}
}}
placeholder={placeholder}
disabled={disabled}
style={style}
className={cn(
"file:text-foreground placeholder:text-muted-foreground selection:bg-primary selection:text-primary-foreground dark:bg-input/30 border-input h-9 w-full min-w-0 rounded-md border bg-transparent px-3 py-1 text-base shadow-xs transition-[color,box-shadow] outline-none disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
"focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]",
className
)}
autoComplete="off"
/>
{isLoading && (
<Loader2
className="absolute right-2 top-1/2 -translate-y-1/2 w-4 h-4 animate-spin"
style={{ color: "var(--text-muted)" }}
/>
)}
</div>
</PopoverAnchor>
<PopoverContent
className="p-0 w-[var(--radix-popover-trigger-width)]"
align="start"
sideOffset={4}
onOpenAutoFocus={(e) => {
// Prevent popover from stealing focus from input
e.preventDefault()
}}
>
<Command shouldFilter={false}>
<CommandList>
<CommandEmpty className="py-3 text-center text-sm" style={{ color: "var(--text-muted)" }}>
No results found
</CommandEmpty>
<CommandGroup>
{suggestions.map((suggestion) => (
<CommandItem
key={suggestion.key}
value={suggestion.key}
onSelect={() => handleSelect(suggestion)}
className="cursor-pointer"
>
<div className="flex items-center justify-between w-full gap-2">
<div className="min-w-0 flex-1">
<div className="text-sm font-medium truncate" style={{ color: "var(--text)" }}>
{suggestion.label}
</div>
{suggestion.description && (
<div className="text-xs truncate" style={{ color: "var(--text-muted)" }}>
{suggestion.description}
</div>
)}
</div>
{suggestion.meta && (
<span className="text-xs shrink-0" style={{ color: "var(--text-muted)" }}>
{suggestion.meta}
</span>
)}
</div>
</CommandItem>
))}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
)
}