117 lines
3.9 KiB
TypeScript
117 lines
3.9 KiB
TypeScript
import { useCallback, useRef, useState } from "react";
|
|
import { useSearchParams, useRevalidator } from "react-router";
|
|
import type { Route } from "./+types/home";
|
|
import { Button } from "~/components/ui/button";
|
|
import { Input } from "~/components/ui/input";
|
|
import { Card, CardContent } from "~/components/ui/card";
|
|
import { Plus } from "lucide-react";
|
|
import { SongCard } from "~/components/song-card";
|
|
import { AddSongSheet } from "~/components/add-song-sheet";
|
|
import { listSongs } from "~/lib/api";
|
|
import type { SongSummary } from "~/lib/types";
|
|
|
|
export function meta({}: Route.MetaArgs) {
|
|
return [
|
|
{ title: "PocketChords" },
|
|
{ name: "description", content: "Your personal chord chart library" },
|
|
];
|
|
}
|
|
|
|
export async function loader({ request }: Route.LoaderArgs) {
|
|
const q = new URL(request.url).searchParams.get("q") ?? "";
|
|
try {
|
|
const songs = await listSongs(q);
|
|
return { songs, q, error: false };
|
|
} catch {
|
|
return { songs: [], q, error: true };
|
|
}
|
|
}
|
|
|
|
export default function Home({ loaderData }: Route.ComponentProps) {
|
|
const { songs, q: initialQ, error } = loaderData;
|
|
const [searchParams, setSearchParams] = useSearchParams();
|
|
const [sheetOpen, setSheetOpen] = useState(false);
|
|
const [localSongs, setLocalSongs] = useState<SongSummary[]>([]);
|
|
const revalidator = useRevalidator();
|
|
|
|
const [inputValue, setInputValue] = useState(initialQ);
|
|
const debounceRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
|
|
const handleSearch = useCallback((value: string) => {
|
|
setInputValue(value);
|
|
if (debounceRef.current) clearTimeout(debounceRef.current);
|
|
debounceRef.current = setTimeout(() => {
|
|
setSearchParams(value.trim() ? { q: value.trim() } : {}, { replace: true });
|
|
}, 300);
|
|
}, [setSearchParams]);
|
|
|
|
const allSongs = [...songs, ...localSongs];
|
|
|
|
return (
|
|
<div className="flex flex-col h-full max-w-lg mx-auto">
|
|
{/* Header */}
|
|
<div className="flex items-center justify-between px-4 pt-4 pb-2">
|
|
<h1 className="text-lg font-bold">PocketChords</h1>
|
|
<Button size="sm" onClick={() => setSheetOpen(true)}>
|
|
<Plus className="w-4 h-4 mr-1" />
|
|
Add
|
|
</Button>
|
|
</div>
|
|
|
|
{/* Search */}
|
|
<div className="px-4 pb-3">
|
|
<Input
|
|
placeholder="Search songs..."
|
|
value={inputValue}
|
|
onChange={(e) => handleSearch(e.target.value)}
|
|
className="w-full"
|
|
/>
|
|
</div>
|
|
|
|
{/* Error state */}
|
|
{error && (
|
|
<div className="flex flex-col items-center gap-3 pt-8 pb-4 px-6 text-center">
|
|
<p className="text-sm text-muted-foreground">
|
|
Couldn't load your songs. Is the API running?
|
|
</p>
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => revalidator.revalidate()}
|
|
>
|
|
Retry
|
|
</Button>
|
|
</div>
|
|
)}
|
|
|
|
{/* Grid */}
|
|
<div className="flex-1 overflow-y-auto px-4 pb-4">
|
|
{!error && allSongs.length === 0 && (
|
|
<p className="text-sm text-muted-foreground text-center pt-8 pb-4">
|
|
{initialQ ? "No songs match your search." : "No songs yet. Tap Add to get started."}
|
|
</p>
|
|
)}
|
|
<div className="grid grid-cols-2 gap-3">
|
|
{allSongs.map((song) => (
|
|
<SongCard key={song.id} song={song} />
|
|
))}
|
|
<Card
|
|
className="h-full border-dashed cursor-pointer hover:bg-accent transition-colors"
|
|
onClick={() => setSheetOpen(true)}
|
|
>
|
|
<CardContent className="p-3 flex items-center justify-center h-full min-h-[80px]">
|
|
<Plus className="w-6 h-6 text-muted-foreground" />
|
|
</CardContent>
|
|
</Card>
|
|
</div>
|
|
</div>
|
|
|
|
<AddSongSheet
|
|
open={sheetOpen}
|
|
onOpenChange={setSheetOpen}
|
|
onSongAdded={(summary) => setLocalSongs((prev) => [...prev, summary])}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|