Rewrite nanostore for saved instances

This commit is contained in:
Nikita Karamov 2023-08-26 15:56:50 +02:00
parent d6363e10f9
commit 870406209e
No known key found for this signature in database
GPG key ID: 41D6F71EE78E77CD
2 changed files with 28 additions and 24 deletions

View file

@ -66,10 +66,7 @@ const { prefilledInstance } = Astro.props;
<script> <script>
import { getUrlDomain, normalizeURL } from "@scripts/util"; import { getUrlDomain, normalizeURL } from "@scripts/util";
import { import { $savedInstances, save } from "@stores/saved-instances";
savedInstances as instanceStore,
saveInstance,
} from "@stores/saved-instances";
const $form = document.querySelector("#js-s2f-form") as HTMLFormElement; const $form = document.querySelector("#js-s2f-form") as HTMLFormElement;
const $instanceContainer = document.querySelector( const $instanceContainer = document.querySelector(
@ -77,7 +74,7 @@ const { prefilledInstance } = Astro.props;
) as HTMLLabelElement; ) as HTMLLabelElement;
const $instance = document.querySelector("#instance") as HTMLInputElement; const $instance = document.querySelector("#instance") as HTMLInputElement;
const savedInstances: Set<string> = instanceStore.get(); const savedInstances: Set<string> = $savedInstances.get();
if (savedInstances.size > 0) { if (savedInstances.size > 0) {
$instanceContainer.append( $instanceContainer.append(
@ -110,7 +107,7 @@ const { prefilledInstance } = Astro.props;
if (formData.get("remember")) { if (formData.get("remember")) {
const instance = normalizeURL(formData.get("instance") as string); const instance = normalizeURL(formData.get("instance") as string);
saveInstance(instance); save(instance);
} }
return true; return true;

View file

@ -1,33 +1,40 @@
import { persistentAtom } from "@nanostores/persistent"; import { persistentAtom } from "@nanostores/persistent";
import { getUrlDomain } from "@scripts/util"; import { getUrlDomain } from "@scripts/util";
import { action, onMount } from "nanostores";
const LOCAL_STORAGE_KEY = "recentInstances"; const OLD_LOCAL_STORAGE_KEY = "recentInstances";
const RECENT_INSTANCES_SIZE = 5; const LOCAL_STORAGE_KEY = "savedInstances";
const CAPACITY = 5;
export const savedInstances = persistentAtom<Set<string>>( export const $savedInstances = persistentAtom<Set<string>>(
LOCAL_STORAGE_KEY, LOCAL_STORAGE_KEY,
new Set(), new Set(),
{ {
encode: (set) => JSON.stringify([...set]), encode: (set) => JSON.stringify([...set]),
decode: (value) => { decode: (value) => new Set(JSON.parse(value)),
return new Set(
// XXX: The conversion to a domain need to be done to support legacy
// users, who may have full URLs in their Storage
JSON.parse(value).map((instanceUrl: string) =>
getUrlDomain(instanceUrl),
),
);
},
}, },
); );
export const saveInstance = (instance: string) => { onMount($savedInstances, () => {
savedInstances.set( // XXX: The conversion to a domain need to be done to support legacy
// users, who may have full URLs in their Storage
const oldItem = localStorage.getItem(OLD_LOCAL_STORAGE_KEY);
if (!oldItem) {
return;
}
$savedInstances.set(
new Set( new Set(
[getUrlDomain(instance), ...savedInstances.get()].slice( JSON.parse(oldItem).map((instanceUrl: string) =>
0, getUrlDomain(instanceUrl),
RECENT_INSTANCES_SIZE,
), ),
), ),
); );
}; localStorage.removeItem(OLD_LOCAL_STORAGE_KEY);
});
export const save = action($savedInstances, "save", (store, instance) => {
store.set(
new Set([getUrlDomain(instance), ...store.get()].slice(0, CAPACITY)),
);
});