11// @flow
22import { Trans } from '@lingui/macro' ;
3+ import { t } from '@lingui/macro' ;
34import { I18n } from '@lingui/react' ;
45import { type I18n as I18nType } from '@lingui/core' ;
56import * as React from 'react' ;
@@ -8,7 +9,11 @@ import FlatButton from '../../UI/FlatButton';
89import { ExtensionStore } from '.' ;
910import EventsFunctionsExtensionsContext from '../../EventsFunctionsExtensionsLoader/EventsFunctionsExtensionsContext' ;
1011import HelpButton from '../../UI/HelpButton' ;
11- import { importExtension , installExtension } from './InstallExtension' ;
12+ import {
13+ useImportExtension ,
14+ useInstallExtension ,
15+ getRequiredExtensions ,
16+ } from './InstallExtension' ;
1217import DismissableInfoBar from '../../UI/Messages/DismissableInfoBar' ;
1318import { type ExtensionShortHeader } from '../../Utils/GDevelopServices/Extension' ;
1419import AuthenticatedUserContext from '../../Profile/AuthenticatedUserContext' ;
@@ -20,7 +25,9 @@ import { useResponsiveWindowSize } from '../../UI/Responsive/ResponsiveWindowMea
2025import Download from '../../UI/CustomSvgIcons/Download' ;
2126import Add from '../../UI/CustomSvgIcons/Add' ;
2227import ErrorBoundary from '../../UI/ErrorBoundary' ;
23- import useAlertDialog from '../../UI/Alert/useAlertDialog' ;
28+ import { checkRequiredExtensionsUpdate } from '../../AssetStore/ExtensionStore/InstallExtension' ;
29+ import { showErrorBox } from '../../UI/Messages/MessageBox' ;
30+ import { ExtensionStoreContext } from './ExtensionStoreContext' ;
2431
2532type Props = { |
2633 project : gdProject ,
@@ -41,6 +48,11 @@ const ExtensionsSearchDialog = ({
4148 onCreateNew,
4249} : Props ) => {
4350 const { isMobile } = useResponsiveWindowSize ( ) ;
51+ const installExtension = useInstallExtension ( ) ;
52+ const {
53+ translatedExtensionShortHeadersByName : extensionShortHeadersByName ,
54+ } = React . useContext ( ExtensionStoreContext ) ;
55+ const importExtension = useImportExtension ( ) ;
4456 const [ isInstalling , setIsInstalling ] = React . useState ( false ) ;
4557 const [ extensionWasInstalled , setExtensionWasInstalled ] = React . useState (
4658 false
@@ -50,48 +62,75 @@ const ExtensionsSearchDialog = ({
5062 ) ;
5163 const authenticatedUser = React . useContext ( AuthenticatedUserContext ) ;
5264
53- const installDisplayedExtension = addCreateBadgePreHookIfNotClaimed (
65+ const createBadgeFistExtension = addCreateBadgePreHookIfNotClaimed (
5466 authenticatedUser ,
5567 TRIVIAL_FIRST_EXTENSION ,
56- installExtension
68+ ( ) => { }
5769 ) ;
58- const { showConfirmation, showAlert } = useAlertDialog ( ) ;
5970
6071 const installOrImportExtension = async (
6172 i18n : I18nType ,
6273 extensionShortHeader ?: ExtensionShortHeader
63- ) => {
74+ ) : Promise < boolean > => {
6475 setIsInstalling ( true ) ;
6576 try {
66- let installedOrImportedExtensionName : string | null = null ;
67- if ( ! ! extensionShortHeader ) {
77+ if ( extensionShortHeader ) {
78+ // TODO do it for all extensions
6879 onInstallExtension ( extensionShortHeader . name ) ;
69- const wasExtensionInstalledOrImported = await installDisplayedExtension (
70- i18n ,
71- project ,
72- eventsFunctionsExtensionsState ,
73- extensionShortHeader
74- ) ;
75- installedOrImportedExtensionName = wasExtensionInstalledOrImported
76- ? extensionShortHeader . name
77- : null ;
80+ try {
81+ const extensionShortHeaders : Array < ExtensionShortHeader > = [
82+ extensionShortHeader,
83+ ];
84+ const requiredExtensions = getRequiredExtensions(
85+ extensionShortHeaders
86+ );
87+ requiredExtensions.push({
88+ extensionName : extensionShortHeader . name ,
89+ extensionVersion : extensionShortHeader . version ,
90+ } );
91+ const requiredExtensionInstallation = await checkRequiredExtensionsUpdate(
92+ {
93+ requiredExtensions ,
94+ project ,
95+ extensionShortHeadersByName ,
96+ }
97+ );
98+ const wasExtensionInstalled = await installExtension({
99+ project ,
100+ requiredExtensionInstallation ,
101+ userSelectedExtensionNames : [ extensionShortHeader . name ] ,
102+ importedSerializedExtensions : [ ] ,
103+ onExtensionInstalled ,
104+ updateMode : 'all' ,
105+ } );
106+ if (!wasExtensionInstalled) {
107+ return false ;
108+ }
109+ createBadgeFistExtension();
110+ setExtensionWasInstalled(true);
111+ } catch ( rawError ) {
112+ showErrorBox ( {
113+ message : i18n . _ (
114+ t `Unable to download and install the extension and its dependencies. Verify that your internet connection is working or try again later.`
115+ ) ,
116+ rawError,
117+ errorId : 'download-extension-error' ,
118+ } ) ;
119+ return false ;
120+ }
78121 } else {
79- installedOrImportedExtensionName = await importExtension (
122+ const installedOrImportedExtensionNames = await importExtension ( {
80123 i18n,
81- eventsFunctionsExtensionsState ,
82124 project,
83- onInstallExtension ,
84- showConfirmation ,
85- showAlert
86- ) ;
125+ onWillInstallExtension : onInstallExtension ,
126+ onExtensionInstalled,
127+ } ) ;
128+ if ( installedOrImportedExtensionNames . length > 0 ) {
129+ setExtensionWasInstalled ( true ) ;
130+ onExtensionInstalled ( installedOrImportedExtensionNames ) ;
131+ return true ;
132+ }
87133 }
88-
89- if ( installedOrImportedExtensionName ) {
90- setExtensionWasInstalled ( true ) ;
91- onExtensionInstalled ( [ installedOrImportedExtensionName ] ) ;
92- return true ;
93- }
94-
95134 return false ;
96135 } finally {
97136 setIsInstalling ( false ) ;
0 commit comments