Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.

Commit ba54314

Browse files
committed
fix: resource entry and empty resource in output
1 parent 4f3a1c4 commit ba54314

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

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

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,21 @@ const ContextDocsForm: React.FC<ContextDocsFormProps> = ({ onSubmit }) => {
2424
setContextDocs(updatedDocs);
2525
};
2626

27-
const handleResourceChange = (index: number, key: string, value: string) => {
27+
const handleResourceChange = (index: number, oldKey: string, newKey: string, value: string) => {
2828
const updatedDocs = [...contextDocs];
29+
const resources = { ...updatedDocs[index].resources };
30+
31+
// Remove the old key-value pair
32+
delete resources[oldKey];
33+
34+
// Add the new key-value pair
35+
if (newKey !== '' || value !== '') {
36+
resources[newKey] = value;
37+
}
38+
2939
updatedDocs[index] = {
3040
...updatedDocs[index],
31-
resources: { ...updatedDocs[index].resources, [key]: value }
41+
resources: resources
3242
};
3343
setContextDocs(updatedDocs);
3444
};
@@ -66,13 +76,19 @@ const ContextDocsForm: React.FC<ContextDocsFormProps> = ({ onSubmit }) => {
6676

6777
const generateContextDocsMd = () => {
6878
let content = '---\ncontextdocs:\n';
69-
contextDocs.forEach((doc) => {
70-
content += ` - name: ${doc.name}\n`;
71-
content += ` relationship: ${doc.relationship}\n`;
72-
content += ' resources:\n';
73-
Object.entries(doc.resources).forEach(([key, value]) => {
74-
content += ` - ${key}: ${value}\n`;
75-
});
79+
const nonEmptyDocs = contextDocs.filter(doc => doc.name || doc.relationship || Object.keys(doc.resources).length > 0);
80+
nonEmptyDocs.forEach((doc) => {
81+
if (doc.name || doc.relationship || Object.keys(doc.resources).length > 0) {
82+
content += ` - name: ${doc.name}\n`;
83+
content += ` relationship: ${doc.relationship}\n`;
84+
const nonEmptyResources = Object.entries(doc.resources).filter(([key, value]) => key && value);
85+
if (nonEmptyResources.length > 0) {
86+
content += ' resources:\n';
87+
nonEmptyResources.forEach(([key, value]) => {
88+
content += ` - ${key}: ${value}\n`;
89+
});
90+
}
91+
}
7692
});
7793
content += '---\n\n';
7894
content += 'This file contains a list of external dependencies and libraries used in the project. ';
@@ -106,13 +122,13 @@ const ContextDocsForm: React.FC<ContextDocsFormProps> = ({ onSubmit }) => {
106122
<TextField
107123
label="Resource Name"
108124
value={key}
109-
onChange={(e) => handleResourceChange(index, e.target.value, value)}
125+
onChange={(e) => handleResourceChange(index, key, e.target.value, value)}
110126
sx={{ flexGrow: 1, mr: 1 }}
111127
/>
112128
<TextField
113129
label="URL"
114130
value={value}
115-
onChange={(e) => handleResourceChange(index, key, e.target.value)}
131+
onChange={(e) => handleResourceChange(index, key, key, e.target.value)}
116132
sx={{ flexGrow: 1, mr: 1 }}
117133
/>
118134
<IconButton onClick={() => removeResource(index, key)}>

0 commit comments

Comments
 (0)