-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathindex.ts
30 lines (26 loc) · 1.12 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import type { Plugin } from '@wordpress/core-data';
const ACTIVE_STATUSES = ['active', 'network-active'] as const;
/**
* Custom hook to check if a plugin is active and whether its resolution has finished.
*
* @param {string} pluginName The name of the plugin to check.
* @returns {[boolean, boolean]} A tuple with the first value being whether the plugin is active and the second value being whether the resolution has finished.
*/
export const useIsPluginActive = (pluginName: string) => {
return useSelect(
(select) => {
const storeSelectors = select(coreStore);
const plugin: Plugin = (storeSelectors as any).getPlugin(pluginName);
const hasResolvedPlugins: boolean = (storeSelectors as any).hasFinishedResolution(
'getPlugin',
[pluginName],
);
// @ts-ignore-next-line - The check here is intentional to see if the plugin is active.
const isPluginActive: boolean = ACTIVE_STATUSES.includes(plugin?.status);
return [isPluginActive, hasResolvedPlugins];
},
[pluginName],
) as [boolean, boolean];
};