| @@ -1,27 +1,22 @@ | ||
| 1 | 1 | import { handleSitePlugins } from '@auto-launch/fetchers/get-plugins'; |
| 2 | -import { activatePlugin, installPlugin } from '@auto-launch/functions/plugins'; | |
| 2 | +import { ensurePluginsActive } from '@auto-launch/functions/plugins'; | |
| 3 | +import { useEffect, useRef } from '@wordpress/element'; | |
| 3 | 4 | import useSWR from 'swr/immutable'; |
| 4 | 5 | |
| 5 | -const { installedPluginsSlugs } = window.extSharedData || {}; | |
| 6 | - | |
| 7 | -export const useInstallRequiredPlugins = () => { | |
| 8 | - const { data, error } = useSWR('required-plugins', () => | |
| 6 | +export const useInstallRequiredPlugins = ({ enabled = true } = {}) => { | |
| 7 | + const { data, error } = useSWR(enabled ? 'required-plugins' : null, () => | |
| 9 | 8 | handleSitePlugins({ requiredOnly: true }), |
| 10 | 9 | ); |
| 10 | + const started = useRef(false); | |
| 11 | 11 | |
| 12 | - if (data?.sitePlugins?.sitePlugins?.length) { | |
| 13 | - const pluginsToInstall = data.sitePlugins.sitePlugins.filter( | |
| 14 | - ({ wordpressSlug: slug }) => !installedPluginsSlugs?.includes(slug), | |
| 12 | + useEffect(() => { | |
| 13 | + if (started.current || !data?.sitePlugins?.length) return; | |
| 14 | + started.current = true; | |
| 15 | + ensurePluginsActive( | |
| 16 | + data.sitePlugins.map(({ wordpressSlug }) => wordpressSlug), | |
| 15 | 17 | ); |
| 16 | - if (pluginsToInstall.length === 0) return; | |
| 17 | - (async function install() { | |
| 18 | - for (const { wordpressSlug: slug } of pluginsToInstall) { | |
| 19 | - const p = await installPlugin(slug); | |
| 20 | - await activatePlugin(p?.plugin ?? slug); | |
| 21 | - } | |
| 22 | - })(); | |
| 23 | - } | |
| 18 | + }, [data]); | |
| 24 | 19 | |
| 25 | 20 | return { |
| 26 | 21 | requiredPlugins: data?.selectedPlugins || [], |
| 27 | 22 | isLoading: !error && !data, |