11import "./ui/JavaScriptSnippet.css" ;
22import { createElement , useEffect , useState } from "react" ;
33
4- export function JavaScriptSnippet ( { attributeList, jsCode, waitForAttributeList , ...rest } ) {
4+ export function JavaScriptSnippet ( { attributeList, jsCode, ...rest } ) {
55 const [ canRender , setCanRender ] = useState ( false ) ;
66 const [ javaScriptString , setJavaScriptString ] = useState ( [ ] ) ;
77 const widgetName = rest . name || "" ;
@@ -28,7 +28,18 @@ export function JavaScriptSnippet({ attributeList, jsCode, waitForAttributeList,
2828 useEffect ( ( ) => {
2929 let JSArray = jsCode ;
3030
31- if ( waitForAttributeList && attributeList . length ) {
31+ // Auto-detect if we need to wait based on attributeList having items and any pending attributes
32+ const hasAttributes = attributeList && attributeList . length > 0 ;
33+ const allAttributesAvailable = hasAttributes
34+ ? attributeList . every ( attr => attr . jsAttribute . status === "available" )
35+ : true ;
36+
37+ if ( hasAttributes ) {
38+ if ( ! allAttributesAvailable ) {
39+ setCanRender ( false ) ;
40+ return ; // Exit early if attributes aren't ready
41+ }
42+
3243 attributeList . map ( attr => {
3344 if ( attr . jsAttribute . status === "available" ) {
3445 const useValue =
@@ -58,17 +69,7 @@ export function JavaScriptSnippet({ attributeList, jsCode, waitForAttributeList,
5869
5970 JSArray = JSArray . split ( "this" ) . join ( `'${ widgetName } _${ uid [ 0 ] } '` ) ;
6071 setJavaScriptString ( JSArray ) ;
61-
62- if ( ! waitForAttributeList && ! attributeList . length ) {
63- setCanRender ( true ) ;
64- }
65-
66- // clean-up function
67- return ( ) => {
68- if ( waitForAttributeList && attributeList . length ) {
69- setCanRender ( true ) ;
70- }
71- } ;
72+ setCanRender ( true ) ; // Only set to true when everything is ready
7273 } , [ attributeList , jsCode , widgetName ] ) ;
7374
7475 if ( canRender ) {
0 commit comments