From 519f8a77d9da6e1419bb969347c51c1d30dd0821 Mon Sep 17 00:00:00 2001 From: Dallas Hoffman Date: Wed, 20 Sep 2023 00:31:27 -0400 Subject: [PATCH] Remember selected tags --- src/lib/stores/tags.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/lib/stores/tags.ts b/src/lib/stores/tags.ts index 3987ff4..bc61341 100644 --- a/src/lib/stores/tags.ts +++ b/src/lib/stores/tags.ts @@ -15,9 +15,35 @@ export class TagsStore implements Readable { selectedTags = derived(this.selected, ($selected) => Array.from($selected)); constructor() { + this.initSelections(); this.refreshTags(); } + private initSelections = () => { + if (!localStorage.noteTagsSelected) return; + + let storedSelections: unknown; + + try { + storedSelections = JSON.parse(localStorage.noteTagsSelected); + } catch (err) { + localStorage.removeItem('noteTagsSelected'); + return; + } + + const selectionsAsArray = storedSelections; + if (!Array.isArray(selectionsAsArray)) return; + + const seletionsAsInts = selectionsAsArray + .map((tagId) => parseInt(tagId)) + .filter((tagId) => !isNaN(tagId)); + + this.selected.update((selected) => { + seletionsAsInts.forEach((tagId) => selected.add(tagId)); + return selected; + }); + }; + subscribe = this.tags.subscribe; refreshTags = async () => { @@ -84,6 +110,10 @@ export class TagsStore implements Readable { this.selected.update((selected) => { const method = selected.has(tagId) ? 'delete' : 'add'; selected[method](tagId); + + const selectionsAsString = JSON.stringify(Array.from(selected)); + localStorage.noteTagsSelected = selectionsAsString; + return selected; });