optionality around what messaging nips can be used

This commit is contained in:
sam committed 2025-10-18 21:37:09 +07:00
1 parent d8e2e62736
commit 67658eb446
4 files changed
+66 -24

No files matched your search

+7 -2
View File
@@ -13,6 +13,7 @@ import { AppProvider } from '@/components/AppProvider';
import { NWCProvider } from '@/contexts/NWCContext'; import { NWCProvider } from '@/contexts/NWCContext';
import { DMProvider } from '@/contexts/DMContext'; import { DMProvider } from '@/contexts/DMContext';
import { AppConfig } from '@/contexts/AppContext'; import { AppConfig } from '@/contexts/AppContext';
import { PROTOCOL_MODE } from '@/lib/dmConstants';
import AppRouter from './AppRouter'; import AppRouter from './AppRouter';
const head = createHead({ const head = createHead({
@@ -44,7 +45,11 @@ const presetRelays = [
]; ];
const dmSettings = { const dmSettings = {
enableNIP17: true, // Choose one:
// PROTOCOL_MODE.NIP04_ONLY - Force NIP-04 (legacy) only
// PROTOCOL_MODE.NIP17_ONLY - Force NIP-17 (private) only
// PROTOCOL_MODE.NIP04_OR_NIP17 - Allow users to choose between NIP-04 and NIP-17 (defaults to NIP-17)
protocolMode: PROTOCOL_MODE.NIP04_OR_NIP17,
}; };
export function App() { export function App() {
@@ -55,7 +60,7 @@ export function App() {
<NostrLoginProvider storageKey='nostr:login'> <NostrLoginProvider storageKey='nostr:login'>
<NostrProvider> <NostrProvider>
<NWCProvider> <NWCProvider>
<DMProvider enableNIP17={dmSettings.enableNIP17}> <DMProvider protocolMode={dmSettings.protocolMode}>
<TooltipProvider> <TooltipProvider>
<Toaster /> <Toaster />
<Suspense> <Suspense>
+39 -17
View File
@@ -3,13 +3,13 @@ import { useConversationMessages, useDMContext } from '@/contexts/DMContext';
import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useCurrentUser } from '@/hooks/useCurrentUser';
import { useAuthor } from '@/hooks/useAuthor'; import { useAuthor } from '@/hooks/useAuthor';
import { genUserName } from '@/lib/genUserName'; import { genUserName } from '@/lib/genUserName';
import { MESSAGE_PROTOCOL } from '@/lib/dmConstants'; import { MESSAGE_PROTOCOL, PROTOCOL_MODE, type MessageProtocol } from '@/lib/dmConstants';
import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { Card } from '@/components/ui/card'; import { Card } from '@/components/ui/card';
import { Textarea } from '@/components/ui/textarea'; import { Textarea } from '@/components/ui/textarea';
import { ScrollArea } from '@/components/ui/scroll-area'; import { ScrollArea } from '@/components/ui/scroll-area';
import { Badge } from '@/components/ui/badge'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip'; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip';
import { ArrowLeft, Send, Loader2, AlertTriangle } from 'lucide-react'; import { ArrowLeft, Send, Loader2, AlertTriangle } from 'lucide-react';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
@@ -137,13 +137,27 @@ const EmptyState = () => {
export const ChatArea = ({ pubkey, onBack, className }: ChatAreaProps) => { export const ChatArea = ({ pubkey, onBack, className }: ChatAreaProps) => {
const { user } = useCurrentUser(); const { user } = useCurrentUser();
const { sendMessage, isNIP17Enabled } = useDMContext(); const { sendMessage, protocolMode } = useDMContext();
const { messages, totalCount } = useConversationMessages(pubkey || ''); const { messages } = useConversationMessages(pubkey || '');
const [messageText, setMessageText] = useState(''); const [messageText, setMessageText] = useState('');
const [isSending, setIsSending] = useState(false); const [isSending, setIsSending] = useState(false);
// Determine default protocol based on mode
const getDefaultProtocol = () => {
if (protocolMode === PROTOCOL_MODE.NIP04_ONLY) return MESSAGE_PROTOCOL.NIP04;
if (protocolMode === PROTOCOL_MODE.NIP17_ONLY) return MESSAGE_PROTOCOL.NIP17;
if (protocolMode === PROTOCOL_MODE.NIP04_OR_NIP17) return MESSAGE_PROTOCOL.NIP17;
// Fallback to NIP-17 for any unexpected mode
return MESSAGE_PROTOCOL.NIP17;
};
const [selectedProtocol, setSelectedProtocol] = useState<MessageProtocol>(getDefaultProtocol());
const scrollAreaRef = useRef<HTMLDivElement>(null); const scrollAreaRef = useRef<HTMLDivElement>(null);
// Determine if selection is allowed
const allowSelection = protocolMode === PROTOCOL_MODE.NIP04_OR_NIP17;
// Auto-scroll to bottom when new messages arrive // Auto-scroll to bottom when new messages arrive
useEffect(() => { useEffect(() => {
if (scrollAreaRef.current) { if (scrollAreaRef.current) {
@@ -162,7 +176,7 @@ export const ChatArea = ({ pubkey, onBack, className }: ChatAreaProps) => {
await sendMessage({ await sendMessage({
recipientPubkey: pubkey, recipientPubkey: pubkey,
content: messageText.trim(), content: messageText.trim(),
protocol: isNIP17Enabled ? MESSAGE_PROTOCOL.NIP17 : MESSAGE_PROTOCOL.NIP04, protocol: selectedProtocol,
}); });
setMessageText(''); setMessageText('');
} catch (error) { } catch (error) {
@@ -223,17 +237,6 @@ export const ChatArea = ({ pubkey, onBack, className }: ChatAreaProps) => {
</ScrollArea> </ScrollArea>
<div className="p-4 border-t"> <div className="p-4 border-t">
<div className="flex items-center gap-2 mb-2">
<Badge variant={isNIP17Enabled ? "default" : "secondary"} className="text-xs">
{isNIP17Enabled ? "NIP-17 (Private)" : "NIP-04 (Legacy)"}
</Badge>
{totalCount > 0 && (
<span className="text-xs text-muted-foreground">
{totalCount} message{totalCount !== 1 ? 's' : ''}
</span>
)}
</div>
<div className="flex gap-2"> <div className="flex gap-2">
<Textarea <Textarea
value={messageText} value={messageText}
@@ -243,11 +246,12 @@ export const ChatArea = ({ pubkey, onBack, className }: ChatAreaProps) => {
className="min-h-[80px] resize-none" className="min-h-[80px] resize-none"
disabled={isSending} disabled={isSending}
/> />
<div className="flex flex-col gap-2">
<Button <Button
onClick={handleSend} onClick={handleSend}
disabled={!messageText.trim() || isSending} disabled={!messageText.trim() || isSending}
size="icon" size="icon"
className="h-[80px] w-[80px]" className="h-[44px] w-[90px]"
> >
{isSending ? ( {isSending ? (
<Loader2 className="h-5 w-5 animate-spin" /> <Loader2 className="h-5 w-5 animate-spin" />
@@ -255,6 +259,24 @@ export const ChatArea = ({ pubkey, onBack, className }: ChatAreaProps) => {
<Send className="h-5 w-5" /> <Send className="h-5 w-5" />
)} )}
</Button> </Button>
<Select
value={selectedProtocol}
onValueChange={(value) => setSelectedProtocol(value as MessageProtocol)}
disabled={!allowSelection}
>
<SelectTrigger className="h-[32px] w-[90px] text-xs px-2">
<SelectValue />
</SelectTrigger>
<SelectContent>
<SelectItem value={MESSAGE_PROTOCOL.NIP17} className="text-xs">
NIP-17
</SelectItem>
<SelectItem value={MESSAGE_PROTOCOL.NIP04} className="text-xs">
NIP-04
</SelectItem>
</SelectContent>
</Select>
</div>
</div> </div>
</div> </div>
</Card> </Card>
+8 -5
View File
@@ -3,7 +3,7 @@ import { useCurrentUser } from '@/hooks/useCurrentUser';
import { useSendDM } from '@/hooks/useSendDM'; import { useSendDM } from '@/hooks/useSendDM';
import { useNostr } from '@nostrify/react'; import { useNostr } from '@nostrify/react';
import { validateDMEvent } from '@/lib/dmUtils'; import { validateDMEvent } from '@/lib/dmUtils';
import { LOADING_PHASES, type LoadingPhase } from '@/lib/dmConstants'; import { LOADING_PHASES, type LoadingPhase, PROTOCOL_MODE, type ProtocolMode } from '@/lib/dmConstants';
import type { NostrEvent } from '@nostrify/nostrify'; import type { NostrEvent } from '@nostrify/nostrify';
import type { MessageProtocol } from '@/lib/dmConstants'; import type { MessageProtocol } from '@/lib/dmConstants';
import { MESSAGE_PROTOCOL } from '@/lib/dmConstants'; import { MESSAGE_PROTOCOL } from '@/lib/dmConstants';
@@ -119,7 +119,7 @@ interface DMContextType {
subscriptions: SubscriptionStatus; subscriptions: SubscriptionStatus;
conversations: ConversationSummary[]; conversations: ConversationSummary[];
sendMessage: (params: { recipientPubkey: string; content: string; protocol?: MessageProtocol }) => Promise<void>; sendMessage: (params: { recipientPubkey: string; content: string; protocol?: MessageProtocol }) => Promise<void>;
isNIP17Enabled: boolean; protocolMode: ProtocolMode;
scanProgress: ScanProgressState; scanProgress: ScanProgressState;
clearCacheAndReload: () => Promise<void>; clearCacheAndReload: () => Promise<void>;
} }
@@ -162,16 +162,19 @@ export function useConversationMessages(conversationId: string) {
interface DMProviderProps { interface DMProviderProps {
children: ReactNode; children: ReactNode;
enableNIP17?: boolean; protocolMode?: ProtocolMode;
} }
export function DMProvider({ children, enableNIP17 = true }: DMProviderProps) { export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY }: DMProviderProps) {
const { user } = useCurrentUser(); const { user } = useCurrentUser();
const { nostr } = useNostr(); const { nostr } = useNostr();
const { sendNIP4Message, sendNIP17Message } = useSendDM(); const { sendNIP4Message, sendNIP17Message } = useSendDM();
const userPubkey = useMemo(() => user?.pubkey, [user?.pubkey]); const userPubkey = useMemo(() => user?.pubkey, [user?.pubkey]);
// Determine if NIP-17 is enabled based on protocol mode
const enableNIP17 = protocolMode !== PROTOCOL_MODE.NIP04_ONLY;
const [messages, setMessages] = useState<MessagesState>(new Map()); const [messages, setMessages] = useState<MessagesState>(new Map());
const [lastSync, setLastSync] = useState<LastSyncData>({ const [lastSync, setLastSync] = useState<LastSyncData>({
nip4: null, nip4: null,
@@ -1117,7 +1120,7 @@ export function DMProvider({ children, enableNIP17 = true }: DMProviderProps) {
lastSync, lastSync,
conversations, conversations,
sendMessage, sendMessage,
isNIP17Enabled: enableNIP17, protocolMode,
scanProgress, scanProgress,
subscriptions, subscriptions,
clearCacheAndReload, clearCacheAndReload,
+12
View File
@@ -12,6 +12,18 @@ export const MESSAGE_PROTOCOL = {
export type MessageProtocol = typeof MESSAGE_PROTOCOL[keyof typeof MESSAGE_PROTOCOL]; export type MessageProtocol = typeof MESSAGE_PROTOCOL[keyof typeof MESSAGE_PROTOCOL];
// ============================================================================
// Protocol Mode (for user selection)
// ============================================================================
export const PROTOCOL_MODE = {
NIP04_ONLY: 'nip04_only',
NIP17_ONLY: 'nip17_only',
NIP04_OR_NIP17: 'nip04_or_nip17',
} as const;
export type ProtocolMode = typeof PROTOCOL_MODE[keyof typeof PROTOCOL_MODE];
// ============================================================================ // ============================================================================
// Loading Phases // Loading Phases
// ============================================================================ // ============================================================================