flotilla/src/app/components/RoomItemMenuMobile.svelte
2026-08-17 11:18:38 -07:00

136 lines
4.7 KiB
Svelte

<script lang="ts">
import type {NativeEmoji} from "emoji-picker-element/shared"
import type {TrustedEvent} from "@welshman/util"
import * as nip19 from "nostr-tools/nip19"
import {getIdOrAddress, tagSpec, tagValue, toNostrURI} from "@welshman/util"
import Bolt from "@assets/icons/bolt.svg?dataurl"
import Reply from "@assets/icons/reply-2.svg?dataurl"
import Code2 from "@assets/icons/code-2.svg?dataurl"
import TrashBin2 from "@assets/icons/trash-bin-2.svg?dataurl"
import SmileCircle from "@assets/icons/smile-circle.svg?dataurl"
import MenuDots from "@assets/icons/menu-dots.svg?dataurl"
import Pin from "@assets/icons/pin.svg?dataurl"
import NotesMinimalistic from "@assets/icons/notes-minimalistic.svg?dataurl"
import Button from "@lib/components/Button.svelte"
import Link from "@lib/components/Link.svelte"
import Icon from "@lib/components/Icon.svelte"
import Modal from "@lib/components/Modal.svelte"
import ModalBody from "@lib/components/ModalBody.svelte"
import EmojiPicker from "@lib/components/EmojiPicker.svelte"
import ZapButton from "@app/components/ZapButton.svelte"
import EventInfo from "@app/components/EventInfo.svelte"
import EventDeleteConfirm from "@app/components/EventDeleteConfirm.svelte"
import ThreadCreate from "@app/components/ThreadCreate.svelte"
import {reactions, relays, roomPinLists, user} from "@app/core"
import {ROOM, deriveUserIsRoomAdmin} from "@app/rooms"
import {ENABLE_ZAPS} from "@app/env"
import {makeContentPath} from "@app/routes"
import {pushModal} from "@app/modal"
import {pushToast} from "@app/toast"
type Props = {
url: string
event: TrustedEvent
reply: () => void
}
const {url, event, reply}: Props = $props()
const h = tagValue(tagSpec(ROOM), event.tags) ?? ""
const path = makeContentPath(url, event.kind, getIdOrAddress(event))
const pinIds = $roomPinLists.pins(url, h).$
const userIsRoomAdmin = deriveUserIsRoomAdmin(url, h)
const isPinned = $derived($pinIds.includes(event.id))
const onEmoji = async (emoji: NativeEmoji) => {
history.back()
const protect = await $relays.hasNip(url, 70)
const command = await $reactions.react(event, emoji.unicode, w => w.setProtected(protect))
command.publishToRelays([url])
}
const showEmojiPicker = () => pushModal(EmojiPicker, {onClick: onEmoji}, {replaceState: true})
const createThread = () =>
pushModal(
ThreadCreate,
{url, h, initialValues: {content: toNostrURI(nip19.neventEncode({...event, relays: [url]}))}},
{replaceState: true},
)
const sendReply = () => {
history.back()
reply()
}
const showInfo = () => pushModal(EventInfo, {url, event}, {replaceState: true})
const showDelete = () => pushModal(EventDeleteConfirm, {url, event})
const togglePin = async () => {
if (!h) return
history.back()
const pins = isPinned ? $pinIds.filter(pin => pin !== event.id) : [...$pinIds, event.id]
const command = await $roomPinLists.setPins(url, h, pins)
const error = await command.publishToRelays([url]).waitForError()
if (error) {
pushToast({theme: "error", message: error})
} else {
pushToast({message: isPinned ? "Message unpinned" : "Message pinned"})
}
}
</script>
<Modal>
<ModalBody>
<div class="flex flex-col gap-2">
{#if event.pubkey === $user.pubkey}
<Button class="button button-neutral text-error" onclick={showDelete}>
<Icon size={4} icon={TrashBin2} />
Delete Message
</Button>
{/if}
<Button class="button button-neutral" onclick={showInfo}>
<Icon size={4} icon={Code2} />
Message Info
</Button>
{#if path}
<Link class="button button-neutral" href={path}>
<Icon size={4} icon={MenuDots} />
View Details
</Link>
{/if}
{#if h && $userIsRoomAdmin}
<Button class="button button-neutral w-full" onclick={togglePin}>
<Icon size={4} icon={Pin} />
{isPinned ? "Unpin Message" : "Pin Message"}
</Button>
{/if}
{#if h}
<Button class="button button-neutral w-full" onclick={createThread}>
<Icon size={4} icon={NotesMinimalistic} />
Create a Thread
</Button>
{/if}
{#if ENABLE_ZAPS}
<ZapButton replaceState {url} {event} class="button button-neutral w-full">
<Icon size={4} icon={Bolt} />
Send Zap
</ZapButton>
{/if}
<Button class="button button-neutral w-full" onclick={sendReply}>
<Icon size={4} icon={Reply} />
Send Reply
</Button>
<Button class="button button-neutral w-full" onclick={showEmojiPicker}>
<Icon size={4} icon={SmileCircle} />
Send Reaction
</Button>
</div>
</ModalBody>
</Modal>