2024-08-16 19:42:00 +00:00
|
|
|
<script lang="ts">
|
2025-09-06 00:20:12 +00:00
|
|
|
import type {Snippet} from "svelte"
|
|
|
|
|
import {tryCatch, fromPairs} from "@welshman/lib"
|
2024-09-11 17:48:34 +00:00
|
|
|
import {isRelayUrl, normalizeRelayUrl} from "@welshman/util"
|
2025-04-09 22:32:18 +00:00
|
|
|
import {Pool, AuthStatus} from "@welshman/net"
|
2025-02-04 03:28:29 +00:00
|
|
|
import {preventDefault} from "@lib/html"
|
2025-09-06 00:20:12 +00:00
|
|
|
import {slideAndFade} from "@lib/transition"
|
2024-08-16 19:42:00 +00:00
|
|
|
import Spinner from "@lib/components/Spinner.svelte"
|
|
|
|
|
import Button from "@lib/components/Button.svelte"
|
|
|
|
|
import Field from "@lib/components/Field.svelte"
|
|
|
|
|
import Icon from "@lib/components/Icon.svelte"
|
2024-10-09 23:11:23 +00:00
|
|
|
import ModalHeader from "@lib/components/ModalHeader.svelte"
|
2024-10-11 00:15:20 +00:00
|
|
|
import ModalFooter from "@lib/components/ModalFooter.svelte"
|
2025-09-06 00:20:12 +00:00
|
|
|
import RelaySummary from "@app/components/RelaySummary.svelte"
|
2025-02-18 17:03:10 +00:00
|
|
|
import SpaceJoinConfirm, {confirmSpaceJoin} from "@app/components/SpaceJoinConfirm.svelte"
|
2025-08-21 22:01:31 +00:00
|
|
|
import {pushToast} from "@app/util/toast"
|
|
|
|
|
import {pushModal} from "@app/util/modal"
|
|
|
|
|
import {attemptRelayAccess} from "@app/core/commands"
|
2024-08-16 19:42:00 +00:00
|
|
|
|
2025-09-06 00:20:12 +00:00
|
|
|
type Props = {
|
|
|
|
|
invite: string
|
|
|
|
|
abortAction?: Snippet
|
|
|
|
|
}
|
2025-05-20 16:35:33 +00:00
|
|
|
|
2025-09-06 00:20:12 +00:00
|
|
|
let {invite = "", abortAction}: Props = $props()
|
2025-05-20 16:35:33 +00:00
|
|
|
|
2025-09-06 00:20:12 +00:00
|
|
|
const back = () => history.back()
|
2025-05-20 16:35:33 +00:00
|
|
|
|
2025-09-06 00:20:12 +00:00
|
|
|
const joinRelay = async () => {
|
|
|
|
|
const {url, claim} = inviteData!
|
2025-05-20 16:35:33 +00:00
|
|
|
|
2025-09-06 00:20:12 +00:00
|
|
|
const error = await attemptRelayAccess(url, claim)
|
2024-09-11 17:48:34 +00:00
|
|
|
|
2024-10-10 21:33:50 +00:00
|
|
|
if (error) {
|
2025-05-07 22:04:35 +00:00
|
|
|
return pushToast({theme: "error", message: error, timeout: 30_000})
|
2024-08-16 19:42:00 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-06 00:20:12 +00:00
|
|
|
if (Pool.get().get(url).auth.status === AuthStatus.None) {
|
|
|
|
|
pushModal(SpaceJoinConfirm, {url}, {replaceState: true})
|
2024-10-11 00:49:42 +00:00
|
|
|
} else {
|
2025-09-06 00:20:12 +00:00
|
|
|
await confirmSpaceJoin(url)
|
2024-10-11 00:49:42 +00:00
|
|
|
}
|
2024-08-16 19:42:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const join = async () => {
|
|
|
|
|
loading = true
|
|
|
|
|
|
|
|
|
|
try {
|
2025-05-20 16:35:33 +00:00
|
|
|
await joinRelay()
|
2024-08-16 19:42:00 +00:00
|
|
|
} finally {
|
|
|
|
|
loading = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-04 00:37:14 +00:00
|
|
|
let loading = $state(false)
|
2024-08-16 19:42:00 +00:00
|
|
|
|
2025-09-06 00:20:12 +00:00
|
|
|
const inviteData = $derived.by(
|
|
|
|
|
() =>
|
|
|
|
|
tryCatch(() => {
|
|
|
|
|
const {r: relay = "", c: claim = ""} = fromPairs(Array.from(new URL(invite).searchParams))
|
|
|
|
|
const url = normalizeRelayUrl(relay)
|
|
|
|
|
|
|
|
|
|
if (isRelayUrl(url)) {
|
|
|
|
|
return {url, claim}
|
|
|
|
|
}
|
|
|
|
|
}) ||
|
|
|
|
|
tryCatch(() => {
|
|
|
|
|
const url = normalizeRelayUrl(invite)
|
|
|
|
|
|
|
|
|
|
if (isRelayUrl(url)) {
|
|
|
|
|
return {url, claim: ""}
|
|
|
|
|
}
|
|
|
|
|
}),
|
2025-02-04 00:37:14 +00:00
|
|
|
)
|
2024-08-16 19:42:00 +00:00
|
|
|
</script>
|
|
|
|
|
|
2025-02-04 00:37:14 +00:00
|
|
|
<form class="column gap-4" onsubmit={preventDefault(join)}>
|
2024-10-09 23:11:23 +00:00
|
|
|
<ModalHeader>
|
2025-02-04 00:37:14 +00:00
|
|
|
{#snippet title()}
|
|
|
|
|
<div>Join a Space</div>
|
|
|
|
|
{/snippet}
|
|
|
|
|
{#snippet info()}
|
2025-09-06 00:20:12 +00:00
|
|
|
<div>Enter a relay URL or invite link below to join an existing space.</div>
|
2025-02-04 00:37:14 +00:00
|
|
|
{/snippet}
|
2024-10-09 23:11:23 +00:00
|
|
|
</ModalHeader>
|
2024-08-16 19:42:00 +00:00
|
|
|
<Field>
|
2025-02-04 00:37:14 +00:00
|
|
|
{#snippet label()}
|
2025-09-06 00:20:12 +00:00
|
|
|
<p>Invite Link*</p>
|
2025-02-04 00:37:14 +00:00
|
|
|
{/snippet}
|
|
|
|
|
{#snippet input()}
|
|
|
|
|
<label class="input input-bordered flex w-full items-center gap-2">
|
|
|
|
|
<Icon icon="link-round" />
|
2025-09-06 00:20:12 +00:00
|
|
|
<input bind:value={invite} class="grow" type="text" />
|
2025-02-04 00:37:14 +00:00
|
|
|
</label>
|
|
|
|
|
{/snippet}
|
2025-05-20 16:35:33 +00:00
|
|
|
</Field>
|
2025-09-06 00:20:12 +00:00
|
|
|
<div class="-my-4">
|
|
|
|
|
{#if inviteData}
|
|
|
|
|
<div transition:slideAndFade class="flex flex-col gap-4 py-4">
|
|
|
|
|
<div class="card2 bg-alt flex flex-col gap-4">
|
|
|
|
|
<p class="opacity-75">You're about to join:</p>
|
|
|
|
|
<RelaySummary url={inviteData.url} />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
|
|
|
|
</div>
|
2024-10-10 21:33:50 +00:00
|
|
|
<ModalFooter>
|
2025-09-06 00:20:12 +00:00
|
|
|
{#if abortAction}
|
|
|
|
|
{@render abortAction?.()}
|
|
|
|
|
{:else}
|
|
|
|
|
<Button class="btn btn-link" onclick={back}>
|
|
|
|
|
<Icon icon="alt-arrow-left" />
|
|
|
|
|
Go back
|
|
|
|
|
</Button>
|
|
|
|
|
{/if}
|
|
|
|
|
<Button type="submit" class="btn btn-primary" disabled={!inviteData || loading}>
|
2024-08-16 19:42:00 +00:00
|
|
|
<Spinner {loading}>Join Space</Spinner>
|
|
|
|
|
<Icon icon="alt-arrow-right" />
|
|
|
|
|
</Button>
|
2024-10-10 21:33:50 +00:00
|
|
|
</ModalFooter>
|
2024-08-16 19:42:00 +00:00
|
|
|
</form>
|