Normalize domains in store
This commit is contained in:
parent
ce3e19626f
commit
47426d5dc7
1 changed files with 14 additions and 2 deletions
|
@ -1,4 +1,5 @@
|
|||
import { persistentAtom } from "@nanostores/persistent";
|
||||
import { getUrlDomain } from "@scripts/util";
|
||||
|
||||
const LOCAL_STORAGE_KEY = "recentInstances";
|
||||
const RECENT_INSTANCES_SIZE = 5;
|
||||
|
@ -8,14 +9,25 @@ export const savedInstances = persistentAtom<Set<string>>(
|
|||
new 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) => {
|
||||
savedInstances.set(
|
||||
new Set(
|
||||
[instance, ...savedInstances.get()].slice(0, RECENT_INSTANCES_SIZE),
|
||||
[getUrlDomain(instance), ...savedInstances.get()].slice(
|
||||
0,
|
||||
RECENT_INSTANCES_SIZE,
|
||||
),
|
||||
),
|
||||
);
|
||||
};
|
||||
|
|
Reference in a new issue