flotilla/src/app/components/SpaceInviteAccept.svelte

91 lines
2.7 KiB
Svelte
Raw Normal View History

2024-08-16 19:42:00 +00:00
<script lang="ts">
import {goto} from "$app/navigation"
import {tryCatch} from "@welshman/lib"
import {isRelayUrl, normalizeRelayUrl} from "@welshman/util"
2024-08-30 23:25:41 +00:00
import {loadRelay} from "@welshman/app"
2024-08-16 19:42:00 +00:00
import CardButton from "@lib/components/CardButton.svelte"
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-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} from "@app/commands"
import {makeSpacePath} from "@app/routes"
2024-08-16 19:42:00 +00:00
const back = () => history.back()
const browse = () => goto("/discover")
const joinRelay = async (url: string) => {
url = normalizeRelayUrl(url)
2024-08-16 19:42:00 +00:00
const relay = await loadRelay(url)
if (!relay?.profile) {
2024-08-16 19:42:00 +00:00
return pushToast({
theme: "error",
message: "Sorry, we weren't able to find that relay.",
})
}
await addSpaceMembership(url)
2024-08-16 19:42:00 +00:00
goto(makeSpacePath(url))
2024-08-16 19:42:00 +00:00
pushToast({
message: "Welcome to the space!",
})
}
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}>
<div class="py-2">
<h1 class="heading">Join a Space</h1>
2024-09-11 18:33:50 +00:00
<p class="text-center">Enter an invite code below to join an existing space.</p>
2024-08-16 19:42:00 +00:00
</div>
<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>
2024-10-09 21:19:27 +00:00
<Button on:click={browse}>
<CardButton>
<div slot="icon"><Icon icon="compass" size={7} /></div>
<div slot="title">Don't have an invite?</div>
<div slot="info">Browse other spaces on the discover page.</div>
</CardButton>
</Button>
2024-08-16 19:42:00 +00:00
<div class="flex flex-row items-center justify-between gap-4">
<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>
</div>
</form>