flotilla/src/app/components/SpaceJoin.svelte

129 lines
4.1 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 {goto} from "$app/navigation"
import {dissoc} 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"
2026-01-30 16:41:58 +00:00
import DangerTriangle from "@assets/icons/danger-triangle.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 StatusIndicator from "@lib/components/StatusIndicator.svelte"
import RelaySummary from "@app/components/RelaySummary.svelte"
import SocketStatusIndicator from "@app/components/SocketStatusIndicator.svelte"
import SpaceAccessRequest from "@app/components/SpaceAccessRequest.svelte"
2026-01-30 22:14:12 +00:00
import {
attemptRelayAccess,
addSpaceMembership,
broadcastUserData,
setSpaceNotifications,
} from "@app/core/commands"
import {relaysMostlyRestricted, notificationSettings} from "@app/core/state"
2026-01-30 16:41:58 +00:00
import {pushModal} from "@app/util/modal"
import {pushToast} from "@app/util/toast"
import {makeSpacePath} from "@app/util/routes"
import {Push} from "@app/util/notifications"
type Props = {
url: string
}
const {url}: Props = $props()
2024-08-09 00:22:17 +00:00
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 {
2026-01-30 16:41:58 +00:00
if (notifications) {
if (!notificationSettings.get().push) {
await setSpaceNotifications(url, true)
} else {
const permissions = await Push.request()
if (permissions === "granted") {
await setSpaceNotifications(url, true)
}
}
} else {
await setSpaceNotifications(url, false)
}
2025-12-18 02:43:13 +00:00
await addSpaceMembership(url)
2026-01-30 16:41:58 +00:00
await goto(makeSpacePath(url), {replaceState: true})
2025-12-18 02:43:13 +00:00
broadcastUserData([url])
2026-01-30 16:41:58 +00:00
relaysMostlyRestricted.update(dissoc(url))
pushToast({message: "Welcome to the space!"})
} 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
}
}
2026-01-30 16:41:58 +00:00
let error: string | undefined = $state()
let loading = $state(true)
let notifications = $state(true)
onMount(async () => {
error = await attemptRelayAccess(url)
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} />
<div class="card2 card2-sm bg-alt">
<div class="flex justify-between gap-12">
<div class="col-1">
<strong>Enable notifications for this space</strong>
<p class="text-xs opacity-75">
Get notified about new activity in this space. You can change this later in settings.
</p>
</div>
<input type="checkbox" class="toggle toggle-primary" bind:checked={notifications} />
2026-01-30 16:41:58 +00:00
</div>
</div>
2026-01-30 22:14:12 +00:00
<div class="card2 card2-sm bg-alt flex flex-col gap-2">
<div class="flex justify-between">
<strong>Connection Status</strong>
{#if error}
<StatusIndicator class="bg-error">Error</StatusIndicator>
{:else}
<SocketStatusIndicator {url} />
{/if}
</div>
2026-01-30 16:41:58 +00:00
{#if error}
2026-01-30 22:14:12 +00:00
<div class="flex items-center gap-2">
<Icon icon={DangerTriangle} />
<p class="text-sm opacity-75">{error}</p>
</div>
2026-01-30 16:41:58 +00:00
{/if}
</div>
2026-01-30 22:14:12 +00:00
</ModalBody>
<ModalFooter>
2026-01-30 16:41:58 +00:00
<Button class="btn btn-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="btn btn-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>