flotilla/src/routes/+layout.svelte

168 lines
4.2 KiB
Svelte
Raw Normal View History

2024-08-05 21:45:16 +00:00
<script lang="ts">
2024-08-06 20:10:10 +00:00
import "@src/app.css"
2024-08-16 17:50:38 +00:00
import {onMount} from "svelte"
2024-08-23 20:33:36 +00:00
import {get, derived} from 'svelte/store'
2024-08-05 22:28:46 +00:00
import {page} from "$app/stores"
2024-08-16 22:17:30 +00:00
import {goto} from "$app/navigation"
import {browser} from "$app/environment"
2024-08-23 20:33:36 +00:00
import {createEventStore, adapter} from "@welshman/store"
2024-08-08 21:02:46 +00:00
import ModalBox from "@lib/components/ModalBox.svelte"
2024-08-08 23:32:46 +00:00
import Toast from "@app/components/Toast.svelte"
2024-08-09 23:22:09 +00:00
import Landing from "@app/components/Landing.svelte"
2024-08-06 20:10:10 +00:00
import PrimaryNav from "@app/components/PrimaryNav.svelte"
2024-08-09 00:22:17 +00:00
import {modals, clearModal} from "@app/modal"
2024-08-14 23:25:36 +00:00
import {theme} from "@app/theme"
2024-08-16 22:17:30 +00:00
import {pk, session, repository, DEFAULT_RELAYS} from "@app/base"
2024-08-23 20:33:36 +00:00
import type {PublishStatusData, PublishStatusDataByUrlById} from "@app/state"
import {relays, freshness, plaintext, handles, loadRelay, publishStatusData} from "@app/state"
2024-08-14 19:46:25 +00:00
import {initStorage} from "@app/storage"
2024-08-16 19:42:00 +00:00
import {loadUserData} from "@app/commands"
2024-08-23 20:33:36 +00:00
import * as base from "@app/base"
import * as state from "@app/state"
2024-08-05 21:45:16 +00:00
2024-08-14 19:46:25 +00:00
let ready: Promise<void>
2024-08-09 00:22:17 +00:00
let dialog: HTMLDialogElement
2024-08-14 00:58:26 +00:00
let prev: any
2024-08-09 00:22:17 +00:00
2024-08-09 23:22:09 +00:00
$: modalId = $page.url.hash.slice(1)
$: modal = modals.get(modalId)
2024-08-05 21:45:16 +00:00
$: {
2024-08-09 23:22:09 +00:00
if (!modal && !$session) {
modal = {component: Landing}
2024-08-16 22:17:30 +00:00
if (browser && $page.route?.id !== "/home") {
goto("/home")
}
2024-08-09 23:22:09 +00:00
}
2024-08-22 20:17:21 +00:00
}
2024-08-09 23:22:09 +00:00
2024-08-22 20:17:21 +00:00
$: {
2024-08-09 00:22:17 +00:00
if (modal) {
dialog?.showModal()
2024-08-14 00:58:26 +00:00
prev = modal
2024-08-05 21:45:16 +00:00
} else {
2024-08-09 00:22:17 +00:00
dialog?.close()
2024-08-05 21:45:16 +00:00
}
}
2024-08-09 00:22:17 +00:00
onMount(() => {
2024-08-23 20:33:36 +00:00
Object.assign(window, {get, base, state})
ready = initStorage(3, {
2024-08-14 19:46:25 +00:00
events: {
2024-08-16 17:50:38 +00:00
keyPath: "id",
2024-08-14 19:46:25 +00:00
store: createEventStore(repository),
},
relays: {
2024-08-16 17:50:38 +00:00
keyPath: "url",
2024-08-14 19:46:25 +00:00
store: relays,
},
handles: {
2024-08-16 17:50:38 +00:00
keyPath: "nip05",
2024-08-14 19:46:25 +00:00
store: handles,
},
2024-08-23 20:33:36 +00:00
publishStatus: {
keyPath: "id",
store: adapter({
store: publishStatusData,
forward: ($psd: PublishStatusDataByUrlById) => {
const data = []
for (const [id, itemsByUrl] of Object.entries($psd)) {
for (const [url, item] of Object.entries(itemsByUrl)) {
data.push(item)
}
}
return data
},
backward: (data: PublishStatusData[]) => {
const result: PublishStatusDataByUrlById = {}
for (const item of data) {
result[item.id] = result[item.id] || {}
result[item.id][item.url] = item
}
return result
},
}),
},
freshness: {
keyPath: "key",
store: adapter({
store: freshness,
forward: ($freshness: Record<string, number>) => Object.entries($freshness).map(([key, ts]) => ({key, ts})),
backward: (data: any[]) => {
const result: Record<string, number> = {}
for (const {key, ts} of data) {
result[key] = ts
}
return result
},
}),
},
plaintext: {
keyPath: "id",
store: adapter({
store: plaintext,
forward: ($plaintext: Record<string, string>) => Object.entries($plaintext).map(([id, plaintext]) => ({id, plaintext})),
backward: (data: any[]) => {
const result: Record<string, string> = {}
for (const {id, plaintext} of data) {
result[id] = plaintext
}
return result
},
}),
},
2024-08-14 19:46:25 +00:00
})
2024-08-16 17:50:38 +00:00
dialog.addEventListener("close", () => {
2024-08-09 00:22:17 +00:00
if (modal) {
clearModal()
}
})
2024-08-19 17:26:25 +00:00
ready.then(() => {
for (const url of DEFAULT_RELAYS) {
loadRelay(url)
}
if ($pk) {
loadUserData($pk)
}
})
2024-08-09 00:22:17 +00:00
})
2024-08-05 21:45:16 +00:00
</script>
2024-08-14 19:46:25 +00:00
{#await ready}
2024-08-14 23:25:36 +00:00
<div data-theme={$theme} />
2024-08-14 19:46:25 +00:00
{:then}
2024-08-14 23:25:36 +00:00
<div data-theme={$theme}>
2024-08-14 19:46:25 +00:00
<div class="flex h-screen">
<PrimaryNav />
2024-08-16 18:12:14 +00:00
<slot />
2024-08-08 21:02:46 +00:00
</div>
2024-08-16 17:50:38 +00:00
<dialog bind:this={dialog} class="modal modal-bottom !z-modal sm:modal-middle">
2024-08-14 19:46:25 +00:00
{#if prev}
{#key prev}
<ModalBox {...prev} />
{/key}
<Toast />
{/if}
{#if $session}
<form method="dialog" class="modal-backdrop">
<button />
</form>
{/if}
</dialog>
<Toast />
2024-08-05 21:45:16 +00:00
</div>
2024-08-14 19:46:25 +00:00
{/await}