@@ -8,16 +8,17 @@ import {
88} from '@mongodb-js/compass-components' ;
99import React from 'react' ;
1010import { connect } from 'react-redux' ;
11+ import type { Dispatch } from 'redux' ;
1112import FieldSelector from './schema-field-selector' ;
1213import FakerMappingSelector from './faker-mapping-selector' ;
1314import { getDefaultFakerMethod } from './script-generation-utils' ;
14- import type {
15- FakerSchema ,
16- FakerFieldMapping ,
17- MockDataGeneratorState ,
18- } from './types' ;
15+ import type { FakerSchema , MockDataGeneratorState } from './types' ;
1916import type { MongoDBFieldType } from '../../schema-analysis-types' ;
2017import { useTelemetry } from '@mongodb-js/compass-telemetry/provider' ;
18+ import {
19+ fakerFieldTypeChanged ,
20+ fakerFieldMethodChanged ,
21+ } from '../../modules/collection-tab' ;
2122
2223const containerStyles = css ( {
2324 display : 'flex' ,
@@ -46,53 +47,39 @@ const schemaEditorLoaderStyles = css({
4647
4748const FakerSchemaEditorContent = ( {
4849 fakerSchema,
50+ originalLlmResponse,
51+ onFieldTypeChanged,
52+ onFieldMethodChanged,
4953} : {
5054 fakerSchema : FakerSchema ;
55+ originalLlmResponse : FakerSchema ;
56+ onFieldTypeChanged : ( fieldPath : string , mongoType : MongoDBFieldType ) => void ;
57+ onFieldMethodChanged : ( fieldPath : string , fakerMethod : string ) => void ;
5158} ) => {
5259 const track = useTelemetry ( ) ;
53- const [ fakerSchemaFormValues , setFakerSchemaFormValues ] =
54- React . useState < FakerSchema > ( fakerSchema ) ;
55-
56- // Store original LLM mappings to restore when reselecting original methods
57- const originalLlmMappings = React . useRef < Record < string , FakerFieldMapping > > (
58- Object . fromEntries (
59- Object . entries ( fakerSchema ) . map ( ( [ field , mapping ] ) => [
60- field ,
61- {
62- ...mapping ,
63- } ,
64- ] )
65- )
66- ) ;
6760
68- const fieldPaths = Object . keys ( fakerSchemaFormValues ) ;
61+ const fieldPaths = Object . keys ( fakerSchema ) ;
6962 const [ activeField , setActiveField ] = React . useState < string > ( fieldPaths [ 0 ] ) ;
7063
71- const activeJsonType = fakerSchemaFormValues [ activeField ] ?. mongoType ;
72- const activeFakerFunction = fakerSchemaFormValues [ activeField ] ?. fakerMethod ;
73- const activeFakerArgs = fakerSchemaFormValues [ activeField ] ?. fakerArgs ;
64+ const activeJsonType = fakerSchema [ activeField ] ?. mongoType ;
65+ const activeFakerFunction = fakerSchema [ activeField ] ?. fakerMethod ;
66+ const activeFakerArgs = fakerSchema [ activeField ] ?. fakerArgs ;
7467
7568 const onJsonTypeSelect = ( newJsonType : MongoDBFieldType ) => {
76- const currentMapping = fakerSchemaFormValues [ activeField ] ;
77- const originalLlmMapping = originalLlmMappings . current [ activeField ] ;
69+ const currentMapping = fakerSchema [ activeField ] ;
70+ const originalLlmMapping = originalLlmResponse [ activeField ] ;
7871
7972 if ( currentMapping ) {
8073 const previousJsonType = currentMapping . mongoType ;
8174 const previousFakerMethod = currentMapping . fakerMethod ;
8275
83- const isSwitchingToOriginalType =
84- originalLlmMapping && newJsonType === originalLlmMapping . mongoType ;
85-
86- const newMapping = isSwitchingToOriginalType
87- ? { ...originalLlmMapping }
88- : {
89- ...currentMapping ,
90- mongoType : newJsonType ,
91- fakerMethod : getDefaultFakerMethod ( newJsonType ) ,
92- fakerArgs : [ ] ,
93- } ;
76+ const newFakerMethod =
77+ originalLlmMapping && newJsonType === originalLlmMapping . mongoType
78+ ? originalLlmMapping . fakerMethod
79+ : getDefaultFakerMethod ( newJsonType ) ;
9480
95- const newFakerMethod = newMapping . fakerMethod ;
81+ onFieldTypeChanged ( activeField , newJsonType ) ;
82+ onFieldMethodChanged ( activeField , newFakerMethod ) ;
9683
9784 track ( 'Mock Data JSON Type Changed' , {
9885 field_name : activeField ,
@@ -101,45 +88,23 @@ const FakerSchemaEditorContent = ({
10188 previous_faker_method : previousFakerMethod ,
10289 new_faker_method : newFakerMethod ,
10390 } ) ;
104-
105- setFakerSchemaFormValues ( {
106- ...fakerSchemaFormValues ,
107- [ activeField ] : newMapping ,
108- } ) ;
10991 }
11092 } ;
11193
11294 const onFakerFunctionSelect = ( newFakerFunction : string ) => {
113- const currentMapping = fakerSchemaFormValues [ activeField ] ;
114- const originalLlmMapping = originalLlmMappings . current [ activeField ] ;
95+ const currentMapping = fakerSchema [ activeField ] ;
11596
11697 if ( currentMapping ) {
11798 const previousFakerMethod = currentMapping . fakerMethod ;
11899
119- const isSwitchingToLlmSuggestion =
120- originalLlmMapping &&
121- currentMapping . mongoType === originalLlmMapping . mongoType &&
122- newFakerFunction === originalLlmMapping . fakerMethod ;
123-
124- const newMapping = isSwitchingToLlmSuggestion
125- ? { ...originalLlmMapping }
126- : {
127- ...currentMapping ,
128- fakerMethod : newFakerFunction ,
129- fakerArgs : [ ] ,
130- } ;
100+ onFieldMethodChanged ( activeField , newFakerFunction ) ;
131101
132102 track ( 'Mock Data Faker Method Changed' , {
133103 field_name : activeField ,
134104 json_type : currentMapping . mongoType ,
135105 previous_faker_method : previousFakerMethod ,
136106 new_faker_method : newFakerFunction ,
137107 } ) ;
138-
139- setFakerSchemaFormValues ( {
140- ...fakerSchemaFormValues ,
141- [ activeField ] : newMapping ,
142- } ) ;
143108 }
144109 } ;
145110
@@ -150,7 +115,6 @@ const FakerSchemaEditorContent = ({
150115 activeField = { activeField }
151116 fields = { fieldPaths }
152117 onFieldSelect = { setActiveField }
153- fakerSchema = { fakerSchemaFormValues }
154118 />
155119 { activeJsonType && activeFakerFunction && (
156120 < FakerMappingSelector
@@ -159,6 +123,11 @@ const FakerSchemaEditorContent = ({
159123 activeFakerArgs = { activeFakerArgs }
160124 onJsonTypeSelect = { onJsonTypeSelect }
161125 onFakerFunctionSelect = { onFakerFunctionSelect }
126+ originalLlmFakerMethod = {
127+ originalLlmResponse [ activeField ] ?. mongoType === activeJsonType
128+ ? originalLlmResponse [ activeField ] ?. fakerMethod
129+ : undefined
130+ }
162131 />
163132 ) }
164133 </ div >
@@ -168,8 +137,12 @@ const FakerSchemaEditorContent = ({
168137
169138const FakerSchemaEditorScreen = ( {
170139 fakerSchemaGenerationState,
140+ onFieldTypeChanged,
141+ onFieldMethodChanged,
171142} : {
172143 fakerSchemaGenerationState : MockDataGeneratorState ;
144+ onFieldTypeChanged : ( fieldPath : string , mongoType : MongoDBFieldType ) => void ;
145+ onFieldMethodChanged : ( fieldPath : string , fakerMethod : string ) => void ;
173146} ) => {
174147 return (
175148 < div data-testid = "faker-schema-editor" className = { containerStyles } >
@@ -197,10 +170,20 @@ const FakerSchemaEditorScreen = ({
197170 { fakerSchemaGenerationState . status === 'completed' && (
198171 < FakerSchemaEditorContent
199172 fakerSchema = { fakerSchemaGenerationState . editedFakerSchema }
173+ originalLlmResponse = { fakerSchemaGenerationState . originalLlmResponse }
174+ onFieldTypeChanged = { onFieldTypeChanged }
175+ onFieldMethodChanged = { onFieldMethodChanged }
200176 />
201177 ) }
202178 </ div >
203179 ) ;
204180} ;
205181
206- export default connect ( ) ( FakerSchemaEditorScreen ) ;
182+ const mapDispatchToProps = ( dispatch : Dispatch ) => ( {
183+ onFieldTypeChanged : ( fieldPath : string , mongoType : MongoDBFieldType ) =>
184+ dispatch ( fakerFieldTypeChanged ( fieldPath , mongoType ) ) ,
185+ onFieldMethodChanged : ( fieldPath : string , fakerMethod : string ) =>
186+ dispatch ( fakerFieldMethodChanged ( fieldPath , fakerMethod ) ) ,
187+ } ) ;
188+
189+ export default connect ( null , mapDispatchToProps ) ( FakerSchemaEditorScreen ) ;
0 commit comments