diff --git a/projects/js-packages/publicize-components/changelog/update-extract-connection-state-logic b/projects/js-packages/publicize-components/changelog/update-extract-connection-state-logic new file mode 100644 index 0000000000000..e4e00762c2c4f --- /dev/null +++ b/projects/js-packages/publicize-components/changelog/update-extract-connection-state-logic @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Extracted the connection toggle state logic diff --git a/projects/js-packages/publicize-components/src/components/form/connections-list.tsx b/projects/js-packages/publicize-components/src/components/form/connections-list.tsx index f04f42e84d86b..404f3d72c1e2f 100644 --- a/projects/js-packages/publicize-components/src/components/form/connections-list.tsx +++ b/projects/js-packages/publicize-components/src/components/form/connections-list.tsx @@ -1,83 +1,24 @@ -import { useSelect } from '@wordpress/data'; -import { useCallback } from '@wordpress/element'; -import { usePublicizeConfig } from '../../..'; -import useAttachedMedia from '../../hooks/use-attached-media'; -import useFeaturedImage from '../../hooks/use-featured-image'; -import useImageGeneratorConfig from '../../hooks/use-image-generator-config'; -import useMediaDetails from '../../hooks/use-media-details'; -import useMediaRestrictions, { NO_MEDIA_ERROR } from '../../hooks/use-media-restrictions'; import useSocialMediaConnections from '../../hooks/use-social-media-connections'; -import { store as socialStore } from '../../social-store'; import PublicizeConnection from '../connection'; import PublicizeSettingsButton from '../settings-button'; import styles from './styles.module.scss'; +import { useConnectionState } from './use-connection-state'; export const ConnectionsList: React.FC = () => { - const { connections, toggleById, enabledConnections } = useSocialMediaConnections(); - const { isPublicizeEnabled, isPublicizeDisabledBySitePlan } = usePublicizeConfig(); - const { isEnabled: isSocialImageGeneratorEnabledForPost } = useImageGeneratorConfig(); - const { showShareLimits, numberOfSharesRemaining } = useSelect( select => { - return { - showShareLimits: select( socialStore ).showShareLimits(), - numberOfSharesRemaining: select( socialStore ).numberOfSharesRemaining(), - }; - }, [] ); + const { connections, toggleById } = useSocialMediaConnections(); - const outOfConnections = showShareLimits && numberOfSharesRemaining <= enabledConnections.length; - - const isAutoConversionEnabled = useSelect( - select => select( socialStore ).isAutoConversionEnabled(), - [] - ); - - const { attachedMedia, shouldUploadAttachedMedia } = useAttachedMedia(); - const featuredImageId = useFeaturedImage(); - const mediaId = attachedMedia[ 0 ]?.id || featuredImageId; - - const { validationErrors, isConvertible } = useMediaRestrictions( - connections, - useMediaDetails( mediaId )[ 0 ], - { - isSocialImageGeneratorEnabledForPost, - shouldUploadAttachedMedia, - } - ); - const shouldAutoConvert = isAutoConversionEnabled && isConvertible; - - const isConnectionEnabled = useCallback( - ( { enabled, is_healthy = true, connection_id } ) => - enabled && - ! isPublicizeDisabledBySitePlan && - false !== is_healthy && - ( ! validationErrors[ connection_id ] || shouldAutoConvert ) && - validationErrors[ connection_id ] !== NO_MEDIA_ERROR, - [ isPublicizeDisabledBySitePlan, validationErrors, shouldAutoConvert ] - ); + const { canBeTurnedOn, shouldBeDisabled } = useConnectionState(); return (