2022-09-14 07:43:30 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useSelect, useDispatch } from '@wordpress/data';
|
2023-03-08 15:21:54 +00:00
|
|
|
import { chain } from 'lodash';
|
2022-09-14 07:43:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { STORE_KEY } from '~/marketing/data/constants';
|
2022-12-09 16:25:56 +00:00
|
|
|
import { InstalledPlugin } from '~/marketing/types';
|
2023-03-08 15:21:54 +00:00
|
|
|
import { useRecommendedChannels } from './useRecommendedChannels';
|
|
|
|
import { useRegisteredChannels } from './useRegisteredChannels';
|
2022-09-14 07:43:30 +00:00
|
|
|
|
2022-11-24 13:46:08 +00:00
|
|
|
export type UseInstalledPlugins = {
|
2022-12-09 16:25:56 +00:00
|
|
|
installedPlugins: InstalledPlugin[];
|
2022-09-14 07:43:30 +00:00
|
|
|
activatingPlugins: string[];
|
|
|
|
activateInstalledPlugin: ( slug: string ) => void;
|
2022-11-24 13:46:08 +00:00
|
|
|
loadInstalledPluginsAfterActivation: ( slug: string ) => void;
|
2022-09-14 07:43:30 +00:00
|
|
|
};
|
|
|
|
|
2022-11-24 13:46:08 +00:00
|
|
|
/**
|
|
|
|
* Hook to return plugins and methods for "Installed extensions" card.
|
2023-03-08 15:21:54 +00:00
|
|
|
*
|
|
|
|
* The list of installed plugins will not include registered and recommended marketing channels.
|
2022-11-24 13:46:08 +00:00
|
|
|
*/
|
|
|
|
export const useInstalledPlugins = (): UseInstalledPlugins => {
|
2023-03-08 15:21:54 +00:00
|
|
|
const { data: dataRegisteredChannels = [] } = useRegisteredChannels();
|
|
|
|
const { data: dataRecommendedChannels = [] } = useRecommendedChannels();
|
|
|
|
|
2022-09-14 07:43:30 +00:00
|
|
|
const { installedPlugins, activatingPlugins } = useSelect( ( select ) => {
|
|
|
|
const { getInstalledPlugins, getActivatingPlugins } =
|
|
|
|
select( STORE_KEY );
|
|
|
|
|
|
|
|
return {
|
2023-03-08 15:21:54 +00:00
|
|
|
installedPlugins: getInstalledPlugins< InstalledPlugin[] >(),
|
2022-09-14 07:43:30 +00:00
|
|
|
activatingPlugins: getActivatingPlugins(),
|
|
|
|
};
|
|
|
|
}, [] );
|
2023-03-08 15:21:54 +00:00
|
|
|
|
|
|
|
const installedPluginsWithoutChannels = chain( installedPlugins )
|
|
|
|
.differenceWith( dataRegisteredChannels, ( a, b ) => a.slug === b.slug )
|
|
|
|
.differenceWith(
|
|
|
|
dataRecommendedChannels,
|
|
|
|
( a, b ) => a.slug === b.product
|
|
|
|
)
|
|
|
|
.value();
|
|
|
|
|
2022-11-24 13:46:08 +00:00
|
|
|
const { activateInstalledPlugin, loadInstalledPluginsAfterActivation } =
|
|
|
|
useDispatch( STORE_KEY );
|
2022-09-14 07:43:30 +00:00
|
|
|
|
|
|
|
return {
|
2023-03-08 15:21:54 +00:00
|
|
|
installedPlugins: installedPluginsWithoutChannels,
|
2022-09-14 07:43:30 +00:00
|
|
|
activatingPlugins,
|
|
|
|
activateInstalledPlugin,
|
2022-11-24 13:46:08 +00:00
|
|
|
loadInstalledPluginsAfterActivation,
|
2022-09-14 07:43:30 +00:00
|
|
|
};
|
|
|
|
};
|