- Replace mobile placeholder with Expo Router app (tabs, playback, auth, library) - Move Convex backend to @otunes/convex for web and mobile reuse - Update web app imports and API routes to use shared package
241 lines
6.9 KiB
TypeScript
241 lines
6.9 KiB
TypeScript
"use client"
|
|
|
|
import { Suspense, useCallback, useEffect, useRef, useState } from "react"
|
|
import { useQuery } from "convex/react"
|
|
import { SearchIcon } from "lucide-react"
|
|
import { usePathname, useSearchParams } from "next/navigation"
|
|
|
|
import { ContentCarousel } from "@/components/content-carousel"
|
|
import { LibraryLoading } from "@/components/library-loading"
|
|
import { SearchSongItem } from "@/components/search-song-item"
|
|
import { SearchTopResultCard, type TopResult } from "@/components/search-top-result-card"
|
|
import { api } from "@otunes/convex/_generated/api"
|
|
|
|
const SEARCH_DEBOUNCE_MS = 300
|
|
|
|
function buildSearchHref(pathname: string, query: string) {
|
|
const trimmed = query.trim()
|
|
if (!trimmed) {
|
|
return pathname
|
|
}
|
|
const params = new URLSearchParams()
|
|
params.set("q", trimmed)
|
|
return `${pathname}?${params.toString()}`
|
|
}
|
|
|
|
function readUrlQuery() {
|
|
return new URLSearchParams(window.location.search).get("q") ?? ""
|
|
}
|
|
|
|
function SearchResults({ query }: { query: string }) {
|
|
const hasQuery = query.length > 0
|
|
|
|
const results = useQuery(
|
|
api.library.searchLibrary,
|
|
hasQuery ? { query } : "skip"
|
|
)
|
|
const playlistResults = useQuery(
|
|
api.playlists.searchPlaylists,
|
|
hasQuery ? { query } : "skip"
|
|
)
|
|
|
|
const showPlaylists =
|
|
hasQuery && playlistResults !== undefined && playlistResults.length > 0
|
|
const isSearching = hasQuery && results === undefined
|
|
const hasLibraryResults =
|
|
results &&
|
|
(results.topResults.length > 0 ||
|
|
results.artists.length > 0 ||
|
|
results.albums.length > 0 ||
|
|
results.songs.length > 0)
|
|
const showResults = hasQuery && results !== undefined
|
|
|
|
if (isSearching) {
|
|
return <LibraryLoading />
|
|
}
|
|
|
|
if (!hasQuery) {
|
|
return (
|
|
<div className="flex flex-1 items-center justify-center px-6 pb-28">
|
|
<p className="text-center text-muted-foreground">
|
|
Search for songs, albums, artists, and playlists in your library.
|
|
</p>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
if (!showResults) {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<div className="flex-1 overflow-y-auto p-6 pb-28">
|
|
<div className="mx-auto flex max-w-6xl flex-col gap-10">
|
|
{!hasLibraryResults && (
|
|
<p className="text-muted-foreground">
|
|
No library results for “{query}”
|
|
</p>
|
|
)}
|
|
|
|
{results.topResults.length > 0 && (
|
|
<section className="flex flex-col gap-4">
|
|
<h2 className="text-xl font-bold tracking-tight">Top Results</h2>
|
|
<div className="grid gap-2 sm:grid-cols-2 xl:grid-cols-3">
|
|
{results.topResults.map((result) => (
|
|
<SearchTopResultCard
|
|
key={`${result.kind}-${result.item.id}`}
|
|
result={result as TopResult}
|
|
/>
|
|
))}
|
|
</div>
|
|
</section>
|
|
)}
|
|
|
|
{results.artists.length > 0 && (
|
|
<ContentCarousel title="Artists" content={results.artists} />
|
|
)}
|
|
|
|
{results.albums.length > 0 && (
|
|
<ContentCarousel title="Albums" content={results.albums} />
|
|
)}
|
|
|
|
{results.songs.length > 0 && (
|
|
<section className="flex flex-col gap-4">
|
|
<h2 className="text-xl font-bold tracking-tight">Songs</h2>
|
|
<div className="grid gap-x-6 gap-y-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
|
|
{results.songs.map((song) => (
|
|
<SearchSongItem key={song.id} song={song} />
|
|
))}
|
|
</div>
|
|
</section>
|
|
)}
|
|
|
|
{showPlaylists && (
|
|
<ContentCarousel
|
|
title="Playlists"
|
|
content={playlistResults ?? []}
|
|
/>
|
|
)}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
function SearchPageContent() {
|
|
const pathname = usePathname()
|
|
const searchParams = useSearchParams()
|
|
const initialQuery = searchParams.get("q") ?? ""
|
|
const urlQueryRef = useRef(initialQuery)
|
|
|
|
const [query, setQuery] = useState(initialQuery)
|
|
const [searchQuery, setSearchQuery] = useState(initialQuery.trim())
|
|
const debounceRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
|
|
|
useEffect(() => {
|
|
return () => {
|
|
if (debounceRef.current) {
|
|
clearTimeout(debounceRef.current)
|
|
}
|
|
}
|
|
}, [])
|
|
|
|
const syncUrl = useCallback(
|
|
(nextQuery: string, mode: "replace" | "push") => {
|
|
const trimmed = nextQuery.trim()
|
|
if (trimmed === urlQueryRef.current.trim()) {
|
|
return
|
|
}
|
|
|
|
const href = buildSearchHref(pathname, trimmed)
|
|
if (mode === "push") {
|
|
window.history.pushState(null, "", href)
|
|
} else {
|
|
window.history.replaceState(null, "", href)
|
|
}
|
|
urlQueryRef.current = trimmed
|
|
},
|
|
[pathname]
|
|
)
|
|
|
|
const commitSearch = useCallback(
|
|
(nextQuery: string, urlMode: "replace" | "push" | "skip") => {
|
|
const trimmed = nextQuery.trim()
|
|
setSearchQuery(trimmed)
|
|
if (urlMode !== "skip") {
|
|
syncUrl(nextQuery, urlMode)
|
|
}
|
|
},
|
|
[syncUrl]
|
|
)
|
|
|
|
useEffect(() => {
|
|
function onPopState() {
|
|
const nextQuery = readUrlQuery()
|
|
urlQueryRef.current = nextQuery
|
|
setQuery(nextQuery)
|
|
setSearchQuery(nextQuery.trim())
|
|
}
|
|
|
|
window.addEventListener("popstate", onPopState)
|
|
return () => window.removeEventListener("popstate", onPopState)
|
|
}, [])
|
|
|
|
const handleQueryChange = useCallback(
|
|
(nextQuery: string) => {
|
|
setQuery(nextQuery)
|
|
|
|
if (debounceRef.current) {
|
|
clearTimeout(debounceRef.current)
|
|
}
|
|
|
|
debounceRef.current = setTimeout(() => {
|
|
commitSearch(nextQuery, "replace")
|
|
}, SEARCH_DEBOUNCE_MS)
|
|
},
|
|
[commitSearch]
|
|
)
|
|
|
|
const handleKeyDown = useCallback(
|
|
(event: React.KeyboardEvent<HTMLInputElement>) => {
|
|
if (event.key !== "Enter") {
|
|
return
|
|
}
|
|
|
|
if (debounceRef.current) {
|
|
clearTimeout(debounceRef.current)
|
|
}
|
|
commitSearch(query, "push")
|
|
},
|
|
[query, commitSearch]
|
|
)
|
|
|
|
return (
|
|
<div className="flex h-svh flex-col">
|
|
<header className="flex items-center justify-center border-b px-4 py-3">
|
|
<div className="relative w-full max-w-xl">
|
|
<SearchIcon className="pointer-events-none absolute top-1/2 left-3 size-4 -translate-y-1/2 text-muted-foreground" />
|
|
<input
|
|
type="search"
|
|
data-slot="input"
|
|
value={query}
|
|
onChange={(event) => handleQueryChange(event.target.value)}
|
|
onKeyDown={handleKeyDown}
|
|
placeholder="Search"
|
|
className="h-10 w-full min-w-0 rounded-full border border-input bg-input/30 pr-3 pl-9 text-sm text-foreground transition-colors outline-none placeholder:text-muted-foreground focus-visible:border-ring focus-visible:ring-2 focus-visible:ring-ring/50 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50"
|
|
/>
|
|
</div>
|
|
</header>
|
|
|
|
<SearchResults query={searchQuery} />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default function Page() {
|
|
return (
|
|
<Suspense fallback={<LibraryLoading />}>
|
|
<SearchPageContent />
|
|
</Suspense>
|
|
)
|
|
}
|