// StreamProfile form import React, { useEffect, useMemo, useState } from 'react'; import { useForm } from 'react-hook-form'; import useUserAgentsStore from '../../store/userAgents'; import { Button, Checkbox, Flex, Modal, Select, Stack, Textarea, TextInput, } from '@mantine/core'; import { addStreamProfile, BUILT_IN_COMMANDS, COMMAND_EXAMPLES, getResolver, toCommandSelection, updateStreamProfile, } from '../../utils/forms/StreamProfileUtils.js'; const StreamProfile = ({ profile = null, isOpen, onClose }) => { const userAgents = useUserAgentsStore((state) => state.userAgents); // Separate state for the dropdown selection so 'Custom…' can be chosen // independently of the actual command string stored in the form. const [commandSelection, setCommandSelection] = useState('ffmpeg'); const defaultValues = useMemo( () => ({ name: profile?.name || '', command: profile?.command || '', parameters: profile?.parameters || '', is_active: profile?.is_active ?? true, user_agent: profile?.user_agent ? `${profile.user_agent}` : '', }), [profile] ); const { register, handleSubmit, formState: { errors, isSubmitting }, reset, setValue, watch, } = useForm({ defaultValues, resolver: getResolver(), }); // Sync form + dropdown selection whenever the target profile or modal state changes useEffect(() => { reset(defaultValues); setCommandSelection(toCommandSelection(profile?.command || '')); }, [defaultValues, reset, profile]); const onSubmit = async (values) => { if (profile?.id) { await updateStreamProfile(profile.id, values); } else { await addStreamProfile(values); } reset(); onClose(); }; if (!isOpen) { return <>; } const isLocked = profile ? profile.locked : false; const isCustom = commandSelection === '__custom__'; const userAgentValue = watch('user_agent'); const isActiveValue = watch('is_active'); const handleOnChangeCommand = (val) => { setCommandSelection(val); // For built-in selections, write the real command value immediately if (val !== '__custom__') { setValue('command', val, { shouldValidate: true }); } else { // Clear so the user enters their own value setValue('command', '', { shouldValidate: false }); } }; return (