import { Button } from '@/components/ui/button' import { DialogFooter } from '@/components/ui/dialog' import { Textarea } from '@/components/ui/textarea' import { ExtendedKind } from '@/constants' import { createPublicMessageDraftEvent } from '@/lib/draft-event' import { createFakeEvent } from '@/lib/event' import { LoginRequiredError } from '@/lib/nostr-errors' import { pubkeyToNpub } from '@/lib/pubkey' import { showSimplePublishSuccess } from '@/lib/publishing-feedback' import { cn } from '@/lib/utils' import { useNostr } from '@/providers/NostrProvider' import { useEffect, useMemo, useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import { toast } from 'sonner' import MarkdownArticle from '../Note/MarkdownArticle/MarkdownArticle' const TIP_NOTICE_DEFAULT_KEY = 'I just sent you a tip!' function defaultTipNoticeMessage(recipientPubkey: string, tipText: string): string { const npub = pubkeyToNpub(recipientPubkey) return `nostr:${npub} ${tipText}` } export default function PublicMessageForm({ recipientPubkey, onBack, onDone }: { recipientPubkey: string onBack: () => void onDone: () => void }) { const { t } = useTranslation() const { publish, checkLogin, pubkey: selfPubkey } = useNostr() const [sending, setSending] = useState(false) const [message, setMessage] = useState('') const textareaRef = useRef(null) const tipText = t(TIP_NOTICE_DEFAULT_KEY) useEffect(() => { setMessage(defaultTipNoticeMessage(recipientPubkey, tipText)) }, [recipientPubkey, tipText]) useEffect(() => { const id = requestAnimationFrame(() => { textareaRef.current?.focus() textareaRef.current?.setSelectionRange( textareaRef.current.value.length, textareaRef.current.value.length ) }) return () => cancelAnimationFrame(id) }, []) const previewEvent = useMemo(() => { return createFakeEvent({ kind: ExtendedKind.PUBLIC_MESSAGE, pubkey: selfPubkey ?? '', content: message, tags: [['p', recipientPubkey]] }) }, [message, recipientPubkey, selfPubkey]) const handleSend = () => { const trimmed = message.trim() if (!trimmed) return checkLogin(async () => { if (selfPubkey === recipientPubkey) { onDone() return } setSending(true) try { const draft = await createPublicMessageDraftEvent(trimmed, [recipientPubkey], { addClientTag: true }) await publish(draft, { disableFallbacks: true }) showSimplePublishSuccess(t('Tip notice sent')) onDone() } catch (error) { if (error instanceof LoginRequiredError) return toast.error( t('Failed to send tip notice', { error: error instanceof Error ? error.message : String(error) }) ) } finally { setSending(false) } }) } return (

{t('Tip notice prompt description')}