|
| 1 | +import { |
| 2 | + Body, |
| 3 | + Button, |
| 4 | + ButtonSize, |
| 5 | + ButtonVariant, |
| 6 | + css, |
| 7 | + Link, |
| 8 | + palette, |
| 9 | + spacing, |
| 10 | + SpinLoaderWithLabel, |
| 11 | +} from '@mongodb-js/compass-components'; |
1 | 12 | import React from 'react';
|
| 13 | +import FieldSelector from './schema-field-selector'; |
| 14 | +import FakerMappingSelector from './faker-mapping-selector'; |
| 15 | +import type { FakerSchemaMapping, MockDataGeneratorState } from './types'; |
2 | 16 |
|
3 |
| -// TODO: More to come from CLOUDP-333853, CLOUDP-333854 |
4 |
| -const FakerSchemaEditorScreen = () => { |
| 17 | +const containerStyles = css({ |
| 18 | + display: 'flex', |
| 19 | + flexDirection: 'column', |
| 20 | + gap: spacing[400], |
| 21 | +}); |
| 22 | + |
| 23 | +const innerEditorStyles = css({ |
| 24 | + display: 'flex', |
| 25 | + flexDirection: 'row', |
| 26 | + justifyContent: 'space-between', |
| 27 | +}); |
| 28 | + |
| 29 | +const titleStyles = css({ |
| 30 | + color: palette.black, |
| 31 | + fontWeight: 600, |
| 32 | + fontSize: '16px', |
| 33 | + lineHeight: '20px', |
| 34 | + marginBottom: 0, |
| 35 | +}); |
| 36 | + |
| 37 | +const bodyStyles = css({ |
| 38 | + color: palette.gray.dark1, |
| 39 | +}); |
| 40 | + |
| 41 | +const confirmMappingsButtonStyles = css({ |
| 42 | + width: '200px', |
| 43 | +}); |
| 44 | + |
| 45 | +const schemaEditorLoaderStyles = css({ |
| 46 | + display: 'flex', |
| 47 | + alignItems: 'center', |
| 48 | + justifyContent: 'center', |
| 49 | +}); |
| 50 | + |
| 51 | +const FakerSchemaEditorContent = ({ |
| 52 | + fakerSchemaMappings, |
| 53 | + onSchemaConfirmed, |
| 54 | +}: { |
| 55 | + fakerSchemaMappings: FakerSchemaMapping[]; |
| 56 | + onSchemaConfirmed: (isConfirmed: boolean) => void; |
| 57 | +}) => { |
| 58 | + const [fakerSchemaFormValues, setFakerSchemaFormValues] = |
| 59 | + React.useState<Array<FakerSchemaMapping>>(fakerSchemaMappings); |
| 60 | + const [activeField, setActiveField] = React.useState<string>( |
| 61 | + fakerSchemaFormValues[0].fieldPath |
| 62 | + ); |
| 63 | + |
| 64 | + const activeJsonType = fakerSchemaFormValues.find( |
| 65 | + (mapping) => mapping.fieldPath === activeField |
| 66 | + )?.mongoType; |
| 67 | + const activeFakerFunction = fakerSchemaFormValues.find( |
| 68 | + (mapping) => mapping.fieldPath === activeField |
| 69 | + )?.fakerMethod; |
| 70 | + |
| 71 | + const resetIsSchemaConfirmed = () => { |
| 72 | + onSchemaConfirmed(false); |
| 73 | + }; |
| 74 | + |
| 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 | + ); |
| 86 | + resetIsSchemaConfirmed(); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + 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 | + ); |
| 101 | + resetIsSchemaConfirmed(); |
| 102 | + } |
| 103 | + }; |
| 104 | + |
| 105 | + return ( |
| 106 | + <> |
| 107 | + <div className={innerEditorStyles}> |
| 108 | + <FieldSelector |
| 109 | + activeField={activeField} |
| 110 | + fields={fakerSchemaFormValues.map((mapping) => mapping.fieldPath)} |
| 111 | + onFieldSelect={setActiveField} |
| 112 | + /> |
| 113 | + {activeJsonType && activeFakerFunction && ( |
| 114 | + <FakerMappingSelector |
| 115 | + activeJsonType={activeJsonType} |
| 116 | + activeFakerFunction={activeFakerFunction} |
| 117 | + onJsonTypeSelect={onJsonTypeSelect} |
| 118 | + onFakerFunctionSelect={onFakerFunctionSelect} |
| 119 | + /> |
| 120 | + )} |
| 121 | + </div> |
| 122 | + <Button |
| 123 | + size={ButtonSize.Small} |
| 124 | + className={confirmMappingsButtonStyles} |
| 125 | + variant={ButtonVariant.Primary} |
| 126 | + onClick={() => onSchemaConfirmed(true)} |
| 127 | + > |
| 128 | + Confirm mappings |
| 129 | + </Button> |
| 130 | + </> |
| 131 | + ); |
| 132 | +}; |
| 133 | + |
| 134 | +const FakerSchemaEditorScreen = ({ |
| 135 | + onSchemaConfirmed, |
| 136 | + fakerSchemaGenerationState, |
| 137 | +}: { |
| 138 | + isSchemaConfirmed: boolean; |
| 139 | + onSchemaConfirmed: (isConfirmed: boolean) => void; |
| 140 | + fakerSchemaGenerationState: MockDataGeneratorState; |
| 141 | +}) => { |
5 | 142 | return (
|
6 |
| - <div data-testid="faker-schema-editor"> |
7 |
| - Schema Editor Content Placeholder |
| 143 | + <div data-testid="faker-schema-editor" className={containerStyles}> |
| 144 | + <div> |
| 145 | + <h3 className={titleStyles}> |
| 146 | + Confirm Field to Faker Function Mappings |
| 147 | + </h3> |
| 148 | + <Body className={bodyStyles}> |
| 149 | + We have sampled your collection and created a schema based on your |
| 150 | + documents. That schema has been sent to an LLM and it has returned the |
| 151 | + following mapping between your schema fields and{' '} |
| 152 | + <Link href="https://fakerjs.dev/api/faker.html">faker functions</Link> |
| 153 | + . |
| 154 | + </Body> |
| 155 | + </div> |
| 156 | + {fakerSchemaGenerationState.status === 'in-progress' && ( |
| 157 | + <div |
| 158 | + data-testid="faker-schema-editor-loader" |
| 159 | + className={schemaEditorLoaderStyles} |
| 160 | + > |
| 161 | + <SpinLoaderWithLabel progressText="Processing Documents..." /> |
| 162 | + </div> |
| 163 | + )} |
| 164 | + {fakerSchemaGenerationState.status === 'completed' && ( |
| 165 | + <FakerSchemaEditorContent |
| 166 | + fakerSchemaMappings={fakerSchemaGenerationState.fakerSchema} |
| 167 | + onSchemaConfirmed={onSchemaConfirmed} |
| 168 | + /> |
| 169 | + )} |
8 | 170 | </div>
|
9 | 171 | );
|
10 | 172 | };
|
|
0 commit comments