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 { 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 (
|
||||
<input type="text" placeholder={props.placeholder}
|
||||
value={inputValue} onChange={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"/>
|
||||
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"/>
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
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