import React, { useCallback, useEffect, useState } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import DescriptionList from 'Components/DescriptionList/DescriptionList'; import DescriptionListItem from 'Components/DescriptionList/DescriptionListItem'; import Button from 'Components/Link/Button'; import SpinnerErrorButton from 'Components/Link/SpinnerErrorButton'; import ModalBody from 'Components/Modal/ModalBody'; import ModalContent from 'Components/Modal/ModalContent'; import ModalFooter from 'Components/Modal/ModalFooter'; import ModalHeader from 'Components/Modal/ModalHeader'; import DownloadProtocol from 'DownloadClient/DownloadProtocol'; import usePrevious from 'Helpers/Hooks/usePrevious'; import { grabRelease } from 'Store/Actions/releaseActions'; import { fetchDownloadClients } from 'Store/Actions/settingsActions'; import createEnabledDownloadClientsSelector from 'Store/Selectors/createEnabledDownloadClientsSelector'; import translate from 'Utilities/String/translate'; import SelectDownloadClientModal from './DownloadClient/SelectDownloadClientModal'; import OverrideMatchData from './OverrideMatchData'; import styles from './OverrideMatchModalContent.css'; type SelectType = 'select' | 'downloadClient'; interface OverrideMatchModalContentProps { indexerId: number; title: string; guid: string; protocol: DownloadProtocol; isGrabbing: boolean; grabError?: string; onModalClose(): void; } function OverrideMatchModalContent(props: OverrideMatchModalContentProps) { const modalTitle = translate('ManualGrab'); const { indexerId, title, guid, protocol, isGrabbing, grabError, onModalClose, } = props; const [downloadClientId, setDownloadClientId] = useState(null); const [selectModalOpen, setSelectModalOpen] = useState( null ); const previousIsGrabbing = usePrevious(isGrabbing); const dispatch = useDispatch(); const { items: downloadClients } = useSelector( createEnabledDownloadClientsSelector(protocol) ); const onSelectModalClose = useCallback(() => { setSelectModalOpen(null); }, [setSelectModalOpen]); const onSelectDownloadClientPress = useCallback(() => { setSelectModalOpen('downloadClient'); }, [setSelectModalOpen]); const onDownloadClientSelect = useCallback( (downloadClientId: number) => { setDownloadClientId(downloadClientId); setSelectModalOpen(null); }, [setDownloadClientId, setSelectModalOpen] ); const onGrabPress = useCallback(() => { dispatch( grabRelease({ indexerId, guid, downloadClientId, }) ); }, [indexerId, guid, downloadClientId, dispatch]); useEffect(() => { if (!isGrabbing && previousIsGrabbing) { onModalClose(); } }, [isGrabbing, previousIsGrabbing, onModalClose]); useEffect( () => { dispatch(fetchDownloadClients()); }, // eslint-disable-next-line react-hooks/exhaustive-deps [] ); return ( {translate('OverrideGrabModalTitle', { title })} {downloadClients.length > 1 ? ( downloadClient.id === downloadClientId )?.name ?? translate('Default') } onPress={onSelectDownloadClientPress} /> } /> ) : null}
{grabError}
{translate('GrabRelease')}
); } export default OverrideMatchModalContent;