2024-12-01 00:23:21 -03:00
< script >
import { enhance } from '$app/forms'
import { beforeNavigate } from '$app/navigation'
2024-12-03 01:23:29 -03:00
import Modal from '$lib/components/modal.svelte'
2024-12-01 00:23:21 -03:00
2024-12-02 01:48:51 -03:00
let { form } = $props()
2024-12-01 00:23:21 -03:00
let capKey = $state()
let capKeyInput = $state()
let newCapKey = $state()
2024-12-03 13:24:57 -03:00
let modalTitle = $state()
let modalText = $state()
2024-12-01 00:23:21 -03:00
let checked = $state(false)
2024-12-03 01:23:29 -03:00
let showModal = $state(false)
2024-12-01 00:23:21 -03:00
const submitKey = () => {
capKey = capKeyInput
}
const persistCapKey = () => {
capKey = newCapKey
newCapKey = undefined
}
const enhanceForm = () => {
return async ({ result , update } ) => {
await update()
2024-12-02 01:48:51 -03:00
if (result.status === 200) {
switch (result.data.endpoint) {
case 'createCapKey': {
newCapKey = result.data.capKey
}
case 'listDirectories': {
capKey = result.data.capKey
}
}
}
2024-12-01 00:23:21 -03:00
}
}
beforeNavigate(({ cancel } ) => {
if (newCapKey && !capKey) {
cancel()
2024-12-03 13:24:57 -03:00
modalTitle = 'Warning'
modalText = 'Please confirm you copied and saved the cap key.'
showModal = true
2024-12-01 00:23:21 -03:00
}
})
2024-12-02 01:48:51 -03:00
$effect(() => {
2024-12-03 01:23:29 -03:00
if (form?.capKey) capKey = form.capKey
2024-12-02 01:48:51 -03:00
})
$effect(() => console.log({ form } ))
2024-12-03 13:24:57 -03:00
$effect(() => {
if (form?.error) {
modalTitle = 'Error'
modalText = form.error
showModal = true
}
})
2024-12-01 00:23:21 -03:00
< / script >
< svelte:head >
< title > Private Facts dashboard< / title >
< meta name = 'description' content = 'Private Facts dashboard.' >
< / svelte:head >
< h1 > Dashboard< / h1 >
2024-12-03 01:23:29 -03:00
{ #if ! capKey && ! newCapKey }
2024-12-01 00:23:21 -03:00
< div class = 'cap-key-div add' >
2024-12-03 01:23:29 -03:00
< form action = '?/listDirectories' method = 'post' enctype = 'form-data' use:enhance = { enhanceForm } >
2024-12-01 00:23:21 -03:00
< label for = 'capKeyInput' > Cap key:< / label >
2024-12-02 01:48:51 -03:00
< input type = 'text' name = 'capKeyInput' / >
< button type = 'submit' > Submit< / button >
< / form >
2024-12-01 00:23:21 -03:00
< form action = '?/createCapKey' method = 'post' enctype = 'form-data' use:enhance = { enhanceForm } >
< button type = 'submit' > New cap key< / button >
< / form >
< / div >
2024-12-03 01:23:29 -03:00
{ :else if ! capKey && newCapKey }
2024-12-01 00:23:21 -03:00
< div class = 'cap-key-div get' >
< label for = 'newCapKey' > < h2 > New cap key< / h2 > < / label >
< input type = 'text' id = 'newCapKey' name = 'newCapKey' value = { newCapKey } / >
< div class = 'warning' >
< h3 > Attention!< / h3 >
< p > This is your new cap key. You need it to access your folders and files. It will not be saved nor ever shown again.< / p >
< p > < strong > Copy it now and save it somewhere safe.< / strong > < / p >
< p > If you lose your cap key you will not be able to access your files anymore. You will have to create a new cap key and upload new files and folders.< / p >
< div class = 'confirm-copy' >
< input type = 'checkbox' id = 'checkbox' name = 'checkbox' bind:checked / >
< label for = 'copyCheckbox' > Copied and saved.< / label >
< / div >
< button disabled = { ! checked } onclick= { persistCapKey } > Ok</ button >
< / div >
< / div >
{ : else }
< h2 > File tree< / h2 >
2024-12-03 01:23:29 -03:00
< ul >
{ #each form ? . list as item }
{ #if item === 'dirnode' }
< li > /< / li >
{ : else }
< li > { Object . keys ( item . children )[ 0 ]} </ li >
{ /if }
{ /each }
< / ul >
< button > Add folder< / button >
< button > Upload file< / button >
< button onclick = {() => capKey = undefined } > Change cap key </ button >
2024-12-01 00:23:21 -03:00
{ /if }
2024-12-03 01:23:29 -03:00
< Modal { showModal } escape = {() => showModal = false } >
2024-12-03 13:24:57 -03:00
< h2 > { modalTitle } </ h2 >
< p > { modalText } </ p >
2024-12-03 01:23:29 -03:00
< button onclick = {() => showModal = false } > Ok</button >
< / Modal >
2024-12-01 00:23:21 -03:00
< style >
2024-12-03 01:23:29 -03:00
form {
2024-12-01 00:23:21 -03:00
display: flex;
2024-12-03 01:23:29 -03:00
flex-direction: column;
text-align: left;
2024-12-01 00:23:21 -03:00
}
form button {
2024-12-03 01:23:29 -03:00
margin: 0 0 1em auto;
}
form input {
margin: 1em 0;
2024-12-01 00:23:21 -03:00
}
.cap-key-div {
2024-12-03 01:23:29 -03:00
max-width: 45em;
2024-12-01 00:23:21 -03:00
margin: 0 auto;
}
.cap-key-div.get {
display: flex;
flex-direction: column;
}
.confirm-copy {
display: flex;
justify-content: center;
align-items: center;
}
.confirm-copy input {
height: unset;
margin-right: 1em;
border: solid thin var(--border-color);
outline: none;
}
.warning {
border: solid thin var(--border-color);
border-radius: 10px;
background-color: var(--shade-color);
padding: 0.5em;
margin: 2em auto;
}
2024-12-03 01:23:29 -03:00
ul {
list-style: none;
text-align: left;
background: var(--shade-color);
padding: 1em;
border: solid thin var(--border-color);
}
2024-12-01 00:23:21 -03:00
< / style >