Sfoglia il codice sorgente

feat: webhook settings frontend

Timothy J. Baek 1 anno fa
parent
commit
2481e48a3a
2 ha cambiato i file con 83 aggiunte e 1 eliminazioni
  1. 57 0
      src/lib/apis/index.ts
  2. 26 1
      src/lib/components/admin/Settings/General.svelte

+ 57 - 0
src/lib/apis/index.ts

@@ -139,3 +139,60 @@ export const updateModelFilterConfig = async (
 
 	return res;
 };
+
+export const getWebhookUrl = async (token: string) => {
+	let error = null;
+
+	const res = await fetch(`${WEBUI_BASE_URL}/api/webhook`, {
+		method: 'GET',
+		headers: {
+			'Content-Type': 'application/json',
+			Authorization: `Bearer ${token}`
+		}
+	})
+		.then(async (res) => {
+			if (!res.ok) throw await res.json();
+			return res.json();
+		})
+		.catch((err) => {
+			console.log(err);
+			error = err;
+			return null;
+		});
+
+	if (error) {
+		throw error;
+	}
+
+	return res.url;
+};
+
+export const updateWebhookUrl = async (token: string, url: string) => {
+	let error = null;
+
+	const res = await fetch(`${WEBUI_BASE_URL}/api/webhook`, {
+		method: 'POST',
+		headers: {
+			'Content-Type': 'application/json',
+			Authorization: `Bearer ${token}`
+		},
+		body: JSON.stringify({
+			url: url
+		})
+	})
+		.then(async (res) => {
+			if (!res.ok) throw await res.json();
+			return res.json();
+		})
+		.catch((err) => {
+			console.log(err);
+			error = err;
+			return null;
+		});
+
+	if (error) {
+		throw error;
+	}
+
+	return res.url;
+};

+ 26 - 1
src/lib/components/admin/Settings/General.svelte

@@ -1,4 +1,5 @@
 <script lang="ts">
+	import { getWebhookUrl, updateWebhookUrl } from '$lib/apis';
 	import {
 		getDefaultUserRole,
 		getJWTExpiresDuration,
@@ -16,6 +17,8 @@
 	let defaultUserRole = 'pending';
 	let JWTExpiresIn = '';
 
+	let webhookUrl = '';
+
 	const toggleSignUpEnabled = async () => {
 		signUpEnabled = await toggleSignUpEnabledStatus(localStorage.token);
 	};
@@ -28,18 +31,23 @@
 		JWTExpiresIn = await updateJWTExpiresDuration(localStorage.token, duration);
 	};
 
+	const updateWebhookUrlHandler = async () => {
+		webhookUrl = await updateWebhookUrl(localStorage.token, webhookUrl);
+	};
+
 	onMount(async () => {
 		signUpEnabled = await getSignUpEnabledStatus(localStorage.token);
 		defaultUserRole = await getDefaultUserRole(localStorage.token);
 		JWTExpiresIn = await getJWTExpiresDuration(localStorage.token);
+		webhookUrl = await getWebhookUrl(localStorage.token);
 	});
 </script>
 
 <form
 	class="flex flex-col h-full justify-between space-y-3 text-sm"
 	on:submit|preventDefault={() => {
-		// console.log('submit');
 		updateJWTExpiresDurationHandler(JWTExpiresIn);
+		updateWebhookUrlHandler();
 		saveHandler();
 	}}
 >
@@ -108,6 +116,23 @@
 
 			<hr class=" dark:border-gray-700 my-3" />
 
+			<div class=" w-full justify-between">
+				<div class="flex w-full justify-between">
+					<div class=" self-center text-xs font-medium">{$i18n.t('Webhook URL')}</div>
+				</div>
+
+				<div class="flex mt-2 space-x-2">
+					<input
+						class="w-full rounded py-1.5 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none border border-gray-100 dark:border-gray-600"
+						type="text"
+						placeholder={`https://example.com/webhook`}
+						bind:value={webhookUrl}
+					/>
+				</div>
+			</div>
+
+			<hr class=" dark:border-gray-700 my-3" />
+
 			<div class=" w-full justify-between">
 				<div class="flex w-full justify-between">
 					<div class=" self-center text-xs font-medium">{$i18n.t('JWT Expiration')}</div>