flotilla/src/app/components/SpaceInvite.svelte

97 lines
2.8 KiB
Svelte
Raw Normal View History

2024-10-16 17:14:33 +00:00
<script lang="ts">
2024-10-17 23:03:50 +00:00
import {onMount} from "svelte"
2025-09-06 00:20:12 +00:00
import {sleep, nthEq} from "@welshman/lib"
2025-04-11 21:44:27 +00:00
import {request} from "@welshman/net"
2024-10-16 17:14:33 +00:00
import {displayRelayUrl, AUTH_INVITE} from "@welshman/util"
2024-10-17 23:03:50 +00:00
import {slide} from "@lib/transition"
2024-10-16 17:14:33 +00:00
import Spinner from "@lib/components/Spinner.svelte"
import Field from "@lib/components/Field.svelte"
import Button from "@lib/components/Button.svelte"
2025-09-10 22:26:39 +00:00
import LinkRound from "@assets/icons/link-round.svg?dataurl"
import Copy from "@assets/icons/copy.svg?dataurl"
2024-10-16 17:14:33 +00:00
import Icon from "@lib/components/Icon.svelte"
import ModalHeader from "@lib/components/ModalHeader.svelte"
import ModalFooter from "@lib/components/ModalFooter.svelte"
import {clip} from "@app/util/toast"
2025-09-06 00:20:12 +00:00
import {PLATFORM_URL} from "@app/core/state"
2024-10-16 17:14:33 +00:00
const {url} = $props()
2024-10-16 17:14:33 +00:00
const back = () => history.back()
const copyInvite = () => clip(invite)
2025-02-04 00:37:14 +00:00
let claim = $state("")
let loading = $state(true)
2024-10-16 17:14:33 +00:00
2025-02-04 00:37:14 +00:00
let invite = $state("")
$effect(() => {
2025-09-06 00:20:12 +00:00
const relay = displayRelayUrl(url)
const params = new URLSearchParams({r: relay, c: claim}).toString()
invite = PLATFORM_URL + "/join?" + params
2025-02-04 00:37:14 +00:00
})
2024-10-16 17:14:33 +00:00
onMount(async () => {
const [[event]] = await Promise.all([
2025-04-11 21:44:27 +00:00
request({
relays: [url],
autoClose: true,
filters: [{kinds: [AUTH_INVITE]}],
}),
2024-10-16 17:14:33 +00:00
sleep(2000),
])
2024-10-17 23:03:50 +00:00
claim = event?.tags.find(nthEq(0, "claim"))?.[1] || ""
2024-10-16 17:14:33 +00:00
loading = false
})
</script>
<div class="col-4">
<ModalHeader>
2025-02-04 00:37:14 +00:00
{#snippet title()}
<div>Create an Invite</div>
{/snippet}
{#snippet info()}
<div>
Get a link that you can use to invite people to
<span class="text-primary">{displayRelayUrl(url)}</span>
</div>
{/snippet}
2024-10-16 17:14:33 +00:00
</ModalHeader>
<div>
{#if loading}
<p class="center" out:slide>
2025-09-06 00:20:12 +00:00
<Spinner {loading}>Requesting an invite link...</Spinner>
2024-10-16 17:14:33 +00:00
</p>
{:else}
<div in:slide>
<Field>
2025-02-04 00:37:14 +00:00
{#snippet input()}
<label class="input input-bordered flex w-full items-center gap-2">
2025-09-10 22:26:39 +00:00
<Icon icon={LinkRound} />
2025-02-04 00:37:14 +00:00
<input bind:value={invite} class="grow" type="text" />
<Button onclick={copyInvite}>
2025-09-10 22:26:39 +00:00
<Icon icon={Copy} />
2025-02-04 00:37:14 +00:00
</Button>
</label>
{/snippet}
{#snippet info()}
<p>
This invite link can be used by clicking "Add Space" and pasting it there.
{#if !claim}
This space did not issue a claim for this link, so additional steps might be
required for people using this invite link.
{/if}
</p>
{/snippet}
2024-10-16 17:14:33 +00:00
</Field>
</div>
{/if}
</div>
<ModalFooter>
<Button class="btn btn-primary flex-grow" onclick={back}>Done</Button>
2024-10-16 17:14:33 +00:00
</ModalFooter>
</div>