flotilla/src/app/components/RoomItemMenu.svelte

184 lines
5.1 KiB
Svelte
Raw Normal View History

2024-10-16 18:27:27 +00:00
<script lang="ts">
2026-08-17 18:18:38 +00:00
import * as nip19 from "nostr-tools/nip19"
2025-10-06 18:23:19 +00:00
import type {TrustedEvent} from "@welshman/util"
2026-08-17 18:18:38 +00:00
import {tagSpec, tagValue, toNostrURI} from "@welshman/util"
2025-10-06 18:23:19 +00:00
import Code2 from "@assets/icons/code-2.svg?dataurl"
import GalleryWide from "@assets/icons/gallery-wide.svg?dataurl"
2026-08-21 15:31:53 +00:00
import ShareCircle from "@assets/icons/share-circle.svg?dataurl"
2025-10-06 18:23:19 +00:00
import TrashBin2 from "@assets/icons/trash-bin-2.svg?dataurl"
import Danger from "@assets/icons/danger.svg?dataurl"
import Pin from "@assets/icons/pin.svg?dataurl"
import NotesMinimalistic from "@assets/icons/notes-minimalistic.svg?dataurl"
import VolumeLoud from "@assets/icons/volume-loud.svg?dataurl"
2024-10-16 18:27:27 +00:00
import Button from "@lib/components/Button.svelte"
import Icon from "@lib/components/Icon.svelte"
2025-11-14 00:39:44 +00:00
import Confirm from "@lib/components/Confirm.svelte"
2024-10-16 18:27:27 +00:00
import EventInfo from "@app/components/EventInfo.svelte"
2025-11-14 19:35:19 +00:00
import Report from "@app/components/Report.svelte"
import PinboardSelect from "@app/components/PinboardSelect.svelte"
import EventDeleteConfirm from "@app/components/EventDeleteConfirm.svelte"
import ThreadCreate from "@app/components/ThreadCreate.svelte"
2026-07-28 16:16:26 +00:00
import {app, relayManagement, roomPinLists, user} from "@app/core"
import {deriveUserIsSpaceAdmin} from "@app/management"
import {ROOM, deriveUserIsRoomAdmin} from "@app/rooms"
2026-08-21 15:31:53 +00:00
import {shareEvent} from "@app/share"
import {readAloud} from "@app/speech"
2026-06-08 21:14:40 +00:00
import {pushModal} from "@app/modal"
import {pushToast} from "@app/toast"
2024-10-16 18:27:27 +00:00
2025-10-06 18:23:19 +00:00
type Props = {
url: string
event: TrustedEvent
onClick: () => void
}
const {url, event, onClick}: Props = $props()
2024-10-16 18:27:27 +00:00
2026-07-28 16:16:26 +00:00
const h = tagValue(tagSpec(ROOM), event.tags) ?? ""
const pinIds = $roomPinLists.pins(url, h).$
2025-11-14 00:39:44 +00:00
const userIsAdmin = deriveUserIsSpaceAdmin(url)
const userIsRoomAdmin = deriveUserIsRoomAdmin(url, h)
const isPinned = $derived($pinIds.includes(event.id))
2025-11-14 00:39:44 +00:00
2026-08-21 15:31:53 +00:00
const share = () => {
onClick()
shareEvent(url, "Message", event)
}
const addToLibrary = () => {
onClick()
pushModal(PinboardSelect, {url, event})
}
const read = () => {
onClick()
readAloud(event)
}
const createThread = () => {
onClick()
2026-08-17 18:18:38 +00:00
pushModal(ThreadCreate, {
url,
h,
initialValues: {content: toNostrURI(nip19.neventEncode({...event, relays: [url]}))},
})
}
2025-01-15 01:23:27 +00:00
const report = () => {
onClick()
2025-11-14 19:35:19 +00:00
pushModal(Report, {url, event})
2025-01-15 01:23:27 +00:00
}
2024-10-16 18:27:27 +00:00
const showInfo = () => {
onClick()
pushModal(EventInfo, {url, event})
2024-10-16 18:27:27 +00:00
}
const showDelete = () => {
onClick()
pushModal(EventDeleteConfirm, {url, event})
2024-10-16 18:27:27 +00:00
}
2025-11-14 00:39:44 +00:00
const showAdminDelete = () =>
pushModal(Confirm, {
title: `Delete Message`,
message: `Are you sure you want to delete this message from the space?`,
confirm: async () => {
2026-07-28 16:16:26 +00:00
const {error} = await $relayManagement.forUrl(url).banEvent(event.id)
2025-11-14 00:39:44 +00:00
if (error) {
pushToast({theme: "error", message: error})
} else {
pushToast({message: "Event has successfully been deleted!"})
2026-07-28 16:16:26 +00:00
$app.repository.removeEvent(event.id)
2025-11-14 00:39:44 +00:00
history.back()
}
},
})
const togglePin = async () => {
onClick()
if (!h) return
2026-09-09 01:07:48 +00:00
// The optimistic write flips isPinned while the publish is in flight, so what this did is read
// before it goes out rather than after.
const wasPinned = isPinned
const pins = wasPinned ? $pinIds.filter(pin => pin !== event.id) : [...$pinIds, event.id]
2026-07-28 16:16:26 +00:00
const command = await $roomPinLists.setPins(url, h, pins)
const error = await command.publishToRelays([url]).waitForError()
if (error) {
pushToast({theme: "error", message: error})
} else {
2026-09-09 01:07:48 +00:00
pushToast({message: wasPinned ? "Message unpinned" : "Message pinned"})
}
}
2024-10-16 18:27:27 +00:00
</script>
<ul class="menu bg-surface whitespace-nowrap rounded-2xl p-2">
2024-10-16 18:27:27 +00:00
<li>
<Button onclick={showInfo}>
2025-09-10 22:26:39 +00:00
<Icon size={4} icon={Code2} />
Message Details
</Button>
</li>
2026-08-21 15:31:53 +00:00
<li>
<Button onclick={share}>
<Icon size={4} icon={ShareCircle} />
Share
</Button>
</li>
<li>
<Button onclick={addToLibrary}>
<Icon size={4} icon={GalleryWide} />
Add to Library
2024-10-16 18:27:27 +00:00
</Button>
</li>
<li>
<Button onclick={read}>
<Icon size={4} icon={VolumeLoud} />
Read Out Loud
</Button>
</li>
{#if h}
<li>
<Button onclick={createThread}>
<Icon size={4} icon={NotesMinimalistic} />
Create a Thread
</Button>
</li>
{/if}
{#if h && $userIsRoomAdmin}
<li>
<Button onclick={togglePin}>
<Icon size={4} icon={Pin} />
{isPinned ? "Unpin Message" : "Pin Message"}
</Button>
</li>
{/if}
2026-07-28 16:16:26 +00:00
{#if event.pubkey === $user.pubkey}
2024-10-16 18:27:27 +00:00
<li>
<Button onclick={showDelete} class="text-error">
2025-09-10 22:26:39 +00:00
<Icon size={4} icon={TrashBin2} />
2024-10-16 18:27:27 +00:00
Delete Message
</Button>
</li>
2025-01-15 01:23:27 +00:00
{:else}
<li>
<Button class="text-error" onclick={report}>
2025-09-10 22:26:39 +00:00
<Icon size={4} icon={Danger} />
2025-01-15 01:23:27 +00:00
Report Content
</Button>
</li>
2025-11-14 00:39:44 +00:00
{#if $userIsAdmin}
<li>
<Button class="text-error" onclick={showAdminDelete}>
<Icon size={4} icon={TrashBin2} />
Delete Message
</Button>
</li>
{/if}
2024-10-16 18:27:27 +00:00
{/if}
</ul>