This is an automated email from the ASF dual-hosted git repository.
dengliming pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu-dashboard.git
The following commit(s) were added to refs/heads/master by this push:
new 944b9ad5 fix: update nested API tree nodes (#694)
944b9ad5 is described below
commit 944b9ad5ef4c673f630d03eaa999095ea21bddbf
Author: Efe <[email protected]>
AuthorDate: Mon Sep 28 06:34:22 2026 +0300
fix: update nested API tree nodes (#694)
Signed-off-by: Efe Gökdemir <[email protected]>
---
src/routes/Document/components/SearchApi.js | 21 ++++++-------
src/routes/Document/components/searchApiTree.js | 35 ++++++++++++++++++++++
.../Document/components/searchApiTree.test.js | 30 +++++++++++++++++++
3 files changed, 74 insertions(+), 12 deletions(-)
diff --git a/src/routes/Document/components/SearchApi.js
b/src/routes/Document/components/SearchApi.js
index b12d7e35..42665453 100644
--- a/src/routes/Document/components/SearchApi.js
+++ b/src/routes/Document/components/SearchApi.js
@@ -35,6 +35,7 @@ import AddAndUpdateTag from "./AddAndUpdateTag";
import AddAndUpdateApiDoc from "./AddAndUpdateApiDoc";
import ImportSwaggerModal from "./ImportSwaggerModal";
import { getIntlContent } from "../../../utils/IntlUtils";
+import { updateTreeNodes } from "./searchApiTree";
const { Text } = Typography;
@@ -247,17 +248,11 @@ const SearchApi = React.forwardRef((props, ref) => {
queryRootTag();
return;
}
- let allNodes = treeData.flatMap((i) =>
- i.children ? [...i.children, i] : i,
- );
- let curNodeIdx = allNodes.findIndex((t) => t.id && t.id === data.id) ?? -1;
- if (curNodeIdx === -1) {
- return;
- }
+ let title;
if (refType === "tag") {
- allNodes[curNodeIdx].title = data.name;
+ title = data.name;
} else if (refType === "api") {
- allNodes[curNodeIdx].title = (
+ title = (
<>
<Text code>{Method[data.httpMethod]}</Text>
<Tooltip placement="topLeft" arrowPointAtCenter title={data.apiPath}>
@@ -266,9 +261,11 @@ const SearchApi = React.forwardRef((props, ref) => {
</>
);
}
- // forceUpdate tree
- setTreeData();
- setTreeData(treeData);
+ const result = updateTreeNodes(treeData, data.id, title);
+ if (!result.updated) {
+ return;
+ }
+ setTreeData(result.nodes);
afterUpdate(data, refType);
};
diff --git a/src/routes/Document/components/searchApiTree.js
b/src/routes/Document/components/searchApiTree.js
new file mode 100644
index 00000000..c1d32cf5
--- /dev/null
+++ b/src/routes/Document/components/searchApiTree.js
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+export const updateTreeNodes = (nodes, id, title) => {
+ let updated = false;
+ const updatedNodes = nodes.map((node) => {
+ if (node.id === id) {
+ updated = true;
+ return { ...node, title };
+ }
+ if (node.children) {
+ const result = updateTreeNodes(node.children, id, title);
+ if (result.updated) {
+ updated = true;
+ return { ...node, children: result.nodes };
+ }
+ }
+ return node;
+ });
+ return { nodes: updatedNodes, updated };
+};
diff --git a/src/routes/Document/components/searchApiTree.test.js
b/src/routes/Document/components/searchApiTree.test.js
new file mode 100644
index 00000000..b315ea63
--- /dev/null
+++ b/src/routes/Document/components/searchApiTree.test.js
@@ -0,0 +1,30 @@
+import { updateTreeNodes } from "./searchApiTree";
+
+describe("updateTreeNodes", () => {
+ it("updates an API nested below multiple tag levels", () => {
+ const tree = [
+ {
+ id: "root",
+ children: [
+ {
+ id: "child",
+ children: [{ id: "api", title: "old" }],
+ },
+ ],
+ },
+ ];
+
+ const result = updateTreeNodes(tree, "api", "new");
+
+ expect(result.updated).toBe(true);
+ expect(result.nodes[0].children[0].children[0].title).toBe("new");
+ expect(tree[0].children[0].children[0].title).toBe("old");
+ });
+
+ it("reports when the tree does not contain the requested node", () => {
+ const result = updateTreeNodes([{ id: "root" }], "missing", "new");
+
+ expect(result.updated).toBe(false);
+ expect(result.nodes).toEqual([{ id: "root" }]);
+ });
+});