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
+77 -35

No files matched your search

+7 -2
View File
@@ -13,6 +13,7 @@ import { AppProvider } from '@/components/AppProvider';
import { NWCProvider } from '@/contexts/NWCContext';
import { DMProvider } from '@/contexts/DMContext';
import { AppConfig } from '@/contexts/AppContext';
import { PROTOCOL_MODE } from '@/lib/dmConstants';
import AppRouter from './AppRouter';
const head = createHead({
@@ -44,7 +45,11 @@ const presetRelays = [
];
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() {
@@ -55,7 +60,7 @@ export function App() {
<NostrLoginProvider storageKey='nostr:login'>
<NostrProvider>
<NWCProvider>
<DMProvider enableNIP17={dmSettings.enableNIP17}>
<DMProvider protocolMode={dmSettings.protocolMode}>
<TooltipProvider>
<Toaster />
<Suspense>
+50 -28
View File
@@ -3,13 +3,13 @@ import { useConversationMessages, useDMContext } from '@/contexts/DMContext';
import { useCurrentUser } from '@/hooks/useCurrentUser';
import { useAuthor } from '@/hooks/useAuthor';
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 { Button } from '@/components/ui/button';
import { Card } from '@/components/ui/card';
import { Textarea } from '@/components/ui/textarea';
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 { ArrowLeft, Send, Loader2, AlertTriangle } from 'lucide-react';
import { cn } from '@/lib/utils';
@@ -137,12 +137,26 @@ const EmptyState = () => {
export const ChatArea = ({ pubkey, onBack, className }: ChatAreaProps) => {
const { user } = useCurrentUser();
const { sendMessage, isNIP17Enabled } = useDMContext();
const { messages, totalCount } = useConversationMessages(pubkey || '');
const { sendMessage, protocolMode } = useDMContext();
const { messages } = useConversationMessages(pubkey || '');
const [messageText, setMessageText] = useState('');
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);
// Determine if selection is allowed
const allowSelection = protocolMode === PROTOCOL_MODE.NIP04_OR_NIP17;
// Auto-scroll to bottom when new messages arrive
useEffect(() => {
@@ -162,7 +176,7 @@ export const ChatArea = ({ pubkey, onBack, className }: ChatAreaProps) => {
await sendMessage({
recipientPubkey: pubkey,
content: messageText.trim(),
protocol: isNIP17Enabled ? MESSAGE_PROTOCOL.NIP17 : MESSAGE_PROTOCOL.NIP04,
protocol: selectedProtocol,
});
setMessageText('');
} catch (error) {
@@ -223,17 +237,6 @@ export const ChatArea = ({ pubkey, onBack, className }: ChatAreaProps) => {
</ScrollArea>
<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">
<Textarea
value={messageText}
@@ -243,18 +246,37 @@ export const ChatArea = ({ pubkey, onBack, className }: ChatAreaProps) => {
className="min-h-[80px] resize-none"
disabled={isSending}
/>
<Button
onClick={handleSend}
disabled={!messageText.trim() || isSending}
size="icon"
className="h-[80px] w-[80px]"
>
{isSending ? (
<Loader2 className="h-5 w-5 animate-spin" />
) : (
<Send className="h-5 w-5" />
)}
</Button>
<div className="flex flex-col gap-2">
<Button
onClick={handleSend}
disabled={!messageText.trim() || isSending}
size="icon"
className="h-[44px] w-[90px]"
>
{isSending ? (
<Loader2 className="h-5 w-5 animate-spin" />
) : (
<Send className="h-5 w-5" />
)}
</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>
</Card>
+8 -5
View File
@@ -3,7 +3,7 @@ import { useCurrentUser } from '@/hooks/useCurrentUser';
import { useSendDM } from '@/hooks/useSendDM';
import { useNostr } from '@nostrify/react';
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 { MessageProtocol } from '@/lib/dmConstants';
import { MESSAGE_PROTOCOL } from '@/lib/dmConstants';
@@ -119,7 +119,7 @@ interface DMContextType {
subscriptions: SubscriptionStatus;
conversations: ConversationSummary[];
sendMessage: (params: { recipientPubkey: string; content: string; protocol?: MessageProtocol }) => Promise<void>;
isNIP17Enabled: boolean;
protocolMode: ProtocolMode;
scanProgress: ScanProgressState;
clearCacheAndReload: () => Promise<void>;
}
@@ -162,15 +162,18 @@ export function useConversationMessages(conversationId: string) {
interface DMProviderProps {
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 { nostr } = useNostr();
const { sendNIP4Message, sendNIP17Message } = useSendDM();
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 [lastSync, setLastSync] = useState<LastSyncData>({
@@ -1117,7 +1120,7 @@ export function DMProvider({ children, enableNIP17 = true }: DMProviderProps) {
lastSync,
conversations,
sendMessage,
isNIP17Enabled: enableNIP17,
protocolMode,
scanProgress,
subscriptions,
clearCacheAndReload,
+12
View File
@@ -12,6 +12,18 @@ export const 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
// ============================================================================