import { Button } from '@/components/ui/button' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@/components/ui/dialog' import { Drawer, DrawerContent, DrawerFooter, DrawerHeader, DrawerTitle } from '@/components/ui/drawer' import { Textarea } from '@/components/ui/textarea' import { ExtendedKind } from '@/constants' import { createPublicMessageDraftEvent } from '@/lib/draft-event' import { createFakeEvent } from '@/lib/event' import { showSimplePublishSuccess } from '@/lib/publishing-feedback' import { LoginRequiredError } from '@/lib/nostr-errors' import { pubkeyToNpub } from '@/lib/pubkey' import { cn } from '@/lib/utils' import { useNostr } from '@/providers/NostrProvider' import { useScreenSize } from '@/providers/ScreenSizeProvider' import { useEffect, useMemo, useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import { toast } from 'sonner' import MarkdownArticle from '../Note/MarkdownArticle/MarkdownArticle' import UserAvatar from '../UserAvatar' import Username from '../Username' 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 TipPublicMessagePrompt({ open, onOpenChange, recipientPubkey }: { open: boolean onOpenChange: (open: boolean) => void recipientPubkey: string | null }) { const { t } = useTranslation() const { isSmallScreen } = useScreenSize() const { publish, checkLogin, pubkey: selfPubkey } = useNostr() const [sending, setSending] = useState(false) const [message, setMessage] = useState('') const cancelRef = useRef(null) const textareaRef = useRef(null) const tipText = t(TIP_NOTICE_DEFAULT_KEY) useEffect(() => { if (!open || !recipientPubkey) return setMessage(defaultTipNoticeMessage(recipientPubkey, tipText)) }, [open, recipientPubkey, tipText]) useEffect(() => { if (!open) return const id = requestAnimationFrame(() => { textareaRef.current?.focus() textareaRef.current?.setSelectionRange( textareaRef.current.value.length, textareaRef.current.value.length ) }) return () => cancelAnimationFrame(id) }, [open]) const previewEvent = useMemo(() => { if (!recipientPubkey) return null return createFakeEvent({ kind: ExtendedKind.PUBLIC_MESSAGE, pubkey: selfPubkey ?? '', content: message, tags: [['p', recipientPubkey]] }) }, [message, recipientPubkey, selfPubkey]) const handleSend = () => { if (!recipientPubkey) return const trimmed = message.trim() if (!trimmed) return checkLogin(async () => { if (selfPubkey === recipientPubkey) { onOpenChange(false) return } setSending(true) try { const draft = await createPublicMessageDraftEvent(trimmed, [recipientPubkey], { addClientTag: true }) await publish(draft, { disableFallbacks: true }) showSimplePublishSuccess(t('Tip notice sent')) onOpenChange(false) } 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) } }) } const body = (

{t('Tip notice success only note')}