From c821d863627104d6d1813c5f0bf1695de6abb49e Mon Sep 17 00:00:00 2001 From: MotorTruck1221 Date: Wed, 3 Jan 2024 17:35:34 -0700 Subject: [PATCH] Final touches --- src/pages/Settings/BareInput.tsx | 37 +++++++++++++++++++++++++------- src/util/SWHelper.js | 19 ++++++++++++++++ 2 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 src/util/SWHelper.js diff --git a/src/pages/Settings/BareInput.tsx b/src/pages/Settings/BareInput.tsx index 77cabd9..df0868e 100644 --- a/src/pages/Settings/BareInput.tsx +++ b/src/pages/Settings/BareInput.tsx @@ -1,5 +1,6 @@ import { useState, useEffect } from "preact/hooks"; import { set } from "../../util/IDB"; +import { uninstallServiceWorkers } from "../../util/SWHelper"; interface BareInputProps { placeholder: string; @@ -8,17 +9,37 @@ interface BareInputProps { function BareInput(props: BareInputProps) { const value = localStorage.getItem(props.storageKey) || "/bare/"; - const [inputValue, setInputValue] = useState(value); - function handleChange(event: any) { - setInputValue(event.target.value); - set(props.storageKey, event.target.value); - localStorage.setItem(props.storageKey, event.target.value); + const [inputValue, setInputValue] = useState(value); + function validateUrl(url: string) { + let finalUrl = url; + if (url === "/bare/" || url === "/bare") { + finalUrl = "/bare/"; + return finalUrl; + } + if (url === null || url === undefined || url === "") { + finalUrl = "/bare/"; + return finalUrl; + } + if (!url.endsWith("/")) { + finalUrl = url + "/"; + } + if (!finalUrl.startsWith("http://") && !finalUrl.startsWith("https://")) { + finalUrl = "https://" + finalUrl; + } + return finalUrl; + } + function handleChange(event: any) { + const url = validateUrl(event.target.value); + setInputValue(event.target.value); + set(props.storageKey, url); + localStorage.setItem(props.storageKey, url); + uninstallServiceWorkers(); + window.location.reload(); } - return ( + value={inputValue} onBlur={handleChange} + className="font-roboto flex flex-row p-4 h-14 w-56 border border-input-border-color bg-input text-center text-xl rounded-2xl"/> ); } diff --git a/src/util/SWHelper.js b/src/util/SWHelper.js new file mode 100644 index 0000000..235f792 --- /dev/null +++ b/src/util/SWHelper.js @@ -0,0 +1,19 @@ +function updateServiceWorkers() { + navigator.serviceWorker.getRegistrations().then(function (registrations) { + for (let registration of registrations) { + registration.update(); + console.log("Service Worker Updated"); + } + }); +} + +function uninstallServiceWorkers() { + navigator.serviceWorker.getRegistrations().then(function (registrations) { + for (let registration of registrations) { + registration.unregister(); + console.log("Service Worker Unregistered"); + } + }); +} + +export { updateServiceWorkers, uninstallServiceWorkers };