From 04a8edd6b141d1193523aeb310cc7dd16d54fb8f Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Fri, 1 Mar 2024 18:03:34 +0530 Subject: [PATCH] move useIsPluginActive to TS --- hooks/use-is-plugin-active/index.js | 20 -------------------- hooks/use-is-plugin-active/index.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 20 deletions(-) delete mode 100644 hooks/use-is-plugin-active/index.js create mode 100644 hooks/use-is-plugin-active/index.ts diff --git a/hooks/use-is-plugin-active/index.js b/hooks/use-is-plugin-active/index.js deleted file mode 100644 index 4f44ebe1..00000000 --- a/hooks/use-is-plugin-active/index.js +++ /dev/null @@ -1,20 +0,0 @@ -import { useSelect } from '@wordpress/data'; -import { store as coreStore } from '@wordpress/core-data'; - -const ACTIVE_STATUSES = ['active', 'network-active']; - -export const useIsPluginActive = (pluginName) => { - return useSelect( - (select) => { - const plugin = select(coreStore).getPlugin(pluginName); - const hasResolvedPlugins = select(coreStore).hasFinishedResolution('getPlugin', [ - pluginName, - ]); - - const isPluginActive = ACTIVE_STATUSES.includes(plugin?.status); - - return [isPluginActive, hasResolvedPlugins]; - }, - [pluginName], - ); -}; diff --git a/hooks/use-is-plugin-active/index.ts b/hooks/use-is-plugin-active/index.ts new file mode 100644 index 00000000..e9476b14 --- /dev/null +++ b/hooks/use-is-plugin-active/index.ts @@ -0,0 +1,29 @@ +import { useSelect } from '@wordpress/data'; +import { store as coreStore } from '@wordpress/core-data'; +import type { Plugin } from '@wordpress/core-data'; + +const ACTIVE_STATUSES: string[] = ['active', 'network-active']; + +/** + * Custom hook to check if a plugin is active and whether its resolution has finished. + * + * @param pluginName The name of the plugin to check. + * @returns A tuple containing two boolean values: the first indicating whether the plugin is active, + * and the second indicating whether the resolution for the plugin has finished. + */ +export const useIsPluginActive = (pluginName: string): [boolean, boolean] => { + return useSelect( + (select) => { + const storeSelectors = select(coreStore); + const plugin: Plugin = (storeSelectors as any).getPlugin(pluginName); + const hasResolvedPlugins: boolean = (storeSelectors as any).hasFinishedResolution('getPlugin', [ + pluginName, + ]); + + const isPluginActive: boolean = ACTIVE_STATUSES.includes(plugin?.status); + + return [isPluginActive, hasResolvedPlugins] as [boolean, boolean]; + }, + [pluginName], + ); +};