100 lines
2.8 KiB
TypeScript
100 lines
2.8 KiB
TypeScript
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>
|
||
);
|
||
}
|