| 1 |
import { handleSitePlugins } from '@auto-launch/fetchers/get-plugins'; |
| 2 |
import { ensurePluginsActive } from '@auto-launch/functions/plugins'; |
| 3 |
import { useEffect, useRef } from '@wordpress/element'; |
| 4 |
import useSWR from 'swr/immutable'; |
| 5 |
|
| 6 |
const { installedPluginsSlugs } = window.extSharedData || {}; |
| 7 |
|
| 8 |
export const useInstallRequiredPlugins = () => { |
| 9 |
const { data, error } = useSWR('required-plugins', () => |
| 10 |
handleSitePlugins({ requiredOnly: true }), |
| 11 |
); |
| 12 |
const started = useRef(false); |
| 13 |
|
| 14 |
useEffect(() => { |
| 15 |
if (started.current || !data?.sitePlugins?.length) return; |
| 16 |
started.current = true; |
| 17 |
ensurePluginsActive( |
| 18 |
data.sitePlugins.map(({ wordpressSlug }) => wordpressSlug), |
| 19 |
{ installedSlugs: installedPluginsSlugs }, |
| 20 |
); |
| 21 |
}, [data]); |
| 22 |
|
| 23 |
return { |
| 24 |
requiredPlugins: data?.selectedPlugins || [], |
| 25 |
isLoading: !error && !data, |
| 26 |
isError: error, |
| 27 |
}; |
| 28 |
}; |
| 29 |
|