2022-09-14 07:43:30 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2022-11-24 13:46:08 +00:00
|
|
|
import { useSelect, useDispatch } from '@wordpress/data';
|
2023-03-08 15:42:02 +00:00
|
|
|
import { differenceWith } from 'lodash';
|
2022-09-14 07:43:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { STORE_KEY } from '~/marketing/data/constants';
|
2023-03-08 15:42:02 +00:00
|
|
|
import { useRecommendedChannels } from '~/marketing/hooks';
|
2022-12-09 16:33:10 +00:00
|
|
|
import { RecommendedPlugin } from '~/marketing/types';
|
2022-09-14 07:43:30 +00:00
|
|
|
|
2023-03-21 13:12:00 +00:00
|
|
|
type UseRecommendedPluginsWithoutChannels = {
|
|
|
|
/**
|
2023-03-22 08:38:21 +00:00
|
|
|
* Boolean indicating whether it is initializing.
|
2023-03-21 13:12:00 +00:00
|
|
|
*/
|
|
|
|
isInitializing: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Boolean indicating whether it is loading.
|
|
|
|
*
|
|
|
|
* This will be true when data is being refetched
|
|
|
|
* after `invalidateResolution` is called in the `installAndActivate` method.
|
|
|
|
*/
|
|
|
|
isLoading: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of recommended marketing plugins without marketing channels.
|
|
|
|
*/
|
|
|
|
data: RecommendedPlugin[];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Install and activate a plugin.
|
|
|
|
*/
|
|
|
|
installAndActivate: ( slug: string ) => void;
|
|
|
|
};
|
|
|
|
|
2022-11-24 13:46:08 +00:00
|
|
|
const selector = 'getRecommendedPlugins';
|
2022-09-14 07:43:30 +00:00
|
|
|
const category = 'marketing';
|
|
|
|
|
2023-03-21 13:12:00 +00:00
|
|
|
/**
|
|
|
|
* A hook to return a list of recommended plugins without marketing channels,
|
|
|
|
* and related methods, to be used with the `DiscoverTools` component.
|
|
|
|
*/
|
|
|
|
export const useRecommendedPluginsWithoutChannels =
|
|
|
|
(): UseRecommendedPluginsWithoutChannels => {
|
|
|
|
const {
|
|
|
|
loading: loadingRecommendedPlugins,
|
|
|
|
data: dataRecommendedPlugins,
|
|
|
|
} = useSelect( ( select ) => {
|
2023-03-21 11:30:24 +00:00
|
|
|
const { getRecommendedPlugins, hasFinishedResolution } =
|
|
|
|
select( STORE_KEY );
|
2022-09-14 07:43:30 +00:00
|
|
|
|
2023-03-21 11:30:24 +00:00
|
|
|
return {
|
|
|
|
loading: ! hasFinishedResolution( selector, [ category ] ),
|
|
|
|
data: getRecommendedPlugins< RecommendedPlugin[] >( category ),
|
|
|
|
};
|
|
|
|
}, [] );
|
|
|
|
|
2023-03-22 08:44:44 +00:00
|
|
|
const {
|
|
|
|
loading: loadingRecommendedChannels,
|
|
|
|
data: dataRecommendedChannels,
|
|
|
|
} = useRecommendedChannels();
|
|
|
|
|
|
|
|
const { invalidateResolution, installAndActivateRecommendedPlugin } =
|
|
|
|
useDispatch( STORE_KEY );
|
|
|
|
|
2023-03-21 14:28:05 +00:00
|
|
|
const isInitializing =
|
|
|
|
( loadingRecommendedPlugins && ! dataRecommendedPlugins.length ) ||
|
|
|
|
( loadingRecommendedChannels && ! dataRecommendedChannels );
|
|
|
|
|
2023-03-21 13:12:00 +00:00
|
|
|
const loading = loadingRecommendedPlugins || loadingRecommendedChannels;
|
2023-03-08 15:42:02 +00:00
|
|
|
|
2023-03-21 13:12:00 +00:00
|
|
|
const recommendedPluginsWithoutChannels = differenceWith(
|
|
|
|
dataRecommendedPlugins,
|
|
|
|
dataRecommendedChannels || [],
|
|
|
|
( a, b ) => a.product === b.product
|
|
|
|
);
|
2023-03-08 15:42:02 +00:00
|
|
|
|
2023-03-22 08:44:44 +00:00
|
|
|
const installAndActivate = ( slug: string ) => {
|
|
|
|
installAndActivateRecommendedPlugin( slug, category );
|
|
|
|
invalidateResolution( selector, [ category ] );
|
|
|
|
};
|
|
|
|
|
2023-03-21 13:12:00 +00:00
|
|
|
return {
|
2023-03-21 14:28:05 +00:00
|
|
|
isInitializing,
|
2023-03-21 13:12:00 +00:00
|
|
|
isLoading: loading,
|
2023-03-21 14:28:05 +00:00
|
|
|
data: isInitializing ? [] : recommendedPluginsWithoutChannels,
|
2023-03-21 13:12:00 +00:00
|
|
|
installAndActivate,
|
|
|
|
};
|
2023-03-08 15:42:02 +00:00
|
|
|
};
|