2024-06-22 09:29:33 +00:00
|
|
|
<script lang="ts">
|
2024-06-22 19:08:32 +00:00
|
|
|
import { toast } from 'svelte-sonner';
|
|
|
|
|
2024-06-22 09:29:33 +00:00
|
|
|
import { config, functions, models, settings, tools, user } from '$lib/stores';
|
|
|
|
import { createEventDispatcher, onMount, getContext, tick } from 'svelte';
|
2024-06-22 19:08:32 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
getUserValvesSpecById as getToolUserValvesSpecById,
|
|
|
|
getUserValvesById as getToolUserValvesById,
|
|
|
|
updateUserValvesById as updateToolUserValvesById
|
|
|
|
} from '$lib/apis/tools';
|
|
|
|
import {
|
|
|
|
getUserValvesSpecById as getFunctionUserValvesSpecById,
|
|
|
|
getUserValvesById as getFunctionUserValvesById,
|
|
|
|
updateUserValvesById as updateFunctionUserValvesById
|
|
|
|
} from '$lib/apis/functions';
|
|
|
|
|
2024-06-22 09:29:33 +00:00
|
|
|
import Tooltip from '$lib/components/common/Tooltip.svelte';
|
2024-06-22 19:08:32 +00:00
|
|
|
import Spinner from '$lib/components/common/Spinner.svelte';
|
2024-07-12 00:18:18 +00:00
|
|
|
import Valves from '$lib/components/common/Valves.svelte';
|
2024-06-22 19:08:32 +00:00
|
|
|
|
2024-06-22 09:29:33 +00:00
|
|
|
const dispatch = createEventDispatcher();
|
|
|
|
|
|
|
|
const i18n = getContext('i18n');
|
|
|
|
|
|
|
|
let tab = 'tools';
|
|
|
|
let selectedId = '';
|
|
|
|
|
2024-06-22 19:08:32 +00:00
|
|
|
let loading = false;
|
|
|
|
|
|
|
|
let valvesSpec = null;
|
|
|
|
let valves = {};
|
|
|
|
|
2024-08-02 15:36:16 +00:00
|
|
|
let debounceTimer;
|
|
|
|
|
|
|
|
const debounceSubmitHandler = async () => {
|
|
|
|
if (debounceTimer) {
|
|
|
|
clearTimeout(debounceTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set a new timer
|
|
|
|
debounceTimer = setTimeout(() => {
|
|
|
|
submitHandler();
|
2024-08-02 15:41:15 +00:00
|
|
|
}, 500); // 0.5 second debounce
|
2024-08-02 15:36:16 +00:00
|
|
|
};
|
|
|
|
|
2024-06-22 19:08:32 +00:00
|
|
|
const getUserValves = async () => {
|
|
|
|
loading = true;
|
|
|
|
if (tab === 'tools') {
|
|
|
|
valves = await getToolUserValvesById(localStorage.token, selectedId);
|
|
|
|
valvesSpec = await getToolUserValvesSpecById(localStorage.token, selectedId);
|
|
|
|
} else if (tab === 'functions') {
|
|
|
|
valves = await getFunctionUserValvesById(localStorage.token, selectedId);
|
|
|
|
valvesSpec = await getFunctionUserValvesSpecById(localStorage.token, selectedId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (valvesSpec) {
|
|
|
|
// Convert array to string
|
|
|
|
for (const property in valvesSpec.properties) {
|
|
|
|
if (valvesSpec.properties[property]?.type === 'array') {
|
|
|
|
valves[property] = (valves[property] ?? []).join(',');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loading = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
const submitHandler = async () => {
|
|
|
|
if (valvesSpec) {
|
|
|
|
// Convert string to array
|
|
|
|
for (const property in valvesSpec.properties) {
|
|
|
|
if (valvesSpec.properties[property]?.type === 'array') {
|
|
|
|
valves[property] = (valves[property] ?? '').split(',').map((v) => v.trim());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tab === 'tools') {
|
|
|
|
const res = await updateToolUserValvesById(localStorage.token, selectedId, valves).catch(
|
|
|
|
(error) => {
|
|
|
|
toast.error(error);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (res) {
|
2024-06-24 16:09:45 +00:00
|
|
|
toast.success($i18n.t('Valves updated'));
|
2024-06-22 19:08:32 +00:00
|
|
|
valves = res;
|
|
|
|
}
|
|
|
|
} else if (tab === 'functions') {
|
|
|
|
const res = await updateFunctionUserValvesById(
|
|
|
|
localStorage.token,
|
|
|
|
selectedId,
|
|
|
|
valves
|
|
|
|
).catch((error) => {
|
|
|
|
toast.error(error);
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (res) {
|
2024-06-24 16:09:45 +00:00
|
|
|
toast.success($i18n.t('Valves updated'));
|
2024-06-22 19:08:32 +00:00
|
|
|
valves = res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-06-22 09:29:33 +00:00
|
|
|
$: if (tab) {
|
|
|
|
selectedId = '';
|
|
|
|
}
|
2024-06-22 19:08:32 +00:00
|
|
|
|
|
|
|
$: if (selectedId) {
|
|
|
|
getUserValves();
|
|
|
|
}
|
2024-06-22 09:29:33 +00:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<form
|
|
|
|
class="flex flex-col h-full justify-between space-y-3 text-sm"
|
|
|
|
on:submit|preventDefault={() => {
|
2024-06-22 19:08:32 +00:00
|
|
|
submitHandler();
|
2024-06-22 09:29:33 +00:00
|
|
|
dispatch('save');
|
|
|
|
}}
|
|
|
|
>
|
2024-08-02 15:19:52 +00:00
|
|
|
<div class="flex flex-col">
|
|
|
|
<div class="space-y-1">
|
|
|
|
<div class="flex gap-2">
|
|
|
|
<div class="flex-1">
|
2024-06-22 18:26:33 +00:00
|
|
|
<select
|
2024-08-02 15:19:52 +00:00
|
|
|
class=" w-full rounded text-xs py-2 px-1 bg-transparent outline-none"
|
2024-06-22 18:26:33 +00:00
|
|
|
bind:value={tab}
|
|
|
|
placeholder="Select"
|
|
|
|
>
|
|
|
|
<option value="tools">{$i18n.t('Tools')}</option>
|
|
|
|
<option value="functions">{$i18n.t('Functions')}</option>
|
|
|
|
</select>
|
|
|
|
</div>
|
2024-06-22 09:29:33 +00:00
|
|
|
|
|
|
|
<div class="flex-1">
|
|
|
|
<select
|
2024-08-02 15:19:52 +00:00
|
|
|
class="w-full rounded py-2 px-1 text-xs bg-transparent outline-none"
|
2024-06-22 09:29:33 +00:00
|
|
|
bind:value={selectedId}
|
|
|
|
on:change={async () => {
|
|
|
|
await tick();
|
|
|
|
}}
|
|
|
|
>
|
2024-06-22 18:26:33 +00:00
|
|
|
{#if tab === 'tools'}
|
|
|
|
<option value="" selected disabled class="bg-gray-100 dark:bg-gray-700"
|
|
|
|
>{$i18n.t('Select a tool')}</option
|
|
|
|
>
|
2024-06-22 09:29:33 +00:00
|
|
|
|
2024-06-22 18:26:33 +00:00
|
|
|
{#each $tools as tool, toolIdx}
|
|
|
|
<option value={tool.id} class="bg-gray-100 dark:bg-gray-700">{tool.name}</option>
|
|
|
|
{/each}
|
|
|
|
{:else if tab === 'functions'}
|
|
|
|
<option value="" selected disabled class="bg-gray-100 dark:bg-gray-700"
|
|
|
|
>{$i18n.t('Select a function')}</option
|
|
|
|
>
|
|
|
|
|
|
|
|
{#each $functions as func, funcIdx}
|
|
|
|
<option value={func.id} class="bg-gray-100 dark:bg-700">{func.name}</option>
|
|
|
|
{/each}
|
|
|
|
{/if}
|
2024-06-22 09:29:33 +00:00
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2024-06-22 19:08:32 +00:00
|
|
|
{#if selectedId}
|
2024-08-02 15:19:52 +00:00
|
|
|
<hr class="dark:border-gray-800 my-1 w-full" />
|
2024-06-22 18:26:33 +00:00
|
|
|
|
2024-08-02 15:19:52 +00:00
|
|
|
<div class="my-2 text-xs">
|
2024-06-22 19:08:32 +00:00
|
|
|
{#if !loading}
|
2024-08-02 15:36:16 +00:00
|
|
|
<Valves
|
|
|
|
{valvesSpec}
|
|
|
|
bind:valves
|
|
|
|
on:change={() => {
|
|
|
|
debounceSubmitHandler();
|
|
|
|
}}
|
|
|
|
/>
|
2024-06-22 19:08:32 +00:00
|
|
|
{:else}
|
|
|
|
<Spinner className="size-5" />
|
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
{/if}
|
2024-06-22 09:29:33 +00:00
|
|
|
</div>
|
|
|
|
</form>
|