| @@ -1,13 +1,11 @@ | ||
| 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 | 3 | import { useEffect, useRef } from '@wordpress/element'; |
| 4 | 4 | import useSWR from 'swr/immutable'; |
| 5 | 5 | |
| 6 | -const { installedPluginsSlugs } = window.extSharedData || {}; | |
| 7 | - | |
| 8 | -export const useInstallRequiredPlugins = () => { | |
| 9 | - const { data, error } = useSWR('required-plugins', () => | |
| 6 | +export const useInstallRequiredPlugins = ({ enabled = true } = {}) => { | |
| 7 | + const { data, error } = useSWR(enabled ? 'required-plugins' : null, () => | |
| 10 | 8 | handleSitePlugins({ requiredOnly: true }), |
| 11 | 9 | ); |
| 12 | 10 | const started = useRef(false); |
| 13 | 11 | |
| @@ -13,23 +11,11 @@ | ||
| 13 | 11 | |
| 14 | 12 | useEffect(() => { |
| 15 | 13 | if (started.current || !data?.sitePlugins?.length) return; |
| 16 | 14 | started.current = true; |
| 17 | - const install = async () => { | |
| 18 | - for (const { wordpressSlug: slug } of data.sitePlugins) { | |
| 19 | - // One plugin failing shouldn't block the rest of the required set. | |
| 20 | - try { | |
| 21 | - let plugin; | |
| 22 | - if (!installedPluginsSlugs?.includes(slug)) { | |
| 23 | - plugin = await installPlugin(slug); | |
| 24 | - } | |
| 25 | - await activatePlugin(plugin?.plugin ?? slug); | |
| 26 | - } catch (error) { | |
| 27 | - console.error(`Failed to set up required plugin ${slug}`, error); | |
| 28 | - } | |
| 29 | - } | |
| 30 | - }; | |
| 31 | - install(); | |
| 15 | + ensurePluginsActive( | |
| 16 | + data.sitePlugins.map(({ wordpressSlug }) => wordpressSlug), | |
| 17 | + ); | |
| 32 | 18 | }, [data]); |
| 33 | 19 | |
| 34 | 20 | return { |
| 35 | 21 | requiredPlugins: data?.selectedPlugins || [], |