Skip to content

Commit 9ab3491

Browse files
committed
fix: make form optional, fix type checking
1 parent bc51dcd commit 9ab3491

9 files changed

+235
-52
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Metadata
2+
- Module Name: Sample Module
3+
- Related Modules: Module A, Module B
4+
- Version: 1.0.0
5+
- Description: This is a sample module for testing the import functionality.
6+
- Diagrams: diagram1.png, diagram2.png
7+
- Technologies: React, TypeScript
8+
- Conventions: Follows React Hooks pattern
9+
- Directives: Use functional components
10+
11+
# Architecture
12+
- Style: Microservices
13+
- Components: Frontend, Backend, Database
14+
- Data Flow: RESTful API
15+
16+
# Development
17+
- Setup Steps: npm install, npm start
18+
- Build Command: npm run build
19+
- Test Command: npm test
20+
21+
# Business Requirements
22+
- Key Features: User authentication, Data visualization
23+
- Target Audience: Developers, Project Managers
24+
- Success Metrics: User engagement, Response time
25+
26+
# Quality Assurance
27+
- Testing Frameworks: Jest, React Testing Library
28+
- Coverage Threshold: 80%
29+
- Performance Benchmarks: Load time < 3s, API response < 200ms
30+
31+
# Deployment
32+
- Platform: AWS
33+
- CI/CD Pipeline: GitHub Actions
34+
- Staging Environment: staging.example.com
35+
- Production Environment: www.example.com
36+
37+
# Additional Information
38+
This section contains any additional markdown content that doesn't fit into the above categories.

