1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2026-04-20 21:54:58 -04:00

Convert Form Components to TypeScript

This commit is contained in:
Mark McDowall
2024-10-26 14:54:23 -07:00
committed by GitHub
parent c114e2ddb7
commit 682d2b4e1b
158 changed files with 5225 additions and 6112 deletions
@@ -0,0 +1,126 @@
import React, { useCallback, useEffect } from 'react';
import { useSelector } from 'react-redux';
import { createSelector } from 'reselect';
import { QualityProfilesAppState } from 'App/State/SettingsAppState';
import createSortedSectionSelector from 'Store/Selectors/createSortedSectionSelector';
import { EnhancedSelectInputChanged } from 'typings/inputs';
import QualityProfile from 'typings/QualityProfile';
import sortByProp from 'Utilities/Array/sortByProp';
import translate from 'Utilities/String/translate';
import EnhancedSelectInput, {
EnhancedSelectInputProps,
EnhancedSelectInputValue,
} from './EnhancedSelectInput';
function createQualityProfilesSelector(
includeNoChange: boolean,
includeNoChangeDisabled: boolean,
includeMixed: boolean
) {
return createSelector(
createSortedSectionSelector(
'settings.qualityProfiles',
sortByProp<QualityProfile, 'name'>('name')
),
(qualityProfiles: QualityProfilesAppState) => {
const values: EnhancedSelectInputValue<number | string>[] =
qualityProfiles.items.map((qualityProfile) => {
return {
key: qualityProfile.id,
value: qualityProfile.name,
};
});
if (includeNoChange) {
values.unshift({
key: 'noChange',
get value() {
return translate('NoChange');
},
isDisabled: includeNoChangeDisabled,
});
}
if (includeMixed) {
values.unshift({
key: 'mixed',
get value() {
return `(${translate('Mixed')})`;
},
isDisabled: true,
});
}
return values;
}
);
}
interface QualityProfileSelectInputConnectorProps
extends Omit<
EnhancedSelectInputProps<
EnhancedSelectInputValue<number | string>,
number | string
>,
'values'
> {
name: string;
includeNoChange?: boolean;
includeNoChangeDisabled?: boolean;
includeMixed?: boolean;
}
function QualityProfileSelectInput({
name,
value,
includeNoChange = false,
includeNoChangeDisabled = true,
includeMixed = false,
onChange,
...otherProps
}: QualityProfileSelectInputConnectorProps) {
const values = useSelector(
createQualityProfilesSelector(
includeNoChange,
includeNoChangeDisabled,
includeMixed
)
);
const handleChange = useCallback(
({ value: newValue }: EnhancedSelectInputChanged<string | number>) => {
onChange({
name,
value: newValue === 'noChange' ? value : newValue,
});
},
[name, value, onChange]
);
useEffect(() => {
if (
!value ||
!values.some((option) => option.key === value || option.key === value)
) {
const firstValue = values.find(
(option) => typeof option.key === 'number'
);
if (firstValue) {
onChange({ name, value: firstValue.key });
}
}
}, [name, value, values, onChange]);
return (
<EnhancedSelectInput
{...otherProps}
name={name}
value={value}
values={values}
onChange={handleChange}
/>
);
}
export default QualityProfileSelectInput;