flotilla/src/routes/+layout.svelte

214 lines
6.1 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"
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-21 15:27:30 +00:00
import {call, on, spec} from "@welshman/lib"
import {defaultSocketPolicies} from "@welshman/net"
2024-08-30 23:25:41 +00:00
import {
repository,
pubkey,
sessions,
2025-07-30 22:54:10 +00:00
signerLog,
shouldUnwrap,
loadRelaySelections,
2025-07-30 22:54:10 +00:00
SignerLogEntryStatus,
2024-08-30 23:25:41 +00:00
} 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"
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"
2025-09-30 23:11:49 +00:00
import {preferencesStorageProvider} from "@lib/storage"
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"
import {userSettingsValues} from "@app/core/state"
2025-10-06 18:23:19 +00:00
import {syncApplicationData} from "@app/core/sync"
import {theme} from "@app/util/theme"
import {toast, pushToast} from "@app/util/toast"
import {initializePushNotifications} from "@app/util/push"
import * as commands from "@app/core/commands"
import * as requests from "@app/core/requests"
import * as appState from "@app/core/state"
2025-09-30 23:11:49 +00:00
import * as notifications from "@app/util/notifications"
import * as storage from "@app/util/storage"
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
2025-10-21 15:27:30 +00:00
// Add stuff to window for convenience
Object.assign(window, {
get,
nip19,
theme,
...lib,
...welshmanSigner,
...router,
...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-10-21 15:27:30 +00:00
// Listen to navigation changes
const unsubscribeHistory = setupHistory()
2025-01-28 21:04:37 +00:00
2025-10-21 15:27:30 +00:00
// Report usage on navigation change
const unsubscribeAnalytics = setupAnalytics()
2025-01-28 21:04:37 +00:00
2025-10-21 15:27:30 +00:00
// Bug tracking
const unsubscribeTracking = setupTracking()
2025-04-11 18:51:15 +00:00
2025-10-21 15:27:30 +00:00
// Load user data, listen for messages, etc
const unsubscribeApplicationData = syncApplicationData()
2025-01-28 21:04:37 +00:00
2025-10-21 15:27:30 +00:00
// Whenever we see a new pubkey, load their outbox event
const unsubscribeRepository = on(repository, "update", ({added}) => {
for (const event of added) {
loadRelaySelections(event.pubkey)
2025-01-28 21:04:37 +00:00
}
2025-10-21 15:27:30 +00:00
})
2025-01-28 21:04:37 +00:00
2025-10-21 15:27:30 +00:00
// Subscribe to badge count for changes
const unsubscribeBadgeCount = notifications.badgeCount.subscribe(
notifications.handleBadgeCountChanges,
)
// Listen for signer errors, report to user via toast
const unsubscribeSignerLog = 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"),
},
})
}
}),
)
2025-05-13 21:31:34 +00:00
2025-10-21 15:27:30 +00:00
// Sync theme
const unsubscribeTheme = theme.subscribe($theme => {
document.body.setAttribute("data-theme", $theme)
})
2024-10-25 21:15:35 +00:00
2025-10-21 15:27:30 +00:00
// Sync font size
const unsubscribeSettings = userSettingsValues.subscribe($userSettingsValues => {
// @ts-ignore
document.documentElement.style["font-size"] = `${$userSettingsValues.font_size}rem`
})
2025-01-24 17:36:24 +00:00
2025-10-21 16:26:06 +00:00
const unsubscribeStorage = call(async () => {
2025-10-21 15:27:30 +00:00
// Sync stuff to localstorage
await Promise.all([
sync({
key: "pubkey",
store: pubkey,
storage: preferencesStorageProvider,
2025-10-21 15:27:30 +00:00
}),
sync({
key: "sessions",
store: sessions,
storage: preferencesStorageProvider,
2025-10-21 15:27:30 +00:00
}),
sync({
key: "shouldUnwrap",
store: shouldUnwrap,
storage: preferencesStorageProvider,
2025-10-21 15:27:30 +00:00
}),
])
2025-10-21 15:27:30 +00:00
// Sync stuff to indexeddb
2025-10-21 16:26:06 +00:00
return await storage.syncDataStores()
2025-10-21 15:27:30 +00:00
})
2025-07-30 22:54:10 +00:00
2025-10-21 15:27:30 +00:00
// Default socket policies
const additionalPolicies = [authPolicy, trustPolicy, mostlyRestrictedPolicy]
defaultSocketPolicies.push(...additionalPolicies)
// Cleanup on hot reload
import.meta.hot?.dispose(() => {
App.removeAllListeners()
unsubscribeHistory()
unsubscribeAnalytics()
unsubscribeTracking()
unsubscribeApplicationData()
unsubscribeRepository()
unsubscribeBadgeCount()
unsubscribeSignerLog()
unsubscribeTheme()
unsubscribeSettings()
2025-10-21 16:26:06 +00:00
unsubscribeStorage.then(call)
2025-10-21 15:27:30 +00:00
defaultSocketPolicies.splice(-additionalPolicies.length)
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>
2025-10-21 16:26:06 +00:00
{#await unsubscribeStorage}
<!-- 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}