@@ -2,12 +2,13 @@ import {
22 Banner ,
33 BannerVariant ,
44 Body ,
5+ Code ,
56 css ,
7+ Label ,
68 Option ,
79 palette ,
810 Select ,
911 spacing ,
10- TextInput ,
1112} from '@mongodb-js/compass-components' ;
1213import React from 'react' ;
1314import { UNRECOGNIZED_FAKER_METHOD } from '../../modules/collection-tab' ;
@@ -26,92 +27,11 @@ const labelStyles = css({
2627 fontWeight : 600 ,
2728} ) ;
2829
29- /**
30- * Renders read-only TextInput components for each key-value pair in a faker arguments object.
31- */
32- const getFakerArgsInputFromObject = ( fakerArgsObject : Record < string , any > ) => {
33- return Object . entries ( fakerArgsObject ) . map ( ( [ key , item ] : [ string , any ] ) => {
34- if ( typeof item === 'string' || typeof item === 'boolean' ) {
35- return (
36- < TextInput
37- key = { `faker-arg-${ key } ` }
38- type = "text"
39- label = { key }
40- aria-label = { `Faker Arg ${ key } ` }
41- readOnly
42- value = { item . toString ( ) }
43- />
44- ) ;
45- } else if ( typeof item === 'number' ) {
46- return (
47- < TextInput
48- key = { `faker-arg-${ key } ` }
49- type = "number"
50- label = { key }
51- aria-label = { `Faker Arg ${ key } ` }
52- readOnly
53- value = { item . toString ( ) }
54- />
55- ) ;
56- } else if (
57- Array . isArray ( item ) &&
58- item . length > 0 &&
59- typeof item [ 0 ] === 'string'
60- ) {
61- return (
62- < TextInput
63- key = { `faker-arg-${ key } ` }
64- type = "text"
65- label = { key }
66- aria-label = { `Faker Arg ${ key } ` }
67- readOnly
68- value = { item . join ( ', ' ) }
69- />
70- ) ;
71- }
72- return null ;
73- } ) ;
74- } ;
75-
76- /**
77- * Renders TextInput components for each faker argument based on its type.
78- */
79- const getFakerArgsInput = ( fakerArgs : FakerArg [ ] ) => {
80- return fakerArgs . map ( ( arg , idx ) => {
81- if ( typeof arg === 'string' || typeof arg === 'boolean' ) {
82- return (
83- < TextInput
84- key = { `faker-arg-${ idx } ` }
85- type = "text"
86- label = "Faker Arg"
87- readOnly
88- value = { arg . toString ( ) }
89- />
90- ) ;
91- } else if ( typeof arg === 'number' ) {
92- return (
93- < TextInput
94- key = { `faker-arg-${ idx } ` }
95- type = "number"
96- label = "Faker Arg"
97- readOnly
98- value = { arg . toString ( ) }
99- />
100- ) ;
101- } else if ( typeof arg === 'object' && 'json' in arg ) {
102- // parse the object
103- let parsedArg ;
104- try {
105- parsedArg = JSON . parse ( arg . json ) ;
106- } catch {
107- // If parsing fails, skip rendering this arg
108- return null ;
109- }
110- if ( typeof parsedArg === 'object' ) {
111- return getFakerArgsInputFromObject ( parsedArg ) ;
112- }
113- }
114- } ) ;
30+ const formatFakerFunctionCallWithArgs = (
31+ fakerFunction : string ,
32+ fakerArgs : FakerArg [ ]
33+ ) => {
34+ return `faker.${ fakerFunction } (${ fakerArgs . join ( ', ' ) } )` ;
11535} ;
11636
11737interface Props {
@@ -158,13 +78,28 @@ const FakerMappingSelector = ({
15878 </ Option >
15979 ) ) }
16080 </ Select >
161- { activeFakerFunction === UNRECOGNIZED_FAKER_METHOD && (
81+ { activeFakerFunction === UNRECOGNIZED_FAKER_METHOD ? (
16282 < Banner variant = { BannerVariant . Warning } >
16383 Please select a function or we will default fill this field with the
16484 string "Unrecognized"
16585 </ Banner >
86+ ) : (
87+ < >
88+ < Label htmlFor = "sample-faker-function-call" >
89+ Sample Faker Function Call
90+ </ Label >
91+ < Code
92+ id = "sample-faker-function-call"
93+ language = "javascript"
94+ copyable = { false }
95+ >
96+ { formatFakerFunctionCallWithArgs (
97+ activeFakerFunction ,
98+ activeFakerArgs
99+ ) }
100+ </ Code >
101+ </ >
166102 ) }
167- { getFakerArgsInput ( activeFakerArgs ) }
168103 </ div >
169104 ) ;
170105} ;
0 commit comments