let messaging be optional

This commit is contained in:
sam committed 2025-10-19 14:28:23 +07:00
1 parent 04c5a84ca9
commit 1ffe78c80c
2 files changed
+22 -12

No files matched your search

+5 -2
View File
@@ -45,7 +45,10 @@ const presetRelays = [
]; ];
const dmSettings = { const dmSettings = {
// Choose one: // Enable or disable DMs entirely
enabled: true, // Set to false to completely disable messaging functionality
// Choose one protocol mode:
// PROTOCOL_MODE.NIP04_ONLY - Force NIP-04 (legacy) only // PROTOCOL_MODE.NIP04_ONLY - Force NIP-04 (legacy) only
// PROTOCOL_MODE.NIP17_ONLY - Force NIP-17 (private) 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) // PROTOCOL_MODE.NIP04_OR_NIP17 - Allow users to choose between NIP-04 and NIP-17 (defaults to NIP-17)
@@ -60,7 +63,7 @@ export function App() {
<NostrLoginProvider storageKey='nostr:login'> <NostrLoginProvider storageKey='nostr:login'>
<NostrProvider> <NostrProvider>
<NWCProvider> <NWCProvider>
<DMProvider protocolMode={dmSettings.protocolMode}> <DMProvider enabled={dmSettings.enabled} protocolMode={dmSettings.protocolMode}>
<TooltipProvider> <TooltipProvider>
<Toaster /> <Toaster />
<Suspense> <Suspense>
+17 -10
View File
@@ -163,9 +163,10 @@ export function useConversationMessages(conversationId: string) {
interface DMProviderProps { interface DMProviderProps {
children: ReactNode; children: ReactNode;
protocolMode?: ProtocolMode; protocolMode?: ProtocolMode;
enabled?: boolean;
} }
export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY }: DMProviderProps) { export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY, enabled = true }: DMProviderProps) {
const { user } = useCurrentUser(); const { user } = useCurrentUser();
const { nostr } = useNostr(); const { nostr } = useNostr();
const { sendNIP4Message, sendNIP17Message } = useSendDM(); const { sendNIP4Message, sendNIP17Message } = useSendDM();
@@ -909,12 +910,14 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY }
// Main effect to load messages // Main effect to load messages
useEffect(() => { useEffect(() => {
if (!userPubkey || hasInitialLoadCompleted || isLoading) return; if (!enabled || !userPubkey || hasInitialLoadCompleted || isLoading) return;
startMessageLoading(); startMessageLoading();
}, [userPubkey, hasInitialLoadCompleted, isLoading]); }, [enabled, userPubkey, hasInitialLoadCompleted, isLoading, startMessageLoading]);
// Cleanup effect // Cleanup effect
useEffect(() => { useEffect(() => {
if (!enabled) return;
return () => { return () => {
if (nip4SubscriptionRef.current) { if (nip4SubscriptionRef.current) {
nip4SubscriptionRef.current.close(); nip4SubscriptionRef.current.close();
@@ -925,10 +928,12 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY }
nip17SubscriptionRef.current = null; nip17SubscriptionRef.current = null;
} }
}; };
}, [userPubkey]); }, [enabled, userPubkey]);
// Cleanup subscriptions // Cleanup subscriptions
useEffect(() => { useEffect(() => {
if (!enabled) return;
return () => { return () => {
if (nip4SubscriptionRef.current) { if (nip4SubscriptionRef.current) {
nip4SubscriptionRef.current.close(); nip4SubscriptionRef.current.close();
@@ -941,7 +946,7 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY }
} }
setSubscriptions({ isNIP4Connected: false, isNIP17Connected: false }); setSubscriptions({ isNIP4Connected: false, isNIP17Connected: false });
}; };
}, []); }, [enabled]);
// Conversations summary // Conversations summary
const conversations = useMemo(() => { const conversations = useMemo(() => {
@@ -1035,7 +1040,7 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY }
// Watch messages and save // Watch messages and save
useEffect(() => { useEffect(() => {
if (messages.size === 0) return; if (!enabled || messages.size === 0) return;
if (shouldSaveImmediately) { if (shouldSaveImmediately) {
setShouldSaveImmediately(false); setShouldSaveImmediately(false);
@@ -1043,10 +1048,12 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY }
} else { } else {
triggerDebouncedWrite(); triggerDebouncedWrite();
} }
}, [messages, shouldSaveImmediately, writeAllMessagesToStore, triggerDebouncedWrite]); }, [enabled, messages, shouldSaveImmediately, writeAllMessagesToStore, triggerDebouncedWrite]);
// Send message // Send message
const sendMessage = useCallback(async (params: { recipientPubkey: string; content: string; protocol?: MessageProtocol }) => { const sendMessage = useCallback(async (params: { recipientPubkey: string; content: string; protocol?: MessageProtocol }) => {
if (!enabled) return;
const { recipientPubkey, content, protocol = MESSAGE_PROTOCOL.NIP04 } = params; const { recipientPubkey, content, protocol = MESSAGE_PROTOCOL.NIP04 } = params;
if (!userPubkey) return; if (!userPubkey) return;
@@ -1075,10 +1082,10 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY }
} catch (error) { } catch (error) {
console.error(`[DM] Failed to send ${protocol} message:`, error); console.error(`[DM] Failed to send ${protocol} message:`, error);
} }
}, [userPubkey, addMessageToState, sendNIP4Message, sendNIP17Message]); }, [enabled, userPubkey, addMessageToState, sendNIP4Message, sendNIP17Message]);
const clearCacheAndReload = useCallback(async () => { const clearCacheAndReload = useCallback(async () => {
if (!userPubkey) return; if (!enabled || !userPubkey) return;
try { try {
// Close existing subscriptions // Close existing subscriptions
@@ -1108,7 +1115,7 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY }
console.error('[DM] Error clearing cache:', error); console.error('[DM] Error clearing cache:', error);
throw error; throw error;
} }
}, [userPubkey]); }, [enabled, userPubkey]);
const isDoingInitialLoad = isLoading && (loadingPhase === LOADING_PHASES.CACHE || loadingPhase === LOADING_PHASES.RELAYS); const isDoingInitialLoad = isLoading && (loadingPhase === LOADING_PHASES.CACHE || loadingPhase === LOADING_PHASES.RELAYS);