mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-03 08:07:21 +00:00
let messaging be optional
This commit is contained in:
1 parent
04c5a84ca9
commit
1ffe78c80c
2 files changed
+22
-12
No files matched your search
+5
-2
@@ -45,7 +45,10 @@ const presetRelays = [
|
||||
];
|
||||
|
||||
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.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)
|
||||
@@ -60,7 +63,7 @@ export function App() {
|
||||
<NostrLoginProvider storageKey='nostr:login'>
|
||||
<NostrProvider>
|
||||
<NWCProvider>
|
||||
<DMProvider protocolMode={dmSettings.protocolMode}>
|
||||
<DMProvider enabled={dmSettings.enabled} protocolMode={dmSettings.protocolMode}>
|
||||
<TooltipProvider>
|
||||
<Toaster />
|
||||
<Suspense>
|
||||
|
||||
+17
-10
@@ -163,9 +163,10 @@ export function useConversationMessages(conversationId: string) {
|
||||
interface DMProviderProps {
|
||||
children: ReactNode;
|
||||
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 { nostr } = useNostr();
|
||||
const { sendNIP4Message, sendNIP17Message } = useSendDM();
|
||||
@@ -909,12 +910,14 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY }
|
||||
|
||||
// Main effect to load messages
|
||||
useEffect(() => {
|
||||
if (!userPubkey || hasInitialLoadCompleted || isLoading) return;
|
||||
if (!enabled || !userPubkey || hasInitialLoadCompleted || isLoading) return;
|
||||
startMessageLoading();
|
||||
}, [userPubkey, hasInitialLoadCompleted, isLoading]);
|
||||
}, [enabled, userPubkey, hasInitialLoadCompleted, isLoading, startMessageLoading]);
|
||||
|
||||
// Cleanup effect
|
||||
useEffect(() => {
|
||||
if (!enabled) return;
|
||||
|
||||
return () => {
|
||||
if (nip4SubscriptionRef.current) {
|
||||
nip4SubscriptionRef.current.close();
|
||||
@@ -925,10 +928,12 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY }
|
||||
nip17SubscriptionRef.current = null;
|
||||
}
|
||||
};
|
||||
}, [userPubkey]);
|
||||
}, [enabled, userPubkey]);
|
||||
|
||||
// Cleanup subscriptions
|
||||
useEffect(() => {
|
||||
if (!enabled) return;
|
||||
|
||||
return () => {
|
||||
if (nip4SubscriptionRef.current) {
|
||||
nip4SubscriptionRef.current.close();
|
||||
@@ -941,7 +946,7 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY }
|
||||
}
|
||||
setSubscriptions({ isNIP4Connected: false, isNIP17Connected: false });
|
||||
};
|
||||
}, []);
|
||||
}, [enabled]);
|
||||
|
||||
// Conversations summary
|
||||
const conversations = useMemo(() => {
|
||||
@@ -1035,7 +1040,7 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY }
|
||||
|
||||
// Watch messages and save
|
||||
useEffect(() => {
|
||||
if (messages.size === 0) return;
|
||||
if (!enabled || messages.size === 0) return;
|
||||
|
||||
if (shouldSaveImmediately) {
|
||||
setShouldSaveImmediately(false);
|
||||
@@ -1043,10 +1048,12 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY }
|
||||
} else {
|
||||
triggerDebouncedWrite();
|
||||
}
|
||||
}, [messages, shouldSaveImmediately, writeAllMessagesToStore, triggerDebouncedWrite]);
|
||||
}, [enabled, messages, shouldSaveImmediately, writeAllMessagesToStore, triggerDebouncedWrite]);
|
||||
|
||||
// Send message
|
||||
const sendMessage = useCallback(async (params: { recipientPubkey: string; content: string; protocol?: MessageProtocol }) => {
|
||||
if (!enabled) return;
|
||||
|
||||
const { recipientPubkey, content, protocol = MESSAGE_PROTOCOL.NIP04 } = params;
|
||||
if (!userPubkey) return;
|
||||
|
||||
@@ -1075,10 +1082,10 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY }
|
||||
} catch (error) {
|
||||
console.error(`[DM] Failed to send ${protocol} message:`, error);
|
||||
}
|
||||
}, [userPubkey, addMessageToState, sendNIP4Message, sendNIP17Message]);
|
||||
}, [enabled, userPubkey, addMessageToState, sendNIP4Message, sendNIP17Message]);
|
||||
|
||||
const clearCacheAndReload = useCallback(async () => {
|
||||
if (!userPubkey) return;
|
||||
if (!enabled || !userPubkey) return;
|
||||
|
||||
try {
|
||||
// Close existing subscriptions
|
||||
@@ -1108,7 +1115,7 @@ export function DMProvider({ children, protocolMode = PROTOCOL_MODE.NIP17_ONLY }
|
||||
console.error('[DM] Error clearing cache:', error);
|
||||
throw error;
|
||||
}
|
||||
}, [userPubkey]);
|
||||
}, [enabled, userPubkey]);
|
||||
|
||||
const isDoingInitialLoad = isLoading && (loadingPhase === LOADING_PHASES.CACHE || loadingPhase === LOADING_PHASES.RELAYS);
|
||||
|
||||
|
||||
Reference in new issue
Block a user