@@ -163,15 +163,17 @@ const stringifyWithFunctions = (string) =>
163163 . replace ( / " < F U N C T I O N / g, '' )
164164 . replace ( / F U N C T I O N > " / g, '' ) ;
165165
166- const ComponentExample = ( { variants, schema, activeMapper, component, schemaVariants } ) => {
167- const [ variant , setVariant ] = useState ( 'basic' ) ;
166+ const ComponentExample = ( { variants, schema, activeMapper, component, schemaVariants, activeSchema } ) => {
168167 const [ expanded , setExpanded ] = useState ( true ) ;
169168
170169 const { pathname, push } = useRouter ( ) ;
171170 const classes = useStyles ( ) ;
172171
173172 const availableVariants = schemaVariants ?. [ activeMapper ] ;
174- const selectedSchema = availableVariants ?. find ( ( { value } ) => value === variant ) ?. schema || schema ;
173+ const selectedSchema =
174+ availableVariants ?. find ( ( { value } ) => value === activeSchema ) ?. schema ||
175+ availableVariants ?. find ( ( { value } ) => value === 'basic' ) ?. schema ||
176+ schema ;
175177 const basicConfiguration = {
176178 ...project ,
177179 dependencies : metadata [ activeMapper ] . dependencies ,
@@ -185,35 +187,23 @@ const ComponentExample = ({ variants, schema, activeMapper, component, schemaVar
185187 const basicEditorSettings = { height : '100%' , hideNavigation : true , forceEmbedLayout : true , openFile : 'schema.js' } ;
186188
187189 useEffect ( ( ) => {
188- if ( availableVariants && ! availableVariants . map ( ( { value } ) => value ) . includes ( variant ) ) {
189- setVariant ( ' basic' ) ;
190+ if ( activeSchema && ! availableVariants ?. find ( ( { value } ) => value === activeSchema ) ) {
191+ push ( ` ${ pathname } ?mapper= ${ activeMapper } &schema= basic` ) ;
190192 }
191193
192194 sdk . embedProject ( 'code-target' , basicConfiguration , basicEditorSettings ) ;
193- } , [ activeMapper , schema ] ) ;
194-
195- const handleVariantChange = ( _e , newVariant ) => {
196- setVariant ( newVariant ) ;
197-
198- const schema = availableVariants . find ( ( { value } ) => value === newVariant ) . schema ;
199-
200- sdk . embedProject (
201- 'code-target' ,
202- { ...basicConfiguration , files : { ...basicConfiguration . files , 'schema.js' : `export default ${ stringifyWithFunctions ( schema ) } ;` } } ,
203- basicEditorSettings
204- ) ;
205- } ;
195+ } , [ activeMapper , schema , activeSchema ] ) ;
206196
207197 const renderMapperTabs = ( ) =>
208198 avalableMappers . map ( ( { title, mapper } ) => (
209199 < Tab
210200 key = { mapper }
211201 value = { mapper }
212- onClick = { ( ) => push ( `${ pathname } ?mapper=${ mapper } ` ) }
202+ onClick = { ( ) => push ( `${ pathname } ?mapper=${ mapper } ${ activeSchema ? `&schema= ${ activeSchema } ` : '' } ` ) }
213203 className = { clsx ( classes . tab , { active : activeMapper === mapper } ) }
214204 label = {
215- < Link href = { `${ pathname } ?mapper=${ mapper } ` } >
216- < a href = { `${ pathname } ?mapper=${ mapper } ` } className = { classes . tabLink } >
205+ < Link href = { `${ pathname } ?mapper=${ mapper } ${ activeSchema ? `&schema= ${ activeSchema } ` : '' } ` } >
206+ < a href = { `${ pathname } ?mapper=${ mapper } ${ activeSchema ? `&schema= ${ activeSchema } ` : '' } ` } className = { classes . tabLink } >
217207 { title }
218208 </ a >
219209 </ Link >
@@ -292,12 +282,22 @@ const ComponentExample = ({ variants, schema, activeMapper, component, schemaVar
292282 < div className = { classes . spinnerCheat } >
293283 < Tabs
294284 hidden = { ! availableVariants }
295- value = { variant }
285+ value = { activeSchema || 'basic' }
296286 className = { clsx ( availableVariants && classes . variantTabs , availableVariants ? 'longer' : classes . hidden ) }
297- onChange = { handleVariantChange }
298287 >
299288 { ( availableVariants || [ ] ) . map ( ( variant ) => (
300- < Tab label = { variant . label } value = { variant . value } key = { variant . value } />
289+ < Tab
290+ onClick = { ( ) => push ( `${ pathname } ?mapper=${ activeMapper } &schema=${ variant . value } ` ) }
291+ label = {
292+ < Link href = { `${ pathname } ?mapper=${ activeMapper } &schema=${ variant . value } ` } >
293+ < a href = { `${ pathname } ?mapper=${ activeMapper } &schema=${ variant . value } ` } className = { classes . tabLink } >
294+ { variant . label }
295+ </ a >
296+ </ Link >
297+ }
298+ value = { variant . value }
299+ key = { variant . value }
300+ />
301301 ) ) }
302302 </ Tabs >
303303 < div id = "code-target" className = "pepa" > </ div >
@@ -342,7 +342,8 @@ ComponentExample.propTypes = {
342342 required : PropTypes . bool
343343 } )
344344 ) . isRequired ,
345- schemaVariants : PropTypes . object
345+ schemaVariants : PropTypes . object ,
346+ activeSchema : PropTypes . string
346347} ;
347348
348349export default ComponentExample ;
0 commit comments