From 3de15b1b4cdf612078eec2812cbedc7ce718845f Mon Sep 17 00:00:00 2001 From: Nikita Karamov Date: Fri, 17 Mar 2023 23:52:42 +0100 Subject: [PATCH] Re-implement saving instances Also implements saving of multiple instances, which closes https://github.com/kytta/share2fedi/issues/29 --- lib/main.js | 29 ------------- src/components/instance-select.astro | 63 +++++++++++++++++++++++++--- src/util.ts | 16 +++++++ 3 files changed, 74 insertions(+), 34 deletions(-) diff --git a/lib/main.js b/lib/main.js index b3b46ed..bbbe886 100644 --- a/lib/main.js +++ b/lib/main.js @@ -27,9 +27,7 @@ */ const LOCAL_STORAGE_KEY = "recentInstances"; -const RECENT_INSTANCES_SIZE = 5; -const $form = document.querySelector("#js-s2f-form"); const $instance = document.querySelector("#instance"); /** @@ -54,31 +52,6 @@ function getRecentInstances() { return JSON.parse(storedValue); } -function rememberInstance(instance) { - const recentInstances = getRecentInstances(); - - const index = recentInstances.indexOf(instance); - if (index >= 0) { - recentInstances.splice(index, 1); - } - recentInstances.unshift(instance); - recentInstances.length = RECENT_INSTANCES_SIZE; - - window.localStorage.setItem( - LOCAL_STORAGE_KEY, - JSON.stringify(recentInstances), - ); -} - -function onFormSubmit(event) { - const formData = new FormData(event.target); - - if (formData.get("remember")) { - rememberInstance(formData.get("instance")); - } - return true; -} - let prefillInstance = getRecentInstances()[0]; const URLParameters = window.location.search.slice(1).split("&"); @@ -96,5 +69,3 @@ for (const URLParameter of URLParameters) { if (prefillInstance != undefined) { $instance.value = normalizeUrl(prefillInstance); } - -$form.addEventListener("submit", onFormSubmit); diff --git a/src/components/instance-select.astro b/src/components/instance-select.astro index de97d21..10f72d1 100644 --- a/src/components/instance-select.astro +++ b/src/components/instance-select.astro @@ -12,7 +12,7 @@ try { {instances.map((instance) => -