Valves.svelte 4.68 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
<script lang="ts">
2
3
	import { toast } from 'svelte-sonner';

Timothy J. Baek's avatar
Timothy J. Baek committed
4
5
	import { config, functions, models, settings, tools, user } from '$lib/stores';
	import { createEventDispatcher, onMount, getContext, tick } from 'svelte';
6
7
8
9
10
11
12
13
14
15
16
17

	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';

Timothy J. Baek's avatar
Timothy J. Baek committed
18
	import Tooltip from '$lib/components/common/Tooltip.svelte';
19
	import Spinner from '$lib/components/common/Spinner.svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
20
	import Valves from '$lib/components/common/Valves.svelte';
21

Timothy J. Baek's avatar
Timothy J. Baek committed
22
23
24
25
26
27
28
	const dispatch = createEventDispatcher();

	const i18n = getContext('i18n');

	let tab = 'tools';
	let selectedId = '';

29
30
31
32
33
	let loading = false;

	let valvesSpec = null;
	let valves = {};

34
35
36
37
38
39
40
41
42
43
	let debounceTimer;

	const debounceSubmitHandler = async () => {
		if (debounceTimer) {
			clearTimeout(debounceTimer);
		}

		// Set a new timer
		debounceTimer = setTimeout(() => {
			submitHandler();
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
44
		}, 500); // 0.5 second debounce
45
46
	};

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
	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) {
SimonOriginal's avatar
SimonOriginal committed
87
					toast.success($i18n.t('Valves updated'));
88
89
90
91
92
93
94
95
96
97
98
99
100
					valves = res;
				}
			} else if (tab === 'functions') {
				const res = await updateFunctionUserValvesById(
					localStorage.token,
					selectedId,
					valves
				).catch((error) => {
					toast.error(error);
					return null;
				});

				if (res) {
SimonOriginal's avatar
SimonOriginal committed
101
					toast.success($i18n.t('Valves updated'));
102
103
104
105
106
107
					valves = res;
				}
			}
		}
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
108
109
110
	$: if (tab) {
		selectedId = '';
	}
111
112
113
114

	$: if (selectedId) {
		getUserValves();
	}
Timothy J. Baek's avatar
Timothy J. Baek committed
115
116
117
118
119
</script>

<form
	class="flex flex-col h-full justify-between space-y-3 text-sm"
	on:submit|preventDefault={() => {
120
		submitHandler();
Timothy J. Baek's avatar
Timothy J. Baek committed
121
122
123
		dispatch('save');
	}}
>
Timothy J. Baek's avatar
Timothy J. Baek committed
124
125
126
127
	<div class="flex flex-col">
		<div class="space-y-1">
			<div class="flex gap-2">
				<div class="flex-1">
Timothy J. Baek's avatar
Timothy J. Baek committed
128
					<select
Timothy J. Baek's avatar
Timothy J. Baek committed
129
						class="  w-full rounded text-xs py-2 px-1 bg-transparent outline-none"
Timothy J. Baek's avatar
Timothy J. Baek committed
130
131
132
						bind:value={tab}
						placeholder="Select"
					>
Timothy J. Baek's avatar
Timothy J. Baek committed
133
134
135
136
						<option value="tools" class="bg-gray-100 dark:bg-gray-800">{$i18n.t('Tools')}</option>
						<option value="functions" class="bg-gray-100 dark:bg-gray-800"
							>{$i18n.t('Functions')}</option
						>
Timothy J. Baek's avatar
Timothy J. Baek committed
137
138
					</select>
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
139
140
141

				<div class="flex-1">
					<select
Timothy J. Baek's avatar
Timothy J. Baek committed
142
						class="w-full rounded py-2 px-1 text-xs bg-transparent outline-none"
Timothy J. Baek's avatar
Timothy J. Baek committed
143
144
145
146
147
						bind:value={selectedId}
						on:change={async () => {
							await tick();
						}}
					>
Timothy J. Baek's avatar
Timothy J. Baek committed
148
						{#if tab === 'tools'}
Timothy J. Baek's avatar
Timothy J. Baek committed
149
							<option value="" selected disabled class="bg-gray-100 dark:bg-gray-800"
Timothy J. Baek's avatar
Timothy J. Baek committed
150
151
								>{$i18n.t('Select a tool')}</option
							>
Timothy J. Baek's avatar
Timothy J. Baek committed
152

Timothy J. Baek's avatar
Timothy J. Baek committed
153
							{#each $tools as tool, toolIdx}
Timothy J. Baek's avatar
Timothy J. Baek committed
154
								<option value={tool.id} class="bg-gray-100 dark:bg-gray-800">{tool.name}</option>
Timothy J. Baek's avatar
Timothy J. Baek committed
155
156
							{/each}
						{:else if tab === 'functions'}
Timothy J. Baek's avatar
Timothy J. Baek committed
157
							<option value="" selected disabled class="bg-gray-100 dark:bg-gray-800"
Timothy J. Baek's avatar
Timothy J. Baek committed
158
159
160
161
								>{$i18n.t('Select a function')}</option
							>

							{#each $functions as func, funcIdx}
Timothy J. Baek's avatar
Timothy J. Baek committed
162
								<option value={func.id} class="bg-gray-100 dark:bg-gray-800">{func.name}</option>
Timothy J. Baek's avatar
Timothy J. Baek committed
163
164
							{/each}
						{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
165
166
167
168
169
					</select>
				</div>
			</div>
		</div>

170
		{#if selectedId}
Timothy J. Baek's avatar
Timothy J. Baek committed
171
			<hr class="dark:border-gray-800 my-1 w-full" />
Timothy J. Baek's avatar
Timothy J. Baek committed
172

Timothy J. Baek's avatar
Timothy J. Baek committed
173
			<div class="my-2 text-xs">
174
				{#if !loading}
175
176
177
178
179
180
181
					<Valves
						{valvesSpec}
						bind:valves
						on:change={() => {
							debounceSubmitHandler();
						}}
					/>
182
183
184
185
186
				{:else}
					<Spinner className="size-5" />
				{/if}
			</div>
		{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
187
188
	</div>
</form>