From ea345e6e614b3e961363a82347fd432bc20aaede Mon Sep 17 00:00:00 2001 From: Dallas Hoffman Date: Sat, 30 Sep 2023 23:21:14 -0400 Subject: [PATCH] Add welcome modal with data persistence permission prompt --- src/lib/components/modal.svelte | 6 +++- src/lib/components/welcome-modal.svelte | 33 +++++++++++++++++++++ src/lib/stores/index.ts | 2 ++ src/lib/stores/persistence.ts | 39 +++++++++++++++++++++++++ src/routes/+page.svelte | 2 ++ 5 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/lib/components/welcome-modal.svelte create mode 100644 src/lib/stores/persistence.ts diff --git a/src/lib/components/modal.svelte b/src/lib/components/modal.svelte index af08125..1272c84 100644 --- a/src/lib/components/modal.svelte +++ b/src/lib/components/modal.svelte @@ -1,11 +1,14 @@ + + +

+ Capture your thoughts for later by writing a quick note. Add tags to categorize it and organize + your notes. +

+

+ This app keeps all of its data on your device. Your notes are private, never sent to an external + server. +

+
+ +
+
diff --git a/src/lib/stores/index.ts b/src/lib/stores/index.ts index b28d793..584cbb5 100644 --- a/src/lib/stores/index.ts +++ b/src/lib/stores/index.ts @@ -3,10 +3,12 @@ import { NotesStore } from './notes'; import { ThemeStore, type Theme } from './theme'; import { TagsStore } from './tags'; import { SettingsStore } from './settings'; +import { PersistenceStore } from './persistence'; export const tags = new TagsStore(); export const notes = new NotesStore(tags); +export const persistence = new PersistenceStore(); export const settings = new SettingsStore(); export const theme = new ThemeStore(); diff --git a/src/lib/stores/persistence.ts b/src/lib/stores/persistence.ts new file mode 100644 index 0000000..2c4090a --- /dev/null +++ b/src/lib/stores/persistence.ts @@ -0,0 +1,39 @@ +import { writable, type Readable } from 'svelte/store'; + +export class PersistenceStore implements Readable { + private persistence = writable(null); + + get prompted() { + return !!localStorage.promptedForPersistence; + } + private set prompted(value: boolean) { + localStorage.promptedForPersistence = value; + } + + constructor() { + this.check(); + } + + subscribe = this.persistence.subscribe; + + async check() { + if (navigator.storage && navigator.storage.persisted) { + let persisted: boolean | null = await navigator.storage.persisted(); + + if (persisted !== true) { + persisted = this.prompted ? false : null; + } + + this.persistence.set(persisted); + } + } + + async prompt() { + this.prompted = true; + + if (navigator.storage && navigator.storage.persist) { + const persisted = await navigator.storage.persist(); + this.persistence.set(persisted); + } + } +} diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index e6878d6..2ed84d9 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -5,6 +5,7 @@ import NoteListPlaceholder from '$lib/components/notes/note-list-placeholder.svelte'; import NoteList from '$lib/components/notes/note-list.svelte'; import SettingsModal from '$lib/components/settings/settings-modal.svelte'; + import WelcomeModal from '$lib/components/welcome-modal.svelte'; import { notes, tags } from '$lib/stores'; const notesCount = notes.count; @@ -24,6 +25,7 @@ +