import { useState } from 'react'; import { rpc } from '../lib/rpc'; import { send, watchStore } from '../lib/watch'; import { toast } from '../lib/app'; import { useStore } from '../lib/store'; import { thumbFor } from '../lib/format'; import type { Track } from '../lib/types'; import { t, tf } from '../i18n'; // Normalize a Twitch channel entry: accept a bare login name, an @handle, or a // full twitch.tv URL, and reduce it to the channel login the embed player // expects. Numeric ids pass through (best effort — resolving them to a login // would need the Helix API, which isn't wired up). function normalizeTwitchChannel(input: string): string { let s = input.trim(); const m = s.match(/twitch\.tv\/([^/?#]+)/i); if (m) s = m[1]; s = s.replace(/^@/, ''); return s.toLowerCase().replace(/[^a-z0-9_]/g, ''); } // Add tracks to a watch room from inside it. YouTube uses the unauthenticated // SearchTracks RPC (results added by title + thumbnail); Twitch has no search // backend, so it offers an add-by-channel-name/id field instead. export function WatchSearch({ disabled }: { disabled: boolean }) { const w = useStore(watchStore); const [query, setQuery] = useState(''); const [channel, setChannel] = useState(''); const [results, setResults] = useState([]); const [open, setOpen] = useState(false); if (w.sourceChoice === 'twitch') { return (
{ e.preventDefault(); const ch = normalizeTwitchChannel(channel); if (!ch) return; send({ type: 'watch.queue.add', source: 'twitch', url: ch, title: ch, }); toast(tf('Added “{0}” to queue', ch)); setChannel(''); }} > setChannel(e.target.value)} placeholder={t( 'Twitch channel name or id to queue…' )} autoComplete="off" disabled={disabled} />
); } return (
{ e.preventDefault(); const q = query.trim(); if (!q) return; try { const res = await rpc.call<{ tracks?: Track[] }>( 'SearchTracks', { query: q } ); setResults((res && res.tracks) || []); setOpen(true); } catch (err) { toast((err as { message?: string }).message || 'error'); } }} > setQuery(e.target.value)} placeholder={t('Search YouTube to queue…')} autoComplete="off" disabled={disabled} />
{open && results.length > 0 && (
{results.slice(0, 8).map((tr, i) => ( ))}
)}
); }