flotilla/src/app/components/SpaceJoin.svelte

83 lines
2.4 KiB
Svelte
Raw Normal View History

2024-08-09 00:22:17 +00:00
<script lang="ts">
2026-01-30 16:41:58 +00:00
import {onMount} from "svelte"
import {maybe} from "@welshman/lib"
2025-02-04 03:28:29 +00:00
import {preventDefault} from "@lib/html"
2025-09-10 22:26:39 +00:00
import AltArrowLeft from "@assets/icons/alt-arrow-left.svg?dataurl"
import AltArrowRight from "@assets/icons/alt-arrow-right.svg?dataurl"
2024-08-16 17:50:38 +00:00
import Icon from "@lib/components/Icon.svelte"
2026-01-30 16:41:58 +00:00
import Button from "@lib/components/Button.svelte"
import Spinner from "@lib/components/Spinner.svelte"
2026-01-30 22:14:12 +00:00
import Modal from "@lib/components/Modal.svelte"
import ModalBody from "@lib/components/ModalBody.svelte"
import ModalFooter from "@lib/components/ModalFooter.svelte"
2026-01-30 16:41:58 +00:00
import RelaySummary from "@app/components/RelaySummary.svelte"
import SpaceAccessRequest from "@app/components/SpaceAccessRequest.svelte"
2026-07-04 00:17:57 +00:00
import SpaceJoinNotifications from "@app/components/SpaceJoinNotifications.svelte"
import SpaceJoinStatus from "@app/components/SpaceJoinStatus.svelte"
import {Access} from "@app/access"
2026-06-08 21:14:40 +00:00
import {pushModal} from "@app/modal"
import {pushToast} from "@app/toast"
import {goToSpace} from "@app/routes"
2026-01-30 16:41:58 +00:00
type Props = {
url: string
}
const {url}: Props = $props()
2024-08-09 00:22:17 +00:00
const access = new Access(url)
2024-08-16 19:42:00 +00:00
const back = () => history.back()
2024-08-15 19:08:07 +00:00
2024-08-09 23:22:09 +00:00
const join = async () => {
2026-01-30 16:41:58 +00:00
if (error) {
return pushModal(SpaceAccessRequest, {url, callback: back})
}
2024-08-09 23:22:09 +00:00
loading = true
try {
await access.completeJoin(notifications)
2025-12-18 02:43:13 +00:00
2026-01-30 16:41:58 +00:00
pushToast({message: "Welcome to the space!"})
2026-07-06 20:25:20 +00:00
await goToSpace(url)
} catch (e) {
2026-01-30 16:41:58 +00:00
console.error("Failed to join space:", e)
pushToast({theme: "error", message: "Failed to join space. Please try again."})
} finally {
2024-08-09 23:22:09 +00:00
loading = false
}
}
let error = $state(maybe<string>())
2026-01-30 16:41:58 +00:00
let loading = $state(true)
let notifications = $state(true)
onMount(async () => {
error = await access.attempt()
2026-01-30 16:41:58 +00:00
loading = false
})
2024-08-09 00:22:17 +00:00
</script>
2026-01-30 22:14:12 +00:00
<Modal tag="form" onsubmit={preventDefault(join)}>
<ModalBody>
<RelaySummary {url} />
2026-07-04 00:17:57 +00:00
<SpaceJoinNotifications bind:notifications />
{#if error}
<SpaceJoinStatus {url} {error} />
{/if}
2026-01-30 22:14:12 +00:00
</ModalBody>
<ModalFooter>
<Button class="button button-link" onclick={back} disabled={loading}>
2025-09-10 22:26:39 +00:00
<Icon icon={AltArrowLeft} />
2024-08-09 00:22:17 +00:00
Go back
2024-08-09 23:22:09 +00:00
</Button>
<Button type="submit" class="button button-primary" disabled={loading}>
2026-01-30 22:14:12 +00:00
<Spinner {loading}>
2026-01-30 16:41:58 +00:00
{error ? "Request Access" : "Join Space"}
</Spinner>
2025-09-10 22:26:39 +00:00
<Icon icon={AltArrowRight} />
2024-08-09 23:22:09 +00:00
</Button>
</ModalFooter>
2026-01-30 22:14:12 +00:00
</Modal>