import Modal from '@app/components/Common/Modal'; import SensitiveInput from '@app/components/Common/SensitiveInput'; import globalMessages from '@app/i18n/globalMessages'; import { Transition } from '@headlessui/react'; import type { RadarrSettings } from '@server/lib/settings'; import axios from 'axios'; import { Field, Formik } from 'formik'; import { useCallback, useEffect, useRef, useState } from 'react'; import { defineMessages, useIntl } from 'react-intl'; import Select from 'react-select'; import { useToasts } from 'react-toast-notifications'; import * as Yup from 'yup'; type OptionType = { value: number; label: string; }; const messages = defineMessages({ createradarr: 'Add New Radarr Server', create4kradarr: 'Add New 4K Radarr Server', editradarr: 'Edit Radarr Server', edit4kradarr: 'Edit 4K Radarr Server', validationNameRequired: 'You must provide a server name', validationHostnameRequired: 'You must provide a valid hostname or IP address', validationPortRequired: 'You must provide a valid port number', validationApiKeyRequired: 'You must provide an API key', validationRootFolderRequired: 'You must select a root folder', validationProfileRequired: 'You must select a quality profile', validationMinimumAvailabilityRequired: 'You must select a minimum availability', toastRadarrTestSuccess: 'Radarr connection established successfully!', toastRadarrTestFailure: 'Failed to connect to Radarr.', add: 'Add Server', defaultserver: 'Default Server', default4kserver: 'Default 4K Server', servername: 'Server Name', hostname: 'Hostname or IP Address', port: 'Port', ssl: 'Use SSL', apiKey: 'API Key', baseUrl: 'URL Base', syncEnabled: 'Enable Scan', externalUrl: 'External URL', qualityprofile: 'Quality Profile', rootfolder: 'Root Folder', minimumAvailability: 'Minimum Availability', server4k: '4K Server', selectQualityProfile: 'Select quality profile', selectRootFolder: 'Select root folder', selectMinimumAvailability: 'Select minimum availability', loadingprofiles: 'Loading quality profiles…', testFirstQualityProfiles: 'Test connection to load quality profiles', loadingrootfolders: 'Loading root folders…', testFirstRootFolders: 'Test connection to load root folders', loadingTags: 'Loading tags…', testFirstTags: 'Test connection to load tags', tags: 'Tags', enableSearch: 'Enable Automatic Search', tagRequests: 'Tag Requests', tagRequestsInfo: "Automatically add an additional tag with the requester's user ID & display name", validationApplicationUrl: 'You must provide a valid URL', validationApplicationUrlTrailingSlash: 'URL must not end in a trailing slash', validationBaseUrlLeadingSlash: 'URL base must have a leading slash', validationBaseUrlTrailingSlash: 'URL base must not end in a trailing slash', notagoptions: 'No tags.', selecttags: 'Select tags', announced: 'Announced', inCinemas: 'In Cinemas', released: 'Released', }); interface TestResponse { profiles: { id: number; name: string; }[]; rootFolders: { id: number; path: string; }[]; tags: { id: number; label: string; }[]; urlBase?: string; } interface RadarrModalProps { radarr: RadarrSettings | null; onClose: () => void; onSave: () => void; } const RadarrModal = ({ onClose, radarr, onSave }: RadarrModalProps) => { const intl = useIntl(); const initialLoad = useRef(false); const { addToast } = useToasts(); const [isValidated, setIsValidated] = useState(radarr ? true : false); const [isTesting, setIsTesting] = useState(false); const [testResponse, setTestResponse] = useState({ profiles: [], rootFolders: [], tags: [], }); const RadarrSettingsSchema = Yup.object().shape({ name: Yup.string().required( intl.formatMessage(messages.validationNameRequired) ), hostname: Yup.string() .required(intl.formatMessage(messages.validationHostnameRequired)) .matches( /^(((([a-z]|\d|_|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*)?([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])):((([a-z]|\d|_|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*)?([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))@)?(([a-z]|\d|_|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*)?([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])$/i, intl.formatMessage(messages.validationHostnameRequired) ), port: Yup.number() .nullable() .required(intl.formatMessage(messages.validationPortRequired)), apiKey: Yup.string().required( intl.formatMessage(messages.validationApiKeyRequired) ), rootFolder: Yup.string().required( intl.formatMessage(messages.validationRootFolderRequired) ), activeProfileId: Yup.string().required( intl.formatMessage(messages.validationProfileRequired) ), minimumAvailability: Yup.string().required( intl.formatMessage(messages.validationMinimumAvailabilityRequired) ), externalUrl: Yup.string() .url(intl.formatMessage(messages.validationApplicationUrl)) .test( 'no-trailing-slash', intl.formatMessage(messages.validationApplicationUrlTrailingSlash), (value) => !value || !value.endsWith('/') ), baseUrl: Yup.string() .test( 'leading-slash', intl.formatMessage(messages.validationBaseUrlLeadingSlash), (value) => !value || value.startsWith('/') ) .test( 'no-trailing-slash', intl.formatMessage(messages.validationBaseUrlTrailingSlash), (value) => !value || !value.endsWith('/') ), }); const testConnection = useCallback( async ({ hostname, port, apiKey, baseUrl, useSsl = false, }: { hostname: string; port: number; apiKey: string; baseUrl?: string; useSsl?: boolean; }) => { setIsTesting(true); try { const response = await axios.post( '/api/v1/settings/radarr/test', { hostname, apiKey, port: Number(port), baseUrl, useSsl, } ); setIsValidated(true); setTestResponse(response.data); if (initialLoad.current) { addToast(intl.formatMessage(messages.toastRadarrTestSuccess), { appearance: 'success', autoDismiss: true, }); } } catch (e) { setIsValidated(false); if (initialLoad.current) { addToast(intl.formatMessage(messages.toastRadarrTestFailure), { appearance: 'error', autoDismiss: true, }); } } finally { setIsTesting(false); initialLoad.current = true; } }, [addToast, intl] ); useEffect(() => { if (radarr) { testConnection({ apiKey: radarr.apiKey, hostname: radarr.hostname, port: radarr.port, baseUrl: radarr.baseUrl, useSsl: radarr.useSsl, }); } }, [radarr, testConnection]); return ( { try { const profileName = testResponse.profiles.find( (profile) => profile.id === Number(values.activeProfileId) )?.name; const submission = { name: values.name, hostname: values.hostname, port: Number(values.port), apiKey: values.apiKey, useSsl: values.ssl, baseUrl: values.baseUrl, activeProfileId: Number(values.activeProfileId), activeProfileName: profileName, activeDirectory: values.rootFolder, is4k: values.is4k, minimumAvailability: values.minimumAvailability, tags: values.tags, isDefault: values.isDefault, externalUrl: values.externalUrl, syncEnabled: values.syncEnabled, preventSearch: !values.enableSearch, tagRequests: values.tagRequests, }; if (!radarr) { await axios.post('/api/v1/settings/radarr', submission); } else { await axios.put( `/api/v1/settings/radarr/${radarr.id}`, submission ); } onSave(); } catch (e) { // set error here } }} > {({ errors, touched, values, handleSubmit, setFieldValue, isSubmitting, isValid, }) => { return ( { if (values.apiKey && values.hostname && values.port) { testConnection({ apiKey: values.apiKey, baseUrl: values.baseUrl, hostname: values.hostname, port: values.port, useSsl: values.ssl, }); if (!values.baseUrl || values.baseUrl === '/') { setFieldValue('baseUrl', testResponse.urlBase); } } }} secondaryDisabled={ !values.apiKey || !values.hostname || !values.port || isTesting || isSubmitting } okDisabled={!isValidated || isSubmitting || isTesting || !isValid} onOk={() => handleSubmit()} title={ !radarr ? intl.formatMessage( values.is4k ? messages.create4kradarr : messages.createradarr ) : intl.formatMessage( values.is4k ? messages.edit4kradarr : messages.editradarr ) } >
) => { setIsValidated(false); setFieldValue('name', e.target.value); }} />
{errors.name && touched.name && typeof errors.name === 'string' && (
{errors.name}
)}
{values.ssl ? 'https://' : 'http://'} ) => { setIsValidated(false); setFieldValue('hostname', e.target.value); }} className="rounded-r-only" />
{errors.hostname && touched.hostname && typeof errors.hostname === 'string' && (
{errors.hostname}
)}
) => { setIsValidated(false); setFieldValue('port', e.target.value); }} /> {errors.port && touched.port && typeof errors.port === 'string' && (
{errors.port}
)}
{ setIsValidated(false); setFieldValue('ssl', !values.ssl); }} />
) => { setIsValidated(false); setFieldValue('apiKey', e.target.value); }} />
{errors.apiKey && touched.apiKey && typeof errors.apiKey === 'string' && (
{errors.apiKey}
)}
) => { setIsValidated(false); setFieldValue('baseUrl', e.target.value); }} />
{errors.baseUrl && touched.baseUrl && typeof errors.baseUrl === 'string' && (
{errors.baseUrl}
)}
{testResponse.profiles.length > 0 && testResponse.profiles.map((profile) => ( ))}
{errors.activeProfileId && touched.activeProfileId && typeof errors.activeProfileId === 'string' && (
{errors.activeProfileId}
)}
{testResponse.rootFolders.length > 0 && testResponse.rootFolders.map((folder) => ( ))}
{errors.rootFolder && touched.rootFolder && typeof errors.rootFolder === 'string' && (
{errors.rootFolder}
)}
{errors.minimumAvailability && touched.minimumAvailability && (
{errors.minimumAvailability}
)}
options={ isValidated ? testResponse.tags.map((tag) => ({ label: tag.label, value: tag.id, })) : [] } isMulti isDisabled={!isValidated || isTesting} placeholder={ !isValidated ? intl.formatMessage(messages.testFirstTags) : isTesting ? intl.formatMessage(messages.loadingTags) : intl.formatMessage(messages.selecttags) } className="react-select-container" classNamePrefix="react-select" value={ values.tags .map((tagId) => { const foundTag = testResponse.tags.find( (tag) => tag.id === tagId ); if (!foundTag) { return undefined; } return { value: foundTag.id, label: foundTag.label, }; }) .filter( (option) => option !== undefined ) as OptionType[] } onChange={(value) => { setFieldValue( 'tags', value.map((option) => option.value) ); }} noOptionsMessage={() => intl.formatMessage(messages.notagoptions) } />
{errors.externalUrl && touched.externalUrl && typeof errors.externalUrl === 'string' && (
{errors.externalUrl}
)}
); }}
); }; export default RadarrModal;