@@ -112,20 +112,22 @@ const appsModule = {
112112 formElement . setAttribute ( 'data-app-type' , app ) ;
113113 appPanel . appendChild ( formElement ) ;
114114
115- // Generate the form using SettingsForms module
116- if ( typeof window . SettingsForms !== 'undefined' ) {
117- // Update global settings store for modal access
118- if ( window . huntarrUI ) {
119- if ( ! window . huntarrUI . originalSettings ) {
120- window . huntarrUI . originalSettings = { } ;
115+ // Wait for SettingsForms to be available before proceeding
116+ const initializeForm = ( ) => {
117+ // Generate the form using SettingsForms module
118+ if ( typeof window . SettingsForms !== 'undefined' && window . SettingsForms ) {
119+ // Update global settings store for modal access
120+ if ( window . huntarrUI ) {
121+ if ( ! window . huntarrUI . originalSettings ) {
122+ window . huntarrUI . originalSettings = { } ;
123+ }
124+ window . huntarrUI . originalSettings [ app ] = appSettings ;
121125 }
122- window . huntarrUI . originalSettings [ app ] = appSettings ;
123- }
124126
125- const formFunction = window . SettingsForms [ `generate${ app . charAt ( 0 ) . toUpperCase ( ) } ${ app . slice ( 1 ) } Form` ] ;
126- if ( typeof formFunction === 'function' ) {
127- // Use .call() to set the 'this' context correctly
128- formFunction . call ( window . SettingsForms , formElement , appSettings ) ;
127+ const formFunction = window . SettingsForms [ `generate${ app . charAt ( 0 ) . toUpperCase ( ) } ${ app . slice ( 1 ) } Form` ] ;
128+ if ( typeof formFunction === 'function' ) {
129+ // Use .call() to set the 'this' context correctly
130+ formFunction . call ( window . SettingsForms , formElement , appSettings ) ;
129131
130132 // Update duration displays for this app
131133 if ( typeof window . SettingsForms . updateDurationDisplay === 'function' ) {
@@ -173,6 +175,27 @@ const appsModule = {
173175 console . error ( 'SettingsForms module not found' ) ;
174176 appPanel . innerHTML = '<div class="error-panel">Unable to generate settings form. Please reload the page.</div>' ;
175177 }
178+ } ;
179+
180+ // Check if SettingsForms is already loaded, otherwise wait for it
181+ if ( typeof window . SettingsForms !== 'undefined' && window . SettingsForms ) {
182+ initializeForm ( ) ;
183+ } else {
184+ console . log ( `[Apps] Waiting for SettingsForms module to load for ${ app } ...` ) ;
185+ let attempts = 0 ;
186+ const checkInterval = setInterval ( ( ) => {
187+ attempts ++ ;
188+ if ( typeof window . SettingsForms !== 'undefined' && window . SettingsForms ) {
189+ clearInterval ( checkInterval ) ;
190+ console . log ( `[Apps] SettingsForms module loaded, initializing ${ app } form` ) ;
191+ initializeForm ( ) ;
192+ } else if ( attempts > 50 ) { // 5 seconds timeout (50 * 100ms)
193+ clearInterval ( checkInterval ) ;
194+ console . error ( 'SettingsForms module failed to load after 5 seconds' ) ;
195+ appPanel . innerHTML = '<div class="error-panel">Unable to generate settings form. Please reload the page.</div>' ;
196+ }
197+ } , 100 ) ;
198+ }
176199 } )
177200 . catch ( error => {
178201 console . error ( `Error loading ${ app } settings:` , error ) ;
0 commit comments