flotilla/src/routes/+layout.svelte

203 lines
6 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"
import "@capacitor-community/safe-area"
2025-07-30 22:54:10 +00:00
import {throttle} from "throttle-debounce"
2025-01-28 21:04:37 +00:00
import * as nip19 from "nostr-tools/nip19"
import type {Unsubscriber} from "svelte/store"
2025-10-06 18:23:19 +00:00
import {get} from "svelte/store"
2025-08-26 16:14:11 +00:00
import {App, type URLOpenListenerEvent} from "@capacitor/app"
2024-10-23 16:45:06 +00:00
import {dev} from "$app/environment"
2025-01-28 21:04:37 +00:00
import {goto} from "$app/navigation"
import {sync} from "@welshman/store"
2025-10-24 20:36:59 +00:00
import {call, spec} from "@welshman/lib"
2025-10-21 15:27:30 +00:00
import {defaultSocketPolicies} from "@welshman/net"
2025-10-24 20:36:59 +00:00
import {pubkey, sessions, signerLog, shouldUnwrap, SignerLogEntryStatus} from "@welshman/app"
import * as lib from "@welshman/lib"
import * as util from "@welshman/util"
2025-06-19 17:22:17 +00:00
import * as feeds from "@welshman/feeds"
import * as router from "@welshman/router"
2025-10-24 20:36:59 +00:00
import * as store from "@welshman/store"
2024-11-15 22:03:54 +00:00
import * as welshmanSigner from "@welshman/signer"
2024-11-14 00:14:05 +00:00
import * as net from "@welshman/net"
2024-09-11 18:09:25 +00:00
import * as app from "@welshman/app"
import AppContainer from "@app/components/AppContainer.svelte"
import ModalContainer from "@app/components/ModalContainer.svelte"
2025-10-06 18:23:19 +00:00
import {setupHistory} from "@app/util/history"
import {setupTracking} from "@app/util/tracking"
import {setupAnalytics} from "@app/util/analytics"
2025-10-21 15:27:30 +00:00
import {authPolicy, trustPolicy, mostlyRestrictedPolicy} from "@app/util/policies"
2025-11-12 01:49:22 +00:00
import {kv, db} from "@app/core/storage"
2025-10-21 15:27:30 +00:00
import {userSettingsValues} from "@app/core/state"
2025-10-06 18:23:19 +00:00
import {syncApplicationData} from "@app/core/sync"
import * as commands from "@app/core/commands"
import * as requests from "@app/core/requests"
import * as appState from "@app/core/state"
2025-11-12 01:49:22 +00:00
import {theme} from "@app/util/theme"
import {toast, pushToast} from "@app/util/toast"
import {initializePushNotifications} from "@app/util/push"
2025-09-30 23:11:49 +00:00
import * as notifications from "@app/util/notifications"
import * as storage from "@app/util/storage"
2025-11-25 23:30:40 +00:00
import {syncKeyboard} from "@app/util/keyboard"
import NewNotificationSound from "@src/app/components/NewNotificationSound.svelte"
2025-09-30 23:11:49 +00:00
2025-10-21 15:27:30 +00:00
const {children} = $props()
2024-08-05 21:45:16 +00:00
const policies = [authPolicy, trustPolicy, mostlyRestrictedPolicy]
2025-10-21 15:27:30 +00:00
// Add stuff to window for convenience
Object.assign(window, {
get,
nip19,
theme,
...lib,
...welshmanSigner,
...router,
2025-10-24 20:36:59 +00:00
...store,
2025-10-21 15:27:30 +00:00
...util,
...feeds,
...net,
...app,
...appState,
...commands,
...requests,
...notifications,
})
2024-10-23 00:29:43 +00:00
2025-06-24 21:15:36 +00:00
// Initialize push notification handler asap
initializePushNotifications()
2025-10-21 15:27:30 +00:00
// Listen for navigation messages from service worker
navigator.serviceWorker?.addEventListener("message", event => {
if (event.data && event.data.type === "NAVIGATE") {
goto(event.data.url)
}
})
2024-08-23 20:33:36 +00:00
2025-10-21 15:27:30 +00:00
// Listen for deep link events
App.addListener("appUrlOpen", (event: URLOpenListenerEvent) => {
const url = new URL(event.url)
const target = `${url.pathname}${url.search}${url.hash}`
goto(target, {replaceState: false, noScroll: false})
})
2025-06-19 17:22:17 +00:00
2025-10-21 15:27:30 +00:00
// Handle back button on mobile
App.addListener("backButton", () => {
if (window.history.length > 1) {
window.history.back()
} else {
App.exitApp()
}
})
2025-08-26 16:14:11 +00:00
2025-11-14 22:59:27 +00:00
// Cleanup on page close
window.addEventListener("beforeunload", () => db.close())
const unsubscribe = call(async () => {
const unsubscribers: Unsubscriber[] = []
2024-10-25 21:15:35 +00:00
2025-10-21 15:27:30 +00:00
// Sync stuff to localstorage
await Promise.all([
sync({
key: "pubkey",
store: pubkey,
2025-11-12 01:49:22 +00:00
storage: kv,
2025-10-21 15:27:30 +00:00
}),
sync({
key: "sessions",
store: sessions,
2025-11-12 01:49:22 +00:00
storage: kv,
2025-10-21 15:27:30 +00:00
}),
sync({
key: "shouldUnwrap",
store: shouldUnwrap,
2025-11-12 01:49:22 +00:00
storage: kv,
2025-10-21 15:27:30 +00:00
}),
])
2025-11-14 22:59:27 +00:00
// Set up our storage adapters
db.adapters = storage.adapters
// Wait until data storage is initialized before syncing other stuff
2025-11-14 22:59:27 +00:00
await db.connect()
// Close the database connection on reload
unsubscribers.push(() => db.close())
2025-11-12 01:49:22 +00:00
// Add our extra policies now that we're set up
defaultSocketPolicies.push(...policies)
2025-07-30 22:54:10 +00:00
// Remove policies when we're done
unsubscribers.push(() => defaultSocketPolicies.splice(-policies.length))
2025-10-21 15:27:30 +00:00
// History, navigation, bug tracking, application data
unsubscribers.push(setupHistory(), setupAnalytics(), setupTracking(), syncApplicationData())
// Subscribe to badge count for changes
unsubscribers.push(notifications.badgeCount.subscribe(notifications.handleBadgeCountChanges))
2025-11-25 23:30:40 +00:00
// Initialize keyboard state tracking
unsubscribers.push(syncKeyboard())
// Listen for signer errors, report to user via toast
unsubscribers.push(
signerLog.subscribe(
throttle(10_000, $log => {
const recent = $log.slice(-10)
const success = recent.filter(spec({status: SignerLogEntryStatus.Success}))
const failure = recent.filter(spec({status: SignerLogEntryStatus.Failure}))
if (!$toast && failure.length > 5 && success.length === 0) {
pushToast({
theme: "error",
timeout: 60_000,
message: "Your signer appears to be unresponsive.",
action: {
message: "Details",
onclick: () => goto("/settings/profile"),
},
})
}
}),
),
)
// Sync theme and font size
unsubscribers.push(
theme.subscribe($theme => {
document.body.setAttribute("data-theme", $theme)
}),
userSettingsValues.subscribe($userSettingsValues => {
// @ts-ignore
document.documentElement.style["font-size"] = `${$userSettingsValues.font_size}rem`
}),
)
return () => unsubscribers.forEach(call)
})
2025-10-21 15:27:30 +00:00
// Cleanup on hot reload
import.meta.hot?.dispose(() => {
App.removeAllListeners()
unsubscribe.then(call)
2024-08-09 00:22:17 +00:00
})
2024-08-05 21:45:16 +00:00
</script>
2024-10-23 16:45:06 +00:00
<svelte:head>
{#if !dev}
<link rel="manifest" href="/manifest.webmanifest" />
{/if}
</svelte:head>
{#await unsubscribe}
2025-10-21 16:26:06 +00:00
<!-- pass -->
2024-08-14 19:46:25 +00:00
{:then}
<div>
<AppContainer>
2025-02-04 00:37:14 +00:00
{@render children()}
</AppContainer>
<ModalContainer />
2025-02-03 23:01:42 +00:00
<div class="tippy-target"></div>
<NewNotificationSound />
2024-08-05 21:45:16 +00:00
</div>
2024-08-14 19:46:25 +00:00
{/await}