flotilla/src/routes/spaces/[relay]/+layout.svelte

101 lines
2.8 KiB
Svelte
Raw Normal View History

2024-08-16 19:42:00 +00:00
<script lang="ts">
import type {Snippet} from "svelte"
2024-11-14 20:16:44 +00:00
import {onMount} from "svelte"
2024-08-16 22:17:30 +00:00
import {page} from "$app/stores"
import {ago, sleep, once, MONTH} from "@welshman/lib"
import {ROOM_META, EVENT_TIME, THREAD, COMMENT, MESSAGE, displayRelayUrl} from "@welshman/util"
import {SocketStatus} from "@welshman/net"
2024-08-16 19:42:00 +00:00
import Page from "@lib/components/Page.svelte"
import Dialog from "@lib/components/Dialog.svelte"
2024-08-16 19:42:00 +00:00
import SecondaryNav from "@lib/components/SecondaryNav.svelte"
2024-10-21 21:35:12 +00:00
import MenuSpace from "@app/components/MenuSpace.svelte"
2024-11-19 04:31:17 +00:00
import SpaceAuthError from "@app/components/SpaceAuthError.svelte"
import SpaceTrustRelay from "@app/components/SpaceTrustRelay.svelte"
import {pushToast} from "@app/util/toast"
import {pushModal} from "@app/util/modal"
import {setChecked} from "@app/util/notifications"
import {
decodeRelay,
deriveRelayAuthError,
relaysPendingTrust,
deriveSocket,
userRoomsByUrl,
} from "@app/core/state"
import {pullConservatively} from "@app/core/requests"
2025-09-22 21:05:57 +00:00
import {hasBlossomSupport} from "@app/core/commands"
import {notifications} from "@app/util/notifications"
type Props = {
children?: Snippet
2025-02-04 00:37:14 +00:00
}
const {children}: Props = $props()
2024-08-16 19:42:00 +00:00
2025-10-13 20:35:02 +00:00
const url = decodeRelay($page.params.relay!)
2024-11-14 20:16:44 +00:00
2025-01-02 23:08:16 +00:00
const rooms = Array.from($userRoomsByUrl.get(url) || [])
const socket = deriveSocket(url)
2024-11-07 23:19:19 +00:00
const authError = deriveRelayAuthError(url)
2024-11-19 04:31:17 +00:00
const showAuthError = once(() => pushModal(SpaceAuthError, {url, error: $authError}))
2024-11-07 23:19:19 +00:00
2024-12-11 22:10:20 +00:00
// We have to watch this one, since on mobile the badge will be visible when active
2025-02-04 03:28:29 +00:00
$effect(() => {
2024-12-16 19:24:10 +00:00
if ($notifications.has($page.url.pathname)) {
2024-11-14 20:16:44 +00:00
setChecked($page.url.pathname)
}
2025-02-04 00:37:14 +00:00
})
2024-11-14 20:16:44 +00:00
// Watch for relay errors and notify the user
$effect(() => {
if ($authError) {
showAuthError()
}
})
onMount(() => {
2025-06-05 20:37:50 +00:00
const since = ago(MONTH)
2025-01-02 23:08:16 +00:00
sleep(2000).then(() => {
if ($socket.status !== SocketStatus.Open) {
pushToast({
theme: "error",
message: `Failed to connect to ${displayRelayUrl(url)}`,
})
}
})
2025-07-30 22:54:10 +00:00
2025-09-22 21:05:57 +00:00
// Prime our cache so we can upload images quicker
hasBlossomSupport(url)
2025-04-11 21:44:27 +00:00
// Load group meta, threads, calendar events, comments, and recent messages
// for user rooms to help with a quick page transition
2025-01-02 23:08:16 +00:00
pullConservatively({
relays: [url],
2025-01-02 23:08:16 +00:00
filters: [
2025-06-16 17:12:24 +00:00
{kinds: [ROOM_META]},
2025-07-09 21:28:07 +00:00
{kinds: [THREAD, EVENT_TIME, MESSAGE], since},
2025-04-11 21:44:27 +00:00
{kinds: [COMMENT], "#K": [String(THREAD), String(EVENT_TIME)], since},
...rooms.map(room => ({kinds: [MESSAGE], "#h": [room], since})),
2025-01-02 23:08:16 +00:00
],
2024-11-15 22:03:54 +00:00
})
})
2024-08-16 19:42:00 +00:00
</script>
2024-11-14 20:16:44 +00:00
<SecondaryNav>
<MenuSpace {url} />
</SecondaryNav>
<Page>
{#key $page.url.pathname}
2025-02-04 00:37:14 +00:00
{@render children?.()}
{/key}
2024-11-14 20:16:44 +00:00
</Page>
{#if $relaysPendingTrust.includes(url)}
<Dialog>
<SpaceTrustRelay {url} />
</Dialog>
{/if}