Skip to content

Commit 0bb8a62

Browse files
committed
refactor: update tool type handling and improve category merging logic
1 parent 1f7ae78 commit 0bb8a62

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

ui/src/views/tool/toolStore/ToolStoreDialog.vue

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
{{ $t('views.tool.toolStore.title') }}
1515
</h4>
1616
<el-radio-group v-model="toolType" @change="radioChange" class="app-radio-button-group">
17-
<el-radio-button value="INTERNAL">{{
18-
$t('views.tool.toolStore.internal')
19-
}}</el-radio-button>
20-
<el-radio-button value="APPSTORE">{{ $t('views.tool.toolStore.title') }}</el-radio-button>
17+
<el-radio-button value="TOOL">{{$t('views.tool.title')}}</el-radio-button>
18+
<el-radio-button value="DATA_SOURCE">{{$t('views.tool.dataSource')}}</el-radio-button>
2119
</el-radio-group>
2220

2321
<div class="flex align-center" style="margin-right: 28px">
@@ -127,14 +125,8 @@ const dialogVisible = ref(false)
127125
const loading = ref(false)
128126
const searchValue = ref('')
129127
const folderId = ref('')
130-
const toolType = ref('APPSTORE')
128+
const toolType = ref('TOOL')
131129
const defaultCategories = ref<ToolCategory[]>([
132-
// 第一版不上
133-
// {
134-
// id: 'recommend',
135-
// title: t('views.tool.toolStore.recommend'),
136-
// tools: []
137-
// },
138130
{
139131
id: 'web_search',
140132
title: t('views.tool.toolStore.webSearch'),
@@ -145,29 +137,14 @@ const defaultCategories = ref<ToolCategory[]>([
145137
title: t('views.tool.toolStore.databaseQuery'),
146138
tools: [],
147139
},
148-
// {
149-
// id: 'image',
150-
// title: t('views.tool.toolStore.image'),
151-
// tools: []
152-
// },
153-
// {
154-
// id: 'developer',
155-
// title: t('views.tool.toolStore.developer'),
156-
// tools: []
157-
// },
158-
// {
159-
// id: 'communication',
160-
// title: t('views.tool.toolStore.communication'),
161-
// tools: []
162-
// }
163140
])
164141
const categories = ref<ToolCategory[]>([...defaultCategories.value])
165142
166143
const filterList = ref<any>(null)
167144
168145
watch(dialogVisible, (bool) => {
169146
if (!bool) {
170-
toolType.value = 'APPSTORE'
147+
toolType.value = 'TOOL'
171148
}
172149
})
173150
@@ -184,31 +161,54 @@ function open(id: string) {
184161
}
185162
186163
async function getList() {
187-
if (toolType.value === 'INTERNAL') {
188-
await getInternalToolList()
164+
if (toolType.value === 'DATA_SOURCE') {
165+
categories.value = [
166+
{
167+
id: 'data_source',
168+
title: t('views.tool.dataSource'),
169+
tools: [],
170+
},
171+
]
189172
} else {
190-
await getStoreToolList()
173+
const [v1, v2] = await Promise.all([
174+
getInternalToolList(),
175+
getStoreToolList()
176+
])
177+
178+
const merged = [...v1, ...v2].reduce((acc, category) => {
179+
const existing = acc.find((item: any) => item.id === category.id)
180+
if (existing) {
181+
existing.tools = [...existing.tools, ...category.tools]
182+
} else {
183+
acc.push({...category})
184+
}
185+
return acc
186+
}, [] as ToolCategory[])
187+
188+
categories.value = merged
191189
}
192190
}
193191
194192
async function getInternalToolList() {
195193
try {
196-
categories.value = defaultCategories.value
194+
const categories = defaultCategories.value
197195
const res = await ToolStoreApi.getInternalToolList({ name: searchValue.value }, loading)
198196
if (searchValue.value.length) {
199197
filterList.value = res.data
200198
} else {
201199
filterList.value = null
202-
categories.value.forEach((category) => {
200+
categories.forEach((category) => {
203201
// if (category.id === 'recommend') {
204202
// category.tools = res.data
205203
// } else {
206204
category.tools = res.data.filter((tool: any) => tool.label === category.id)
207205
// }
208206
})
209207
}
208+
return categories
210209
} catch (error) {
211210
console.error(error)
211+
return []
212212
}
213213
}
214214
@@ -217,6 +217,7 @@ async function getStoreToolList() {
217217
const res = await ToolStoreApi.getStoreToolList({ name: searchValue.value }, loading)
218218
const tags = res.data.additionalProperties.tags
219219
const storeTools = res.data.apps
220+
let categories = []
220221
//
221222
storeTools.forEach((tool: any) => {
222223
tool.desc = tool.description
@@ -225,14 +226,16 @@ async function getStoreToolList() {
225226
filterList.value = res.data.apps
226227
} else {
227228
filterList.value = null
228-
categories.value = tags.map((tag: any) => ({
229+
categories = tags.map((tag: any) => ({
229230
id: tag.key,
230231
title: tag.name, // 国际化
231232
tools: storeTools.filter((tool: any) => tool.label === tag.key),
232233
}))
233234
}
235+
return categories
234236
} catch (error) {
235237
console.error(error)
238+
return []
236239
}
237240
}
238241
@@ -242,7 +245,8 @@ const handleClick = (e: MouseEvent) => {
242245
243246
const internalDescDrawerRef = ref<InstanceType<typeof InternalDescDrawer>>()
244247
async function handleDetail(tool: any) {
245-
if (toolType.value === 'INTERNAL') {
248+
console.log(tool)
249+
if (tool.tool_type === 'INTERNAL') {
246250
const index = tool.icon.replace('icon.png', 'detail.md')
247251
const response = await fetch(index)
248252
const content = await response.text()
@@ -259,7 +263,7 @@ function handleOpenAdd(data?: any, isEdit?: boolean) {
259263
260264
const addLoading = ref(false)
261265
async function handleAdd(tool: any) {
262-
if (toolType.value === 'INTERNAL') {
266+
if (tool.tool_type === 'INTERNAL') {
263267
await handleInternalAdd(tool)
264268
} else {
265269
await handleStoreAdd(tool)

0 commit comments

Comments
 (0)