Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@ import {
SpinLoaderWithLabel,
} from '@mongodb-js/compass-components';
import React from 'react';
import { connect } from 'react-redux';
import FieldSelector from './schema-field-selector';
import FakerMappingSelector from './faker-mapping-selector';
import type { FakerSchema, MockDataGeneratorState } from './types';
import type { MongoDBFieldType } from '@mongodb-js/compass-generative-ai';
import {
fakerFieldTypeChanged,
fakerFieldMethodChanged,
type FakerFieldTypeChangedAction,
type FakerFieldMethodChangedAction,
} from '../../modules/collection-tab';

type FakerSchemaDispatch = (
action: FakerFieldTypeChangedAction | FakerFieldMethodChangedAction
) => void;

const containerStyles = css({
display: 'flex',
Expand Down Expand Up @@ -52,48 +63,35 @@ const schemaEditorLoaderStyles = css({
const FakerSchemaEditorContent = ({
fakerSchema,
onSchemaConfirmed,
dispatch,
}: {
fakerSchema: FakerSchema;
onSchemaConfirmed: (isConfirmed: boolean) => void;
dispatch: FakerSchemaDispatch;
}) => {
const [fakerSchemaFormValues, setFakerSchemaFormValues] =
React.useState<FakerSchema>(fakerSchema);

const fieldPaths = Object.keys(fakerSchemaFormValues);
const fieldPaths = Object.keys(fakerSchema);
const [activeField, setActiveField] = React.useState<string>(fieldPaths[0]);

const activeJsonType = fakerSchemaFormValues[activeField]?.mongoType;
const activeFakerFunction = fakerSchemaFormValues[activeField]?.fakerMethod;
const activeFakerArgs = fakerSchemaFormValues[activeField]?.fakerArgs;
const activeJsonType = fakerSchema[activeField]?.mongoType;
const activeFakerFunction = fakerSchema[activeField]?.fakerMethod;
const activeFakerArgs = fakerSchema[activeField]?.fakerArgs;

const resetIsSchemaConfirmed = () => {
onSchemaConfirmed(false);
};

const onJsonTypeSelect = (newJsonType: MongoDBFieldType) => {
const currentMapping = fakerSchemaFormValues[activeField];
const currentMapping = fakerSchema[activeField];
if (currentMapping) {
setFakerSchemaFormValues({
...fakerSchemaFormValues,
[activeField]: {
...currentMapping,
mongoType: newJsonType,
},
});
dispatch(fakerFieldTypeChanged(activeField, newJsonType));
resetIsSchemaConfirmed();
}
};

const onFakerFunctionSelect = (newFakerFunction: string) => {
const currentMapping = fakerSchemaFormValues[activeField];
const currentMapping = fakerSchema[activeField];
if (currentMapping) {
setFakerSchemaFormValues({
...fakerSchemaFormValues,
[activeField]: {
...currentMapping,
fakerMethod: newFakerFunction,
},
});
dispatch(fakerFieldMethodChanged(activeField, newFakerFunction));
resetIsSchemaConfirmed();
}
};
Expand Down Expand Up @@ -131,10 +129,11 @@ const FakerSchemaEditorContent = ({
const FakerSchemaEditorScreen = ({
onSchemaConfirmed,
fakerSchemaGenerationState,
dispatch,
}: {
isSchemaConfirmed: boolean;
onSchemaConfirmed: (isConfirmed: boolean) => void;
fakerSchemaGenerationState: MockDataGeneratorState;
dispatch: FakerSchemaDispatch;
}) => {
return (
<div data-testid="faker-schema-editor" className={containerStyles}>
Expand All @@ -160,12 +159,13 @@ const FakerSchemaEditorScreen = ({
)}
{fakerSchemaGenerationState.status === 'completed' && (
<FakerSchemaEditorContent
fakerSchema={fakerSchemaGenerationState.fakerSchema}
fakerSchema={fakerSchemaGenerationState.editedFakerSchema}
onSchemaConfirmed={onSchemaConfirmed}
dispatch={dispatch}
/>
)}
</div>
);
};

export default FakerSchemaEditorScreen;
export default connect()(FakerSchemaEditorScreen);
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,15 @@ describe('MockDataGeneratorModal', () => {
currentStep: MockDataGeneratorStep.GENERATE_DATA,
fakerSchemaGeneration: {
status: 'completed',
fakerSchema: {
originalLlmResponse: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
probability: 1.0,
mongoType: 'String',
},
},
editedFakerSchema: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
Expand All @@ -848,7 +856,15 @@ describe('MockDataGeneratorModal', () => {
currentStep: MockDataGeneratorStep.GENERATE_DATA,
fakerSchemaGeneration: {
status: 'completed',
fakerSchema: {
originalLlmResponse: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
probability: 1.0,
mongoType: 'String',
},
},
editedFakerSchema: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
Expand All @@ -872,7 +888,15 @@ describe('MockDataGeneratorModal', () => {
currentStep: MockDataGeneratorStep.GENERATE_DATA,
fakerSchemaGeneration: {
status: 'completed',
fakerSchema: {
originalLlmResponse: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
probability: 1.0,
mongoType: 'String',
},
},
editedFakerSchema: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
Expand Down Expand Up @@ -919,7 +943,15 @@ describe('MockDataGeneratorModal', () => {
connectionInfo: atlasConnectionInfo,
fakerSchemaGeneration: {
status: 'completed',
fakerSchema: {
originalLlmResponse: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
probability: 1.0,
mongoType: 'String',
},
},
editedFakerSchema: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
Expand Down Expand Up @@ -971,7 +1003,15 @@ describe('MockDataGeneratorModal', () => {
currentStep: MockDataGeneratorStep.GENERATE_DATA,
fakerSchemaGeneration: {
status: 'completed',
fakerSchema: {
originalLlmResponse: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
probability: 1.0,
mongoType: 'String',
},
},
editedFakerSchema: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
Expand Down Expand Up @@ -1001,7 +1041,21 @@ describe('MockDataGeneratorModal', () => {
currentStep: MockDataGeneratorStep.GENERATE_DATA,
fakerSchemaGeneration: {
status: 'completed',
fakerSchema: {
originalLlmResponse: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
probability: 1.0,
mongoType: 'String',
},
email: {
fakerMethod: 'internet.email',
fakerArgs: [],
probability: 1.0,
mongoType: 'String',
},
},
editedFakerSchema: {
name: {
fakerMethod: 'person.firstName',
fakerArgs: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ const MockDataGeneratorModal = ({
case MockDataGeneratorStep.SCHEMA_EDITOR:
return (
<FakerSchemaEditorScreen
isSchemaConfirmed={isSchemaConfirmed}
onSchemaConfirmed={setIsSchemaConfirmed}
fakerSchemaGenerationState={fakerSchemaGenerationState}
/>
Expand Down Expand Up @@ -111,7 +110,6 @@ const MockDataGeneratorModal = ({
}, [
currentStep,
fakerSchemaGenerationState,
isSchemaConfirmed,
documentCount,
setDocumentCount,
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ const mapStateToProps = (state: CollectionState) => {
return {
fakerSchema:
fakerSchemaGeneration.status === 'completed'
? fakerSchemaGeneration.fakerSchema
? fakerSchemaGeneration.editedFakerSchema
: null,
namespace,
arrayLengthMap:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ type MockDataGeneratorInProgressState = {

type MockDataGeneratorCompletedState = {
status: 'completed';
fakerSchema: FakerSchema;
originalLlmResponse: FakerSchema; // Immutable LLM response
Copy link
Collaborator

@kpamaran kpamaran Oct 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typing as Readonly and/or using Object.freeze when it's created will help enforce immutability

editedFakerSchema: FakerSchema; // User-modified version
requestId: string;
};

Expand Down
Loading
Loading