From e7e17f3899e140d14b08f725eb5a73965ada50c3 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Mon, 8 Jan 2018 10:13:42 -0800 Subject: [PATCH] use svelte store and localstorage for ui state --- routes/_utils/store.js | 31 +++++++++++++++++++++ routes/index.html | 7 +++-- routes/settings/add-instance.html | 45 ++++++++++++++++--------------- 3 files changed, 58 insertions(+), 25 deletions(-) create mode 100644 routes/_utils/store.js diff --git a/routes/_utils/store.js b/routes/_utils/store.js new file mode 100644 index 00000000..7381a5d6 --- /dev/null +++ b/routes/_utils/store.js @@ -0,0 +1,31 @@ +import { Store } from 'svelte/store.js' + +const key = 'ui-store' + +class LocalStorageStore extends Store { + constructor(state) { + super(state) + if (process.browser) { + let cached = localStorage.getItem(key) + if (cached) { + this.set(JSON.parse(cached)) + } + } + } + + save() { + if (process.browser) { + localStorage.setItem(key, JSON.stringify(this._state)) + } + } +} + +const store = new LocalStorageStore({ + instanceNameInSearch: '' +}) + +if (process.browser) { + window.store = store +} + +export { store } \ No newline at end of file diff --git a/routes/index.html b/routes/index.html index dfa4e454..7367ebb6 100644 --- a/routes/index.html +++ b/routes/index.html @@ -16,24 +16,23 @@