Skip to content

Commit 9a235ed

Browse files
author
fuyoo
committed
fix type error
1 parent c535cd2 commit 9a235ed

File tree

1 file changed

+50
-32
lines changed

1 file changed

+50
-32
lines changed

src/pages/host/components/CoDatabase/CoKeys.vue

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,48 +56,66 @@ interface Tree {
5656
}
5757
const ID = () => Math.random().toString(36).slice(2)
5858
// below code is aim to parse the key name to tree structure
59-
const parseLevel = (ori: Tree[], key: string) => {
60-
// todo: split symbol should be configurable.
61-
// if not matched with `:` add it to tree structure immediately.
62-
if (!key.includes(':')) {
63-
ori.push({ label: key, icon: "key", type: 'key', id: ID() })
64-
return
59+
const parseLevel = (ori: Tree[], key: string, delimiter: string = ':') => {
60+
// Handle empty key
61+
if (!key) {
62+
return;
6563
}
66-
// otherwise split it to and parse it to tree structure.
67-
const arr = key.split(':')
68-
// append children to root
64+
65+
// If not matched with delimiter, add it to tree structure immediately.
66+
if (!key.includes(delimiter)) {
67+
ori.push(createTreeNode(key, 'key'));
68+
return;
69+
}
70+
71+
// Otherwise split it and parse it to tree structure.
72+
const arr = key.split(delimiter);
73+
6974
const appendChildren = (ori: Tree[], arr: string[]) => {
70-
// search root key.
71-
const root = ori.find((e) => e.label === arr[0] && e.type != 'key')
72-
// finded root key
75+
// Search root key.
76+
const root = ori.find((e) => e.label === arr[0] && e.type !== 'key');
77+
78+
// Found root key
7379
if (root) {
74-
// if not children, create it.
75-
if (!root.children) root.children = []
76-
// recursive parse it.
77-
appendChildren(root.children, arr.splice(1))
80+
// If not children, create it.
81+
if (!root.children) root.children = [];
82+
// Recursive parse it.
83+
appendChildren(root.children, arr.slice(1));
7884
} else {
79-
// if key length is less than 2, it means it is a key.
85+
// If key length is less than 2, it means it is a key.
8086
if (arr.length <= 1) {
81-
const obj = { label: key, icon: "key", type: 'key', id: ID() } as Tree
82-
ori.push(obj)
83-
return
87+
ori.push(createTreeNode(key, 'key'));
88+
return;
8489
}
85-
// otherwise create as a folder.
86-
const obj = { label: arr[0], icon: "folder", type: 'folder', children: [], id: ID() } as Tree
87-
// push it to as root.
88-
ori.push(obj)
89-
// and recursive parse it.
90-
appendChildren(obj.children!, arr.splice(1))
90+
// Otherwise create as a folder.
91+
const obj = createTreeNode(arr[0], 'folder');
92+
obj.children = [];
93+
// Push it to as root.
94+
ori.push(obj);
95+
// And recursive parse it.
96+
appendChildren(obj.children!, arr.slice(1));
9197
}
98+
};
99+
100+
// Parse it
101+
try {
102+
appendChildren(ori, arr);
103+
} catch (error) {
104+
console.error('Error parsing level:', error);
92105
}
93-
// parse it
94-
appendChildren(ori, arr)
95-
}
106+
};
107+
108+
const createTreeNode = (label: string, type: 'key' | 'folder'): Tree => {
109+
try {
110+
return { label, icon: type === 'key' ? "key" : "folder", type, id: ID() };
111+
} catch (error) {
112+
console.error('Error creating tree node:', error);
113+
throw error;
114+
}
115+
};
96116
// todo: configurable name space enable.
97117
const nameSpaceEnable = ref(true)
98-
const icon = (e) => {
99-
console.log(e.target)
100-
}
118+
101119
</script>
102120
<template>
103121
<div class="flex flex-col h-full">

0 commit comments

Comments
 (0)