|
2 | 2 | * External dependencies |
3 | 3 | */ |
4 | 4 | import { useAnalytics } from '@automattic/jetpack-shared-extension-utils'; |
| 5 | +import { useDispatch } from '@wordpress/data'; |
5 | 6 | import { useState, useCallback } from '@wordpress/element'; |
| 7 | +import { store as noticesStore } from '@wordpress/notices'; |
6 | 8 | /** |
7 | 9 | * Internal dependencies |
8 | 10 | */ |
| 11 | +import useConfigValue from '../../../../../hooks/use-config-value.ts'; |
9 | 12 | import { installAndActivatePlugin, activatePlugin } from '../../../util/plugin-management.js'; |
10 | 13 |
|
| 14 | +type NoticeOptions = Record< string, unknown >; |
| 15 | + |
| 16 | +type NoticeConfig = { |
| 17 | + message?: string; |
| 18 | + options?: NoticeOptions; |
| 19 | +}; |
| 20 | + |
| 21 | +type SuccessNotices = { |
| 22 | + install?: NoticeConfig; |
| 23 | + activate?: NoticeConfig; |
| 24 | +}; |
| 25 | + |
| 26 | +type UsePluginInstallationArgs = { |
| 27 | + slug: string; |
| 28 | + pluginPath: string; |
| 29 | + isInstalled: boolean; |
| 30 | + trackEventName?: string; |
| 31 | + trackEventProps?: Record< string, unknown >; |
| 32 | + onSuccess?: () => void | Promise< void >; |
| 33 | + successNotices?: SuccessNotices; |
| 34 | + errorNotice?: NoticeConfig; |
| 35 | +}; |
| 36 | + |
11 | 37 | type PluginInstallation = { |
12 | 38 | isInstalling: boolean; |
13 | 39 | installPlugin: () => Promise< boolean >; |
| 40 | + canInstallPlugins: boolean; |
| 41 | + canActivatePlugins: boolean; |
14 | 42 | }; |
15 | 43 |
|
16 | 44 | /** |
17 | 45 | * Custom hook to handle plugin installation and activation flows. |
18 | 46 | * |
19 | | - * @param {string} slug - The plugin slug (e.g., 'akismet') |
20 | | - * @param {string} pluginPath - The plugin path (e.g., 'akismet/akismet') |
21 | | - * @param {boolean} isInstalled - Whether the plugin is installed |
22 | | - * @param {string} tracksEventName - The name of the tracks event to record |
23 | | - * @return {object} Plugin installation states and handlers |
| 47 | + * @param {UsePluginInstallationArgs} args - Hook arguments. |
| 48 | + * @return {PluginInstallation} Plugin installation states and handlers. |
24 | 49 | */ |
25 | | -export const usePluginInstallation = ( |
26 | | - slug: string, |
27 | | - pluginPath: string, |
28 | | - isInstalled: boolean, |
29 | | - tracksEventName: string |
30 | | -): PluginInstallation => { |
| 50 | +export const usePluginInstallation = ( { |
| 51 | + slug, |
| 52 | + pluginPath, |
| 53 | + isInstalled, |
| 54 | + trackEventName, |
| 55 | + trackEventProps = {}, |
| 56 | + onSuccess, |
| 57 | + successNotices, |
| 58 | + errorNotice, |
| 59 | +}: UsePluginInstallationArgs ): PluginInstallation => { |
31 | 60 | const [ isInstalling, setIsInstalling ] = useState( false ); |
32 | 61 | const { tracks } = useAnalytics(); |
| 62 | + const { createSuccessNotice, createErrorNotice } = useDispatch( noticesStore ); |
| 63 | + const canInstallPlugins = useConfigValue( 'canInstallPlugins' ); |
| 64 | + const canActivatePlugins = useConfigValue( 'canActivatePlugins' ); |
33 | 65 |
|
34 | 66 | const installPlugin = useCallback( async () => { |
35 | 67 | setIsInstalling( true ); |
36 | 68 |
|
37 | | - if ( tracksEventName ) { |
38 | | - tracks.recordEvent( tracksEventName, { |
39 | | - screen: 'block-editor', |
| 69 | + if ( trackEventName ) { |
| 70 | + tracks.recordEvent( trackEventName, { |
40 | 71 | intent: isInstalled ? 'activate-plugin' : 'install-plugin', |
| 72 | + ...( trackEventProps ?? {} ), |
41 | 73 | } ); |
42 | 74 | } |
43 | 75 |
|
44 | 76 | try { |
45 | 77 | if ( isInstalled ) { |
| 78 | + if ( ! canActivatePlugins ) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + |
46 | 82 | await activatePlugin( pluginPath ); |
47 | 83 | } else { |
| 84 | + if ( ! canInstallPlugins ) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
48 | 88 | await installAndActivatePlugin( slug ); |
49 | 89 | } |
| 90 | + |
| 91 | + const successNoticeConfig = isInstalled ? successNotices?.activate : successNotices?.install; |
| 92 | + |
| 93 | + if ( successNoticeConfig?.message ) { |
| 94 | + createSuccessNotice( successNoticeConfig.message, successNoticeConfig.options ); |
| 95 | + } |
| 96 | + |
| 97 | + if ( onSuccess ) { |
| 98 | + await onSuccess(); |
| 99 | + } |
| 100 | + |
50 | 101 | return true; |
51 | | - } catch { |
52 | | - // Let the component handle the error state |
| 102 | + } catch ( error ) { |
| 103 | + if ( errorNotice ) { |
| 104 | + const noticeMessage = |
| 105 | + errorNotice.message || ( error instanceof Error ? error.message : undefined ); |
| 106 | + |
| 107 | + if ( noticeMessage ) { |
| 108 | + createErrorNotice( noticeMessage, errorNotice.options ); |
| 109 | + } |
| 110 | + } |
| 111 | + |
53 | 112 | return false; |
54 | 113 | } finally { |
55 | 114 | setIsInstalling( false ); |
56 | 115 | } |
57 | | - }, [ slug, pluginPath, isInstalled, tracks, tracksEventName ] ); |
| 116 | + }, [ |
| 117 | + trackEventName, |
| 118 | + tracks, |
| 119 | + isInstalled, |
| 120 | + trackEventProps, |
| 121 | + successNotices?.activate, |
| 122 | + successNotices?.install, |
| 123 | + onSuccess, |
| 124 | + canActivatePlugins, |
| 125 | + pluginPath, |
| 126 | + canInstallPlugins, |
| 127 | + slug, |
| 128 | + createSuccessNotice, |
| 129 | + errorNotice, |
| 130 | + createErrorNotice, |
| 131 | + ] ); |
58 | 132 |
|
59 | 133 | return { |
60 | 134 | isInstalling, |
61 | 135 | installPlugin, |
| 136 | + canInstallPlugins, |
| 137 | + canActivatePlugins, |
62 | 138 | }; |
63 | 139 | }; |
0 commit comments