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
|
|
|
|
2023-03-21 11:08:13 +00:00
|
|
|
type InstalledExtension = InstalledPlugin;
|
|
|
|
|
|
|
|
export type UseInstalledExtensions = {
|
|
|
|
installedExtensionsWithoutChannels: InstalledExtension[];
|
|
|
|
activatingExtensions: string[];
|
|
|
|
activateInstalledExtension: ( slug: string ) => void;
|
|
|
|
loadInstalledExtensionsAfterActivation: ( 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
|
|
|
*/
|
2023-03-21 11:08:13 +00:00
|
|
|
export const useInstalledExtensions = (): UseInstalledExtensions => {
|
2023-03-21 10:35:38 +00:00
|
|
|
const { loading: loadingRegisteredChannels, data: dataRegisteredChannels } =
|
|
|
|
useRegisteredChannels();
|
2023-03-21 10:33:57 +00:00
|
|
|
const {
|
|
|
|
loading: loadingRecommendedChannels,
|
2023-03-21 10:35:38 +00:00
|
|
|
data: dataRecommendedChannels,
|
2023-03-21 10:33:57 +00:00
|
|
|
} = useRecommendedChannels();
|
2023-03-08 15:21:54 +00:00
|
|
|
|
2022-09-14 07:43:30 +00:00
|
|
|
const { installedPlugins, activatingPlugins } = useSelect( ( select ) => {
|
|
|
|
const { getInstalledPlugins, getActivatingPlugins } =
|
|
|
|
select( STORE_KEY );
|
|
|
|
|
|
|
|
return {
|
2023-03-21 11:08:13 +00:00
|
|
|
installedPlugins: getInstalledPlugins< InstalledExtension[] >(),
|
2022-09-14 07:43:30 +00:00
|
|
|
activatingPlugins: getActivatingPlugins(),
|
|
|
|
};
|
|
|
|
}, [] );
|
2023-03-08 15:21:54 +00:00
|
|
|
|
2023-03-21 10:33:57 +00:00
|
|
|
const { activateInstalledPlugin, loadInstalledPluginsAfterActivation } =
|
|
|
|
useDispatch( STORE_KEY );
|
|
|
|
|
|
|
|
const loading = loadingRegisteredChannels || loadingRecommendedChannels;
|
2023-03-08 15:21:54 +00:00
|
|
|
const installedPluginsWithoutChannels = chain( installedPlugins )
|
|
|
|
.differenceWith(
|
2023-03-21 10:35:38 +00:00
|
|
|
dataRegisteredChannels || [],
|
|
|
|
( a, b ) => a.slug === b.slug
|
|
|
|
)
|
|
|
|
.differenceWith(
|
|
|
|
dataRecommendedChannels || [],
|
2023-03-08 15:21:54 +00:00
|
|
|
( a, b ) => a.slug === b.product
|
|
|
|
)
|
|
|
|
.value();
|
|
|
|
|
2022-09-14 07:43:30 +00:00
|
|
|
return {
|
2023-03-21 11:08:13 +00:00
|
|
|
installedExtensionsWithoutChannels: loading
|
|
|
|
? []
|
|
|
|
: installedPluginsWithoutChannels,
|
|
|
|
activatingExtensions: activatingPlugins,
|
|
|
|
activateInstalledExtension: activateInstalledPlugin,
|
|
|
|
loadInstalledExtensionsAfterActivation:
|
|
|
|
loadInstalledPluginsAfterActivation,
|
2022-09-14 07:43:30 +00:00
|
|
|
};
|
|
|
|
};
|