@@ -35,22 +35,43 @@ function Customize(props) {
3535 ) ,
3636 [ ]
3737 ) ;
38- const [ rendererJson , setRendererJson ] = useState (
39- inJSON ?? defaultRendererJson
40- ) ;
4138
42- useEffect ( ( ) => {
43- if ( inJSON === undefined || inJSON === null ) {
44- if ( metaGame === "_default" ) {
45- setRendererJson ( defaultRendererJson ) ;
46- } else {
47- fetch ( `https://thumbnails.abstractplay.com/${ metaGame } .json` )
48- . then ( ( res ) => ( res . ok ? res . json ( ) : Promise . reject ( res ) ) )
49- . then ( ( data ) => setRendererJson ( data ) )
50- . catch ( ( ) => {
51- setRendererJson ( defaultRendererJson ) ;
52- } ) ;
39+ const normalizeData = ( input ) => {
40+ try {
41+ let data = typeof input === "string" ? JSON . parse ( input ) : input ;
42+ // Handle potential double stringification
43+ if ( typeof data === "string" ) data = JSON . parse ( data ) ;
44+ // Recursively pick the last item if it's an array
45+ while ( Array . isArray ( data ) && data . length > 0 ) {
46+ data = data [ data . length - 1 ] ;
5347 }
48+ return data ? JSON . stringify ( data , null , 2 ) : null ;
49+ } catch ( e ) {
50+ return null ;
51+ }
52+ } ;
53+
54+ const [ rendererJson , setRendererJson ] = useState ( ( ) => {
55+ const normalized = normalizeData ( inJSON ) ;
56+ return normalized ?? defaultRendererJson ;
57+ } ) ;
58+
59+ useEffect ( ( ) => {
60+ if ( inJSON !== undefined && inJSON !== null ) {
61+ const normalized = normalizeData ( inJSON ) ;
62+ setRendererJson ( normalized ?? defaultRendererJson ) ;
63+ } else if ( metaGame === "_default" ) {
64+ setRendererJson ( defaultRendererJson ) ;
65+ } else {
66+ fetch ( `https://thumbnails.abstractplay.com/${ metaGame } .json` )
67+ . then ( ( res ) => ( res . ok ? res . json ( ) : Promise . reject ( res ) ) )
68+ . then ( ( data ) => {
69+ const normalized = normalizeData ( data ) ;
70+ setRendererJson ( normalized ?? defaultRendererJson ) ;
71+ } )
72+ . catch ( ( ) => {
73+ setRendererJson ( defaultRendererJson ) ;
74+ } ) ;
5475 }
5576 } , [ metaGame , inJSON , defaultRendererJson ] ) ;
5677
@@ -360,7 +381,7 @@ function Customize(props) {
360381 const availableGlyphs = useMemo ( ( ) => {
361382 try {
362383 const json = JSON . parse ( rendererJson ) ;
363- if ( ! json . legend ) return [ ] ;
384+ if ( ! json || ! json . legend ) return [ ] ;
364385 const names = new Set ( ) ;
365386 const processGlyph = ( g ) => {
366387 if ( typeof g === "string" ) {
@@ -500,6 +521,7 @@ function Customize(props) {
500521
501522 try {
502523 const json = JSON . parse ( rendererJson ) ;
524+ console . log ( "Preview JSON:" , json ) ;
503525 const options = {
504526 divid : divId ,
505527 svgid : svgId ,
0 commit comments