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

90 lines
2.7 KiB
Svelte
Raw Normal View History

2024-08-16 19:42:00 +00:00
<script lang="ts">
2025-02-04 00:37:14 +00:00
import {run} from "svelte/legacy"
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-01-24 21:35:09 +00:00
import {ago, MONTH} from "@welshman/lib"
2025-01-24 17:36:24 +00:00
import {GROUPS, THREAD, COMMENT, MESSAGE, DELETE} from "@welshman/util"
2025-01-24 21:35:09 +00:00
import {subscribe, load} from "@welshman/app"
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 00:37:14 +00:00
run(() => {
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-01-24 21:35:09 +00:00
const since = ago(MONTH)
2025-01-02 23:08:16 +00:00
2025-01-24 21:35:09 +00:00
// Load all groups for this space to populate navigation. It would be nice to sync, but relay29
// is too picky about how requests are built.
load({relays, filters: [{kinds: [GROUPS]}], delay: 0})
2025-01-02 23:08:16 +00:00
2025-01-24 21:35:09 +00:00
// Load threads, 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-01-24 17:36:24 +00:00
{kinds: [THREAD], since},
{kinds: [COMMENT], "#K": [String(THREAD)], since},
2025-01-24 21:35:09 +00:00
...rooms.map(r => ({kinds: [MESSAGE], "#h": [r], since})),
2025-01-02 23:08:16 +00:00
],
2024-11-15 22:03:54 +00:00
})
2025-01-03 00:58:04 +00:00
// Listen for deletes that would apply to messages we already have, and new groups
const sub = subscribe({relays, filters: [{kinds: [DELETE, GROUPS], since}]})
2025-01-02 23:08:16 +00:00
2024-11-15 22:03:54 +00:00
return () => {
sub.close()
}
})
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>