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

100 lines
2.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { useEffect, useRef, useState } from 'react';
import type { PlayerExtraControl } from '../lib/embedProtocols';
import { t } from '../i18n';
import { GearIcon } from './icons';
export function PlayerSettingsMenu(props: {
extras: PlayerExtraControl[];
rate: number;
rateOptions?: number[];
canControl: boolean;
onExtra: (id: string, value: string) => void;
onRate: (rate: number) => void;
}) {
const [open, setOpen] = useState(false);
const rootRef = useRef<HTMLDivElement>(null);
const local = props.extras.filter((control) => control.scope === 'local');
const room = props.extras.filter(
(control) => control.scope === 'synced' && control.id !== 'speed'
);
const hasRoom = room.length > 0 || !!props.rateOptions?.length;
useEffect(() => {
if (!open) return;
const close = (event: MouseEvent) => {
if (!rootRef.current?.contains(event.target as Node)) setOpen(false);
};
document.addEventListener('mousedown', close);
return () => document.removeEventListener('mousedown', close);
}, [open]);
if (local.length === 0 && !hasRoom) return null;
const renderControl = (control: PlayerExtraControl) => (
<label className="player-setting" key={control.id}>
<span>{t(control.label)}</span>
<select
value={control.current ?? ''}
disabled={control.scope === 'synced' && !props.canControl}
title={
control.scope === 'synced' && !props.canControl
? t('Only the host can change this')
: undefined
}
onChange={(event) => props.onExtra(control.id, event.target.value)}
>
{control.current == null && <option value="">{t('Choose')}</option>}
{control.options.map((option) => (
<option key={option.value} value={option.value}>
{t(option.label)}
</option>
))}
</select>
</label>
);
return (
<div className="player-settings" ref={rootRef}>
<button
className="ctrl"
title={t('Settings')}
aria-expanded={open}
onClick={() => setOpen((value) => !value)}
>
<GearIcon />
</button>
{open && (
<div className="player-settings-popover">
{local.length > 0 && (
<section>
<div className="player-settings-heading">{t('Local')}</div>
{local.map(renderControl)}
</section>
)}
{hasRoom && (
<section>
<div className="player-settings-heading">{t('Room')}</div>
{props.rateOptions && (
<label className="player-setting">
<span>{t('Speed')}</span>
<select
value={String(props.rate)}
disabled={!props.canControl}
onChange={(event) =>
props.onRate(Number(event.target.value))
}
>
{props.rateOptions.map((rate) => (
<option key={rate} value={rate}>{rate}×</option>
))}
</select>
</label>
)}
{room.map(renderControl)}
</section>
)}
</div>
)}
</div>
);
}