147 lines
5.5 KiB
TypeScript
147 lines
5.5 KiB
TypeScript
import { useState, useEffect, useMemo, ComponentPropsWithoutRef } from "react";
|
|
|
|
import Card from "./Card"
|
|
import { Input } from "./FormElements"
|
|
import { Song, SongBookDetailed } from "@/lib/db/response";
|
|
|
|
interface SongsProps extends Omit<ComponentPropsWithoutRef<typeof Card>, "title" | "children"> {
|
|
onSelectSong?: (song: Song, book: { id: number; title: string }, songNumber: string) => void;
|
|
actionName?: string;
|
|
songBooks?: SongBookDetailed[];
|
|
}
|
|
|
|
function Songs({ actionName, onSelectSong, songBooks, className = "", ...props }: SongsProps) {
|
|
const [query, setQuery] = useState("");
|
|
const [fetchedBooks, setFetchedBooks] = useState<SongBookDetailed[]>([]);
|
|
|
|
useEffect(() => {
|
|
if (songBooks) return;
|
|
async function load() {
|
|
try {
|
|
const res = await fetch("/api/songbooks");
|
|
if (!res.ok) return;
|
|
const list: { id: number }[] = await res.json();
|
|
|
|
const details = await Promise.all(
|
|
list.map(async (b) => {
|
|
const r = await fetch(`/api/songbooks/${b.id}`);
|
|
return r.ok ? await r.json() : null;
|
|
})
|
|
);
|
|
setFetchedBooks(details.filter(Boolean));
|
|
} catch (error) {
|
|
console.error("Loading songbooks failed: ", error);
|
|
}
|
|
}
|
|
load();
|
|
}, [songBooks]);
|
|
|
|
const books = songBooks ?? fetchedBooks;
|
|
|
|
// Pro Buch filtern; Bücher ohne Treffer werden ausgeblendet
|
|
const groups = useMemo(() => {
|
|
const q = query.trim().toLowerCase();
|
|
return books
|
|
.map((book) => {
|
|
const bookMatches = q !== "" && book.title.toLowerCase().includes(q);
|
|
const songs = (book.songs ?? []).filter(
|
|
(e) =>
|
|
!q ||
|
|
bookMatches ||
|
|
e.song.title.toLowerCase().includes(q) ||
|
|
String(e.songNumber).toLowerCase().includes(q)
|
|
);
|
|
return { book, songs };
|
|
})
|
|
.filter((g) => g.songs.length > 0);
|
|
}, [books, query]);
|
|
|
|
const th = "p-3 text-xs font-semibold text-neutral-600 dark:text-neutral-400 uppercase tracking-wider";
|
|
|
|
return (
|
|
<Card
|
|
{...props}
|
|
title="Alle Lieder"
|
|
className={`h-[500px] ${className}`}
|
|
>
|
|
<div className="mb-4">
|
|
<Input.Text
|
|
type="text"
|
|
placeholder="Nach Songtitel, Nummer oder Liederbuch suchen..."
|
|
value={query}
|
|
onChange={(e) => setQuery(e.target.value)}
|
|
/>
|
|
</div>
|
|
<div className="flex-1 min-h-0 overflow-auto border border-neutral-200 dark:border-neutral-800 rounded-lg bg-white dark:bg-black">
|
|
<table className="w-full text-left border-collapse text-sm">
|
|
<thead className="bg-neutral-100 dark:bg-neutral-900 sticky top-0 border-b border-neutral-200 dark:border-neutral-800 z-10">
|
|
<tr>
|
|
<th className={`${th} w-24 text-center`}>Nummer</th>
|
|
<th className={th}>Titel</th>
|
|
<th className={`${th} text-right`}>Aktion</th>
|
|
</tr>
|
|
</thead>
|
|
{groups.length === 0 ? (
|
|
<tbody>
|
|
<tr>
|
|
<td colSpan={3} className="p-8 text-center text-neutral-400 dark:text-neutral-500">
|
|
Keine Lieder gefunden
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
) : (
|
|
groups.map(({ book, songs }) => (
|
|
<tbody key={book.id} className="divide-y divide-neutral-200 dark:divide-neutral-800">
|
|
<tr className="bg-neutral-50 dark:bg-neutral-950">
|
|
<th
|
|
colSpan={3}
|
|
className="p-3 text-sm font-bold text-neutral-900 dark:text-white border-y border-neutral-200 dark:border-neutral-800"
|
|
>
|
|
{book.title}
|
|
{book.language && (
|
|
<span className="ml-2 text-xs font-normal text-neutral-500 dark:text-neutral-400">
|
|
({book.language})
|
|
</span>
|
|
)}
|
|
</th>
|
|
</tr>
|
|
{songs.map((entry) => (
|
|
<tr
|
|
key={`${book.id}-${entry.song.id}`}
|
|
className="hover:bg-neutral-100/60 dark:hover:bg-neutral-900/40 transition"
|
|
>
|
|
<td className="p-3 text-center font-mono text-blue-600 dark:text-blue-400 font-bold">
|
|
{entry.songNumber}
|
|
</td>
|
|
<td className="p-3 font-medium text-neutral-900 dark:text-white">
|
|
{entry.song.title}
|
|
</td>
|
|
<td className="p-3 text-right">
|
|
<Input.Button
|
|
onClick={() =>
|
|
onSelectSong?.(
|
|
entry.song,
|
|
{ id: book.id, title: book.title },
|
|
String(entry.songNumber)
|
|
)
|
|
}
|
|
className="bg-white dark:bg-black hover:bg-blue-600 dark:hover:bg-blue-600 border border-neutral-300 dark:border-neutral-700 hover:border-blue-600 dark:hover:border-blue-500 hover:text-white font-medium rounded-md px-3 py-1.5 transition text-xs"
|
|
>
|
|
{actionName}
|
|
</Input.Button>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
))
|
|
)}
|
|
</table>
|
|
</div>
|
|
</Card>
|
|
)
|
|
}
|
|
|
|
export const Screens = {
|
|
Songs: Songs,
|
|
}
|