-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #297 from 10up/upkeep/use-is-plugin-active
upkeep: add TS support for `useIsPluginActive` hook
- Loading branch information
Showing
2 changed files
with
29 additions
and
20 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}; |