| 1 |
import { handleSitePlugins } from '@auto-launch/fetchers/get-plugins'; |
| 2 |
import { activatePlugin, installPlugin } from '@auto-launch/functions/plugins'; |
| 3 |
import useSWR from 'swr/immutable'; |
| 4 |
|
| 5 |
const { installedPluginsSlugs } = window.extSharedData || {}; |
| 6 |
|
| 7 |
export const useInstallRequiredPlugins = () => { |
| 8 |
const { data, error } = useSWR('required-plugins', () => |
| 9 |
handleSitePlugins({ requiredOnly: true }), |
| 10 |
); |
| 11 |
|
| 12 |
if (data?.sitePlugins?.sitePlugins?.length) { |
| 13 |
const pluginsToInstall = data.sitePlugins.sitePlugins.filter( |
| 14 |
({ wordpressSlug: slug }) => !installedPluginsSlugs?.includes(slug), |
| 15 |
); |
| 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 |
} |
| 24 |
|
| 25 |
return { |
| 26 |
requiredPlugins: data?.selectedPlugins || [], |
| 27 |
isLoading: !error && !data, |
| 28 |
isError: error, |
| 29 |
}; |
| 30 |
}; |
| 31 |
|