2026-07-03 16:20:45 +00:00
|
|
|
<script lang="ts">
|
|
|
|
|
import {onMount} from "svelte"
|
2026-08-21 15:31:53 +00:00
|
|
|
import type {Maybe} from "@welshman/lib"
|
2026-08-14 17:34:46 +00:00
|
|
|
import {MembershipStatus, publish} from "@welshman/app"
|
2026-07-03 16:20:45 +00:00
|
|
|
import Pen from "@assets/icons/pen.svg?dataurl"
|
|
|
|
|
import TrashBin2 from "@assets/icons/trash-bin-2.svg?dataurl"
|
|
|
|
|
import Login3 from "@assets/icons/login-3.svg?dataurl"
|
|
|
|
|
import ClockCircle from "@assets/icons/clock-circle.svg?dataurl"
|
|
|
|
|
import Icon from "@lib/components/Icon.svelte"
|
|
|
|
|
import Button from "@lib/components/Button.svelte"
|
|
|
|
|
import Spinner from "@lib/components/Spinner.svelte"
|
|
|
|
|
import Confirm from "@lib/components/Confirm.svelte"
|
|
|
|
|
import RoomEdit from "@app/components/RoomEdit.svelte"
|
2026-07-28 16:16:26 +00:00
|
|
|
import {app, roomLists, rooms} from "@app/core"
|
2026-08-21 15:31:53 +00:00
|
|
|
import {joinRoom, leaveRoom} from "@app/access"
|
2026-08-14 17:34:46 +00:00
|
|
|
import {deriveUserIsRoomAdmin, deriveUserRoomMembershipStatus} from "@app/rooms"
|
2026-09-10 23:14:52 +00:00
|
|
|
import {forgetSpacePage, makeSpacePath} from "@app/routes"
|
2026-09-08 17:23:22 +00:00
|
|
|
import {navigate, pushModal} from "@app/modal"
|
2026-07-03 16:20:45 +00:00
|
|
|
import {pushToast} from "@app/toast"
|
|
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
url: string
|
|
|
|
|
h: string
|
|
|
|
|
onClick: () => void
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const {url, h, onClick}: Props = $props()
|
|
|
|
|
|
|
|
|
|
const userIsAdmin = deriveUserIsRoomAdmin(url, h)
|
|
|
|
|
const membershipStatus = deriveUserRoomMembershipStatus(url, h)
|
|
|
|
|
|
|
|
|
|
const startEdit = () => pushModal(RoomEdit, {url, h})
|
|
|
|
|
|
2026-08-21 15:31:53 +00:00
|
|
|
const handleLoading = async (action: () => Promise<Maybe<string>>, successMessage: string) => {
|
2026-07-03 16:20:45 +00:00
|
|
|
loading = true
|
|
|
|
|
|
|
|
|
|
try {
|
2026-08-21 15:31:53 +00:00
|
|
|
const message = await action()
|
2026-07-03 16:20:45 +00:00
|
|
|
|
2026-08-21 15:31:53 +00:00
|
|
|
pushToast(message ? {theme: "error", message} : {message: successMessage})
|
2026-07-03 16:20:45 +00:00
|
|
|
} finally {
|
|
|
|
|
loading = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-21 15:31:53 +00:00
|
|
|
const join = () => handleLoading(() => joinRoom(url, h), "You have joined the room.")
|
2026-07-03 16:20:45 +00:00
|
|
|
|
2026-08-21 15:31:53 +00:00
|
|
|
const leave = () => handleLoading(() => leaveRoom(url, h), "You have left the room.")
|
2026-07-03 16:20:45 +00:00
|
|
|
|
|
|
|
|
const startDelete = () =>
|
|
|
|
|
pushModal(Confirm, {
|
|
|
|
|
title: "Are you sure you want to delete this room?",
|
|
|
|
|
message:
|
|
|
|
|
"This room will no longer be accessible to space members, and all messages posted to it will be deleted.",
|
|
|
|
|
confirm: async () => {
|
2026-07-28 16:16:26 +00:00
|
|
|
const command = await $rooms.deleteRoom(url, {h})
|
|
|
|
|
const thunk = command.publish()
|
|
|
|
|
const message = await thunk.waitForError()
|
2026-07-03 16:20:45 +00:00
|
|
|
|
|
|
|
|
if (message) {
|
2026-07-28 16:16:26 +00:00
|
|
|
$app.repository.removeEvent(thunk.event.id)
|
2026-07-03 16:20:45 +00:00
|
|
|
pushToast({theme: "error", message})
|
|
|
|
|
} else {
|
2026-07-28 16:16:26 +00:00
|
|
|
await $roomLists.removeRoom(h, url).then(publish)
|
2026-09-10 23:14:52 +00:00
|
|
|
|
|
|
|
|
forgetSpacePage(url)
|
2026-09-08 17:23:22 +00:00
|
|
|
navigate(makeSpacePath(url))
|
2026-07-03 16:20:45 +00:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
let loading = $state(false)
|
|
|
|
|
let ul: Element
|
|
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
|
ul.addEventListener("click", onClick)
|
|
|
|
|
})
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<ul class="menu whitespace-nowrap rounded-2xl bg-surface p-2" bind:this={ul}>
|
|
|
|
|
{#if $userIsAdmin}
|
|
|
|
|
<li>
|
|
|
|
|
<Button onclick={startEdit}>
|
|
|
|
|
<Icon icon={Pen} />
|
|
|
|
|
Edit Room
|
|
|
|
|
</Button>
|
|
|
|
|
</li>
|
|
|
|
|
<li>
|
|
|
|
|
<Button class="text-error" onclick={startDelete}>
|
|
|
|
|
<Icon icon={TrashBin2} />
|
|
|
|
|
Delete Room
|
|
|
|
|
</Button>
|
|
|
|
|
</li>
|
|
|
|
|
{:else if $membershipStatus === MembershipStatus.Initial}
|
|
|
|
|
<li>
|
|
|
|
|
<Button disabled={loading} onclick={join}>
|
|
|
|
|
{#if loading}
|
|
|
|
|
<Spinner size="sm" />
|
|
|
|
|
{:else}
|
|
|
|
|
<Icon icon={Login3} />
|
|
|
|
|
{/if}
|
|
|
|
|
Join member list
|
|
|
|
|
</Button>
|
|
|
|
|
</li>
|
|
|
|
|
{:else if $membershipStatus === MembershipStatus.Pending}
|
|
|
|
|
<li>
|
|
|
|
|
<Button>
|
|
|
|
|
<Icon icon={ClockCircle} />
|
|
|
|
|
Membership pending
|
|
|
|
|
</Button>
|
|
|
|
|
</li>
|
|
|
|
|
{:else}
|
|
|
|
|
<li>
|
|
|
|
|
<Button disabled={loading} onclick={leave}>
|
|
|
|
|
{#if loading}
|
|
|
|
|
<Spinner size="sm" />
|
|
|
|
|
{:else}
|
|
|
|
|
<Icon icon={Login3} />
|
|
|
|
|
{/if}
|
|
|
|
|
Leave member list
|
|
|
|
|
</Button>
|
|
|
|
|
</li>
|
|
|
|
|
{/if}
|
|
|
|
|
</ul>
|