Final touches
This commit is contained in:
parent
52f901c6c4
commit
c821d86362
2 changed files with 48 additions and 8 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import { useState, useEffect } from "preact/hooks";
|
import { useState, useEffect } from "preact/hooks";
|
||||||
import { set } from "../../util/IDB";
|
import { set } from "../../util/IDB";
|
||||||
|
import { uninstallServiceWorkers } from "../../util/SWHelper";
|
||||||
|
|
||||||
interface BareInputProps {
|
interface BareInputProps {
|
||||||
placeholder: string;
|
placeholder: string;
|
||||||
|
|
@ -8,17 +9,37 @@ interface BareInputProps {
|
||||||
|
|
||||||
function BareInput(props: BareInputProps) {
|
function BareInput(props: BareInputProps) {
|
||||||
const value = localStorage.getItem(props.storageKey) || "/bare/";
|
const value = localStorage.getItem(props.storageKey) || "/bare/";
|
||||||
const [inputValue, setInputValue] = useState(value);
|
const [inputValue, setInputValue] = useState(value);
|
||||||
function handleChange(event: any) {
|
function validateUrl(url: string) {
|
||||||
setInputValue(event.target.value);
|
let finalUrl = url;
|
||||||
set(props.storageKey, event.target.value);
|
if (url === "/bare/" || url === "/bare") {
|
||||||
localStorage.setItem(props.storageKey, event.target.value);
|
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 (
|
return (
|
||||||
<input type="text" placeholder={props.placeholder}
|
<input type="text" placeholder={props.placeholder}
|
||||||
value={inputValue} onChange={handleChange}
|
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"/>
|
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"/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
19
src/util/SWHelper.js
Normal file
19
src/util/SWHelper.js
Normal file
|
|
@ -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 };
|
||||||
Loading…
Add table
Reference in a new issue