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

75 lines
2.2 KiB
Svelte
Raw Normal View History

<script module lang="ts">
const joinPrompted = new Set<string>()
</script>
2024-08-16 19:42:00 +00:00
<script lang="ts">
import type {Snippet} from "svelte"
2024-08-16 22:17:30 +00:00
import {page} from "$app/stores"
import {once} from "@welshman/lib"
2024-08-16 19:42:00 +00:00
import Page from "@lib/components/Page.svelte"
import SecondaryNav from "@lib/components/SecondaryNav.svelte"
2025-11-13 18:36:28 +00:00
import SpaceMenu from "@app/components/SpaceMenu.svelte"
2024-11-19 04:31:17 +00:00
import SpaceAuthError from "@app/components/SpaceAuthError.svelte"
import SpaceTrustRelay from "@app/components/SpaceTrustRelay.svelte"
import SpaceJoin from "@app/components/SpaceJoin.svelte"
import {deriveRelayAuthError} from "@app/access"
import {loadUserGroupList, userSpaceUrls} from "@app/groups"
import {modal, pushModal} from "@app/modal"
import {relaysPendingTrust} from "@app/policies"
import {decodeRelay} from "@app/relays"
import {makeSpacePath} from "@app/routes"
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
const authError = deriveRelayAuthError(url)
2024-11-19 04:31:17 +00:00
const showAuthError = once(() =>
pushModal(SpaceAuthError, {url, error: $authError}, {noEscape: true}),
)
const showPendingTrust = once(() => pushModal(SpaceTrustRelay, {url}, {noEscape: true}))
2024-11-07 23:19:19 +00:00
// Track this manually since we want to avoid race conditions in which we show this prompt before we load
2026-07-03 22:57:45 +00:00
let spacesLoaded = $state(false)
2026-07-01 17:23:23 +00:00
// Watch for relay errors and notify the user
// Direct links skip Discover — prompt to join when relay is not in the user's space list.
$effect(() => {
if ($modal) return
if (!$userSpaceUrls.includes(url) && !joinPrompted.has(url)) {
if (spacesLoaded) {
joinPrompted.add(url)
pushModal(SpaceJoin, {url})
} else {
loadUserGroupList([url]).then(() => {
spacesLoaded = true
})
}
} else if ($authError) {
showAuthError()
} else if ($relaysPendingTrust.includes(url)) {
showPendingTrust()
}
})
2024-08-16 19:42:00 +00:00
</script>
{#if $page.url.pathname === makeSpacePath(url)}
{@render children?.()}
{:else}
<SecondaryNav>
<SpaceMenu {url} />
</SecondaryNav>
<Page>
{#key $page.url.pathname}
{@render children?.()}
{/key}
</Page>
{/if}