| 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 |
export const useInstallRequiredPlugins = ({ enabled = true } = {}) => { |
| 7 |
const { data, error } = useSWR(enabled ? 'required-plugins' : null, () => |
| 8 |
handleSitePlugins({ requiredOnly: true }), |
| 9 |
); |
| 10 |
const started = useRef(false); |
| 11 |
|
| 12 |
useEffect(() => { |
| 13 |
if (started.current || !data?.sitePlugins?.length) return; |
| 14 |
started.current = true; |
| 15 |
ensurePluginsActive( |
| 16 |
data.sitePlugins.map(({ wordpressSlug }) => wordpressSlug), |
| 17 |
); |
| 18 |
}, [data]); |
| 19 |
|
| 20 |
return { |
| 21 |
requiredPlugins: data?.selectedPlugins || [], |
| 22 |
isLoading: !error && !data, |
| 23 |
isError: error, |
| 24 |
}; |
| 25 |
}; |
| 26 |
|