ora/src/options/App.svelte

110 lines
2.5 KiB
Svelte
Raw Normal View History

2020-09-19 01:58:28 +02:00
<script>
import { onMount } from 'svelte'
import browser from 'webextension-polyfill'
2020-10-11 23:46:06 +02:00
import { saveAs } from 'file-saver'
import dj from 'dayjs'
2020-09-19 01:58:28 +02:00
2020-10-11 23:46:06 +02:00
import FileUpload from './FileUpload.svelte'
2020-10-12 01:39:39 +02:00
import Footer from '../shared/footer.svelte'
2020-10-11 23:46:06 +02:00
import { dashboard, isDev } from '../shared/utils'
import { dump as dumpDB, load as loadDB, clear as clearDB, validate } from '../shared/db'
import { longPress } from '../shared/lib'
2020-09-19 01:58:28 +02:00
const DEFAULT = {
retention: 90,
}
let settings = DEFAULT
2020-10-11 23:46:06 +02:00
let uploaded
let disabled = true
2020-09-19 01:58:28 +02:00
2020-10-11 23:46:06 +02:00
async function read() {
2020-09-19 01:58:28 +02:00
settings = {
...DEFAULT,
...(await browser.storage.local.get()),
}
}
2020-10-11 23:46:06 +02:00
function write() {
2020-09-19 01:58:28 +02:00
return browser.storage.local.set(settings)
}
async function reset() {
await browser.storage.local.clear()
2020-10-11 23:46:06 +02:00
await read()
}
async function dump() {
const data = await dumpDB()
const blob = new Blob([JSON.stringify(data)], { type: 'application/json;charset=utf-8' })
const filename = `Ora [${dj().format('YYYY-MM-DD HH-mm-ss')}].json`
saveAs(blob, filename)
2020-09-19 01:58:28 +02:00
}
2020-10-11 23:46:06 +02:00
async function clear() {
await clearDB()
alert('Done')
}
async function load() {
try {
await loadDB(uploaded)
alert('Imported')
} catch {
alert('Error importing')
}
}
$: {
disabled = !validate(uploaded)
}
onMount(read)
2020-09-19 01:58:28 +02:00
</script>
2020-10-11 23:46:06 +02:00
<style>
main {
padding: 1em;
margin: auto;
width: 100%;
max-width: 50em;
}
</style>
<main>
<a href={dashboard} target={isDev ? '' : '_blank'}><button class="btn">Dashboard</button></a>
2020-09-19 01:58:28 +02:00
2020-10-11 23:46:06 +02:00
<h2 class="mt-8 text-2xl">Settings</h2>
<form class="mt-2" on:submit|preventDefault={write}>
2020-09-19 01:58:28 +02:00
<div class="form-group">
<label class="form-label">
2020-10-11 23:46:06 +02:00
Retention
<small>(Days)</small>
2020-09-19 01:58:28 +02:00
<input
id="retention"
class="form-input"
type="number"
min="3"
max="365"
step="1"
bind:value={settings.retention} />
</label>
2020-10-11 23:46:06 +02:00
<div class="mt-2">
<button type="reset" class="btn" on:click={reset}>Reset</button>
<button type="submit" class="btn btn-primary">Save</button>
</div>
2020-09-19 01:58:28 +02:00
</div>
</form>
2020-10-11 23:46:06 +02:00
<h2 class="mt-8 text-2xl">Your Data</h2>
<div class="mt-2">
<FileUpload bind:value={uploaded} />
<button class="btn btn-primary" on:click={load} {disabled}>Import</button>
<button class="btn btn-primary" on:click={dump}>Export</button>
<button class="btn btn-error tooltip" data-tooltip="Hold to delete" use:longPress={clear}>Delete all data</button>
</div>
2020-10-12 01:39:39 +02:00
<Footer />
2020-09-19 01:58:28 +02:00
</main>