1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2026-04-19 21:46:43 -04:00

Use react-query for Indexers

This commit is contained in:
Mark McDowall
2026-02-16 15:48:47 -08:00
parent bcceb22512
commit c4c0ec25ac
32 changed files with 689 additions and 729 deletions
@@ -1,43 +1,9 @@
import React, { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { createSelector } from 'reselect';
import AppState from 'App/State/AppState';
import { fetchIndexers } from 'Store/Actions/settingsActions';
import React, { useMemo } from 'react';
import { useSortedIndexers } from 'Settings/Indexers/useIndexers';
import { EnhancedSelectInputChanged } from 'typings/inputs';
import sortByProp from 'Utilities/Array/sortByProp';
import translate from 'Utilities/String/translate';
import EnhancedSelectInput from './EnhancedSelectInput';
function createIndexersSelector(includeAny: boolean) {
return createSelector(
(state: AppState) => state.settings.indexers,
(indexers) => {
const { isFetching, isPopulated, error, items } = indexers;
const values = items.sort(sortByProp('name')).map((indexer) => {
return {
key: indexer.id,
value: indexer.name,
};
});
if (includeAny) {
values.unshift({
key: 0,
value: `(${translate('Any')})`,
});
}
return {
isFetching,
isPopulated,
error,
values,
};
}
);
}
export interface IndexerSelectInputProps {
name: string;
value: number | number[];
@@ -51,16 +17,23 @@ function IndexerSelectInput({
includeAny = false,
onChange,
}: IndexerSelectInputProps) {
const dispatch = useDispatch();
const { isFetching, isPopulated, values } = useSelector(
createIndexersSelector(includeAny)
);
const { isFetching, data } = useSortedIndexers();
useEffect(() => {
if (!isPopulated) {
dispatch(fetchIndexers());
const values = useMemo(() => {
const indexerOptions = data.map((indexer) => ({
key: indexer.id,
value: indexer.name,
}));
if (includeAny) {
indexerOptions.unshift({
key: 0,
value: `(${translate('Any')})`,
});
}
}, [isPopulated, dispatch]);
return indexerOptions;
}, [data, includeAny]);
return (
<EnhancedSelectInput