Skip to content

Commit

Permalink
move useIsPluginActive to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidsector9 committed Mar 1, 2024
1 parent 21e0b3e commit 04a8edd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
20 changes: 0 additions & 20 deletions hooks/use-is-plugin-active/index.js

This file was deleted.

29 changes: 29 additions & 0 deletions hooks/use-is-plugin-active/index.ts
Original file line number Diff line number Diff line change
@@ -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],
);
};

0 comments on commit 04a8edd

Please sign in to comment.