refine dual composer scoping and agent message layout

Keep root and thread composers independent so opening a thread doesn't hijack send, typing, or draft state. Remove the extra bot identicon in message rows so agent replies align with normal users in the main timeline.

Made-with: Cursor
This commit is contained in:
Thomas Petersen
2026-04-10 09:49:43 +02:00
parent daa639e810
commit e7e253179e
6 changed files with 161 additions and 104 deletions

View File

@@ -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<void>;
onOpenThread?: (message: TimelineMessage) => void;
onReply: (message: TimelineMessage) => void;
onSend: (
onSendMain: (
content: string,
mentionPubkeys: string[],
mediaTags?: string[][],
) => Promise<void>;
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}
/>
<MessageComposer
channelId={activeChannel?.id ?? null}
channelName={activeChannel?.name ?? "channel"}
disabled={composerDisabled}
draftKey={activeChannel ? `channel:${activeChannel.id}:root` : null}
editTarget={editTarget}
isSending={isSending}
onCancelEdit={onCancelEdit}
onCancelReply={onCancelReply}
onEditSave={onEditSave}
onSend={onSendMain}
placeholder={mainPlaceholder}
replyTarget={
replyTargetMessage
? {
author: replyTargetMessage.author,
body: replyTargetMessage.body,
id: replyTargetMessage.id,
}
: null
}
typingThreadRootId={null}
/>
{!threadRootId ? (
<MessageComposer
channelId={activeChannel?.id ?? null}
channelName={activeChannel?.name ?? "channel"}
disabled={composerDisabled}
editTarget={editTarget}
isSending={isSending}
onCancelEdit={onCancelEdit}
onCancelReply={onCancelReply}
onEditSave={onEditSave}
onSend={onSend}
placeholder={mainPlaceholder}
replyTarget={
replyTargetMessage
? {
author: replyTargetMessage.author,
body: replyTargetMessage.body,
id: replyTargetMessage.id,
}
: null
}
/>
) : null}
</div>
{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}

View File

@@ -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}
/>
</React.Suspense>
)

View File

@@ -6,28 +6,28 @@ export type DraftState = {
selectionEnd: number;
};
export function useDrafts() {
const draftsRef = React.useRef(new Map<string, DraftState>());
const sharedDrafts = new Map<string, DraftState>();
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 };

View File

@@ -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<void>;
isSending: boolean;
editTarget?: {
author: string;
body: string;
id: string;
} | null;
onCancelEdit?: () => void;
onEditSave?: (content: string) => Promise<void>;
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({
)}
</div>
<TypingIndicatorRow
channel={channel}
currentPubkey={currentPubkey}
profiles={profiles}
typingPubkeys={threadTypingPubkeys}
/>
<div className="shrink-0 border-t border-border/60 bg-background/80 p-2 backdrop-blur-sm">
<MessageComposer
channelId={channel.id}
channelName={channel.name}
disabled={disabledComposer}
editTarget={editTarget}
draftKey={`channel:${channel.id}:thread:${rootEventId}`}
isSending={isSending}
onCancelEdit={onCancelEdit}
onEditSave={onEditSave}
onSend={onSend}
placeholder={`Reply in thread…`}
typingThreadRootId={rootEventId}
/>
</div>
</aside>

View File

@@ -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<string | null>(null);
const previousDraftKeyRef = React.useRef<string | null>(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
</Button>
</div>
) : replyTarget ? (
) : replyTarget && showReplyTargetBanner ? (
<div
className="mb-3 flex items-start justify-between gap-3 rounded-2xl border border-border/70 bg-muted/40 px-3 py-2"
data-testid="reply-target"

View File

@@ -10,14 +10,9 @@ import { rewriteRelayUrl } from "@/shared/lib/mediaUrl";
import { useChannelNavigation } from "@/shared/context/ChannelNavigationContext";
import { resolveMentionNames } from "@/shared/lib/resolveMentionNames";
import { Markdown } from "@/shared/ui/markdown";
import { BotIdenticon } from "./BotIdenticon";
import { MessageActionBar } from "./MessageActionBar";
import { MessageTimestamp } from "./MessageTimestamp";
/** Returns true if this message is from a bot instance. */
function isBotInstance(role?: string): boolean {
return role === "bot";
}
const DiffMessage = React.lazy(() => import("./DiffMessage"));
const DiffMessageExpanded = React.lazy(() => import("./DiffMessageExpanded"));
@@ -178,13 +173,6 @@ export const MessageRow = React.memo(
data-testid="message-row"
>
<div className="flex shrink-0 items-center gap-1">
{isBotInstance(message.role) ? (
<BotIdenticon
value={message.author}
size={20}
className="rounded"
/>
) : null}
{message.pubkey ? (
<UserProfilePopover pubkey={message.pubkey}>
<button