import { useFieldArray, useForm } from 'react-hook-form'; import { Button, IconButton, Input, Select, Switch } from '@chakra-ui/react'; import { IoAdd } from '@react-icons/all-files/io5/IoAdd'; import { IoTrash } from '@react-icons/all-files/io5/IoTrash'; import { OSCSettings } from 'ontime-types'; import { generateId } from 'ontime-utils'; import { maybeAxiosError } from '../../../../common/api/apiUtils'; import useOscSettings, { useOscSettingsMutation } from '../../../../common/hooks-query/useOscSettings'; import { isKeyEscape } from '../../../../common/utils/keyEvent'; import { isIPAddress, isOnlyNumbers, startsWithSlash } from '../../../../common/utils/regex'; import * as Panel from '../PanelUtils'; import { cycles } from './integrationUtils'; import style from './IntegrationsPanel.module.css'; export default function OscIntegrations() { const { data } = useOscSettings(); const { mutateAsync } = useOscSettingsMutation(); const { control, handleSubmit, reset, register, setError, formState: { errors, isSubmitting, isDirty, isValid }, } = useForm({ mode: 'onBlur', defaultValues: data, values: data, resetOptions: { keepDirtyValues: true, }, }); const { fields, prepend, remove } = useFieldArray({ name: 'subscriptions', control, }); const onSubmit = async (values: OSCSettings) => { if (values.portIn === values.portOut) { setError('portIn', { message: 'OSC IN and OUT Ports cant be the same' }); return; } const parsedValues = { ...values, portIn: Number(values.portIn), portOut: Number(values.portOut) }; try { await mutateAsync(parsedValues); } catch (error) { setError('root', { message: maybeAxiosError(error) }); } }; const preventEscape = (event: React.KeyboardEvent) => { if (isKeyEscape(event)) { event.preventDefault(); event.stopPropagation(); } }; const handleAddNewSubscription = () => { prepend({ id: generateId(), cycle: 'onLoad', message: '', enabled: false, }); }; const handleDeleteSubscription = (index: number) => { remove(index); }; const canSubmit = !isSubmitting && isDirty && isValid; return ( Open Sound Control
OSC Settings {errors?.root && {errors.root.message}} OSC Integrations {fields.length > 0 && ( Enabled Cycle Message {fields.map((field, index) => { // @ts-expect-error -- not sure why it is not finding the type, it is ok const maybeError = errors.subscriptions?.[index]?.message?.message; return ( {maybeError && {maybeError}} } aria-label='Delete entry' onClick={() => handleDeleteSubscription(index)} /> ); })} )}
); }