examples/context-editor/src/components/ArchitectureSection.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const ArchitectureSection: React.FC<ArchitectureSectionProps> = ({ formData, han
2323
name="architecture.style"
2424
value={formData.architecture.style}
2525
onChange={handleNestedChange('architecture', 'style')}
26+
helperText="Optional: Architectural style of the module"
2627
/>
2728
</Grid>
2829
<Grid item xs={12}>
@@ -34,7 +35,7 @@ const ArchitectureSection: React.FC<ArchitectureSectionProps> = ({ formData, han
3435
onChange={handleNestedChange('architecture', 'components')}
3536
multiline
3637
rows={3}
37-
helperText="One component per line"
38+
helperText="Optional: One component per line"
3839
/>
3940
</Grid>
4041
<Grid item xs={12}>
@@ -46,7 +47,7 @@ const ArchitectureSection: React.FC<ArchitectureSectionProps> = ({ formData, han
4647
onChange={handleNestedChange('architecture', 'dataFlow')}
4748
multiline
4849
rows={3}
49-
helperText="One data flow step per line"
50+
helperText="Optional: One data flow step per line"
5051
/>
5152
</Grid>
5253
</Grid>

examples/context-editor/src/components/BusinessRequirementsSection.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const BusinessRequirementsSection: React.FC<BusinessRequirementsSectionProps> =
2525
onChange={handleNestedChange('businessRequirements', 'keyFeatures')}
2626
multiline
2727
rows={3}
28-
helperText="One feature per line"
28+
helperText="Optional: One feature per line"
2929
/>
3030
</Grid>
3131
<Grid item xs={12}>
@@ -35,6 +35,7 @@ const BusinessRequirementsSection: React.FC<BusinessRequirementsSectionProps> =
3535
name="businessRequirements.targetAudience"
3636
value={formData.businessRequirements.targetAudience}
3737
onChange={handleNestedChange('businessRequirements', 'targetAudience')}
38+
helperText="Optional: Describe the target audience"
3839
/>
3940
</Grid>
4041
<Grid item xs={12}>
@@ -46,7 +47,7 @@ const BusinessRequirementsSection: React.FC<BusinessRequirementsSectionProps> =
4647
onChange={handleNestedChange('businessRequirements', 'successMetrics')}
4748
multiline
4849
rows={3}
49-
helperText="One metric per line"
50+
helperText="Optional: One metric per line"
5051
/>
5152
</Grid>
5253
</Grid>

examples/context-editor/src/components/ContextForm.tsx

Lines changed: 107 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react';
1+
import React, { useState, useRef, ChangeEvent, useEffect } from 'react';
22
import { Button, Box, Alert, Paper } from '@mui/material';
33
import MetadataSection from './MetadataSection';
44
import ArchitectureSection from './ArchitectureSection';
@@ -16,11 +16,11 @@ interface ContextFormProps {
1616

1717
export type FormDataType = {
1818
moduleName: string;
19-
relatedModules: string;
19+
relatedModules: string[];
2020
version: string;
2121
description: string;
22-
diagrams: string;
23-
technologies: string;
22+
diagrams: string[];
23+
technologies: string[];
2424
conventions: string;
2525
directives: string;
2626
architecture: {
@@ -55,11 +55,11 @@ export type FormDataType = {
5555
const ContextForm: React.FC<ContextFormProps> = ({ onSubmit }) => {
5656
const [formData, setFormData] = useState<FormDataType>({
5757
moduleName: '',
58-
relatedModules: '',
58+
relatedModules: [],
5959
version: '',
6060
description: '',
61-
diagrams: '',
62-
technologies: '',
61+
diagrams: [],
62+
technologies: [],
6363
conventions: '',
6464
directives: '',
6565
architecture: {
@@ -92,6 +92,11 @@ const ContextForm: React.FC<ContextFormProps> = ({ onSubmit }) => {
9292
});
9393

9494
const [errors, setErrors] = useState<{ [key: string]: string }>({});
95+
const fileInputRef = useRef<HTMLInputElement>(null);
96+
97+
useEffect(() => {
98+
console.log('Form data updated:', formData);
99+
}, [formData]);
95100

96101
const handleChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
97102
const { name, value } = e.target;
@@ -112,6 +117,13 @@ const ContextForm: React.FC<ContextFormProps> = ({ onSubmit }) => {
112117
}));
113118
};
114119

120+
const handleMultiChange = (field: string) => (event: React.SyntheticEvent, value: string[]) => {
121+
setFormData((prevData) => ({
122+
...prevData,
123+
[field]: value,
124+
}));
125+
};
126+
115127
const handleSubmit = (e: React.FormEvent) => {
116128
e.preventDefault();
117129
const validationErrors = validateForm(formData);
@@ -125,10 +137,97 @@ const ContextForm: React.FC<ContextFormProps> = ({ onSubmit }) => {
125137
}
126138
};
127139

140+
const handleFileImport = (e: ChangeEvent<HTMLInputElement>) => {
141+
const file = e.target.files?.[0];
142+
if (file) {
143+
const reader = new FileReader();
144+
reader.onload = (e) => {
145+
try {
146+
const content = e.target?.result as string;
147+
console.log('Imported file content:', content);
148+
const parsedData = parseContextMd(content);
149+
console.log('Parsed data:', parsedData);
150+
setFormData(parsedData);
151+
} catch (error) {
152+
console.error('Error parsing imported file:', error);
153+
// You might want to show an error message to the user here
154+
}
155+
};
156+
reader.onerror = (error) => {
157+
console.error('Error reading file:', error);
158+
// You might want to show an error message to the user here
159+
};
160+
reader.readAsText(file);
161+
}
162+
};
163+
164+
const parseContextMd = (content: string): FormDataType => {
165+
const lines = content.split('\n');
166+
const parsedData: FormDataType = JSON.parse(JSON.stringify(formData));
167+
168+
let currentSection: keyof FormDataType | 'metadata' | null = null;
169+
let nestedSection: string | null = null;
170+
171+
lines.forEach((line) => {
172+
if (line.startsWith('# ')) {
173+
currentSection = line.substring(2).trim().toLowerCase() as keyof FormDataType | 'metadata';
174+
nestedSection = null;
175+
} else if (line.startsWith('## ')) {
176+
nestedSection = line.substring(3).trim().toLowerCase();
177+
} else if (line.startsWith('- ')) {
178+
const [key, ...valueParts] = line.substring(2).split(':');
179+
const value = valueParts.join(':').trim();
180+
if (key && value) {
181+
if (currentSection === 'metadata') {
182+
const metadataKey = key.replace(/\s+/g, '') as keyof FormDataType;
183+
if (metadataKey === 'relatedModules' || metadataKey === 'diagrams' || metadataKey === 'technologies') {
184+
(parsedData[metadataKey] as string[]) = value.split(',').map((s) => s.trim());
185+
} else if (metadataKey in parsedData) {
186+
(parsedData[metadataKey] as string) = value;
187+
}
188+
} else if (currentSection && currentSection in parsedData && typeof parsedData[currentSection] === 'object') {
189+
const sectionData = parsedData[currentSection] as Record<string, unknown>;
190+
if (nestedSection) {
191+
if (!(nestedSection in sectionData)) {
192+
sectionData[nestedSection] = {};
193+
}
194+
(sectionData[nestedSection] as Record<string, string>)[key] = value;
195+
} else {
196+
(sectionData as Record<string, string>)[key] = value;
197+
}
198+
}
199+
}
200+
}
201+
});
202+
203+
return parsedData;
204+
};
205+
128206
return (
129207
<form onSubmit={handleSubmit}>
208+
<Box sx={{ mb: 2 }}>
209+
<input
210+
type="file"
211+
accept=".md"
212+
ref={fileInputRef}
213+
style={{ display: 'none' }}
214+
onChange={handleFileImport}
215+
/>
216+
<Button
217+
variant="outlined"
218+
onClick={() => fileInputRef.current?.click()}
219+
>
220+
Import .context.md
221+
</Button>
222+
</Box>
223+
130224
<Paper elevation={3} sx={{ p: 3, mb: 3, bgcolor: 'background.default' }}>
131-
<MetadataSection formData={formData} errors={errors} handleChange={handleChange} />
225+
<MetadataSection
226+
formData={formData}
227+
errors={errors}
228+
handleChange={handleChange}
229+
handleMultiChange={handleMultiChange}
230+
/>
132231
</Paper>
133232
<Paper elevation={3} sx={{ p: 3, mb: 3, bgcolor: 'background.default' }}>
134233
<ArchitectureSection formData={formData} handleNestedChange={handleNestedChange} />

examples/context-editor/src/components/DeploymentSection.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const DeploymentSection: React.FC<DeploymentSectionProps> = ({ formData, handleN
2323
name="deployment.platform"
2424
value={formData.deployment.platform}
2525
onChange={handleNestedChange('deployment', 'platform')}
26+
helperText="Optional: Deployment platform"
2627
/>
2728
</Grid>
2829
<Grid item xs={12} sm={6}>
@@ -32,6 +33,7 @@ const DeploymentSection: React.FC<DeploymentSectionProps> = ({ formData, handleN
3233
name="deployment.cicdPipeline"
3334
value={formData.deployment.cicdPipeline}
3435
onChange={handleNestedChange('deployment', 'cicdPipeline')}
36+
helperText="Optional: CI/CD pipeline details"
3537
/>
3638
</Grid>
3739
<Grid item xs={12} sm={6}>
@@ -41,6 +43,7 @@ const DeploymentSection: React.FC<DeploymentSectionProps> = ({ formData, handleN
4143
name="deployment.stagingEnvironment"
4244
value={formData.deployment.stagingEnvironment}
4345
onChange={handleNestedChange('deployment', 'stagingEnvironment')}
46+
helperText="Optional: Staging environment details"
4447
/>
4548
</Grid>
4649
<Grid item xs={12} sm={6}>
@@ -50,6 +53,7 @@ const DeploymentSection: React.FC<DeploymentSectionProps> = ({ formData, handleN
5053
name="deployment.productionEnvironment"
5154
value={formData.deployment.productionEnvironment}
5255
onChange={handleNestedChange('deployment', 'productionEnvironment')}
56+
helperText="Optional: Production environment details"
5357
/>
5458
</Grid>
5559
</Grid>

examples/context-editor/src/components/DevelopmentSection.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const DevelopmentSection: React.FC<DevelopmentSectionProps> = ({ formData, handl
2525
onChange={handleNestedChange('development', 'setupSteps')}
2626
multiline
2727
rows={3}
28-
helperText="One setup step per line"
28+
helperText="Optional: One setup step per line"
2929
/>
3030
</Grid>
3131
<Grid item xs={12} sm={6}>
@@ -35,6 +35,7 @@ const DevelopmentSection: React.FC<DevelopmentSectionProps> = ({ formData, handl
3535
name="development.buildCommand"
3636
value={formData.development.buildCommand}
3737
onChange={handleNestedChange('development', 'buildCommand')}
38+
helperText="Optional: Command to build the project"
3839
/>
3940
</Grid>
4041
<Grid item xs={12} sm={6}>
@@ -44,6 +45,7 @@ const DevelopmentSection: React.FC<DevelopmentSectionProps> = ({ formData, handl
4445
name="development.testCommand"
4546
value={formData.development.testCommand}
4647
onChange={handleNestedChange('development', 'testCommand')}
48+
helperText="Optional: Command to run tests"
4749
/>
4850
</Grid>
4951
</Grid>

0 commit comments

Comments
 (0)