Pipelines.svelte 4.98 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
<script lang="ts">
	import { v4 as uuidv4 } from 'uuid';

4
	import { getContext, onMount, tick } from 'svelte';
Timothy J. Baek's avatar
Timothy J. Baek committed
5
6
7
8

	import type { Writable } from 'svelte/store';
	import type { i18n as i18nType } from 'i18next';
	import { stringify } from 'postcss';
9
10
11
12
	import {
		getPipelineValves,
		getPipelineValvesSpec,
		updatePipelineValves,
Timothy J. Baek's avatar
Timothy J. Baek committed
13
14
		getPipelines,
		getModels
15
	} from '$lib/apis';
Timothy J. Baek's avatar
Timothy J. Baek committed
16
	import Spinner from '$lib/components/common/Spinner.svelte';
17
	import { toast } from 'svelte-sonner';
Timothy J. Baek's avatar
Timothy J. Baek committed
18
	import { models } from '$lib/stores';
Timothy J. Baek's avatar
Timothy J. Baek committed
19

Timothy J. Baek's avatar
Timothy J. Baek committed
20
21
22
23
	const i18n: Writable<i18nType> = getContext('i18n');

	export let saveHandler: Function;

Timothy J. Baek's avatar
Timothy J. Baek committed
24
	let pipelines = null;
25

Timothy J. Baek's avatar
Timothy J. Baek committed
26
	let valves = null;
27
28
29
30
31
32
	let valves_spec = null;

	let selectedPipelineIdx = null;

	const updateHandler = async () => {
		const pipeline = pipelines[selectedPipelineIdx];
Timothy J. Baek's avatar
Timothy J. Baek committed
33

34
		if (pipeline && (pipeline?.pipeline?.valves ?? false)) {
35
36
37
38
			if (valves?.pipelines ?? false) {
				valves.pipelines = valves.pipelines.split(',').map((v) => v.trim());
			}

39
40
41
42
43
			const res = await updatePipelineValves(localStorage.token, pipeline.id, valves).catch(
				(error) => {
					toast.error(error);
				}
			);
Timothy J. Baek's avatar
Timothy J. Baek committed
44

45
46
			if (res) {
				toast.success('Valves updated successfully');
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
47
				setPipelines();
Timothy J. Baek's avatar
Timothy J. Baek committed
48
				models.set(await getModels(localStorage.token));
49
50
51
52
53
54
				saveHandler();
			}
		} else {
			toast.error('No valves to update');
		}
	};
Timothy J. Baek's avatar
Timothy J. Baek committed
55

Timothy J. Baek's avatar
Timothy J. Baek committed
56
57
58
	const getValves = async (idx) => {
		valves_spec = await getPipelineValvesSpec(localStorage.token, pipelines[idx].id);
		valves = await getPipelineValves(localStorage.token, pipelines[idx].id);
59
60
61
62

		if (valves?.pipelines ?? false) {
			valves.pipelines = valves.pipelines.join(',');
		}
Timothy J. Baek's avatar
Timothy J. Baek committed
63
64
	};

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
65
	const setPipelines = async () => {
Timothy J. Baek's avatar
Timothy J. Baek committed
66
		pipelines = await getPipelines(localStorage.token);
67
68
69

		if (pipelines.length > 0) {
			selectedPipelineIdx = 0;
Timothy J. Baek's avatar
Timothy J. Baek committed
70
			await getValves(selectedPipelineIdx);
71
		}
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
72
73
74
75
	};

	onMount(async () => {
		setPipelines();
Timothy J. Baek's avatar
Timothy J. Baek committed
76
77
78
79
80
81
	});
</script>

<form
	class="flex flex-col h-full justify-between space-y-3 text-sm"
	on:submit|preventDefault={async () => {
82
		updateHandler();
Timothy J. Baek's avatar
Timothy J. Baek committed
83
84
	}}
>
Timothy J. Baek's avatar
Timothy J. Baek committed
85
	<div class=" space-y-2 pr-1.5 overflow-y-scroll max-h-80 h-full">
Timothy J. Baek's avatar
fix  
Timothy J. Baek committed
86
		{#if pipelines !== null && pipelines.length > 0}
Timothy J. Baek's avatar
Timothy J. Baek committed
87
88
			<div class="flex w-full justify-between mb-2">
				<div class=" self-center text-sm font-semibold">
Timothy J. Baek's avatar
Timothy J. Baek committed
89
					{$i18n.t('Pipelines')}
Timothy J. Baek's avatar
Timothy J. Baek committed
90
91
				</div>
			</div>
92
			<div class="space-y-1">
Timothy J. Baek's avatar
Timothy J. Baek committed
93
94
95
96
97
98
				{#if pipelines.length > 0}
					<div class="flex gap-2">
						<div class="flex-1 pb-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={selectedPipelineIdx}
99
100
101
								placeholder={$i18n.t('Select a pipeline')}
								on:change={async () => {
									await tick();
Timothy J. Baek's avatar
Timothy J. Baek committed
102
									await getValves(selectedPipelineIdx);
103
								}}
Timothy J. Baek's avatar
Timothy J. Baek committed
104
105
							>
								{#each pipelines as pipeline, idx}
106
107
108
									<option value={idx} class="bg-gray-100 dark:bg-gray-700"
										>{pipeline.name} ({pipeline.pipeline.type ?? 'pipe'})</option
									>
Timothy J. Baek's avatar
Timothy J. Baek committed
109
110
111
								{/each}
							</select>
						</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
112
					</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
113
114
115
116
				{/if}

				<div class="text-sm font-medium">{$i18n.t('Valves')}</div>

117
				<div class="space-y-1">
Timothy J. Baek's avatar
Timothy J. Baek committed
118
119
					{#if pipelines[selectedPipelineIdx].pipeline.valves}
						{#if valves}
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
							{#each Object.keys(valves_spec.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">
											{valves_spec.properties[property].title}
										</div>

										<button
											class="p-1 px-3 text-xs flex rounded transition"
											type="button"
											on:click={() => {
												valves[property] = (valves[property] ?? null) === null ? '' : null;
											}}
										>
											{#if (valves[property] ?? null) === null}
												<span class="ml-2 self-center"> {$i18n.t('None')} </span>
											{:else}
												<span class="ml-2 self-center"> {$i18n.t('Custom')} </span>
											{/if}
										</button>
									</div>

									{#if (valves[property] ?? null) !== null}
										<div class="flex mt-0.5 space-x-2">
											<div class=" flex-1">
												<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={valves_spec.properties[property].title}
													bind:value={valves[property]}
													autocomplete="off"
												/>
											</div>
										</div>
									{/if}
								</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
							{/each}
						{:else}
							<Spinner className="size-5" />
						{/if}
					{:else}
						<div>No valves</div>
					{/if}
				</div>
			</div>
		{:else if pipelines !== null && pipelines.length === 0}
			<div>Pipelines Not Detected</div>
		{:else}
			<div class="flex h-full justify-center">
				<div class="my-auto">
					<Spinner className="size-6" />
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
172
			</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
173
		{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
174
175
176
177
178
179
180
181
182
183
	</div>
	<div class="flex justify-end pt-3 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"
		>
			Save
		</button>
	</div>
</form>