Skip to content

Commit 3bc16e5

Browse files
committed
refine
1 parent 54e8575 commit 3bc16e5

File tree

1 file changed

+50
-13
lines changed

1 file changed

+50
-13
lines changed

src/routes/page/agent/[agentId]/agent-components/agent-utility.svelte

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434
/** @type {any} */
3535
let utilityMapper = {};
3636
37-
/** @type {string[]} */
38-
let utilityCategoryOptions = [];
39-
4037
/** @type {import('$agentTypes').AgentUtility[]} */
4138
let innerUtilities = [];
4239
@@ -58,8 +55,6 @@
5855
contents.push(content);
5956
utilityMapper[utility.category] = contents;
6057
});
61-
const keys = Object.keys(utilityMapper).sort((/** @type {string} */ a, /** @type {string} */ b) => a.localeCompare(b));
62-
utilityCategoryOptions = ["", ...keys];
6358
});
6459
});
6560
@@ -218,6 +213,36 @@
218213
}
219214
handleAgentChange();
220215
}
216+
217+
/**
218+
* @param {any[]} options
219+
* @param {string} placeholder
220+
*/
221+
function getUtilityOptions(options, placeholder = '') {
222+
let list = options?.map(x => {
223+
return {
224+
label: x,
225+
value: x
226+
};
227+
}) || [];
228+
229+
list = [{
230+
label: placeholder || '',
231+
value: ''
232+
}, ...list];
233+
return list;
234+
}
235+
236+
/** @param {number} uid */
237+
function revertUtility(uid) {
238+
const found = innerUtilities.find((_, index) => index === uid);
239+
if (!found) return;
240+
241+
const originalItems = utilityMapper[found.category]?.find((/** @type {any} */ x) => x.name === found.name)?.items || [];
242+
found.items = [...originalItems];
243+
innerRefresh(innerUtilities);
244+
handleAgentChange();
245+
}
221246
</script>
222247
223248
<Card>
@@ -278,11 +303,12 @@
278303
type="select"
279304
value={utility.category}
280305
disabled={utility.disabled}
281-
placeholder={'Select a category'}
282306
on:change={e => changeUtilityCategory(e, uid)}
283307
>
284-
{#each utilityCategoryOptions as option}
285-
<option value={option} selected={option == utility.category}>{option}</option>
308+
{#each getUtilityOptions(Object.keys(utilityMapper), 'Select a category') as option}
309+
<option value={option.value} selected={option.value == utility.category}>
310+
{option.label}
311+
</option>
286312
{/each}
287313
</Input>
288314
</div>
@@ -302,20 +328,31 @@
302328
{#if utility.category}
303329
<div class="utility-content">
304330
<div class="utility-list-item">
305-
<div class="utility-label line-align-center">
306-
{'Name'}
331+
<div class="utility-label d-flex" style="gap: 10px;">
332+
<div class="line-align-center">{'Name'}</div>
333+
{#if utility.name}
334+
<div class="line-align-center">
335+
<!-- svelte-ignore a11y-click-events-have-key-events -->
336+
<!-- svelte-ignore a11y-no-static-element-interactions -->
337+
<i
338+
class="mdi mdi-refresh clickable"
339+
on:click={() => revertUtility(uid)}
340+
/>
341+
</div>
342+
{/if}
307343
</div>
308344
<div class="utility-value">
309345
<div class="utility-input line-align-center">
310346
<Input
311347
type="select"
312348
value={utility.name}
313349
disabled={utility.disabled}
314-
placeholder={'Select a utility'}
315350
on:change={e => changeUtilityName(e, uid)}
316351
>
317-
{#each ['', ...utilityMapper[utility.category]?.map((/** @type {any} */ x) => x.name) || []] as option}
318-
<option value={option} selected={option == utility.name}>{option}</option>
352+
{#each getUtilityOptions(utilityMapper[utility.category]?.map((/** @type {any} */ x) => x.name), 'Select a utility') as option}
353+
<option value={option.value} selected={option.value == utility.name}>
354+
{option.label}
355+
</option>
319356
{/each}
320357
</Input>
321358
</div>

0 commit comments

Comments
 (0)