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

51 lines
1.2 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';
import {
RecommendedChannelsState,
RecommendedChannel,
} 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 );
2023-01-20 18:14:36 +00:00
const { data, error } =
getRecommendedChannels< RecommendedChannelsState >();
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
};