import { useState, useRef, useEffect } from 'react';
import { useParams, Link, useSearchParams } from 'react-router-dom';
import { useSeoMeta } from '@unhead/react';
import { nip19 } from 'nostr-tools';
import { LoginArea } from '@/components/auth/LoginArea';
import { Card, CardContent } from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { Textarea } from '@/components/ui/textarea';
import { Avatar, AvatarImage, AvatarFallback } from '@/components/ui/avatar';
import { Skeleton } from '@/components/ui/skeleton';
import { useCurrentUser } from '@/hooks/useCurrentUser';
import { useTranslation } from '@/contexts/language';
import { useToast } from '@/hooks/useToast';
import { useNostrLogin } from '@nostrify/react/login';
import { hasNip44 as checkNip44 } from '@/lib/signerCapabilities';
import { useConversation, useSendDirectMessage, type DirectMessage } from '@/hooks/useDirectMessages';
import { usePendingTradeConfirmations, useTradeAttestation, useIncomingTradeRequests } from '@/hooks/useTradeAttestations';
import { useCashuEscrow } from '@/hooks/wallet/useCashuEscrow';
import { EscrowPanel } from '@/components/EscrowPanel';
import { useDMRead } from '@/hooks/useDMRead';
import { useAuthor } from '@/hooks/useAuthor';
import { genUserName } from '@/lib/genUserName';
import { formatDistanceToNow } from 'date-fns';
import {
ArrowLeft,
Send,
Lock,
Loader2,
AlertCircle,
RefreshCw,
CheckCircle2,
Handshake,
} from 'lucide-react';
import { cn } from '@/lib/utils';
import { AmberApprovalHint } from '@/components/AmberApprovalHint';
import NotFound from './NotFound';
// Single message bubble
function MessageBubble({ message, isFromMe }: { message: DirectMessage; isFromMe: boolean }) {
const timeAgo = formatDistanceToNow(new Date(message.createdAt * 1000), { addSuffix: true });
return (
{message.content}
{timeAgo}
);
}
function MessageSkeleton({ isFromMe }: { isFromMe: boolean }) {
return (
);
}
export default function ConversationPage() {
const { t } = useTranslation();
const { npub } = useParams<{ npub: string }>();
const [searchParams] = useSearchParams();
const { user } = useCurrentUser();
const { toast } = useToast();
const { logins } = useNostrLogin();
const messagesEndRef = useRef(null);
// Support pre-filled message from URL query param
const prefillMessage = searchParams.get('message') || '';
const [newMessage, setNewMessage] = useState('');
// Set pre-filled message when URL param changes
useEffect(() => {
if (prefillMessage) {
setNewMessage(prefillMessage);
}
}, [prefillMessage]);
// Decode npub to get pubkey
let otherPubkey: string | null = null;
try {
if (npub?.startsWith('npub1')) {
const decoded = nip19.decode(npub);
if (decoded.type === 'npub') {
otherPubkey = decoded.data;
}
}
} catch {
// Invalid npub
}
const { data: messages, isLoading, error, refetch, isRefetching } = useConversation(otherPubkey || '');
const { mutate: sendMessage, isPending: isSending } = useSendDirectMessage();
const { data: pendingTrades } = usePendingTradeConfirmations(user?.pubkey);
const { data: incomingRequests } = useIncomingTradeRequests(user?.pubkey);
const { publishTrade, isPending: confirmingTrade } = useTradeAttestation();
const escrow = useCashuEscrow();
const [isRedeeming, setIsRedeeming] = useState(false);
const { markRead } = useDMRead();
const author = useAuthor(otherPubkey || undefined);
const metadata = author.data?.metadata;
const displayName = metadata?.name ?? (otherPubkey ? genUserName(otherPubkey) : t('socialUi.conversation.unknown'));
const avatar = metadata?.picture;
useSeoMeta({
title: t('socialUi.conversation.seoTitle', { name: displayName }),
description: t('socialUi.conversation.seoDescription'),
});
// Scroll to bottom when messages change
useEffect(() => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
}, [messages]);
// Viewing a conversation marks it read up to its newest message, so the
// unread dot on the inbox clears the moment you open it.
useEffect(() => {
if (!otherPubkey || !messages || messages.length === 0) return;
const newest = messages.reduce((max, m) => Math.max(max, m.createdAt), 0);
markRead(otherPubkey, newest);
}, [otherPubkey, messages, markRead]);
const handleSend = () => {
if (!newMessage.trim() || !otherPubkey || isSending) return;
sendMessage(
{ recipients: otherPubkey, content: newMessage.trim() },
{
onSuccess: () => {
setNewMessage('');
},
onError: (error) => {
// Without this a failed send is silent (common with interactive
// signers / relay hiccups) and the draft just sits there.
toast({
title: t('socialUi.conversation.sendFailed'),
description: error instanceof Error ? error.message : t('socialUi.conversation.sendFailedDesc'),
variant: 'destructive',
});
},
}
);
};
// A pending trade the other party recorded but we haven't confirmed yet.
const pendingTrade = otherPubkey
? pendingTrades?.find((t) => t.seller === otherPubkey)
: undefined;
const handleConfirmTrade = async () => {
if (!pendingTrade || !otherPubkey) return;
try {
await publishTrade({ listing: pendingTrade.listing, role: 'bought', counterparty: otherPubkey });
toast({ title: t('socialUi.conversation.tradeConfirmed'), description: t('socialUi.conversation.tradeConfirmedDesc') });
} catch (e) {
toast({
title: t('socialUi.conversation.tradeConfirmFailed'),
description: e instanceof Error ? e.message : t('socialUi.conversation.somethingWrong'),
variant: 'destructive',
});
}
};
// The other party asked us (the seller) to record a sale (kind 31872).
const incomingRequest = otherPubkey
? incomingRequests?.find((r) => r.requestor === otherPubkey)
: undefined;
const handleFulfillRequest = async () => {
if (!incomingRequest || !otherPubkey) return;
try {
await publishTrade({
listing: incomingRequest.listing,
role: 'sold',
counterparty: otherPubkey,
requestCoord: incomingRequest.coord,
note: t('socialUi.conversation.soldNote', { name: displayName }),
});
toast({
title: t('socialUi.conversation.saleConfirmed'),
description: t('socialUi.conversation.saleConfirmedDesc', { name: displayName }),
});
} catch (e) {
toast({
title: t('socialUi.conversation.saleConfirmFailed'),
description: e instanceof Error ? e.message : t('socialUi.conversation.somethingWrong'),
variant: 'destructive',
});
}
};
// Escrow token attached to the incoming request, if any.
const escrowToken = incomingRequest?.event.tags.find(([t]) => t === 'cashu_token')?.[1];
const handleRedeemEscrow = async () => {
if (!escrowToken) return;
setIsRedeeming(true);
try {
await escrow.redeemEscrow(escrowToken);
toast({
title: t('socialUi.conversation.escrowRedeemed'),
description: t('socialUi.conversation.escrowRedeemedDesc'),
});
} catch (e) {
toast({
title: t('socialUi.conversation.escrowRedeemFailed'),
description: e instanceof Error ? e.message : t('socialUi.conversation.somethingWrong'),
variant: 'destructive',
});
} finally {
setIsRedeeming(false);
}
};
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSend();
}
};
// Invalid npub
if (!otherPubkey) {
return ;
}
// Not logged in
if (!user) {
return (
<>
{t('socialUi.conversation.loginTitle')}
>
);
}
// Check NIP-44 support
if (!checkNip44(user, logins)) {
const isAndroid = /Android/i.test(navigator.userAgent);
const isMobile = isAndroid || /iPhone|iPad|iPod/i.test(navigator.userAgent);
return (
<>
{t('socialUi.conversation.secureLoginTitle')}
{t('socialUi.conversation.secureLoginDesc')}
{isMobile && isAndroid && (
{t('socialUi.dms.getAmber')}
)}
>
);
}
return (
<>
{/* Header */}
{displayName.slice(0, 2).toUpperCase()}
{displayName}
{t('socialUi.conversation.encrypted')}
{/* Pending trade confirmation (the seller recorded the sale) */}
{pendingTrade && (
{t('socialUi.conversation.didCompleteTrade')}
{t('socialUi.conversation.recordedSale', { name: displayName })}
)}
{/* Incoming request: the other party asked us (seller) to record the sale */}
{incomingRequest && (
{t('socialUi.conversation.confirmSale')}
{t('socialUi.conversation.askedToRecord', { name: displayName })}
{escrowToken && (
{escrow.available ? (
) : (
{t('socialUi.conversation.escrowAppOnly')}
)}
)}
)}
{/* Messages Area */}
{isLoading ? (
<>
>
) : error ? (
{t('socialUi.dms.loadFailed')}
) : messages && messages.length > 0 ? (
messages.map((msg) => (
))
) : (
{t('socialUi.dms.emptyTitle')}
{t('socialUi.conversation.startThread')}
)}
{/* Message Input */}
{t('socialUi.conversation.e2eeNote')}
{/* A 1:1 NIP-17 DM encrypts + signs to the recipient AND to yourself
(so you keep a copy) = up to 4 signer approvals. Warn Amber users. */}
>
);
}