import { toDMConversation } from '@/lib/link' import { useSecondaryPage } from '@/PageManager' import { useDM } from '@/providers/DMProvider' import { useNostr } from '@/providers/NostrProvider' import { nip19 } from 'nostr-tools' import { Loader2, Plus, RefreshCw, X } from 'lucide-react' import { useRef, useState } from 'react' import { useTranslation } from 'react-i18next' import ConversationList from './ConversationList' import { Button } from '../ui/button' import RelayConfigurationRequired from '../RelayConfigurationRequired' export default function InboxContent() { const { t } = useTranslation() const { relayList } = useNostr() const { isLoading, error, refreshConversations, startConversation } = useDM() const { push } = useSecondaryPage() const [showNewDM, setShowNewDM] = useState(false) const [newDMInput, setNewDMInput] = useState('') const [newDMError, setNewDMError] = useState('') const inputRef = useRef(null) // Check if user has relay list configured for DMs const hasRelayList = relayList && (relayList.read.length > 0 || relayList.write.length > 0) if (!hasRelayList) { return (
) } if (isLoading) { return (
{t('Loading messages...')}
) } if (error) { return (

{error}

) } const handleNewDM = () => { setNewDMError('') const input = newDMInput.trim() if (!input) return let hexPubkey: string try { if (input.startsWith('npub1')) { const decoded = nip19.decode(input) if (decoded.type !== 'npub') { setNewDMError(t('Invalid npub')) return } hexPubkey = decoded.data } else if (/^[0-9a-f]{64}$/i.test(input)) { hexPubkey = input.toLowerCase() } else { setNewDMError(t('Enter an npub or 64-char hex pubkey')) return } } catch { setNewDMError(t('Invalid npub')) return } startConversation(hexPubkey) push(toDMConversation(hexPubkey)) setShowNewDM(false) setNewDMInput('') } // Conversations list - clicking opens in secondary panel (or overlay on mobile) return (
{showNewDM ? (
{ setNewDMInput(e.target.value); setNewDMError('') }} onKeyDown={(e) => e.key === 'Enter' && handleNewDM()} placeholder="npub1..." className="flex-1 bg-transparent text-sm outline-none placeholder:text-muted-foreground" autoFocus />
) : ( )}
{newDMError && (
{newDMError}
)}
) }