Files
EchoionWeb/frontend/src/components/WatchSearch.tsx
T
faumaray 9574aa8a23 making WEB
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-11 00:47:52 +03:00

146 lines
3.8 KiB
TypeScript

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<Track[]>([]);
const [open, setOpen] = useState(false);
if (w.sourceChoice === 'twitch') {
return (
<div className="watch-search">
<form
className="search-row"
onSubmit={(e) => {
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('');
}}
>
<input
value={channel}
onChange={(e) => setChannel(e.target.value)}
placeholder={t(
'Twitch channel name or id to queue…'
)}
autoComplete="off"
disabled={disabled}
/>
<button
className="btn btn-accent"
type="submit"
disabled={disabled}
>
{t('Add channel')}
</button>
</form>
</div>
);
}
return (
<div className="watch-search">
<form
className="search-row"
onSubmit={async (e) => {
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');
}
}}
>
<input
value={query}
onChange={(e) => setQuery(e.target.value)}
placeholder={t('Search YouTube to queue…')}
autoComplete="off"
disabled={disabled}
/>
<button className="btn btn-accent" type="submit" disabled={disabled}>
{t('Search')}
</button>
</form>
{open && results.length > 0 && (
<div className="watch-search-results">
{results.slice(0, 8).map((tr, i) => (
<button
key={tr.url || tr.id || i}
className="watch-search-item"
onClick={() => {
send({
type: 'watch.queue.add',
source: 'youtube',
url: tr.url || tr.id,
title: tr.title || '',
});
toast(
tf(
'Added “{0}” to queue',
tr.title || ''
)
);
setOpen(false);
setQuery('');
}}
>
<div
className="track-cover"
style={
thumbFor(tr)
? {
background: `url("${thumbFor(tr)}") center/cover`,
}
: undefined
}
/>
<span className="watch-search-title">
{tr.title}
</span>
</button>
))}
</div>
)}
</div>
);
}