From a9831a40a3cc576bce5beff86541cce44f7b7e36 Mon Sep 17 00:00:00 2001 From: wukko Date: Sat, 23 Nov 2024 23:21:54 +0600 Subject: [PATCH] web/SettingsInput: fix uuid support & refactor --- .../components/settings/SettingsInput.svelte | 79 +++++++------------ .../routes/settings/instances/+page.svelte | 1 + 2 files changed, 30 insertions(+), 50 deletions(-) diff --git a/web/src/components/settings/SettingsInput.svelte b/web/src/components/settings/SettingsInput.svelte index 3fda62b4..0c96e338 100644 --- a/web/src/components/settings/SettingsInput.svelte +++ b/web/src/components/settings/SettingsInput.svelte @@ -19,73 +19,52 @@ export let settingContext: Context; export let placeholder: string; export let type: "url" | "uuid" = "url"; + export let showInstanceWarning = false; - const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/; + const regex = { + url: "https?:\\/\\/[a-z0-9.\\-]+(:\\d+)?/?", + uuid: "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", + }; + + let input: HTMLInputElement; let inputValue: string = String(get(settings)[settingContext][settingId]); let inputFocused = false; let validInput: boolean; - const checkInput = () => { - try { - if (type === "uuid") { - const regex = UUID_REGEX.test(inputValue); - if (!regex) throw new Error("invalid uuid"); - } else { - new URL(inputValue).origin.toString(); - } - validInput = true; - } catch { - validInput = false; - } + const writeToSettings = (value: string, type: "url" | "uuid" | "text") => { + updateSetting({ + [settingContext]: { + [settingId]: + type === "url" ? new URL(value).origin.toString() : value, + }, + }); + inputValue = String(get(settings)[settingContext][settingId]); }; - const writeInput = () => { - let input; - try { - if (type === "uuid") { - const regex = UUID_REGEX.test(inputValue); - if (regex) input = inputValue.toString(); - } else { - input = new URL(inputValue).origin.toString(); + const save = async () => { + if (showInstanceWarning) { + await customInstanceWarning(); + + if ($settings.processing.seenCustomWarning && inputValue) { + return writeToSettings(inputValue, type); } - } catch { - validInput = false; + return; } - updateSetting({ - [settingContext]: { - [settingId]: input, - }, - }); - inputValue = String(get(settings)[settingContext][settingId]); + return writeToSettings(inputValue, type); }; - - const reset = () => { - updateSetting({ - [settingContext]: { - [settingId]: "", - }, - }); - inputValue = String(get(settings)[settingContext][settingId]); - } - - const save = async () => { - await customInstanceWarning(); - if ($settings.processing.seenCustomWarning && inputValue) { - writeInput(); - } - }
checkInput()} + on:input={() => (validInput = input.checkValidity())} on:input={() => (inputFocused = true)} on:focus={() => (inputFocused = true)} on:blur={() => (inputFocused = false)} @@ -93,6 +72,7 @@ autocomplete="off" autocapitalize="off" maxlength="64" + pattern={regex[type]} {placeholder} />
@@ -101,9 +81,8 @@ diff --git a/web/src/routes/settings/instances/+page.svelte b/web/src/routes/settings/instances/+page.svelte index 6b749e21..b1f318c7 100644 --- a/web/src/routes/settings/instances/+page.svelte +++ b/web/src/routes/settings/instances/+page.svelte @@ -23,6 +23,7 @@ settingContext="processing" settingId="customInstanceURL" placeholder="https://instance.url.example/" + showInstanceWarning /> {/if}