diff --git a/desktop/src/features/channels/ui/ChannelPane.tsx b/desktop/src/features/channels/ui/ChannelPane.tsx index bfe861ffe3..3e2c234a1e 100644 --- a/desktop/src/features/channels/ui/ChannelPane.tsx +++ b/desktop/src/features/channels/ui/ChannelPane.tsx @@ -10,6 +10,7 @@ import type { Channel } from "@/shared/api/types"; type ChannelPaneProps = { activeChannel: Channel | null; + channelTypingPubkeys: string[]; currentPubkey?: string; editTarget?: { author: string; @@ -30,7 +31,12 @@ type ChannelPaneProps = { onEditSave?: (content: string) => Promise; onOpenThread?: (message: TimelineMessage) => void; onReply: (message: TimelineMessage) => void; - onSend: ( + onSendMain: ( + content: string, + mentionPubkeys: string[], + mediaTags?: string[][], + ) => Promise; + onSendThread: ( content: string, mentionPubkeys: string[], mediaTags?: string[][], @@ -47,12 +53,13 @@ type ChannelPaneProps = { replyTargetId: string | null; replyTargetMessage: TimelineMessage | null; targetMessageId: string | null; + threadTypingPubkeys: string[]; threadRootId: string | null; - typingPubkeys: string[]; }; export const ChannelPane = React.memo(function ChannelPane({ activeChannel, + channelTypingPubkeys, currentPubkey, editTarget = null, fetchOlder, @@ -69,7 +76,8 @@ export const ChannelPane = React.memo(function ChannelPane({ onEditSave, onOpenThread, onReply, - onSend, + onSendMain, + onSendThread, onTargetReached, onToggleReaction, personaLookup, @@ -77,8 +85,8 @@ export const ChannelPane = React.memo(function ChannelPane({ replyTargetId, replyTargetMessage, targetMessageId, + threadTypingPubkeys, threadRootId, - typingPubkeys, }: ChannelPaneProps) { const composerDisabled = !activeChannel || @@ -135,31 +143,31 @@ export const ChannelPane = React.memo(function ChannelPane({ channel={activeChannel} currentPubkey={currentPubkey} profiles={profiles} - typingPubkeys={typingPubkeys} + typingPubkeys={channelTypingPubkeys} + /> + - {!threadRootId ? ( - - ) : null} {threadRootId && activeChannel && onCloseThread ? ( @@ -167,13 +175,11 @@ export const ChannelPane = React.memo(function ChannelPane({ channel={activeChannel} currentPubkey={currentPubkey} disabledComposer={composerDisabled} - editTarget={editTarget} isSending={isSending} - onCancelEdit={onCancelEdit} onClose={onCloseThread} - onEditSave={onEditSave} - onSend={onSend} + onSend={onSendThread} profiles={profiles} + threadTypingPubkeys={threadTypingPubkeys} rootEventId={threadRootId} /> ) : null} diff --git a/desktop/src/features/channels/ui/ChannelScreen.tsx b/desktop/src/features/channels/ui/ChannelScreen.tsx index bf56f9a933..6cd0ed70cc 100644 --- a/desktop/src/features/channels/ui/ChannelScreen.tsx +++ b/desktop/src/features/channels/ui/ChannelScreen.tsx @@ -138,14 +138,51 @@ export function ChannelScreen({ () => resolvedMessages[resolvedMessages.length - 1] ?? null, [resolvedMessages], ); - const typingPubkeys = useChannelTyping( - activeChannel, - currentPubkey, - latestMessageEvent, - ); + const typingLists = ( + useChannelTyping as unknown as ( + channel: Channel | null, + currentPubkey?: string, + latestMessageEvent?: RelayEvent | null, + activeThreadRootId?: string | null, + ) => unknown + )(activeChannel, currentPubkey, latestMessageEvent, threadRootId); + // Defensive normalization: if Vite hot reload leaves an older flat-array + // shape in memory, fall back to treating it as root-scope typing rather than + // crashing ChannelScreen while opening a channel. + const channelTypingPubkeys = React.useMemo(() => { + if ( + typingLists && + typeof typingLists === "object" && + "channelTypingPubkeys" in typingLists + ) { + const value = (typingLists as { channelTypingPubkeys?: unknown }) + .channelTypingPubkeys; + return Array.isArray(value) ? value : []; + } + return Array.isArray(typingLists) ? typingLists : []; + }, [typingLists]); + const threadTypingPubkeys = React.useMemo(() => { + if ( + typingLists && + typeof typingLists === "object" && + "threadTypingPubkeys" in typingLists + ) { + const value = (typingLists as { threadTypingPubkeys?: unknown }) + .threadTypingPubkeys; + return Array.isArray(value) ? value : []; + } + return []; + }, [typingLists]); const messageProfilePubkeys = React.useMemo( - () => [...new Set([...messageAuthorPubkeys, ...typingPubkeys])], - [messageAuthorPubkeys, typingPubkeys], + () => + [ + ...new Set([ + ...messageAuthorPubkeys, + ...channelTypingPubkeys, + ...threadTypingPubkeys, + ]), + ], + [channelTypingPubkeys, messageAuthorPubkeys, threadTypingPubkeys], ); const messageProfilesQuery = useUsersBatchQuery(messageProfilePubkeys, { enabled: messageProfilePubkeys.length > 0, @@ -279,36 +316,49 @@ export function ChannelScreen({ setReplyTargetId(null); }, []); - const handleSend = React.useCallback( + const handleSendMain = React.useCallback( async ( content: string, mentionPubkeys: string[], mediaTags?: string[][], ) => { - const parentEventId = threadRootId ?? replyTargetId; + await sendMessageMutation.mutateAsync({ + content, + mentionPubkeys, + parentEventId: replyTargetId, + mediaTags, + }); + setReplyTargetId(null); + }, + [replyTargetId, sendMessageMutation], + ); + const handleSendThread = React.useCallback( + async ( + content: string, + mentionPubkeys: string[], + mediaTags?: string[][], + ) => { + if (!threadRootId) { + await handleSendMain(content, mentionPubkeys, mediaTags); + return; + } await sendMessageMutation.mutateAsync({ content, mentionPubkeys, - parentEventId, + parentEventId: threadRootId, mediaTags, }); - - if (threadRootId) { - setReplyTargetId(null); - if (activeChannelId) { - void queryClient.invalidateQueries({ - queryKey: channelThreadKey(activeChannelId, threadRootId), - }); - } - } else { - setReplyTargetId(null); + if (activeChannelId) { + void queryClient.invalidateQueries({ + queryKey: channelThreadKey(activeChannelId, threadRootId), + }); } }, [ activeChannelId, + handleSendMain, queryClient, - replyTargetId, sendMessageMutation, threadRootId, ], @@ -518,15 +568,17 @@ export function ChannelScreen({ onEditSave={handleEditSave} onOpenThread={handleOpenThread} onReply={handleReplyOpenThread} - onSend={handleSend} + onSendMain={handleSendMain} + onSendThread={handleSendThread} onToggleReaction={effectiveToggleReaction} personaLookup={personaLookup} profiles={messageProfiles} replyTargetId={replyTargetId} replyTargetMessage={replyTargetMessage} + channelTypingPubkeys={channelTypingPubkeys} + threadTypingPubkeys={threadTypingPubkeys} threadRootId={threadRootId} targetMessageId={targetMessageId} - typingPubkeys={typingPubkeys} /> ) diff --git a/desktop/src/features/messages/lib/useDrafts.ts b/desktop/src/features/messages/lib/useDrafts.ts index 66bfedfe1e..76a9f9bc64 100644 --- a/desktop/src/features/messages/lib/useDrafts.ts +++ b/desktop/src/features/messages/lib/useDrafts.ts @@ -6,28 +6,28 @@ export type DraftState = { selectionEnd: number; }; -export function useDrafts() { - const draftsRef = React.useRef(new Map()); +const sharedDrafts = new Map(); +export function useDrafts() { const saveDraft = React.useCallback( - (channelId: string, draft: DraftState) => { + (draftKey: string, draft: DraftState) => { if (draft.content.trim().length === 0) { return; } - draftsRef.current.set(channelId, draft); + sharedDrafts.set(draftKey, draft); }, [], ); const loadDraft = React.useCallback( - (channelId: string): DraftState | undefined => { - return draftsRef.current.get(channelId); + (draftKey: string): DraftState | undefined => { + return sharedDrafts.get(draftKey); }, [], ); - const clearDraft = React.useCallback((channelId: string) => { - draftsRef.current.delete(channelId); + const clearDraft = React.useCallback((draftKey: string) => { + sharedDrafts.delete(draftKey); }, []); return { saveDraft, loadDraft, clearDraft }; diff --git a/desktop/src/features/messages/ui/ChannelThreadPanel.tsx b/desktop/src/features/messages/ui/ChannelThreadPanel.tsx index 0ea1ca47de..51d59cf317 100644 --- a/desktop/src/features/messages/ui/ChannelThreadPanel.tsx +++ b/desktop/src/features/messages/ui/ChannelThreadPanel.tsx @@ -3,6 +3,7 @@ import * as React from "react"; import { useQuery } from "@tanstack/react-query"; import { MessageComposer } from "@/features/messages/ui/MessageComposer"; +import { TypingIndicatorRow } from "@/features/messages/ui/TypingIndicatorRow"; import { channelThreadKey } from "@/features/messages/lib/messageQueryKeys"; import { getForumThread } from "@/shared/api/forum"; import type { Channel } from "@/shared/api/types"; @@ -31,14 +32,8 @@ type ChannelThreadPanelProps = { mediaTags?: string[][], ) => Promise; isSending: boolean; - editTarget?: { - author: string; - body: string; - id: string; - } | null; - onCancelEdit?: () => void; - onEditSave?: (content: string) => Promise; disabledComposer: boolean; + threadTypingPubkeys: string[]; }; function ThreadReplyRow({ @@ -105,10 +100,8 @@ export function ChannelThreadPanel({ onClose, onSend, isSending, - editTarget = null, - onCancelEdit, - onEditSave, disabledComposer, + threadTypingPubkeys, }: ChannelThreadPanelProps) { const { channels } = useChannelNavigation(); const channelNames = React.useMemo( @@ -218,17 +211,22 @@ export function ChannelThreadPanel({ )} +
diff --git a/desktop/src/features/messages/ui/MessageComposer.tsx b/desktop/src/features/messages/ui/MessageComposer.tsx index 6aff994a42..7bf7d54733 100644 --- a/desktop/src/features/messages/ui/MessageComposer.tsx +++ b/desktop/src/features/messages/ui/MessageComposer.tsx @@ -19,6 +19,7 @@ import { MessageComposerToolbar } from "./MessageComposerToolbar"; type MessageComposerProps = { channelId?: string | null; channelName: string; + draftKey?: string | null; disabled?: boolean; editTarget?: { author: string; @@ -40,6 +41,8 @@ type MessageComposerProps = { body: string; id: string; } | null; + showReplyTargetBanner?: boolean; + typingThreadRootId?: string | null; }; const MAX_TEXTAREA_ROWS = 4; @@ -47,6 +50,7 @@ const MAX_TEXTAREA_ROWS = 4; export function MessageComposer({ channelId = null, channelName, + draftKey = null, disabled = false, editTarget = null, isSending = false, @@ -56,6 +60,8 @@ export function MessageComposer({ onSend, placeholder, replyTarget = null, + showReplyTargetBanner = true, + typingThreadRootId = null, }: MessageComposerProps) { const [content, setContent] = React.useState(""); const contentRef = React.useRef(content); @@ -70,11 +76,16 @@ export function MessageComposer({ contentRef.current = content; const drafts = useDrafts(); - const previousChannelIdRef = React.useRef(null); + const previousDraftKeyRef = React.useRef(null); const mentions = useMentions(channelId); const channelLinks = useChannelLinks(); - const notifyTyping = useTypingBroadcast(channelId); + const notifyTyping = ( + useTypingBroadcast as unknown as ( + channelId: string | null | undefined, + threadRootId?: string | null, + ) => () => void + )(channelId, typingThreadRootId); const media = useMediaUpload(setContent); @@ -86,34 +97,36 @@ export function MessageComposer({ const onEditSaveRef = React.useRef(onEditSave); const editTargetRef = React.useRef(editTarget); const channelIdRef = React.useRef(channelId); + const draftKeyRef = React.useRef(draftKey); disabledRef.current = disabled; isSendingRef.current = isSending; onSendRef.current = onSend; onEditSaveRef.current = onEditSave; editTargetRef.current = editTarget; channelIdRef.current = channelId; + draftKeyRef.current = draftKey; - // biome-ignore lint/correctness/useExhaustiveDependencies: channelId is the sole trigger — save draft for previous channel, restore draft for new channel, reset transient state + // biome-ignore lint/correctness/useExhaustiveDependencies: draftKey is the sole trigger — save current draft, restore scoped draft, reset transient state React.useEffect(() => { - // Save draft for the channel we're leaving - const prevId = previousChannelIdRef.current; - if (prevId) { + // Save draft for the composer scope we're leaving. + const prevKey = previousDraftKeyRef.current; + if (prevKey) { const currentContent = contentRef.current; const sel = draftSelectionRef.current; if (currentContent.trim().length > 0) { - drafts.saveDraft(prevId, { + drafts.saveDraft(prevKey, { content: currentContent, selectionEnd: sel.end, selectionStart: sel.start, }); } else { - drafts.clearDraft(prevId); + drafts.clearDraft(prevKey); } } - previousChannelIdRef.current = channelId; + previousDraftKeyRef.current = draftKey; - // Restore draft for the channel we're entering - const saved = channelId ? drafts.loadDraft(channelId) : undefined; + // Restore draft for the composer scope we're entering. + const saved = draftKey ? drafts.loadDraft(draftKey) : undefined; if (saved) { setContent(saved.content); contentRef.current = saved.content; @@ -136,7 +149,7 @@ export function MessageComposer({ mentions.clearMentions(); channelLinks.clearChannels(); lineHeightRef.current = null; - }, [channelId]); + }, [draftKey]); const applyMentionInsert = React.useCallback( (suggestion: MentionSuggestion) => { @@ -316,11 +329,11 @@ export function MessageComposer({ channelLinks.clearChannels(); setIsEmojiPickerOpen(false); - const sendChannelId = channelIdRef.current; + const sendDraftKey = draftKeyRef.current; try { await onSendRef.current(trimmed, pubkeys, mediaTags); - if (sendChannelId) { - drafts.clearDraft(sendChannelId); + if (sendDraftKey) { + drafts.clearDraft(sendDraftKey); } } catch { setContent(savedContent); @@ -542,7 +555,7 @@ export function MessageComposer({ Cancel - ) : replyTarget ? ( + ) : replyTarget && showReplyTargetBanner ? (
import("./DiffMessage")); const DiffMessageExpanded = React.lazy(() => import("./DiffMessageExpanded")); @@ -178,13 +173,6 @@ export const MessageRow = React.memo( data-testid="message-row" >
- {isBotInstance(message.role) ? ( - - ) : null} {message.pubkey ? (