lbry-desktop/ui/component/settingDefaultQuality/view.jsx

72 lines
1.9 KiB
React
Raw Normal View History

// @flow
import React from 'react';
import { FormField } from 'component/common/form';
2022-04-20 20:40:21 +02:00
import { VIDEO_QUALITY_OPTIONS } from 'constants/player';
import { toCapitalCase } from 'util/string';
type Props = {
defaultQuality: ?string,
doSetDefaultVideoQuality: (value: ?string) => void,
};
export default function SettingDefaultQuality(props: Props) {
const { defaultQuality, doSetDefaultVideoQuality } = props;
const [enabled, setEnabled] = React.useState<boolean>(Boolean(defaultQuality));
const valueRef = React.useRef(VIDEO_QUALITY_OPTIONS[0]);
function handleEnable() {
if (enabled) {
setEnabled(false);
// From enabled to disabled -> clear the setting
2022-04-20 20:55:27 +02:00
valueRef.current = defaultQuality;
doSetDefaultVideoQuality(null);
} else {
setEnabled(true);
// From to disabled to enabled -> set the current shown value
doSetDefaultVideoQuality(valueRef.current);
}
}
function handleSetQuality(e) {
const { value } = e.target;
doSetDefaultVideoQuality(value);
valueRef.current = value;
}
return (
<>
<fieldset-section>
<FormField
name="default_video_quality"
type="select"
onChange={handleSetQuality}
disabled={!enabled}
2022-04-20 20:55:27 +02:00
value={defaultQuality || valueRef.current}
>
{VIDEO_QUALITY_OPTIONS.map((quality) => {
const qualityStr = typeof quality === 'number' ? quality + 'p' : toCapitalCase(quality);
return (
<option key={'quality' + qualityStr} value={quality}>
2022-04-20 21:13:11 +02:00
{__(qualityStr)}
</option>
);
})}
</FormField>
</fieldset-section>
<fieldset-section>
<FormField
type="checkbox"
name="enable_default_quality"
onChange={handleEnable}
checked={enabled}
label={__('Enable default quality setting')}
/>
</fieldset-section>
</>
);
}