Valves.svelte 7.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
19
	import ManageModal from './Personalization/ManageModal.svelte';
	import Tooltip from '$lib/components/common/Tooltip.svelte';
20
	import Spinner from '$lib/components/common/Spinner.svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
21
	import Switch from '$lib/components/common/Switch.svelte';
22

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

	const i18n = getContext('i18n');

	export let saveSettings: Function;

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

32
33
34
35
36
37
38
39
40
41
42
43
44
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
	let loading = false;

	let valvesSpec = null;
	let valves = {};

	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
77
					toast.success($i18n.t('Valves updated'));
78
79
80
81
82
83
84
85
86
87
88
89
90
					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
91
					toast.success($i18n.t('Valves updated'));
92
93
94
95
96
97
					valves = res;
				}
			}
		}
	};

Timothy J. Baek's avatar
Timothy J. Baek committed
98
99
100
	$: if (tab) {
		selectedId = '';
	}
101
102
103
104

	$: if (selectedId) {
		getUserValves();
	}
Timothy J. Baek's avatar
Timothy J. Baek committed
105
106
107
108
109
</script>

<form
	class="flex flex-col h-full justify-between space-y-3 text-sm"
	on:submit|preventDefault={() => {
110
		submitHandler();
Timothy J. Baek's avatar
Timothy J. Baek committed
111
112
113
114
		dispatch('save');
	}}
>
	<div class="flex flex-col pr-1.5 overflow-y-scroll max-h-[25rem]">
Timothy J. Baek's avatar
Timothy J. Baek committed
115
116
117
118
119
120
121
		<div>
			<div class="flex items-center justify-between mb-2">
				<Tooltip content="">
					<div class="text-sm font-medium">
						{$i18n.t('Manage Valves')}
					</div>
				</Tooltip>
Timothy J. Baek's avatar
Timothy J. Baek committed
122

Timothy J. Baek's avatar
Timothy J. Baek committed
123
124
125
126
127
128
129
130
131
132
133
				<div class=" self-end">
					<select
						class=" dark:bg-gray-900 w-fit pr-8 rounded text-xs bg-transparent outline-none text-right"
						bind:value={tab}
						placeholder="Select"
					>
						<option value="tools">{$i18n.t('Tools')}</option>
						<option value="functions">{$i18n.t('Functions')}</option>
					</select>
				</div>
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
134
135
		</div>

Timothy J. Baek's avatar
Timothy J. Baek committed
136
		<div class="space-y-1">
Timothy J. Baek's avatar
Timothy J. Baek committed
137
138
139
140
141
142
143
144
145
			<div class="flex gap-2">
				<div class="flex-1">
					<select
						class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
						bind:value={selectedId}
						on:change={async () => {
							await tick();
						}}
					>
Timothy J. Baek's avatar
Timothy J. Baek committed
146
147
148
149
						{#if tab === 'tools'}
							<option value="" selected disabled class="bg-gray-100 dark:bg-gray-700"
								>{$i18n.t('Select a tool')}</option
							>
Timothy J. Baek's avatar
Timothy J. Baek committed
150

Timothy J. Baek's avatar
Timothy J. Baek committed
151
152
153
154
155
156
157
158
159
160
161
162
							{#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}
Timothy J. Baek's avatar
Timothy J. Baek committed
163
164
165
166
167
					</select>
				</div>
			</div>
		</div>

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

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
			<div>
				{#if !loading}
					{#if valvesSpec}
						{#each Object.keys(valvesSpec.properties) as property, idx}
							<div class=" py-0.5 w-full justify-between">
								<div class="flex w-full justify-between">
									<div class=" self-center text-xs font-medium">
										{valvesSpec.properties[property].title}

										{#if (valvesSpec?.required ?? []).includes(property)}
											<span class=" text-gray-500">*required</span>
										{/if}
									</div>

									<button
										class="p-1 px-3 text-xs flex rounded transition"
										type="button"
										on:click={() => {
Timothy J. Baek's avatar
Timothy J. Baek committed
189
190
191
192
											valves[property] =
												(valves[property] ?? null) === null
													? valvesSpec.properties[property]?.default ?? ''
													: null;
193
194
195
										}}
									>
										{#if (valves[property] ?? null) === null}
196
197
198
199
200
201
202
											<span class="ml-2 self-center">
												{#if (valvesSpec?.required ?? []).includes(property)}
													{$i18n.t('None')}
												{:else}
													{$i18n.t('Default')}
												{/if}
											</span>
203
204
205
206
207
208
209
										{:else}
											<span class="ml-2 self-center"> {$i18n.t('Custom')} </span>
										{/if}
									</button>
								</div>

								{#if (valves[property] ?? null) !== null}
Timothy J. Baek's avatar
Timothy J. Baek committed
210
									<!-- {valves[property]} -->
Timothy J. Baek's avatar
Timothy J. Baek committed
211
									<div class="flex mt-0.5 mb-1.5 space-x-2">
212
										<div class=" flex-1">
Timothy J. Baek's avatar
Timothy J. Baek committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
											{#if valvesSpec.properties[property]?.enum ?? null}
												<select
													class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
													bind:value={valves[property]}
												>
													{#each valvesSpec.properties[property].enum as option}
														<option value={option} selected={option === valves[property]}>
															{option}
														</option>
													{/each}
												</select>
											{:else if (valvesSpec.properties[property]?.type ?? null) === 'boolean'}
												<div class="flex justify-between items-center">
													<div class="text-xs text-gray-500">
														{valves[property] ? 'Enabled' : 'Disabled'}
													</div>

													<div class=" pr-2">
														<Switch bind:state={valves[property]} />
													</div>
												</div>
											{:else}
												<input
													class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
													type="text"
													placeholder={valvesSpec.properties[property].title}
													bind:value={valves[property]}
													autocomplete="off"
													required
												/>
											{/if}
244
245
246
										</div>
									</div>
								{/if}
247
248
249
250
251
252

								{#if (valvesSpec.properties[property]?.description ?? null) !== null}
									<div class="text-xs text-gray-500">
										{valvesSpec.properties[property].description}
									</div>
								{/if}
253
254
255
256
257
258
259
260
261
262
							</div>
						{/each}
					{:else}
						<div>No valves</div>
					{/if}
				{:else}
					<Spinner className="size-5" />
				{/if}
			</div>
		{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
263
264
265
266
267
268
269
270
271
272
273
	</div>

	<div class="flex justify-end text-sm font-medium">
		<button
			class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
			type="submit"
		>
			{$i18n.t('Save')}
		</button>
	</div>
</form>