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

87 lines
2.6 KiB
Svelte
Raw Normal View History

2024-08-16 19:42:00 +00:00
<script lang="ts">
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"
2025-04-11 21:44:27 +00:00
import {ago, WEEK} from "@welshman/lib"
import {GROUP_META, EVENT_TIME, GROUPS, THREAD, COMMENT, MESSAGE} from "@welshman/util"
2025-04-11 15:41:50 +00:00
import {request} from "@welshman/net"
2024-08-16 19:42:00 +00:00
import Page from "@lib/components/Page.svelte"
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"
2024-11-07 23:19:19 +00:00
import {pushToast} from "@app/toast"
2024-11-19 04:31:17 +00:00
import {pushModal} from "@app/modal"
2024-11-14 18:53:13 +00:00
import {setChecked} from "@app/notifications"
2024-11-19 04:31:17 +00:00
import {checkRelayConnection, checkRelayAuth, checkRelayAccess} from "@app/commands"
2025-01-24 17:36:24 +00:00
import {decodeRelay, userRoomsByUrl} from "@app/state"
2025-01-02 23:08:16 +00:00
import {pullConservatively} from "@app/requests"
2024-12-16 19:24:10 +00:00
import {notifications} from "@app/notifications"
2025-02-04 00:37:14 +00:00
interface Props {
children?: import("svelte").Snippet
}
const {children}: Props = $props()
2024-08-16 19:42:00 +00:00
2024-11-14 20:16:44 +00:00
const url = decodeRelay($page.params.relay)
2025-01-02 23:08:16 +00:00
const rooms = Array.from($userRoomsByUrl.get(url) || [])
2024-11-07 23:19:19 +00:00
const checkConnection = async () => {
2024-11-19 04:31:17 +00:00
const connectionError = await checkRelayConnection(url)
2024-11-07 23:19:19 +00:00
2024-11-19 04:31:17 +00:00
if (connectionError) {
return pushToast({theme: "error", message: connectionError})
}
const [authError, accessError] = await Promise.all([checkRelayAuth(url), checkRelayAccess(url)])
const error = authError || accessError
if (error) {
pushModal(SpaceAuthError, {url, error})
}
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
onMount(() => {
2024-11-07 23:19:19 +00:00
checkConnection()
2024-11-15 22:03:54 +00:00
2025-01-02 23:08:16 +00:00
const relays = [url]
2025-04-11 21:44:27 +00:00
const since = ago(WEEK)
const controller = new AbortController()
2025-01-02 23:08:16 +00:00
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,
filters: [
2025-04-11 21:44:27 +00:00
{kinds: [GROUP_META]},
{kinds: [THREAD, EVENT_TIME], since},
{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
})
2025-04-11 15:41:50 +00:00
// Completely refresh our groups list and listen for new ones
2025-04-11 16:27:19 +00:00
request({relays, filters: [{kinds: [GROUPS]}], signal: controller.signal})
2025-01-02 23:08:16 +00:00
2024-11-15 22:03:54 +00:00
return () => {
2025-04-11 16:27:19 +00:00
controller.abort()
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>