Add sorting to Quick Links

This commit is contained in:
Fred Boniface
2023-07-24 11:35:27 +01:00
parent 69949fb8f4
commit 2ffa3510ee
5 changed files with 16 additions and 19 deletions

View File

@@ -1,10 +1,10 @@
import { writable } from 'svelte/store';
import { writable, type Writable } from 'svelte/store';
import { browser } from '$app/environment';
export const ql = writable(fromLocalStorage('ql', []));
toLocalStorage(ql, 'ql');
function fromLocalStorage(storageKey, fallback) {
function fromLocalStorage(storageKey: string, fallback: string[]): string[] {
if (browser) {
const storedValue = localStorage.getItem(storageKey);
if (storedValue !== 'undefined' && storedValue !== null) {
@@ -14,10 +14,10 @@ function fromLocalStorage(storageKey, fallback) {
return fallback;
}
function toLocalStorage(store, storageKey) {
function toLocalStorage(store: Writable<string[]>, storageKey: string) {
if (browser) {
store.subscribe((value) => {
let storageValue = typeof value === 'object' ? JSON.stringify(value) : value;
let storageValue = typeof value === 'object' ? JSON.stringify(value.sort()) : value;
localStorage.setItem(storageKey, storageValue);
});