@@ -12,7 +12,8 @@ import {
12
12
import React from 'react' ;
13
13
import FieldSelector from './schema-field-selector' ;
14
14
import FakerMappingSelector from './faker-mapping-selector' ;
15
- import type { FakerSchemaMapping , MockDataGeneratorState } from './types' ;
15
+ import type { FakerSchema , MockDataGeneratorState } from './types' ;
16
+ import type { MongoDBFieldType } from '@mongodb-js/compass-generative-ai' ;
16
17
17
18
const containerStyles = css ( {
18
19
display : 'flex' ,
@@ -49,55 +50,49 @@ const schemaEditorLoaderStyles = css({
49
50
} ) ;
50
51
51
52
const FakerSchemaEditorContent = ( {
52
- fakerSchemaMappings ,
53
+ fakerSchema ,
53
54
onSchemaConfirmed,
54
55
} : {
55
- fakerSchemaMappings : FakerSchemaMapping [ ] ;
56
+ fakerSchema : FakerSchema ;
56
57
onSchemaConfirmed : ( isConfirmed : boolean ) => void ;
57
58
} ) => {
58
59
const [ fakerSchemaFormValues , setFakerSchemaFormValues ] =
59
- React . useState < Array < FakerSchemaMapping > > ( fakerSchemaMappings ) ;
60
- const [ activeField , setActiveField ] = React . useState < string > (
61
- fakerSchemaFormValues [ 0 ] . fieldPath
62
- ) ;
60
+ React . useState < FakerSchema > ( fakerSchema ) ;
61
+
62
+ const fieldPaths = Object . keys ( fakerSchemaFormValues ) ;
63
+ const [ activeField , setActiveField ] = React . useState < string > ( fieldPaths [ 0 ] ) ;
63
64
64
- const activeJsonType = fakerSchemaFormValues . find (
65
- ( mapping ) => mapping . fieldPath === activeField
66
- ) ?. mongoType ;
67
- const activeFakerFunction = fakerSchemaFormValues . find (
68
- ( mapping ) => mapping . fieldPath === activeField
69
- ) ?. fakerMethod ;
65
+ const activeJsonType = fakerSchemaFormValues [ activeField ] ?. mongoType ;
66
+ const activeFakerFunction = fakerSchemaFormValues [ activeField ] ?. fakerMethod ;
70
67
71
68
const resetIsSchemaConfirmed = ( ) => {
72
69
onSchemaConfirmed ( false ) ;
73
70
} ;
74
71
75
- const onJsonTypeSelect = ( newJsonType : string ) => {
76
- const updatedFakerFieldMapping = fakerSchemaFormValues . find (
77
- ( mapping ) => mapping . fieldPath === activeField
78
- ) ;
79
- if ( updatedFakerFieldMapping ) {
80
- updatedFakerFieldMapping . mongoType = newJsonType ;
81
- setFakerSchemaFormValues (
82
- fakerSchemaFormValues . map ( ( mapping ) =>
83
- mapping . fieldPath === activeField ? updatedFakerFieldMapping : mapping
84
- )
85
- ) ;
72
+ const onJsonTypeSelect = ( newJsonType : MongoDBFieldType ) => {
73
+ const currentMapping = fakerSchemaFormValues [ activeField ] ;
74
+ if ( currentMapping ) {
75
+ setFakerSchemaFormValues ( {
76
+ ...fakerSchemaFormValues ,
77
+ [ activeField ] : {
78
+ ...currentMapping ,
79
+ mongoType : newJsonType ,
80
+ } ,
81
+ } ) ;
86
82
resetIsSchemaConfirmed ( ) ;
87
83
}
88
84
} ;
89
85
90
86
const onFakerFunctionSelect = ( newFakerFunction : string ) => {
91
- const updatedFakerFieldMapping = fakerSchemaFormValues . find (
92
- ( mapping ) => mapping . fieldPath === activeField
93
- ) ;
94
- if ( updatedFakerFieldMapping ) {
95
- updatedFakerFieldMapping . fakerMethod = newFakerFunction ;
96
- setFakerSchemaFormValues (
97
- fakerSchemaFormValues . map ( ( mapping ) =>
98
- mapping . fieldPath === activeField ? updatedFakerFieldMapping : mapping
99
- )
100
- ) ;
87
+ const currentMapping = fakerSchemaFormValues [ activeField ] ;
88
+ if ( currentMapping ) {
89
+ setFakerSchemaFormValues ( {
90
+ ...fakerSchemaFormValues ,
91
+ [ activeField ] : {
92
+ ...currentMapping ,
93
+ fakerMethod : newFakerFunction ,
94
+ } ,
95
+ } ) ;
101
96
resetIsSchemaConfirmed ( ) ;
102
97
}
103
98
} ;
@@ -107,7 +102,7 @@ const FakerSchemaEditorContent = ({
107
102
< div className = { innerEditorStyles } >
108
103
< FieldSelector
109
104
activeField = { activeField }
110
- fields = { fakerSchemaFormValues . map ( ( mapping ) => mapping . fieldPath ) }
105
+ fields = { fieldPaths }
111
106
onFieldSelect = { setActiveField }
112
107
/>
113
108
{ activeJsonType && activeFakerFunction && (
@@ -163,7 +158,7 @@ const FakerSchemaEditorScreen = ({
163
158
) }
164
159
{ fakerSchemaGenerationState . status === 'completed' && (
165
160
< FakerSchemaEditorContent
166
- fakerSchemaMappings = { fakerSchemaGenerationState . fakerSchema }
161
+ fakerSchema = { fakerSchemaGenerationState . fakerSchema }
167
162
onSchemaConfirmed = { onSchemaConfirmed }
168
163
/>
169
164
) }
0 commit comments