flotilla/src/app/components/EventMenu.svelte

120 lines
3.5 KiB
Svelte
Raw Normal View History

2025-02-06 17:29:30 +00:00
<script lang="ts">
2025-03-18 22:36:52 +00:00
import {onMount} from "svelte"
import type {Snippet} from "svelte"
2025-10-06 18:23:19 +00:00
import {goto} from "$app/navigation"
2025-02-06 18:14:29 +00:00
import type {TrustedEvent} from "@welshman/util"
2025-11-13 22:25:59 +00:00
import {COMMENT, ManagementMethod} from "@welshman/util"
import {pubkey, repository, relaysByUrl, manageRelay} from "@welshman/app"
2025-10-06 18:23:19 +00:00
import ShareCircle from "@assets/icons/share-circle.svg?dataurl"
import Code2 from "@assets/icons/code-2.svg?dataurl"
import TrashBin2 from "@assets/icons/trash-bin-2.svg?dataurl"
import Danger from "@assets/icons/danger.svg?dataurl"
import {setKey} from "@lib/implicit"
2025-02-06 17:29:30 +00:00
import Button from "@lib/components/Button.svelte"
2025-11-13 22:25:59 +00:00
import Confirm from "@lib/components/Confirm.svelte"
2025-02-06 17:29:30 +00:00
import Icon from "@lib/components/Icon.svelte"
import EventInfo from "@app/components/EventInfo.svelte"
2025-11-14 19:35:19 +00:00
import Report from "@app/components/Report.svelte"
2025-02-06 18:14:29 +00:00
import EventShare from "@app/components/EventShare.svelte"
import EventDeleteConfirm from "@app/components/EventDeleteConfirm.svelte"
2025-11-13 22:25:59 +00:00
import {hasNip29, deriveUserIsSpaceAdmin} from "@app/core/state"
import {pushModal} from "@app/util/modal"
2025-11-13 22:25:59 +00:00
import {pushToast} from "@app/util/toast"
2025-10-06 18:23:19 +00:00
import {makeSpaceChatPath} from "@app/util/routes"
2025-02-06 17:29:30 +00:00
2025-03-18 22:36:52 +00:00
type Props = {
2025-02-06 18:14:29 +00:00
url: string
noun: string
event: TrustedEvent
onClick: () => void
2025-03-18 22:36:52 +00:00
customActions?: Snippet
}
const {url, noun, event, onClick, customActions}: Props = $props()
2025-02-06 17:29:30 +00:00
const isRoot = event.kind !== COMMENT
2025-11-13 22:25:59 +00:00
const userIsAdmin = deriveUserIsSpaceAdmin(url)
2025-02-06 17:29:30 +00:00
2025-11-14 19:35:19 +00:00
const report = () => pushModal(Report, {url, event})
2025-02-06 17:29:30 +00:00
2025-03-18 22:36:52 +00:00
const showInfo = () => pushModal(EventInfo, {url, event})
2025-02-06 17:29:30 +00:00
2025-10-06 18:23:19 +00:00
const share = async () => {
if (hasNip29($relaysByUrl.get(url))) {
pushModal(EventShare, {url, event})
} else {
setKey("share", event)
goto(makeSpaceChatPath(url))
}
}
2025-02-06 17:29:30 +00:00
2025-03-18 22:36:52 +00:00
const showDelete = () => pushModal(EventDeleteConfirm, {url, event})
2025-11-13 22:25:59 +00:00
const showAdminDelete = () =>
pushModal(Confirm, {
title: `Delete ${noun}`,
message: `Are you sure you want to delete this ${noun.toLowerCase()} from the space?`,
confirm: async () => {
const {error} = await manageRelay(url, {
method: ManagementMethod.BanEvent,
params: [event.id],
})
if (error) {
pushToast({theme: "error", message: error})
} else {
pushToast({message: "Event has successfully been deleted!"})
repository.removeEvent(event.id)
history.back()
}
},
})
2025-03-18 22:36:52 +00:00
let ul: Element
onMount(() => {
ul.addEventListener("click", onClick)
})
2025-02-06 17:29:30 +00:00
</script>
2025-11-05 23:32:55 +00:00
<ul class="menu whitespace-nowrap rounded-box bg-base-100 p-2 shadow-md" bind:this={ul}>
2025-02-06 17:29:30 +00:00
{#if isRoot}
<li>
<Button onclick={share}>
2025-09-10 22:26:39 +00:00
<Icon size={4} icon={ShareCircle} />
2025-02-06 17:29:30 +00:00
Share to Chat
</Button>
</li>
{/if}
<li>
<Button onclick={showInfo}>
2025-09-10 22:26:39 +00:00
<Icon size={4} icon={Code2} />
2025-02-06 18:14:29 +00:00
{noun} Details
2025-02-06 17:29:30 +00:00
</Button>
</li>
2025-03-18 22:36:52 +00:00
{@render customActions?.()}
2025-02-06 17:29:30 +00:00
{#if event.pubkey === $pubkey}
<li>
<Button onclick={showDelete} class="text-error">
2025-09-10 22:26:39 +00:00
<Icon size={4} icon={TrashBin2} />
2025-02-06 18:14:29 +00:00
Delete {noun}
2025-02-06 17:29:30 +00:00
</Button>
</li>
{:else}
<li>
<Button class="text-error" onclick={report}>
2025-09-10 22:26:39 +00:00
<Icon size={4} icon={Danger} />
2025-02-06 17:29:30 +00:00
Report Content
</Button>
</li>
2025-11-13 22:25:59 +00:00
{#if $userIsAdmin}
<li>
<Button class="text-error" onclick={showAdminDelete}>
<Icon size={4} icon={TrashBin2} />
Delete {noun}
</Button>
</li>
{/if}
2025-02-06 17:29:30 +00:00
{/if}
</ul>