2024-08-05 21:45:16 +00:00
|
|
|
<script lang="ts">
|
2024-08-06 20:10:10 +00:00
|
|
|
import "@src/app.css"
|
2025-05-15 17:15:41 +00:00
|
|
|
import "@capacitor-community/safe-area"
|
2025-01-28 21:04:37 +00:00
|
|
|
import * as nip19 from "nostr-tools/nip19"
|
2025-10-21 17:29:29 +00:00
|
|
|
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"
|
2026-01-06 23:30:54 +00:00
|
|
|
import {sync, throttled} from "@welshman/store"
|
|
|
|
|
import {call} from "@welshman/lib"
|
2025-10-21 15:27:30 +00:00
|
|
|
import {defaultSocketPolicies} from "@welshman/net"
|
2026-01-06 23:30:54 +00:00
|
|
|
import {pubkey, sessions, signerLog, shouldUnwrap} from "@welshman/app"
|
2024-10-11 00:09:01 +00:00
|
|
|
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"
|
2025-06-16 23:56:59 +00:00
|
|
|
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"
|
2026-01-06 18:00:35 +00:00
|
|
|
import {isMobile} from "@lib/html"
|
2026-01-16 18:06:25 +00:00
|
|
|
import * as implicit from "@lib/implicit"
|
2024-10-15 22:13:11 +00:00
|
|
|
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"
|
2025-08-21 22:01:31 +00:00
|
|
|
import {setupTracking} from "@app/util/tracking"
|
|
|
|
|
import {setupAnalytics} from "@app/util/analytics"
|
2026-01-16 21:10:48 +00:00
|
|
|
import {authPolicy, blockPolicy, 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"
|
2025-08-21 22:01:31 +00:00
|
|
|
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"
|
2025-09-24 17:32:55 +00:00
|
|
|
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-01-16 21:10:48 +00:00
|
|
|
const policies = [authPolicy, blockPolicy, trustPolicy, mostlyRestrictedPolicy]
|
2025-10-21 17:29:29 +00:00
|
|
|
|
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,
|
|
|
|
|
...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())
|
|
|
|
|
|
2025-10-21 17:29:29 +00:00
|
|
|
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({
|
2025-07-15 18:06:47 +00:00
|
|
|
key: "pubkey",
|
|
|
|
|
store: pubkey,
|
2025-11-12 01:49:22 +00:00
|
|
|
storage: kv,
|
2025-10-21 15:27:30 +00:00
|
|
|
}),
|
|
|
|
|
sync({
|
2025-07-15 18:06:47 +00:00
|
|
|
key: "sessions",
|
|
|
|
|
store: sessions,
|
2025-11-12 01:49:22 +00:00
|
|
|
storage: kv,
|
2025-10-21 15:27:30 +00:00
|
|
|
}),
|
|
|
|
|
sync({
|
2025-10-17 17:19:21 +00:00
|
|
|
key: "shouldUnwrap",
|
|
|
|
|
store: shouldUnwrap,
|
2025-11-12 01:49:22 +00:00
|
|
|
storage: kv,
|
2025-10-21 15:27:30 +00:00
|
|
|
}),
|
|
|
|
|
])
|
2025-10-17 17:19:21 +00:00
|
|
|
|
2025-11-14 22:59:27 +00:00
|
|
|
// Set up our storage adapters
|
|
|
|
|
db.adapters = storage.adapters
|
|
|
|
|
|
2025-10-21 17:29:29 +00:00
|
|
|
// 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
|
|
|
|
2025-10-21 17:29:29 +00:00
|
|
|
// Add our extra policies now that we're set up
|
|
|
|
|
defaultSocketPolicies.push(...policies)
|
2025-07-30 22:54:10 +00:00
|
|
|
|
2025-10-21 17:29:29 +00:00
|
|
|
// Remove policies when we're done
|
|
|
|
|
unsubscribers.push(() => defaultSocketPolicies.splice(-policies.length))
|
2025-10-21 15:27:30 +00:00
|
|
|
|
2025-10-21 17:29:29 +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())
|
|
|
|
|
|
2025-10-21 17:29:29 +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 => {
|
|
|
|
|
const cutoff = Date.now() - 3_000
|
|
|
|
|
const recent = $log.filter(x => x.started_at < cutoff).slice(-10)
|
|
|
|
|
const ok = recent.filter(x => x.ok)
|
|
|
|
|
|
|
|
|
|
if (!$toast && recent.length > 5 && ok.length === 0) {
|
|
|
|
|
pushToast({
|
|
|
|
|
theme: "error",
|
|
|
|
|
timeout: 60_000,
|
|
|
|
|
message: "Your signer appears to be unresponsive.",
|
|
|
|
|
action: {
|
|
|
|
|
message: "Details",
|
|
|
|
|
onclick: () => goto("/settings/profile"),
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}),
|
2025-10-21 17:29:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 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()
|
2025-10-21 17:29:29 +00:00
|
|
|
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>
|
|
|
|
|
|
2025-10-21 17:29:29 +00:00
|
|
|
{#await unsubscribe}
|
2025-10-21 16:26:06 +00:00
|
|
|
<!-- pass -->
|
2024-08-14 19:46:25 +00:00
|
|
|
{:then}
|
2026-01-06 18:00:35 +00:00
|
|
|
<div class={isMobile ? "mobile" : ""}>
|
2024-10-15 22:13:11 +00:00
|
|
|
<AppContainer>
|
2025-02-04 00:37:14 +00:00
|
|
|
{@render children()}
|
2024-10-15 22:13:11 +00:00
|
|
|
</AppContainer>
|
|
|
|
|
<ModalContainer />
|
2025-02-03 23:01:42 +00:00
|
|
|
<div class="tippy-target"></div>
|
2025-09-24 17:32:55 +00:00
|
|
|
<NewNotificationSound />
|
2024-08-05 21:45:16 +00:00
|
|
|
</div>
|
2024-08-14 19:46:25 +00:00
|
|
|
{/await}
|