flotilla/src/app/components/SpaceInviteAccept.svelte

108 lines
3.1 KiB
Svelte
Raw Normal View History

2024-08-16 19:42:00 +00:00
<script lang="ts">
import {goto} from "$app/navigation"
2024-10-11 00:49:42 +00:00
import {ctx, tryCatch} from "@welshman/lib"
import {isRelayUrl, normalizeRelayUrl} from "@welshman/util"
2025-02-04 03:28:29 +00:00
import {preventDefault} from "@lib/html"
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-11 00:49:42 +00:00
import Confirm from "@lib/components/Confirm.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"
2024-09-11 18:33:50 +00:00
import InfoRelay from "@app/components/InfoRelay.svelte"
2024-08-16 19:42:00 +00:00
import {pushToast} from "@app/toast"
2024-09-11 18:33:50 +00:00
import {pushModal} from "@app/modal"
import {addSpaceMembership, attemptRelayAccess} from "@app/commands"
import {makeSpacePath} from "@app/routes"
2024-08-16 19:42:00 +00:00
const back = () => history.back()
2024-10-11 00:49:42 +00:00
const confirm = async (url: string) => {
2024-10-17 19:08:19 +00:00
await addSpaceMembership(url)
2024-10-11 00:49:42 +00:00
goto(makeSpacePath(url), {replaceState: true})
pushToast({
message: "Welcome to the space!",
})
}
const joinRelay = async (invite: string) => {
2024-10-16 17:14:33 +00:00
const [raw, claim] = invite.split("|")
const url = normalizeRelayUrl(raw)
const error = await attemptRelayAccess(url, claim)
if (error) {
return pushToast({theme: "error", message: error})
2024-08-16 19:42:00 +00:00
}
2024-10-11 00:49:42 +00:00
const connection = ctx.net.pool.get(url)
2024-08-16 19:42:00 +00:00
2024-11-13 04:50:34 +00:00
if (connection.stats.lastAuth === 0) {
2024-10-11 00:49:42 +00:00
pushModal(Confirm, {
confirm: () => confirm(url),
message: `This space does not appear to limit who can post to it. This can result
in a large amount of spam or other objectionable content. Continue?`,
})
} else {
2024-10-15 18:48:11 +00:00
await confirm(url)
2024-10-11 00:49:42 +00:00
}
2024-08-16 19:42:00 +00:00
}
const join = async () => {
loading = true
try {
await joinRelay(url)
2024-08-16 19:42:00 +00:00
} finally {
loading = false
}
}
2025-02-04 00:37:14 +00:00
let url = $state("")
let loading = $state(false)
2024-08-16 19:42:00 +00:00
const linkIsValid = $derived(
2025-02-04 00:37:14 +00:00
Boolean(tryCatch(() => isRelayUrl(normalizeRelayUrl(url.split("|")[0])))),
)
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()}
<div>Enter an invite code below to join an existing space.</div>
{/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()}
<p>Invite code*</p>
{/snippet}
{#snippet input()}
<label class="input input-bordered flex w-full items-center gap-2">
<Icon icon="link-round" />
<input bind:value={url} class="grow" type="text" />
</label>
{/snippet}
{#snippet info()}
<p>
You can also directly join any relay by entering its URL here.
<Button class="link" onclick={() => pushModal(InfoRelay)}>What is a relay?</Button>
2025-02-04 00:37:14 +00:00
</p>
{/snippet}
2024-08-16 19:42:00 +00:00
</Field>
<ModalFooter>
<Button class="btn btn-link" onclick={back}>
2024-08-16 19:42:00 +00:00
<Icon icon="alt-arrow-left" />
Go back
</Button>
<Button type="submit" class="btn btn-primary" disabled={!linkIsValid || loading}>
<Spinner {loading}>Join Space</Spinner>
<Icon icon="alt-arrow-right" />
</Button>
</ModalFooter>
2024-08-16 19:42:00 +00:00
</form>