Skip to content

Commit f523370

Browse files
bartoszpopBartosz PopielaCopilot
authored
fix(vscode): preserve categories when toggling Custom only (#1665)
Apply custom and category filters consistently to regular and preferred cards without mutating the category selection. Co-authored-by: Bartosz Popiela <bartosz.popiela@bp.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 493f177 commit f523370

3 files changed

Lines changed: 172 additions & 15 deletions

File tree

‎karavan-vscode/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,7 @@
677677
"lint": "eslint src webview",
678678
"lint:fix": "eslint --fix src webview",
679679
"test": "node ./out/test/runTest.js",
680+
"test:dsl-selector": "node --test test/dsl-selector.test.cjs",
680681
"open-in-browser": "vscode-test-web --extensionDevelopmentPath=. ../karavan-demo/hello-world/"
681682
},
682683
"dependencies": {
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
const assert = require("node:assert/strict");
18+
const {readFileSync} = require("node:fs");
19+
const Module = require("node:module");
20+
const path = require("node:path");
21+
const {test} = require("node:test");
22+
const ts = require("typescript");
23+
const React = require("react");
24+
25+
test("Custom only preserves categories and filters both regular and preferred cards", () => {
26+
const states = [];
27+
const effects = [];
28+
let cursor = 0;
29+
let mounted = false;
30+
const react = {
31+
...React,
32+
useState: initial => {
33+
const index = cursor++;
34+
if (!(index in states)) states[index] = initial;
35+
return [states[index], value => {
36+
states[index] = typeof value === "function" ? value(states[index]) : value;
37+
}];
38+
},
39+
useEffect: effect => {
40+
if (!mounted) effects.push(effect);
41+
},
42+
};
43+
const selector = {
44+
showSelector: true,
45+
showSteps: true,
46+
parentId: "route",
47+
parentDsl: "FromDefinition",
48+
selectedToggles: ["eip", "components", "kamelets"],
49+
setShowSelector: () => {},
50+
setShowProperties: () => {},
51+
setSelectedDsl: () => {},
52+
addSelectedToggle: toggle => selector.selectedToggles.push(toggle),
53+
deleteSelectedToggle: toggle => {
54+
selector.selectedToggles = selector.selectedToggles.filter(value => value !== toggle);
55+
},
56+
};
57+
const elements = [
58+
{name: "log", dsl: "LogDefinition", navigation: "eip"},
59+
{name: "direct", uri: "direct", navigation: "component"},
60+
{name: "custom", navigation: "kamelet"},
61+
{name: "other", navigation: "kamelet"},
62+
];
63+
let customNames = ["custom"];
64+
const mocks = {
65+
react,
66+
"@patternfly/react-core": new Proxy({}, {get: (_target, name) => name}),
67+
"./DslSelector.css": {},
68+
"../utils/CamelUi": {CamelUi: {
69+
getSelectorModelsForParentFiltered: (_parent, navigation) => elements.filter(element => element.navigation === navigation),
70+
checkFilter: (element, filter) => element.name.includes(filter),
71+
}},
72+
"../utils/DslMetaModel": {},
73+
"../DesignerStore": {useSelectorStore: select => select(selector)},
74+
"zustand/shallow": {},
75+
"@karavan-core/api/ComponentApi": {ComponentApi: {getBlockedComponentNames: () => []}},
76+
"@karavan-core/api/KameletApi": {KameletApi: {getCustomKameletNames: () => customNames}},
77+
"@patternfly/react-icons/dist/esm/icons/times-icon": "TimesIcon",
78+
"@patternfly/react-icons/dist/esm/icons/exclamation-circle-icon": "ExclamationCircleIcon",
79+
"./DslPreferences": {
80+
getPreferredElements: () => ["LogDefinition", "direct", "custom", "other"],
81+
},
82+
"./DslFastCard": {DslFastCard: "DslFastCard"},
83+
"./DslCard": {DslCard: "DslCard"},
84+
"usehooks-ts": {useDebounceValue: initial => react.useState(initial)},
85+
uuid: {},
86+
"@features/project/designer/utils/ValidatorUtils": {},
87+
"@stores/ProjectStore": {useFilesStore: select => select({files: []})},
88+
};
89+
// Exercise the selector TypeScript without a browser or VS Code host.
90+
const fullPath = path.join(__dirname, "..", "webview", "karavan", "features", "project", "designer", "selector", "DslSelector.tsx");
91+
const loaded = new Module(fullPath, module);
92+
loaded.filename = fullPath;
93+
loaded.paths = module.paths;
94+
loaded.require = request => {
95+
if (request in mocks) return mocks[request];
96+
if (request.startsWith("@karavan-core/")) return {};
97+
return require(request);
98+
};
99+
const code = ts.transpileModule(readFileSync(fullPath, "utf8"), {
100+
compilerOptions: {
101+
module: ts.ModuleKind.CommonJS,
102+
target: ts.ScriptTarget.ES2022,
103+
jsx: ts.JsxEmit.React,
104+
esModuleInterop: true,
105+
},
106+
}).outputText;
107+
loaded._compile(code, fullPath);
108+
const render = () => {
109+
cursor = 0;
110+
return loaded.exports.DslSelector({onDslSelect: () => {}});
111+
};
112+
const nodes = tree => {
113+
if (Array.isArray(tree)) return tree.flatMap(nodes);
114+
if (!React.isValidElement(tree)) return [];
115+
return [tree, ...nodes(tree.props.children)];
116+
};
117+
const cardNames = (tree, type) => nodes(tree).filter(node => node.type === type).map(node => node.props.dsl.name);
118+
const toggleCustom = (tree, checked) => nodes(tree).find(node => node.type === "Switch").props.onChange({}, checked);
119+
const assertCards = expected => {
120+
const tree = render();
121+
assert.deepEqual(cardNames(tree, "DslCard"), expected);
122+
assert.deepEqual(cardNames(tree, "DslFastCard"), expected);
123+
};
124+
render();
125+
mounted = true;
126+
effects.forEach(effect => effect());
127+
for (const categories of [["eip", "components", "kamelets"], ["components", "kamelets"], ["kamelets"]]) {
128+
selector.selectedToggles = [...categories];
129+
const tree = render();
130+
const before = cardNames(tree, "DslCard");
131+
assertCards(before);
132+
toggleCustom(tree, true);
133+
assert.deepEqual(selector.selectedToggles, categories);
134+
assertCards(["custom"]);
135+
toggleCustom(render(), false);
136+
assert.deepEqual(selector.selectedToggles, categories);
137+
assertCards(before);
138+
}
139+
140+
customNames = [];
141+
const before = cardNames(render(), "DslCard");
142+
toggleCustom(render(), true);
143+
assertCards([]);
144+
toggleCustom(render(), false);
145+
assertCards(before);
146+
147+
customNames = ["custom"];
148+
selector.selectedToggles = ["eip", "components", "kamelets"];
149+
const search = value => nodes(render()).find(node => node.type === "TextInput").props.onChange({}, value);
150+
search("other");
151+
assertCards(["other"]);
152+
toggleCustom(render(), true);
153+
assertCards([]);
154+
search("custom");
155+
assertCards(["custom"]);
156+
toggleCustom(render(), false);
157+
assertCards(["custom"]);
158+
search("");
159+
160+
for (const categories of [["eip"], ["components"], []]) {
161+
selector.selectedToggles = [...categories];
162+
const expected = elements.filter(element => categories.includes(element.navigation === "component" ? "components" : element.navigation)).map(element => element.name);
163+
assertCards(expected);
164+
}
165+
});

‎karavan-vscode/webview/karavan/features/project/designer/selector/DslSelector.tsx‎

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,6 @@ export function DslSelector(props: Props) {
274274
isChecked={customOnly}
275275
onChange={(_event, checked) => {
276276
setCustomOnly(checked);
277-
if (checked) {
278-
deleteSelectedToggle('eip');
279-
deleteSelectedToggle('components');
280-
}
281277
}}
282278
/>}
283279
</div>
@@ -322,15 +318,11 @@ export function DslSelector(props: Props) {
322318
const filteredElements: DslMetaModel[] = selectedDsl
323319
? [selectedDsl]
324320
: elements.filter(d => {
321+
if (customOnly && (d.navigation !== 'kamelet' || !KameletApi.getCustomKameletNames().includes(d.name))) return false;
325322
if (selectedToggles.includes('eip') && d.navigation === 'eip') return true
326323
else if (selectedToggles.includes('components') && d.navigation === 'component') return true
327-
else if (selectedToggles.includes('kamelets') && d.navigation === 'kamelet') {
328-
if (customOnly) {
329-
return KameletApi.getCustomKameletNames().includes(d.name);
330-
} else {
331-
return true;
332-
}
333-
} else return false;
324+
else if (selectedToggles.includes('kamelets') && d.navigation === 'kamelet') return true
325+
else return false;
334326
})
335327
.filter(d => CamelUi.checkFilter(d, filter));
336328

@@ -343,16 +335,15 @@ export function DslSelector(props: Props) {
343335

344336
const fastElements: DslMetaModel[] = selectedDsl
345337
? [selectedDsl]
346-
: elements.filter((d: DslMetaModel) => {
347-
if (selectedToggles.includes('eip') && d.navigation === 'eip') {
338+
: filteredElements.filter((d: DslMetaModel) => {
339+
if (d.navigation === 'eip') {
348340
return preferredElements.includes(d.dsl);
349-
} else if (d.navigation === 'component' && d.navigation === 'component') {
341+
} else if (d.navigation === 'component') {
350342
return d.uri && preferredElements.includes(d.uri)
351343
} else {
352344
return preferredElements.includes(d.name)
353345
}
354346
})
355-
.filter(d => CamelUi.checkFilter(d, filter))
356347
.filter((_, i) => i < 7);
357348

358349
function onKeyDown(event: React.KeyboardEvent<HTMLDivElement>): void {

0 commit comments

Comments
 (0)