cryptgeon/frontend/src/routes/note/[id].svelte

82 lines
1.7 KiB
Svelte
Raw Normal View History

2021-05-02 03:08:30 +02:00
<script context="module" lang="ts">
export async function load({ page }) {
return {
props: page.params,
}
}
</script>
<script lang="ts">
import type { NotePublic } from '$lib/api'
2021-12-21 00:15:04 +01:00
import { get, info } from '$lib/api'
2021-05-02 03:08:30 +02:00
import { decrypt, getKeyFromString } from '$lib/crypto'
import Button from '$lib/ui/Button.svelte'
2021-12-21 00:15:04 +01:00
import ShowNote from '$lib/ui/ShowNote.svelte'
import { onMount } from 'svelte'
2021-05-02 03:08:30 +02:00
export let id: string
2021-05-03 12:21:51 +02:00
2021-05-16 11:16:25 +02:00
let password: string
2021-05-02 03:08:30 +02:00
let note: NotePublic | null = null
let exists = false
let loading = true
let error = false
onMount(async () => {
try {
loading = true
error = null
2021-05-16 11:16:25 +02:00
password = window.location.hash.slice(1)
2021-05-03 12:21:51 +02:00
await info(id)
2021-05-02 03:08:30 +02:00
exists = true
} catch {
exists = false
} finally {
loading = false
}
})
async function show() {
2021-05-03 12:21:51 +02:00
try {
error = false
2021-05-08 21:46:52 +02:00
loading = true
2021-05-03 12:21:51 +02:00
const data = note || (await get(id)) // Don't get the content twice on wrong password.
const key = await getKeyFromString(password)
data.contents = await decrypt(data.contents, key)
note = data
} catch {
error = true
2021-05-08 21:46:52 +02:00
} finally {
loading = false
2021-05-02 03:08:30 +02:00
}
}
</script>
{#if !loading}
{#if !exists}
2021-05-10 09:58:13 +02:00
<p class="error-text" data-testid="note-not-found">
note was not found or was already deleted.
</p>
2021-05-02 03:08:30 +02:00
{:else if note && !error}
2021-12-21 00:15:04 +01:00
<ShowNote {note} />
2021-05-02 03:08:30 +02:00
{:else}
<form on:submit|preventDefault={show}>
2021-05-08 21:46:52 +02:00
<fieldset>
<p>click below to show and delete the note if the counter has reached it's limit</p>
2021-05-10 09:58:13 +02:00
<Button type="submit" data-testid="button-show">show note</Button>
2021-05-08 21:46:52 +02:00
{#if error}
2021-05-02 15:54:27 +02:00
<br />
2021-05-08 21:46:52 +02:00
<p class="error-text">
wrong password. could not decipher. probably a broken link. note was destroyed.
<br />
</p>
{/if}
</fieldset>
2021-05-02 03:08:30 +02:00
</form>
{/if}
{/if}
2021-05-08 21:46:52 +02:00
{#if loading}
<p>loading...</p>
{/if}