A Playlists page lists the user's playlists and makes a new one. A playlist has its own page: play it (songs that are not available are skipped), rename it, delete it after a confirmation, move a song up or down and take one out. Every song of a list gets an Add to playlist button that opens a window to choose a playlist or make a new one, and says what it did. TrackRow keeps the song as one button and takes extra buttons at its end, so the add button is not inside the button that plays. A long playlist is read whole, page after page. English and Italian texts, error texts for the new codes. Closes #92
110 lines
4.1 KiB
TypeScript
110 lines
4.1 KiB
TypeScript
import { ListPlus } from 'lucide-react'
|
|
import { useState, type FormEvent } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import type { Track } from '@/api/catalog'
|
|
import { ApiError } from '@/api/client'
|
|
import { useAddTracks, useCreatePlaylist, usePlaylists } from '@/api/playlists'
|
|
import LoadMore from '@/components/LoadMore'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog'
|
|
import { Input } from '@/components/ui/input'
|
|
import { Label } from '@/components/ui/label'
|
|
import { errorMessageKey } from '@/i18n'
|
|
|
|
/** The button at the end of a song: opens a window to put the song in a playlist. */
|
|
export default function AddToPlaylist({ track }: { track: Track }) {
|
|
const { t } = useTranslation()
|
|
const [open, setOpen] = useState(false)
|
|
return (
|
|
<>
|
|
<Button variant="ghost" size="icon" className="mr-2 size-10" aria-label={t('playlists.add')} onClick={() => setOpen(true)}>
|
|
<ListPlus />
|
|
</Button>
|
|
{open && <AddDialog track={track} onClose={() => setOpen(false)} />}
|
|
</>
|
|
)
|
|
}
|
|
|
|
function AddDialog({ track, onClose }: { track: Track; onClose: () => void }) {
|
|
const { t } = useTranslation()
|
|
const playlists = usePlaylists()
|
|
const add = useAddTracks()
|
|
const create = useCreatePlaylist()
|
|
const [name, setName] = useState('')
|
|
const [done, setDone] = useState<string | null>(null)
|
|
const items = playlists.data?.items ?? []
|
|
const busy = add.isPending || create.isPending
|
|
const error = add.error ?? create.error
|
|
|
|
function addTo(id: number, playlistName: string) {
|
|
setDone(null)
|
|
add.mutate({ id, track_ids: [track.id] }, { onSuccess: () => setDone(playlistName) })
|
|
}
|
|
|
|
async function createAndAdd(event: FormEvent) {
|
|
event.preventDefault()
|
|
const clean = name.trim()
|
|
if (clean === '' || busy) return
|
|
setDone(null)
|
|
try {
|
|
const made = await create.mutateAsync({ name: clean })
|
|
await add.mutateAsync({ id: made.id, track_ids: [track.id] })
|
|
setDone(made.name)
|
|
setName('')
|
|
} catch {
|
|
// The mutations keep the error, and it is shown below.
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Dialog open onOpenChange={(isOpen) => !isOpen && onClose()}>
|
|
<DialogContent>
|
|
<DialogHeader>
|
|
<DialogTitle>{t('playlists.addTitle', { title: track.title })}</DialogTitle>
|
|
<DialogDescription>{t('playlists.addBody')}</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
{playlists.isPending ? (
|
|
<p role="status" className="text-muted-foreground">
|
|
{t('common.loading')}
|
|
</p>
|
|
) : items.length === 0 ? (
|
|
<p className="text-muted-foreground">{t('playlists.noneYet')}</p>
|
|
) : (
|
|
<ul className="flex max-h-64 flex-col gap-1 overflow-y-auto">
|
|
{items.map((p) => (
|
|
<li key={p.id}>
|
|
<Button variant="ghost" className="h-10 w-full justify-between" disabled={busy} onClick={() => addTo(p.id, p.name)}>
|
|
<span className="truncate">{p.name}</span>
|
|
<span className="text-muted-foreground">{t('playlists.size', { count: p.track_count })}</span>
|
|
</Button>
|
|
</li>
|
|
))}
|
|
</ul>
|
|
)}
|
|
<LoadMore query={playlists} />
|
|
|
|
<form onSubmit={createAndAdd} className="flex flex-col gap-2">
|
|
<Label htmlFor="add-new-playlist">{t('playlists.newName')}</Label>
|
|
<div className="flex gap-2">
|
|
<Input id="add-new-playlist" value={name} maxLength={200} autoComplete="off" onChange={(e) => setName(e.target.value)} />
|
|
<Button type="submit" disabled={name.trim() === '' || busy}>
|
|
{t('playlists.createAndAdd')}
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
|
|
{done && (
|
|
<p role="status" className="text-sm font-semibold">
|
|
{t('playlists.added', { name: done })}
|
|
</p>
|
|
)}
|
|
{error && (
|
|
<p role="alert" className="text-sm text-destructive">
|
|
{t(errorMessageKey(error instanceof ApiError ? error.code : 'unknown'))}
|
|
</p>
|
|
)}
|
|
</DialogContent>
|
|
</Dialog>
|
|
)
|
|
}
|