woocommerce/plugins/woocommerce-admin/client/marketing/hooks/useRecommendedChannels.ts

48 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-01-19 11:33:51 +00:00
/**
* External dependencies
*/
import { useSelect } from '@wordpress/data';
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';
2022-12-14 12:54:29 +00:00
import { RecommendedChannel } from '~/marketing/types';
2023-01-19 11:33:51 +00:00
import { RecommendedChannels } from '~/marketing/data-multichannel/types';
2022-12-08 15:53:36 +00:00
type UseRecommendedChannels = {
loading: boolean;
data: Array< RecommendedChannel >;
};
2023-01-19 11:33:51 +00:00
export const useRecommendedChannels = (): UseRecommendedChannels => {
return useSelect( ( select ) => {
const { hasFinishedResolution, getRecommendedChannels } =
select( STORE_KEY );
const { data, error } = getRecommendedChannels< RecommendedChannels >();
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.
*/
const nonActiveRecommendedChannels = differenceWith(
data,
activePlugins,
( a, b ) => {
return a.product === b;
}
);
2023-01-19 11:33:51 +00:00
return {
loading: ! hasFinishedResolution( 'getRecommendedChannels' ),
data: nonActiveRecommendedChannels,
error,
2023-01-19 11:33:51 +00:00
};
} );
2022-12-08 15:53:36 +00:00
};