Skip to content

Commit

Permalink
Merge pull request #297 from 10up/upkeep/use-is-plugin-active
Browse files Browse the repository at this point in the history
upkeep: add TS support for `useIsPluginActive` hook
  • Loading branch information
fabiankaegy authored Mar 5, 2024
2 parents 35eed23 + c66af52 commit 78c2ca4
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 = ['active', 'network-active'] as const;

/**
* 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) => {
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];
},
[pluginName],
) as [boolean, boolean];
};

0 comments on commit 78c2ca4

Please sign in to comment.