elmeg-demo/frontend/components/ui/search-dialog.tsx
fullsizemalt 5c53fbc497
Some checks are pending
Deploy Elmeg / deploy (push) Waiting to run
feat(frontend): Spec out SearchDialog for maximum utility
2025-12-21 01:53:44 -08:00

211 lines
9.7 KiB
TypeScript

"use client"
import * as React from "react"
import {
CommandDialog,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
CommandSeparator,
} from "@/components/ui/command"
import {
Search, Music, MapPin, Calendar, Users, Globe,
LayoutDashboard, Library, Star, History, ArrowRight
} from "lucide-react"
import { useRouter } from "next/navigation"
import { getApiUrl } from "@/lib/api-config"
import { Badge } from "@/components/ui/badge"
export function SearchDialog() {
const [open, setOpen] = React.useState(false)
const [query, setQuery] = React.useState("")
const [loading, setLoading] = React.useState(false)
const [results, setResults] = React.useState<any>({
songs: [],
venues: [],
tours: [],
groups: [],
users: [],
nicknames: [],
performances: [],
shows: [] // Ensure backend sends this or we default to empty
})
const router = useRouter()
React.useEffect(() => {
const down = (e: KeyboardEvent) => {
if (e.key === "k" && (e.metaKey || e.ctrlKey)) {
e.preventDefault()
setOpen((open) => !open)
}
}
document.addEventListener("keydown", down)
return () => document.removeEventListener("keydown", down)
}, [])
React.useEffect(() => {
if (query.length < 2) {
setResults({ songs: [], venues: [], tours: [], groups: [], users: [], nicknames: [], performances: [], shows: [] })
return
}
setLoading(true)
const timer = setTimeout(() => {
fetch(`${getApiUrl()}/search/?q=${query}`)
.then(res => res.json())
.then(data => {
setResults(data)
setLoading(false)
})
.catch(err => {
console.error(err)
setLoading(false)
})
}, 300)
return () => clearTimeout(timer)
}, [query])
const handleSelect = (url: string) => {
setOpen(false)
router.push(url)
}
return (
<>
<button
onClick={() => setOpen(true)}
className="inline-flex items-center rounded-lg font-medium transition-all focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 border border-input bg-background/50 shadow-sm hover:bg-accent hover:text-accent-foreground h-9 px-4 py-2 relative w-full justify-start text-sm text-muted-foreground sm:pr-12 md:w-40 lg:w-64"
>
<Search className="mr-2 h-4 w-4 shrink-0 opacity-50" />
<span className="hidden lg:inline-flex">Search Elmeg...</span>
<span className="inline-flex lg:hidden">Search...</span>
<kbd className="pointer-events-none absolute right-1.5 top-1.5 hidden h-5 select-none items-center gap-1 rounded border bg-muted px-1.5 font-mono text-[10px] font-medium opacity-100 sm:flex">
<span className="text-xs"></span>K
</kbd>
</button>
<CommandDialog open={open} onOpenChange={setOpen}>
<CommandInput placeholder="Type to search songs, venues, tours..." value={query} onValueChange={setQuery} />
<CommandList className="max-h-[500px]">
<CommandEmpty>
{loading ? (
<div className="flex items-center justify-center py-6 text-sm text-muted-foreground">
Searching...
</div>
) : (
"No results found."
)}
</CommandEmpty>
{query.length < 2 && (
<CommandGroup heading="Quick Navigation">
<CommandItem onSelect={() => handleSelect("/archive")}>
<Library className="mr-2 h-4 w-4" />
<span>Archive</span>
</CommandItem>
<CommandItem onSelect={() => handleSelect("/leaderboards")}>
<Star className="mr-2 h-4 w-4" />
<span>Leaderboards</span>
</CommandItem>
<CommandItem onSelect={() => handleSelect("/shows")}>
<Calendar className="mr-2 h-4 w-4" />
<span>All Shows</span>
</CommandItem>
<CommandItem onSelect={() => handleSelect("/tours")}>
<Globe className="mr-2 h-4 w-4" />
<span>Tours</span>
</CommandItem>
</CommandGroup>
)}
{results.songs?.length > 0 && (
<CommandGroup heading="Songs">
{results.songs.map((song: any) => (
<CommandItem key={song.id} onSelect={() => handleSelect(`/songs/${song.id}`)}>
<Music className="mr-2 h-4 w-4" />
<div className="flex flex-col">
<span>{song.title}</span>
{song.original_artist && (
<span className="text-[10px] text-muted-foreground">{song.original_artist}</span>
)}
</div>
</CommandItem>
))}
</CommandGroup>
)}
{results.venues?.length > 0 && (
<CommandGroup heading="Venues">
{results.venues.map((venue: any) => (
<CommandItem key={venue.id} onSelect={() => handleSelect(`/venues/${venue.id}`)}>
<MapPin className="mr-2 h-4 w-4" />
<span>{venue.name}</span>
<span className="ml-2 text-xs text-muted-foreground hidden sm:inline-block">
{venue.city}, {venue.state}
</span>
</CommandItem>
))}
</CommandGroup>
)}
{results.tours?.length > 0 && (
<CommandGroup heading="Tours">
{results.tours.map((tour: any) => (
<CommandItem key={tour.id} onSelect={() => handleSelect(`/tours/${tour.id}`)}>
<Calendar className="mr-2 h-4 w-4" />
<span>{tour.name}</span>
</CommandItem>
))}
</CommandGroup>
)}
{results.performances?.length > 0 && (
<CommandGroup heading="Performances">
{results.performances.map((perf: any) => (
<CommandItem key={perf.id} onSelect={() => handleSelect(`/shows/${perf.show_id}`)}>
<Music className="mr-2 h-4 w-4" />
<div className="flex flex-col">
<span>{perf.song?.title || "Unknown Song"}</span>
<span className="text-[10px] text-muted-foreground">
{perf.notes}
{/* We rely on frontend resolving show date if available, or just link to show */}
</span>
</div>
<Badge variant="outline" className="ml-auto text-[10px] h-5">Performance</Badge>
</CommandItem>
))}
</CommandGroup>
)}
{results.nicknames?.length > 0 && (
<CommandGroup heading="Heady Nicknames">
{results.nicknames.map((nickname: any) => (
<CommandItem key={nickname.id} onSelect={() => handleSelect(`/shows/${nickname.performance?.show_id}`)}>
<Star className="mr-2 h-4 w-4 text-yellow-500" />
<span>{nickname.nickname}</span>
<span className="ml-2 text-xs text-muted-foreground">
({nickname.performance?.song?.title})
</span>
</CommandItem>
))}
</CommandGroup>
)}
{results.users?.length > 0 && (
<CommandGroup heading="Users">
{results.users.map((user: any) => (
<CommandItem key={user.id} onSelect={() => handleSelect(`/profile/${user.id}`)}>
<Users className="mr-2 h-4 w-4" />
<span>{user.username || user.email.split('@')[0]}</span>
</CommandItem>
))}
</CommandGroup>
)}
</CommandList>
</CommandDialog>
</>
)
}