flotilla/src/routes/+layout.svelte

312 lines
9.4 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"
2026-04-07 21:53:19 +00:00
import "@welshman/editor/index.css"
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 {page} from "$app/stores"
2026-01-06 23:30:54 +00:00
import {sync, throttled} from "@welshman/store"
import {always, call} from "@welshman/lib"
import {defaultSocketPolicies, netContext} from "@welshman/net"
import {appContext, pubkey, sessions, signerLog, shouldUnwrap} from "@welshman/app"
import {routerContext} from "@welshman/router"
import {verifyEvent} from "@welshman/util"
import type {TrustedEvent} from "@welshman/util"
import {context as pomadeContext} from "@pomade/core"
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 {isMobile} from "@lib/html"
2026-01-16 18:06:25 +00:00
import * as implicit from "@lib/implicit"
2026-06-01 21:50:13 +00:00
import {maybeInstallRelayMocks} from "@lib/test/relayMocks"
import AppContainer from "@app/components/AppContainer.svelte"
import ModalContainer from "@app/components/ModalContainer.svelte"
2026-06-08 21:14:40 +00:00
import {setupHistory} from "@app/routes"
import {setupAnalytics} from "@app/analytics"
import {authPolicy, blockPolicy, trustPolicy, mostlyRestrictedPolicy} from "@app/policies"
import {db, kv, ss} from "@app/storage"
import {device} from "@app/device"
import {getSetting, userSettingsValues, notificationSettings} from "@app/settings"
import {DUFFLEPUD_URL, INDEXER_RELAYS, POMADE_SIGNERS} from "@app/env"
import {pushState} from "@app/push/adapters/common"
import {syncApplicationData} from "@app/sync"
import * as groups from "@app/groups"
import * as comments from "@app/comments"
import * as deletes from "@app/deletes"
import * as reactions from "@app/reactions"
import * as profiles from "@app/profiles"
import * as lightning from "@app/lightning"
import * as uploads from "@app/uploads"
import * as appPolls from "@app/polls"
import * as reports from "@app/reports"
import * as relays from "@app/relays"
import * as settings from "@app/settings"
import * as members from "@app/members"
import * as chats from "@app/chats"
import * as content from "@app/content"
import * as env from "@app/env"
import * as repository from "@app/repository"
import * as social from "@app/social"
import * as appDevice from "@app/device"
import * as actionItems from "@app/actionItems"
import * as appFeeds from "@app/feeds"
import * as invites from "@app/invites"
import * as healthChecks from "@app/healthChecks"
2026-06-08 21:14:40 +00:00
import {theme} from "@app/theme"
import {toast, pushToast} from "@app/toast"
import * as notifications from "@app/notifications"
import {Push} from "@app/push"
import {onPushNotificationAction} from "@app/push/adapters/common"
import * as storage from "@app/storage"
import {syncKeyboard} from "@app/keyboard"
import {getPageTitle} from "@app/title"
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
2026-06-01 21:50:13 +00:00
// Test-only: when Playwright has injected window.__RELAY_MOCKS__, serve relays from in-memory
// fixtures instead of the network. No-op for real users; stripped from production builds.
if (import.meta.env.DEV) {
maybeInstallRelayMocks()
}
2026-01-16 21:10:48 +00:00
const policies = [authPolicy, blockPolicy, trustPolicy, mostlyRestrictedPolicy]
2025-10-21 15:27:30 +00:00
// Add stuff to window for convenience
Object.assign(window, {
get,
nip19,
theme,
...lib,
2026-01-16 18:06:25 +00:00
...implicit,
2025-10-21 15:27:30 +00:00
...welshmanSigner,
...router,
2025-10-24 20:36:59 +00:00
...store,
2025-10-21 15:27:30 +00:00
...util,
...feeds,
...net,
...app,
...groups,
...relays,
...settings,
...members,
...chats,
...content,
...env,
...repository,
...social,
...appDevice,
...actionItems,
...appFeeds,
...invites,
...healthChecks,
...comments,
...deletes,
...reactions,
...profiles,
...lightning,
...uploads,
...appPolls,
...reports,
2025-10-21 15:27:30 +00:00
...notifications,
})
2024-10-23 00:29:43 +00:00
// Set up context for various modules
pomadeContext.setSignerUrls(POMADE_SIGNERS)
pomadeContext.setArgonWorker(import("@pomade/core/argon-worker.js?worker"))
appContext.dufflepudUrl = DUFFLEPUD_URL
routerContext.getIndexerRelays = always(INDEXER_RELAYS)
netContext.isEventValid = (event: TrustedEvent, url: string) =>
getSetting<string[]>("trusted_relays").includes(url) || verifyEvent(event)
2025-10-21 15:27:30 +00:00
// Listen for deep link events
App.addListener("appUrlOpen", async (event: URLOpenListenerEvent) => {
2025-10-21 15:27:30 +00:00
const url = new URL(event.url)
const relay = url.searchParams.get("relay")
const id = url.searchParams.get("id")
if (relay && id) {
onPushNotificationAction({notification: {data: {relay, id}}} as any)
return
}
if (url.host === "x-callback-url") {
if (url.pathname === "/authError") {
const errorMessage = url.searchParams.get("errorMessage")
pushToast({
theme: "error",
message: errorMessage || "Signer authorization failed.",
})
}
if (["/authSuccess", "/authError"].includes(url.pathname)) {
return
}
}
2025-10-21 15:27:30 +00:00
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
2026-03-24 00:51:51 +00:00
// Sync stuff to storage
2025-10-21 15:27:30 +00:00
await Promise.all([
2026-01-28 01:15:22 +00:00
sync({
key: "device",
store: device,
storage: kv,
}),
2025-10-21 15:27:30 +00:00
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,
2026-03-24 00:51:51 +00:00
storage: ss,
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
}),
2026-01-24 00:51:02 +00:00
sync({
key: "notificationSettings",
store: notificationSettings,
storage: kv,
}),
sync({
key: "notificationState",
store: pushState,
storage: kv,
}),
2025-10-21 15:27:30 +00:00
])
2026-03-12 18:33:04 +00:00
const storageSync = storage.sync()
2025-11-14 22:59:27 +00:00
2026-03-12 18:33:04 +00:00
unsubscribers.push(storageSync.unsubscribe)
// Wait for critical storage data only
await storageSync.ready
2025-11-14 22:59:27 +00:00
// 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
2026-01-16 23:19:52 +00:00
// History, navigation, application data
unsubscribers.push(setupHistory(), setupAnalytics(), syncApplicationData())
2025-11-25 23:30:40 +00:00
// Initialize keyboard state tracking
unsubscribers.push(syncKeyboard())
// Subscribe to badge count for changes
unsubscribers.push(notifications.syncBadges())
// Subscribe to page history to update checked state
unsubscribers.push(notifications.syncChecked())
// Sync checked state across devices
unsubscribers.push(notifications.syncCheckedRemote())
2026-01-20 18:40:33 +00:00
// Initialize background notifications
unsubscribers.push(Push.sync())
2026-01-20 18:40:33 +00:00
// Listen for signer errors, report to user via toast
unsubscribers.push(
2026-01-06 23:30:54 +00:00
throttled(10_000, signerLog).subscribe($log => {
2026-02-09 19:12:54 +00:00
if ($toast) return
2026-01-06 23:30:54 +00:00
2026-02-09 19:12:54 +00:00
const longCutoff = Date.now() - 30_000
const shortCutoff = Date.now() - 10_000
const pending = $log.filter(x => !x.finished_at && x.started_at < longCutoff)
const completed = $log.filter(x => x.finished_at && x.finished_at > shortCutoff)
const showPendingError = pending.length > 10
const showCompletedError = completed.length > 5 && completed.filter(x => x.ok).length === 0
if (showPendingError || showCompletedError) {
2026-01-06 23:30:54 +00:00
pushToast({
theme: "error",
timeout: 60_000,
2026-02-09 19:12:54 +00:00
message: "Your signer isn't responding.",
2026-01-06 23:30:54 +00:00
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
})
$effect(() => {
document.title = getPageTitle({page: $page, pubkey: $pubkey})
})
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 class={isMobile ? "mobile" : ""}>
<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}