Files
Echoion/EchoionWeb/frontend/src/components/Sidebar.tsx
T
faumaray bdd69f0aad update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-25 23:44:48 +03:00

200 lines
5.0 KiB
TypeScript

import { useState, type ReactNode } from 'react';
import {
appStore,
changeLanguage,
connectWithToken,
getStoredToken,
logout,
requestToken,
showView,
type ViewName,
} from '../lib/app';
import { useStore } from '../lib/store';
import { promptDialog } from '../lib/dialog';
import { Avatar } from './Avatar';
import {
CodeIcon,
GearIcon,
HeartIcon,
PlayIcon,
QueueIcon,
SearchIcon,
StatsIcon,
UpdateIcon,
WatchIcon,
} from './icons';
import { t, useLanguage } from '../i18n';
const NAV: Array<{ view: ViewName; icon: ReactNode; label: string }> = [
{ view: 'dashboard', icon: <PlayIcon />, label: 'Dashboard' },
{ view: 'queue', icon: <QueueIcon />, label: 'Queue' },
{ view: 'search', icon: <SearchIcon />, label: 'Search' },
{ view: 'library', icon: <HeartIcon />, label: 'Library' },
{ view: 'stats', icon: <StatsIcon />, label: 'History & Stats' },
{ view: 'server', icon: <GearIcon />, label: 'Server & Admin' },
{ view: 'updates', icon: <UpdateIcon />, label: 'Updates' },
{ view: 'rpc', icon: <CodeIcon />, label: 'RPC / API' },
];
export function Sidebar() {
const s = useStore(appStore);
const language = useLanguage();
const [token, setToken] = useState(getStoredToken() || '');
const isAdmin = !!(s.identity && s.identity.isAdmin);
const playing = !!(
s.status &&
s.status.isPlaying &&
!s.status.isPaused
);
const name = s.identity
? s.identity.nickname || s.identity.uid || t('user')
: '';
return (
<aside className={'sidebar' + (s.sidebarOpen ? ' open' : '')}>
<div className="brand">
<div className="brand-icon">
<svg viewBox="0 0 24 24" width="22" height="22" fill="#fff">
<rect x="4" y="9" width="3.4" height="10" rx="1.6" />
<rect x="10.3" y="4" width="3.4" height="15" rx="1.6" />
<rect x="16.6" y="12" width="3.4" height="7" rx="1.6" />
</svg>
</div>
<div>
<div className="brand-name">Echoion</div>
<div className="brand-sub">{t('MUSIC BOT · TS3')}</div>
</div>
</div>
<nav className="nav">
{NAV.filter(
(item) =>
(item.view !== 'server' && item.view !== 'updates') ||
isAdmin
).map((item) => (
<button
key={item.view}
className={
'nav-item' +
(s.view === item.view ? ' active' : '')
}
aria-current={s.view === item.view ? 'page' : undefined}
onClick={() => showView(item.view)}
>
<span className="nav-ico">{item.icon}</span>
{t(item.label)}
{item.view === 'dashboard' && playing && (
<span className="live-dot" />
)}
{item.view === 'queue' && s.queue.length > 0 && (
<span className="nav-badge">{s.queue.length}</span>
)}
</button>
))}
{s.watchUrl && (
<a
className="nav-item"
href={s.watchUrl}
target="_blank"
rel="noreferrer"
>
<span className="nav-ico">
<WatchIcon />
</span>
{t('Watch Together')}
<span className="nav-external" aria-hidden="true">
↗
</span>
</a>
)}
</nav>
<div className="sidebar-foot">
<div className="conn-row">
<span
className={
'conn-dot' +
(s.connection === 'connected'
? ' on'
: s.connection === 'error'
? ' err'
: '')
}
/>
<span>{t(s.connection)}</span>
<select
className="lang-select"
value={language}
title={t('Language')}
onChange={(e) =>
changeLanguage(e.target.value as 'en' | 'ru')
}
>
<option value="en">EN</option>
<option value="ru">RU</option>
</select>
</div>
{!s.identity ? (
<div>
<input
type="password"
placeholder={t('RPC token')}
autoComplete="off"
value={token}
onChange={(e) => setToken(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter') {
connectWithToken(token);
}
}}
/>
<button
className="btn btn-accent"
onClick={() => connectWithToken(token)}
>
{t('Connect')}
</button>
<button
className="btn btn-ghost"
title={t('Send my token via TeamSpeak PM')}
onClick={async () => {
const nickname = await promptDialog({
title: t('Get token by nickname'),
message: t(
'Your TeamSpeak nickname (must be subscribed):'
),
placeholder: t('Nickname'),
confirmLabel: t('Send'),
});
if (nickname) requestToken(nickname);
}}
>
{t('Get token by nickname…')}
</button>
</div>
) : (
<div className="whoami">
<Avatar name={name} uid={s.identity.uid} />
<span>{name}</span>
{isAdmin && (
<span className="admin-chip">{t('ADMIN')}</span>
)}
{!isAdmin && s.identity.isDj && (
<span className="admin-chip">{t('DJ')}</span>
)}
<button
className="btn btn-ghost"
onClick={() => {
logout();
setToken('');
}}
>
{t('Logout')}
</button>
</div>
)}
</div>
</aside>
);
}