flotilla/src/app/components/SpaceInviteAccept.svelte

95 lines
2.9 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"
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) => {
await addSpaceMembership(url)
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-10-11 00:49:42 +00:00
if (connection.meta.lastAuth === 0) {
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
}
}
let url = ""
2024-08-16 19:42:00 +00:00
let loading = false
$: linkIsValid = Boolean(tryCatch(() => isRelayUrl(normalizeRelayUrl(url))))
2024-08-16 19:42:00 +00:00
</script>
<form class="column gap-4" on:submit|preventDefault={join}>
2024-10-09 23:11:23 +00:00
<ModalHeader>
<div slot="title">Join a Space</div>
<div slot="info">Enter an invite code below to join an existing space.</div>
</ModalHeader>
2024-08-16 19:42:00 +00:00
<Field>
2024-09-11 18:33:50 +00:00
<p slot="label">Invite code*</p>
2024-08-16 19:42:00 +00:00
<label class="input input-bordered flex w-full items-center gap-2" slot="input">
<Icon icon="link-round" />
<input bind:value={url} class="grow" type="text" />
2024-08-16 19:42:00 +00:00
</label>
2024-09-11 18:33:50 +00:00
<p slot="info">
You can also directly join any relay by entering its URL here.
<Button class="link" on:click={() => pushModal(InfoRelay)}>What is a relay?</Button>
</p>
2024-08-16 19:42:00 +00:00
</Field>
<ModalFooter>
2024-08-16 19:42:00 +00:00
<Button class="btn btn-link" on:click={back}>
<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>