import React from 'react'; import SemverSatisfies from 'semver/functions/satisfies'; import Grid from '@mui/material/Grid'; import Button from '@mui/material/Button'; import Typography from '@mui/material/Typography'; import { Trans } from '@lingui/macro'; import Sources from './Sources'; function initConfig(initialConfig) { let config = {}; for (let s of Sources.List()) { config[s.id] = {}; } config = { ...config, ...initialConfig, }; return config; } function init(source) { const settings = {}; for (let id of Sources.IDs()) { settings[id] = {}; } settings[source.type] = source.settings; return settings; } function reducer(settings, data) { const newSettings = { ...settings, ...data, }; return newSettings; } export default function SourceSelect(props) { // $source holds the currently selected device. It is initialized with the // last stored source. const [$source, setSource] = React.useState(props.source.type); // $settings is for storing the settings of the different devices, such that if // the user switches between them, they can be restored. It takes the last // stored source settings as initial value. const [$settings, setSettings] = React.useReducer(reducer, props.source, init); const config = initConfig(props.config); const handleSource = (source) => { props.onChange(props.type); setSource(source); props.onSelect(props.type, source); }; const handleRefresh = async () => { await props.onRefresh(); }; const handleProbe = async (settings, inputs) => { await props.onProbe(props.type, $source, settings, inputs); }; const handleChange = (source) => (settings) => { setSettings({ ...$settings, [source]: settings, }); props.onChange(props.type, source, settings); }; let sourceControl = null; const s = Sources.Get($source); if (s !== null) { const Component = s.component; if (SemverSatisfies(props.skills.ffmpeg.version, s.ffversion)) { sourceControl = ( ); } } return (