Normalize domains in store

This commit is contained in:
Nikita Karamov 2023-03-27 21:25:26 +02:00
parent ce3e19626f
commit 47426d5dc7
No known key found for this signature in database
GPG key ID: 41D6F71EE78E77CD

View file

@ -1,4 +1,5 @@
import { persistentAtom } from "@nanostores/persistent"; import { persistentAtom } from "@nanostores/persistent";
import { getUrlDomain } from "@scripts/util";
const LOCAL_STORAGE_KEY = "recentInstances"; const LOCAL_STORAGE_KEY = "recentInstances";
const RECENT_INSTANCES_SIZE = 5; const RECENT_INSTANCES_SIZE = 5;
@ -8,14 +9,25 @@ export const savedInstances = persistentAtom<Set<string>>(
new Set(), new Set(),
{ {
encode: (set) => JSON.stringify([...set]), encode: (set) => JSON.stringify([...set]),
decode: (value) => new Set(JSON.parse(value)), decode: (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) => { export const saveInstance = (instance: string) => {
savedInstances.set( savedInstances.set(
new Set( new Set(
[instance, ...savedInstances.get()].slice(0, RECENT_INSTANCES_SIZE), [getUrlDomain(instance), ...savedInstances.get()].slice(
0,
RECENT_INSTANCES_SIZE,
),
), ),
); );
}; };