import { useState } from 'react'; import * as api from '../../plex/api'; import type { Item } from '../../plex/types'; import { useServer } from '../../app/session'; import { invalidateLibrary, useServerQuery } from '../../app/queries'; import { Dialog, toast, toastError } from '../../ui/overlays'; import { Button, PageLoader } from '../../ui/primitives'; import { Icon } from '../../ui/Icon'; export function CollectionDialog({ item, onClose }: { item: Item; onClose: () => void }) { const server = useServer(); const collections = useServerQuery(['collections', item.librarySectionId], (s) => api.collections(s, item.librarySectionId), { enabled: !!item.librarySectionId }); const [name, setName] = useState(''); const [busy, setBusy] = useState(false); const current = new Set(item.collections.map((c) => c.tag)); const add = async (title: string) => { setBusy(true); try { await api.addToCollection(server, item, title); toast(`Added to “${title}”`, 'success'); invalidateLibrary(); onClose(); } catch (e) { toastError(e); } finally { setBusy(false); } }; return (
{ e.preventDefault(); if (name.trim()) add(name.trim()); }} > setName(e.target.value)} placeholder="New collection name" />
{collections.isLoading && }
{collections.data?.map((c) => ( ))}
); } export function PlaylistDialog({ item, onClose }: { item: Item; onClose: () => void }) { const server = useServer(); const playlists = useServerQuery(['playlists'], (s) => api.playlists(s)); const [name, setName] = useState(''); const [busy, setBusy] = useState(false); const run = async (fn: () => Promise, done: string) => { setBusy(true); try { await fn(); toast(done, 'success'); onClose(); } catch (e) { toastError(e); } finally { setBusy(false); } }; return (
{ e.preventDefault(); if (name.trim()) run(() => api.createPlaylist(server, name.trim(), server.machineId, item.id), `Created “${name.trim()}”`); }} > setName(e.target.value)} placeholder="New playlist name" />
{playlists.isLoading && }
{playlists.data?.map((p) => ( ))} {playlists.data && !playlists.data.length &&

No video playlists yet. Create one above.

}
); }