2023-01-19 11:33:51 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useSelect } from '@wordpress/data';
|
2023-01-19 14:23:00 +00:00
|
|
|
import { PLUGINS_STORE_NAME } from '@woocommerce/data';
|
|
|
|
import { differenceWith } from 'lodash';
|
2023-01-19 11:33:51 +00:00
|
|
|
|
2022-12-08 15:53:36 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2023-01-19 11:33:51 +00:00
|
|
|
import { STORE_KEY } from '~/marketing/data-multichannel/constants';
|
2023-01-20 18:20:06 +00:00
|
|
|
import {
|
|
|
|
RecommendedChannelsState,
|
|
|
|
RecommendedChannel,
|
|
|
|
} from '~/marketing/data-multichannel/types';
|
2022-12-08 15:53:36 +00:00
|
|
|
|
|
|
|
type UseRecommendedChannels = {
|
|
|
|
loading: boolean;
|
2023-01-27 13:38:38 +00:00
|
|
|
data?: Array< RecommendedChannel >;
|
2022-12-08 15:53:36 +00:00
|
|
|
};
|
|
|
|
|
2023-01-19 11:33:51 +00:00
|
|
|
export const useRecommendedChannels = (): UseRecommendedChannels => {
|
|
|
|
return useSelect( ( select ) => {
|
|
|
|
const { hasFinishedResolution, getRecommendedChannels } =
|
|
|
|
select( STORE_KEY );
|
2023-01-20 18:14:36 +00:00
|
|
|
const { data, error } =
|
|
|
|
getRecommendedChannels< RecommendedChannelsState >();
|
2022-12-14 12:19:20 +00:00
|
|
|
|
2023-01-19 14:23:00 +00:00
|
|
|
const { getActivePlugins } = select( PLUGINS_STORE_NAME );
|
|
|
|
const activePlugins = getActivePlugins();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recommended channels that are not in "active" state,
|
|
|
|
* i.e. channels that are not installed or not activated yet.
|
|
|
|
*/
|
2023-01-27 13:38:38 +00:00
|
|
|
const nonActiveRecommendedChannels =
|
|
|
|
data &&
|
|
|
|
differenceWith( data, activePlugins, ( a, b ) => {
|
2023-01-19 14:23:00 +00:00
|
|
|
return a.product === b;
|
2023-01-27 13:38:38 +00:00
|
|
|
} );
|
2022-12-14 12:19:20 +00:00
|
|
|
|
2023-01-19 11:33:51 +00:00
|
|
|
return {
|
2023-03-13 19:35:20 +00:00
|
|
|
loading: ! hasFinishedResolution( 'getRecommendedChannels', [] ),
|
2023-01-19 14:23:00 +00:00
|
|
|
data: nonActiveRecommendedChannels,
|
|
|
|
error,
|
2023-01-19 11:33:51 +00:00
|
|
|
};
|
|
|
|
} );
|
2022-12-08 15:53:36 +00:00
|
|
|
};
|