Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(new-channels): hotkeys (#1964)

teoed110bdfaa9c
diff --git a/js/app/packages/app/index.css b/js/app/packages/app/index.css
index 1e0cad086d..2174d58074 100644
--- a/js/app/packages/app/index.css
+++ b/js/app/packages/app/index.css
@@ -704,6 +704,17 @@ html[data-modality="keyboard"] .suppress-css-brackets .allow-css-brackets *:focu
pointer-events: none;
}
+/* Channel message highlight: hover only in mouse nav, keyboard selection only in keyboard nav */
+[data-channel-nav="mouse"] [data-message]:hover,
+[data-channel-nav="keyboard"] [data-message][data-highlighted] {
+ @apply bg-accent/5 outline-1 outline-accent/20 outline-offset-[-1px];
+}
+
+[data-channel-nav="mouse"] [data-message]:hover > .message-accent-bar,
+[data-channel-nav="keyboard"] [data-message][data-highlighted] > .message-accent-bar {
+ @apply opacity-100;
+}
+
html[data-modality="keyboard"] input:focus-visible::after,
html[data-modality="keyboard"] textarea:focus-visible::after,
html[data-modality="keyboard"] [contenteditable="true"]:focus-visible::after {
diff --git a/js/app/packages/channel/Channel/Channel.tsx b/js/app/packages/channel/Channel/Channel.tsx
index a2d6446415..3e391405e2 100644
--- a/js/app/packages/channel/Channel/Channel.tsx
+++ b/js/app/packages/channel/Channel/Channel.tsx
@@ -36,7 +36,11 @@ import type { DateValue } from '@core/util/date';
import { buildChannelMessageListMeta } from './message-list-meta';
import { ScrollToBottomOverlay } from './ScrollToBottomOverlay';
import { ChannelThread } from '../Thread';
-import { ChannelInput, createInputAttachmentTracker } from '../Input';
+import {
+ ChannelInput,
+ createInputAttachmentTracker,
+ type InputSnapshot,
+} from '../Input';
import { createChannelMessageActions } from './create-channel-message-actions';
import { createActivityTracker } from '@channel/activity-tracker';
import { useChannelActivity } from '@core/context/channels';
@@ -53,6 +57,8 @@ import {
} from '@channel/Input/utils/persistence';
import { createStickyScrollEffect } from './sticky-scroll';
import { createMessageEditor } from './create-message-editor';
+import { createMessageSelection } from './create-message-selection';
+import { createChannelHotkeys } from './create-channel-hotkeys';
import type { ChannelInputProps } from '@channel/Input/ChannelInput';
type ChannelProps = {
@@ -73,6 +79,9 @@ export function Channel(props: ChannelProps) {
string | undefined
>(props.targetMessageId);
+ const [channelInputSnapshot, setChannelInputSnapshot] =
+ createSignal<InputSnapshot>();
+
const messagesQuery = useChannelMessagesQuery(
() => props.channelId,
targetMessageId
@@ -158,12 +167,28 @@ export function Channel(props: ChannelProps) {
},
});
+ const selection = createMessageSelection({
+ keys: () => messageIndex().keys,
+ });
+
+ const { messageListScopeId, attachMessageListRef, attachInputRef } =
+ createChannelHotkeys({
+ selection,
+ navigation: threadListNavigation,
+ messageById,
+ getMessageActions,
+ userId,
+ isInputEmpty: () =>
+ (channelInputSnapshot()?.value.trim().length ?? 0) === 0,
+ });
+
createStickyScrollEffect({
isNearBottom: () => threadListScrollState()?.isNearBottom ?? false,
hasMoreBelow: () => threadPaginator.hasMoreShifting(),
messages,
scrollToBottom: () => threadListNavigation()?.scrollToBottom(),
});
+
const onSend: ChannelInputProps['onSend'] = (snapshot) => {
const senderId = userId();
if (!senderId) return;
@@ -181,7 +206,19 @@ export function Channel(props: ChannelProps) {
<StaticMarkdownContext>
<ChannelDropZone dragState={dragState}>
<Show when={messages().length > 0}>
- <div class="relative flex-1 min-h-0">
+ <div
+ class="relative flex-1 min-h-0 suppress-css-brackets suppress-css-bracket outline-none"
+ ref={attachMessageListRef}
+ tabIndex={-1}
+ data-channel-message-list
+ data-channel-nav="keyboard"
+ onMouseMove={(e) => {
+ const el = e.currentTarget;
+ if (el.dataset.channelNav !== 'mouse') {
+ el.dataset.channelNav = 'mouse';
+ }
+ }}
+ >
<ThreadList
keys={() => messageIndex().keys}
initialScrollTarget={threadListInitialScrollTarget()}
@@ -215,6 +252,8 @@ export function Channel(props: ChannelProps) {
activityTracker.dismissNewMessages,
}}
isNewMessage={activityTracker.isNewMessage}
+ selectedMessageId={selection.selectedId}
+ messageListScopeId={messageListScopeId}
/>
)}
</Show>
@@ -228,7 +267,7 @@ export function Channel(props: ChannelProps) {
</div>
</Show>
<Suspense>
- <div class="pb-2 w-full flex justify-center">
+ <div class="pb-2 w-full flex justify-center" ref={attachInputRef}>
<ChannelInput
input={{
mode: 'channel',
@@ -244,6 +283,7 @@ export function Channel(props: ChannelProps) {
onReady={(handle) => {
dragState.setAttachFilesToChannel(handle.attachFiles);
}}
+ onChange={(snapshot) => void setChannelInputSnapshot(snapshot)}
onSend={onSend}
/>
</div>
diff --git a/js/app/packages/channel/Channel/InlineMessageEditor.tsx b/js/app/packages/channel/Channel/InlineMessageEditor.tsx
index 7025e22802..e35e73e5f2 100644
--- a/js/app/packages/channel/Channel/InlineMessageEditor.tsx
+++ b/js/app/packages/channel/Channel/InlineMessageEditor.tsx
@@ -1,4 +1,6 @@
import { Input, ChannelInput, createInputAttachmentTracker } from '../Input';
+import { registerHotkey, useHotkeyDOMScope } from '@core/hotkey/hotkeys';
+import { TOKENS } from '@core/hotkey/tokens';
import type { MessageEditor } from './create-message-editor';
import { useMessage } from '../Message';
@@ -14,34 +16,50 @@ export function MessageEditorContent(props: MessageEditorContentProps) {
initialAttachments: snapshot()?.attachments,
});
+ const [attachHotkeys, scopeId] = useHotkeyDOMScope('inline-message-editor');
+
+ registerHotkey({
+ scopeId,
+ hotkey: 'escape',
+ hotkeyToken: TOKENS.channel.clearSelection,
+ description: 'Discard edit',
+ runWithInputFocused: true,
+ keyDownHandler: () => {
+ props.messageEditor.cancel(message().id);
+ return true;
+ },
+ });
+
return (
- <ChannelInput
- input={{
- mode: 'channel',
- id: `edit-message-input-${message().id}`,
- value: snapshot()?.value,
- attachments: snapshot()?.attachments,
- placeholder: 'Edit message',
- }}
- attachmentTracker={attachmentTracker}
- markdownNamespace={`edit-message-${props.channelId}-${message().id}`}
- onChange={(nextSnapshot) =>
- props.messageEditor.update(message(), nextSnapshot)
- }
- onClose={() => props.messageEditor.cancel(message().id)}
- onSend={(nextSnapshot) =>
- props.messageEditor.save(message(), nextSnapshot)
- }
- >
- <Input.Actions>
- <Input.Actions.Left>
- <Input.ToggleFormatAction />
- <Input.DiscardDraftAction />
- </Input.Actions.Left>
- <Input.Actions.Right>
- <Input.SendAction />
- </Input.Actions.Right>
- </Input.Actions>
- </ChannelInput>
+ <div ref={attachHotkeys}>
+ <ChannelInput
+ input={{
+ mode: 'channel',
+ id: `edit-message-input-${message().id}`,
+ value: snapshot()?.value,
+ attachments: snapshot()?.attachments,
+ placeholder: 'Edit message',
+ }}
+ attachmentTracker={attachmentTracker}
+ markdownNamespace={`edit-message-${props.channelId}-${message().id}`}
+ onChange={(nextSnapshot) =>
+ props.messageEditor.update(message(), nextSnapshot)
+ }
+ onClose={() => props.messageEditor.cancel(message().id)}
+ onSend={(nextSnapshot) =>
+ props.messageEditor.save(message(), nextSnapshot)
+ }
+ >
+ <Input.Actions>
+ <Input.Actions.Left>
+ <Input.ToggleFormatAction />
+ <Input.DiscardDraftAction />
+ </Input.Actions.Left>
+ <Input.Actions.Right>
+ <Input.SendAction />
+ </Input.Actions.Right>
+ </Input.Actions>
+ </ChannelInput>
+ </div>
);
}
diff --git a/js/app/packages/channel/Channel/create-channel-hotkeys.ts b/js/app/packages/channel/Channel/create-channel-hotkeys.ts
new file mode 100644
index 0000000000..9aa36e1285
--- /dev/null
+++ b/js/app/packages/channel/Channel/create-channel-hotkeys.ts
@@ -0,0 +1,145 @@
+import { registerHotkey, useHotkeyDOMScope } from '@core/hotkey/hotkeys';
+import { TOKENS } from '@core/hotkey/tokens';
+import type { Accessor } from 'solid-js';
+import { useSubscribeToKeypress } from '@app/signal/hotkeyRoot';
+import type { ThreadListNavigation } from './ThreadList';
+import type { MessageSelection } from './create-message-selection';
+import type { ApiChannelMessage } from '@service-comms/client';
+import type { MessageActions, MessageData } from '../Message';
+
+type CreateChannelHotkeysOptions = {
+ selection: MessageSelection;
+ navigation: Accessor<ThreadListNavigation | undefined>;
+ messageById: Accessor<Map<string, ApiChannelMessage>>;
+ getMessageActions: (message: MessageData) => MessageActions | undefined;
+ userId: Accessor<string | undefined>;
+ isInputEmpty: Accessor<boolean>;
+};
+
+export function createChannelHotkeys(options: CreateChannelHotkeysOptions) {
+ const [attachMessageList, messageListScope] =
+ useHotkeyDOMScope('channel-messages');
+ const [attachInput, inputScope] = useHotkeyDOMScope('channel-input');
+
+ let messageListEl: HTMLElement | undefined;
+ let inputEl: HTMLElement | undefined;
+
+ useSubscribeToKeypress(() => {
+ if (messageListEl && messageListEl.dataset.channelNav !== 'keyboard') {
+ messageListEl.dataset.channelNav = 'keyboard';
+ }
+ });
+
+ const hasSelection = () => !!options.selection.selectedId();
+
+ const getSelectedMessage = () => {
+ const id = options.selection.selectedId();
+ if (!id) return undefined;
+ return options.messageById().get(id);
+ };
+
+ registerHotkey({
+ scopeId: messageListScope,
+ hotkey: 'arrowup',
+ hotkeyToken: TOKENS.channel.focusPreviousMessage,
+ description: 'Previous message',
+ keyDownHandler: () => {
+ const id = options.selection.selectPrevious();
+ if (id) {
+ options.navigation()?.scrollToId(id, { align: 'nearest' });
+ }
+ return true;
+ },
+ });
+
+ registerHotkey({
+ scopeId: messageListScope,
+ hotkey: 'arrowdown',
+ hotkeyToken: TOKENS.channel.focusNextMessage,
+ description: 'Next message',
+ keyDownHandler: () => {
+ const id = options.selection.selectNext();
+ if (id) {
+ options.navigation()?.scrollToId(id, { align: 'nearest' });
+ } else {
+ inputEl?.querySelector<HTMLElement>('[contenteditable]')?.focus();
+ }
+ return true;
+ },
+ });
+
+ registerHotkey({
+ scopeId: messageListScope,
+ hotkey: 'enter',
+ hotkeyToken: TOKENS.channel.replyToMessage,
+ description: 'Reply to message',
+ condition: hasSelection,
+ keyDownHandler: () => {
+ const msg = getSelectedMessage();
+ if (!msg) return false;
+ const actions = options.getMessageActions(msg);
+ actions?.onReply?.({ message: msg });
+ return true;
+ },
+ });
+
+ registerHotkey({
+ scopeId: messageListScope,
+ hotkey: 'e',
+ hotkeyToken: TOKENS.channel.editMessage,
+ description: 'Edit message',
+ condition: () => {
+ if (!hasSelection()) return false;
+ const msg = getSelectedMessage();
+ return !!msg && msg.sender_id === options.userId();
+ },
+ keyDownHandler: () => {
+ const msg = getSelectedMessage();
+ if (!msg) return false;
+ const actions = options.getMessageActions(msg);
+ actions?.onEdit?.({ message: msg });
+ return true;
+ },
+ });
+
+ registerHotkey({
+ scopeId: messageListScope,
+ hotkey: 'escape',
+ hotkeyToken: TOKENS.channel.clearSelection,
+ description: 'Clear selection',
+ condition: hasSelection,
+ keyDownHandler: () => {
+ options.selection.clear();
+ return true;
+ },
+ });
+
+ registerHotkey({
+ scopeId: inputScope,
+ hotkey: 'arrowup',
+ hotkeyToken: TOKENS.channel.focusPreviousMessage,
+ description: 'Select last message',
+ runWithInputFocused: true,
+ condition: options.isInputEmpty,
+ keyDownHandler: () => {
+ const id = options.selection.selectPrevious();
+ if (id) {
+ options.navigation()?.scrollToId(id, { align: 'nearest' });
+ messageListEl?.focus();
+ }
+ return true;
+ },
+ });
+
+ return {
+ messageListScopeId: messageListScope,
+ attachMessageListRef: (el: HTMLElement) => {
+ messageListEl = el;
+ attachMessageList(el);
+ },
+ attachInputRef: (el: HTMLElement) => {
+ inputEl = el;
+ attachInput(el);
+ },
+ };
+}
diff --git a/js/app/packages/channel/Channel/create-message-selection.ts b/js/app/packages/channel/Channel/create-message-selection.ts
new file mode 100644
index 0000000000..40e8ace7a6
--- /dev/null
+++ b/js/app/packages/channel/Channel/create-message-selection.ts
@@ -0,0 +1,78 @@
+import { createEffect, createSignal, on, type Accessor } from 'solid-js';
+
+type CreateMessageSelectionOptions = {
+ keys: Accessor<string[]>;
+};
+
+export type MessageSelection = {
+ selectedId: Accessor<string | undefined>;
+ select: (id: string) => void;
+ clear: () => void;
+ selectFirst: () => string | undefined;
+ selectPrevious: () => string | undefined;
+ selectNext: () => string | undefined;
+};
+
+export function createMessageSelection(
+ options: CreateMessageSelectionOptions
+): MessageSelection {
+ const [selectedId, setSelectedId] = createSignal<string | undefined>();
+
+ // Auto-clear if selected ID disappears from keys
+ createEffect(
+ on(options.keys, (keys) => {
+ const current = selectedId();
+ if (current && !keys.includes(current)) {
+ setSelectedId(undefined);
+ }
+ })
+ );
+
+ const select = (id: string) => setSelectedId(id);
+ const clear = () => setSelectedId(undefined);
+
+ const selectFirst = (): string | undefined => {
+ const keys = options.keys();
+ if (keys.length === 0) return undefined;
+ const first = keys[0];
+ setSelectedId(first);
+ return first;
+ };
+
+ const selectPrevious = (): string | undefined => {
+ const keys = options.keys();
+ if (keys.length === 0) return undefined;
+
+ const current = selectedId();
+ if (!current) {
+ const last = keys[keys.length - 1];
+ setSelectedId(last);
+ return last;
+ }
+
+ const idx = keys.indexOf(current);
+ if (idx <= 0) return current;
+
+ const prev = keys[idx - 1];
+ setSelectedId(prev);
+ return prev;
+ };
+
+ const selectNext = (): string | undefined => {
+ const keys = options.keys();
+ const current = selectedId();
+ if (!current) return undefined;
+
+ const idx = keys.indexOf(current);
+ if (idx === -1 || idx >= keys.length - 1) {
+ setSelectedId(undefined);
+ return undefined;
+ }
+
+ const next = keys[idx + 1];
+ setSelectedId(next);
+ return next;
+ };
+
+ return { selectedId, select, clear, selectFirst, selectPrevious, selectNext };
+}
diff --git a/js/app/packages/channel/Input/ChannelInput.tsx b/js/app/packages/channel/Input/ChannelInput.tsx
index 9a9031b0e0..438f9e9eae 100644
--- a/js/app/packages/channel/Input/ChannelInput.tsx
+++ b/js/app/packages/channel/Input/ChannelInput.tsx
@@ -16,7 +16,7 @@ import type {
InputPersistenceKey,
} from './types';
import { applyInlineFormat, applyNodeFormat } from './utils/formatting';
-import { children, Match, Show, Switch, type JSX } from 'solid-js';
+import { Match, Show, Switch, type JSX } from 'solid-js';
import { isReplyInput } from './types';
export type ChannelInputProps = InputCallbacks & {
@@ -47,7 +47,6 @@ function DefaultActions(props: { input: InputData }) {
export function ChannelInput(props: ChannelInputProps) {
const mentionsTracker = createMentionsTracker();
- const customActions = children(() => props.children);
const attachmentTracker =
props.attachmentTracker ??
createInputAttachmentTracker({
@@ -147,7 +146,7 @@ export function ChannelInput(props: ChannelInputProps) {
<Input.Attachments kind="document" />
<Input.Footer>
<Switch>
- <Match when={customActions()}>{(actions) => actions()}</Match>
+ <Match when={props.children}>{props.children}</Match>
<Match when>
<DefaultActions input={inputState.view()} />
</Match>
diff --git a/js/app/packages/channel/Message/ActionMenu.tsx b/js/app/packages/channel/Message/ActionMenu.tsx
index 163ad3ebf3..023ce2f3b2 100644
--- a/js/app/packages/channel/Message/ActionMenu.tsx
+++ b/js/app/packages/channel/Message/ActionMenu.tsx
@@ -7,7 +7,7 @@ import { cn } from '@ui/utils/classname';
import { createSignal, For, Show, type Component, type JSX } from 'solid-js';
import { EmojiReactionPopover } from './EmojiReactionPopover';
import { HoverActions } from './HoverActions';
-import { useMessage, useMessageActions } from './context';
+import { useMessage, useMessageActions, useMessageSelection } from './context';
import { renderIcon } from './render-icon';
import type { MessageActionEvent, MessageActionHandler } from './types';
@@ -53,6 +53,7 @@ function ActionButton(props: {
export function ActionMenu(props: ActionMenuProps) {
const message = useMessage();
const actions = useMessageActions();
+ const selection = useMessageSelection();
const [emojiMenuOpen, setEmojiMenuOpen] = createSignal(false);
const handleReaction = (emoji: string, event?: MessageActionEvent) => {
@@ -97,7 +98,10 @@ export function ActionMenu(props: ActionMenuProps) {
return (
<Show when={hasReactAction() || visibleActions.length > 0}>
- <HoverActions class={props.class} persistentVisible={emojiMenuOpen()}>
+ <HoverActions
+ class={props.class}
+ persistentVisible={emojiMenuOpen() || !!selection?.isSelected}
+ >
<div class="flex flex-row bg-menu border border-edge-muted items-center allow-css-brackets">
<Show when={hasReactAction()}>
<For each={QUICK_REACTION_EMOJIS}>
diff --git a/js/app/packages/channel/Message/ChannelMessage.tsx b/js/app/packages/channel/Message/ChannelMessage.tsx
index 1437c9d646..5fc7f209d6 100644
--- a/js/app/packages/channel/Message/ChannelMessage.tsx
+++ b/js/app/packages/channel/Message/ChannelMessage.tsx
@@ -2,7 +2,8 @@ import { Match, Show, Switch } from 'solid-js';
import type { MessageActions, MessageData } from './types';
import { Message } from './Message';
import type { ChannelMessageListMeta } from './list-meta';
-import { useMessage } from './context';
+import { useMessage, MessageSelectionProvider } from './context';
+import type { MessageSelectionState } from './context';
import type { MessageEditor } from '../Channel/create-message-editor';
import { MessageEditorContent } from '../Channel/InlineMessageEditor';
@@ -12,6 +13,8 @@ type ChannelMessageProps = {
actions?: MessageActions;
listMeta?: ChannelMessageListMeta;
messageEditor?: MessageEditor;
+ highlighted?: boolean;
+ selectionState?: MessageSelectionState;
};
function isEditingMessage(
@@ -152,21 +155,27 @@ export function ChannelMessage(props: ChannelMessageProps) {
const isGrouped = () => props.listMeta?.isGroupedWithPrevious === true;
return (
- <Message.Root message={props.message} actions={props.actions}>
- <Switch>
- <Match when={isGrouped()}>
- <GroupedMessageLayout
- channelId={props.channelId}
- messageEditor={props.messageEditor}
- />
- </Match>
- <Match when={true}>
- <RegularMessageLayout
- channelId={props.channelId}
- messageEditor={props.messageEditor}
- />
- </Match>
- </Switch>
+ <Message.Root
+ message={props.message}
+ actions={props.actions}
+ highlighted={props.highlighted}
+ >
+ <MessageSelectionProvider value={props.selectionState}>
+ <Switch>
+ <Match when={isGrouped()}>
+ <GroupedMessageLayout
+ channelId={props.channelId}
+ messageEditor={props.messageEditor}
+ />
+ </Match>
+ <Match when={true}>
+ <RegularMessageLayout
+ channelId={props.channelId}
+ messageEditor={props.messageEditor}
+ />
+ </Match>
+ </Switch>
+ </MessageSelectionProvider>
</Message.Root>
);
}
diff --git a/js/app/packages/channel/Message/Root.tsx b/js/app/packages/channel/Message/Root.tsx
index fc5a3922e2..af6fb98a32 100644
--- a/js/app/packages/channel/Message/Root.tsx
+++ b/js/app/packages/channel/Message/Root.tsx
@@ -20,24 +20,13 @@ export function Root(props: RootProps) {
return (
<div
- class={cn(
- 'group/message relative hover:bg-accent/5 hover:outline-1 hover:outline-accent/20 hover:outline-offset-[-1px]',
- local.class,
- {
- 'bg-accent/5 outline-1 outline-accent/20 outline-offset-[-1px]':
- local.highlighted,
- }
- )}
+ class={cn('group/message relative', local.class)}
data-message
data-message-id={local.message.id}
+ data-highlighted={local.highlighted ? '' : undefined}
{...rest}
>
- <div
- class={cn(
- 'absolute h-full w-[3px] left-0 top-0 bg-accent opacity-0 group-hover/message:opacity-100',
- { 'opacity-100': local.highlighted }
- )}
- />
+ <div class="absolute h-full w-[3px] left-0 top-0 bg-accent opacity-0 message-accent-bar" />
<MessageProvider value={() => local.message}>
<MessageActionsProvider value={local.actions}>
{props.children}
diff --git a/js/app/packages/channel/Message/context.ts b/js/app/packages/channel/Message/context.ts
index 9fa838e1e1..82e6e81be1 100644
--- a/js/app/packages/channel/Message/context.ts
+++ b/js/app/packages/channel/Message/context.ts
@@ -16,3 +16,14 @@ export function useMessage(): Accessor<MessageData> {
export function useMessageActions(): MessageActions | undefined {
return useContext(MessageActionsContext);
}
+
+export type MessageSelectionState = {
+ isSelected: boolean;
+};
+
+const MessageSelectionContext = createContext<MessageSelectionState>();
+export const MessageSelectionProvider = MessageSelectionContext.Provider;
+
+export function useMessageSelection(): MessageSelectionState | undefined {
+ return useContext(MessageSelectionContext);
+}
diff --git a/js/app/packages/channel/Message/index.ts b/js/app/packages/channel/Message/index.ts
index 0ddbb7e83f..75636e719d 100644
--- a/js/app/packages/channel/Message/index.ts
+++ b/js/app/packages/channel/Message/index.ts
@@ -2,6 +2,8 @@ import {
useMessage,
useMessageActions,
MessageActionsProvider,
+ MessageSelectionProvider,
+ useMessageSelection,
} from './context';
export { Message } from './Message';
@@ -13,7 +15,14 @@ export { DateDivider } from './DateDivider';
export { NewDivider } from './NewDivider';
export { MessageFlag } from './MessageFlag';
export { ActionMenu } from './ActionMenu';
-export { useMessage, useMessageActions, MessageActionsProvider };
+export {
+ useMessage,
+ useMessageActions,
+ MessageActionsProvider,
+ MessageSelectionProvider,
+ useMessageSelection,
+};
+export type { MessageSelectionState } from './context';
export type { MessageData } from './types';
export type {
MessageActionContext,
diff --git a/js/app/packages/channel/Thread/ChannelThread.tsx b/js/app/packages/channel/Thread/ChannelThread.tsx
index 0a3925b432..026fa4a538 100644
--- a/js/app/packages/channel/Thread/ChannelThread.tsx
+++ b/js/app/packages/channel/Thread/ChannelThread.tsx
@@ -13,6 +13,8 @@ import {
getThreadLatestReplyAt,
getUniqueReplyUserIds,
} from './utils/thread-reply-indicator-helpers';
+import { createMessageSelection } from '../Channel/create-message-selection';
+import { createThreadHotkeys } from './create-thread-hotkeys';
function sliceIf<T>(
val: Array<T>,
@@ -32,6 +34,8 @@ export function ChannelThread(props: ThreadProps) {
const hasReplies = () => thread().reply_count > 0;
const fetchRepliesEnabled = () => props.data().thread.reply_count > 0;
+ const isSelected = () => props.selectedMessageId?.() === props.data().id;
+
const repliesQuery = useThreadRepliesQuery(
props.channelId,
() => props.data().id,
@@ -51,6 +55,31 @@ export function ChannelThread(props: ThreadProps) {
if (replies && !repliesQuery.isLoading) return replies;
return thread().preview ?? [];
};
+
+ // Thread-local reply selection
+ const replySelection = createMessageSelection({
+ keys: () => activeReplies().map((r) => r.id),
+ });
+
+ const isThreadFocused = () => isSelected() && !!replySelection.selectedId();
+
+ const { attachReplyInputRef } = createThreadHotkeys({
+ messageListScopeId: props.messageListScopeId!,
+ replySelection,
+ isThreadFocused,
+ activeReplies,
+ threadId: () => props.data().id,
+ getMessageActions: (msg) => props.getMessageActions?.(msg),
+ userId,
+ parentMessage: props.data,
+ collapseThread: () => props.setIsExpanded(false),
+ isSelected,
+ hasReplies,
+ expandThread: () => props.setIsExpanded(true),
+ isThreadExpanded: props.isExpanded,
+ setIsReplying: (v) => props.setIsReplying(v),
+ });
+
const firstReplyIsNewMessage = () => {
const first = activeReplies()[0];
return first ? props.isNewMessage?.(first) : false;
@@ -91,6 +120,12 @@ export function ChannelThread(props: ThreadProps) {
actions={props.getMessageActions?.(props.data())}
listMeta={props.listMeta}
messageEditor={props.messageEditor}
+ highlighted={isSelected() && !isThreadFocused()}
+ selectionState={
+ isSelected() && !isThreadFocused()
+ ? { isSelected: true }
+ : undefined
+ }
/>
</MarkMessaageNotifications>
<Show when={hasReplies() || props.isReplying()}>
@@ -111,6 +146,8 @@ export function ChannelThread(props: ThreadProps) {
getMessageActions={props.getMessageActions}
messageEditor={props.messageEditor}
isNewMessage={props.isNewMessage}
+ selectedReplyId={replySelection.selectedId}
+ isThreadFocused={isThreadFocused}
/>
}
>
@@ -122,24 +159,28 @@ export function ChannelThread(props: ThreadProps) {
getMessageActions={props.getMessageActions}
messageEditor={props.messageEditor}
isNewMessage={props.isNewMessage}
+ selectedReplyId={replySelection.selectedId}
+ isThreadFocused={isThreadFocused}
/>
</Suspense>
</Show>
<Show when={props.isReplying()}>
- <Show when={!hasReplies()}>
- <Thread.ReplyAuthor
- userId={replyUserId()}
- displayName={displayName()}
+ <div ref={attachReplyInputRef}>
+ <Show when={!hasReplies()}>
+ <Thread.ReplyAuthor
+ userId={replyUserId()}
+ displayName={displayName()}
+ />
+ </Show>
+ <Thread.ReplyInput
+ channelId={props.channelId()}
+ messageId={props.data().id}
+ replyInputState={props.replyInputState}
+ setReplyInputState={props.setReplyInputState}
+ setIsReplying={props.setIsReplying}
/>
- </Show>
- <Thread.ReplyInput
- channelId={props.channelId()}
- messageId={props.data().id}
- replyInputState={props.replyInputState}
- setReplyInputState={props.setReplyInputState}
- setIsReplying={props.setIsReplying}
- />
+ </div>
</Show>
<Show
diff --git a/js/app/packages/channel/Thread/ThreadReplyList.tsx b/js/app/packages/channel/Thread/ThreadReplyList.tsx
index 248db66b91..dc092fba35 100644
--- a/js/app/packages/channel/Thread/ThreadReplyList.tsx
+++ b/js/app/packages/channel/Thread/ThreadReplyList.tsx
@@ -1,4 +1,4 @@
-import { For, createMemo } from 'solid-js';
+import { For, createMemo, type Accessor } from 'solid-js';
import {
ChannelMessage,
type MessageActions,
@@ -18,6 +18,8 @@ export function ThreadReplyList(props: {
getMessageActions?: (message: MessageData) => MessageActions | undefined;
messageEditor?: MessageEditor;
isNewMessage?: (message: NewMessageCheckable) => boolean;
+ selectedReplyId?: Accessor<string | undefined>;
+ isThreadFocused?: Accessor<boolean>;
}) {
const listMetaByReplyId = createMemo(() =>
buildThreadReplyListMeta(props.replies, props.isNewMessage)
@@ -31,6 +33,9 @@ export function ThreadReplyList(props: {
thread_id: props.threadId,
});
+ const isReplySelected = () =>
+ !!props.isThreadFocused?.() && props.selectedReplyId?.() === reply.id;
+
return (
<div class="relative">
<ThreadRail
@@ -46,6 +51,10 @@ export function ThreadReplyList(props: {
actions={props.getMessageActions?.(replyMessage())}
listMeta={listMetaByReplyId()[reply.id]}
messageEditor={props.messageEditor}
+ highlighted={isReplySelected()}
+ selectionState={
+ isReplySelected() ? { isSelected: true } : undefined
+ }
/>
</MarkMessaageNotifications>
</div>
diff --git a/js/app/packages/channel/Thread/create-thread-hotkeys.ts b/js/app/packages/channel/Thread/create-thread-hotkeys.ts
new file mode 100644
index 0000000000..0034badcd3
--- /dev/null
+++ b/js/app/packages/channel/Thread/create-thread-hotkeys.ts
@@ -0,0 +1,192 @@
+import {
+ createHotkeyGroup,
+ registerHotkey,
+ useHotkeyDOMScope,
+} from '@core/hotkey/hotkeys';
+import { TOKENS } from '@core/hotkey/tokens';
+import { HOTKEY_PRIORITY_HIGH } from '@core/hotkey/types';
+import { onCleanup, type Accessor } from 'solid-js';
+import type { MessageSelection } from '../Channel/create-message-selection';
+import type { ApiChannelMessage, ApiThreadReply } from '@service-comms/client';
+import type { MessageActions, MessageData } from '../Message';
+
+type CreateThreadHotkeysOptions = {
+ messageListScopeId: string;
+ replySelection: MessageSelection;
+ isThreadFocused: Accessor<boolean>;
+ activeReplies: Accessor<Array<ApiThreadReply>>;
+ threadId: Accessor<string>;
+ getMessageActions: (message: MessageData) => MessageActions | undefined;
+ userId: Accessor<string | undefined>;
+ parentMessage: Accessor<ApiChannelMessage>;
+ collapseThread: () => void;
+ isSelected: Accessor<boolean>;
+ hasReplies: Accessor<boolean>;
+ expandThread: () => void;
+ isThreadExpanded: Accessor<boolean>;
+ setIsReplying: (replying: boolean) => void;
+};
+
+function scrollReplyIntoView(replyId: string) {
+ document
+ .querySelector<HTMLElement>(`[data-message-id="${replyId}"]`)
+ ?.scrollIntoView({ block: 'nearest' });
+}
+
+export function createThreadHotkeys(options: CreateThreadHotkeysOptions) {
+ const scope = options.messageListScopeId;
+ const group = createHotkeyGroup();
+
+ const getReplyById = (id: string): MessageData | undefined => {
+ const reply = options.activeReplies().find((r) => r.id === id);
+ if (!reply) return undefined;
+ return { ...reply, thread_id: options.threadId() } as MessageData;
+ };
+
+ registerHotkey({
+ scopeId: scope,
+ hotkey: 'arrowright',
+ hotkeyToken: TOKENS.channel.expandThread,
+ description: 'Enter thread',
+ registrationType: 'add',
+ handlerPriority: HOTKEY_PRIORITY_HIGH,
+ condition: () => options.isSelected() && options.hasReplies(),
+ keyDownHandler: () => {
+ options.expandThread();
+ const id = options.replySelection.selectFirst();
+ if (id) {
+ requestAnimationFrame(() => scrollReplyIntoView(id));
+ }
+ return true;
+ },
+ }).withGroup(group);
+
+ registerHotkey({
+ scopeId: scope,
+ hotkey: 'arrowup',
+ hotkeyToken: TOKENS.channel.threadPreviousReply,
+ description: 'Previous reply',
+ registrationType: 'add',
+ handlerPriority: HOTKEY_PRIORITY_HIGH,
+ condition: () => options.isThreadFocused() && options.isThreadExpanded(),
+ keyDownHandler: () => {
+ const before = options.replySelection.selectedId();
+ const id = options.replySelection.selectPrevious();
+ if (id && id !== before) {
+ scrollReplyIntoView(id);
+ } else {
+ options.replySelection.clear();
+ }
+ return true;
+ },
+ }).withGroup(group);
+
+ registerHotkey({
+ scopeId: scope,
+ hotkey: 'arrowdown',
+ hotkeyToken: TOKENS.channel.threadNextReply,
+ description: 'Next reply',
+ registrationType: 'add',
+ handlerPriority: HOTKEY_PRIORITY_HIGH,
+ condition: () => options.isThreadFocused() && options.isThreadExpanded(),
+ keyDownHandler: () => {
+ const id = options.replySelection.selectNext();
+ if (id) {
+ scrollReplyIntoView(id);
+ return true;
+ }
+ options.replySelection.clear();
+ return false;
+ },
+ }).withGroup(group);
+
+ registerHotkey({
+ scopeId: scope,
+ hotkey: 'arrowleft',
+ hotkeyToken: TOKENS.channel.threadCollapse,
+ description: 'Collapse thread',
+ registrationType: 'add',
+ handlerPriority: HOTKEY_PRIORITY_HIGH,
+ condition: () => options.isThreadFocused() && options.isThreadExpanded(),
+ keyDownHandler: () => {
+ options.collapseThread();
+ options.replySelection.clear();
+ return true;
+ },
+ }).withGroup(group);
+
+ registerHotkey({
+ scopeId: scope,
+ hotkey: 'escape',
+ hotkeyToken: TOKENS.channel.threadExit,
+ description: 'Exit thread',
+ registrationType: 'add',
+ handlerPriority: HOTKEY_PRIORITY_HIGH,
+ condition: options.isThreadFocused,
+ keyDownHandler: () => {
+ options.replySelection.clear();
+ return true;
+ },
+ }).withGroup(group);
+
+ registerHotkey({
+ scopeId: scope,
+ hotkey: 'enter',
+ hotkeyToken: TOKENS.channel.threadReply,
+ description: 'Reply to thread',
+ registrationType: 'add',
+ handlerPriority: HOTKEY_PRIORITY_HIGH,
+ condition: options.isThreadFocused,
+ keyDownHandler: () => {
+ const parentMsg = options.parentMessage();
+ const actions = options.getMessageActions(parentMsg);
+ actions?.onReply?.({ message: parentMsg });
+ return true;
+ },
+ }).withGroup(group);
+
+ registerHotkey({
+ scopeId: scope,
+ hotkey: 'e',
+ hotkeyToken: TOKENS.channel.threadEditReply,
+ description: 'Edit reply',
+ registrationType: 'add',
+ handlerPriority: HOTKEY_PRIORITY_HIGH,
+ condition: () => {
+ if (!options.isThreadFocused()) return false;
+ const replyId = options.replySelection.selectedId();
+ if (!replyId) return false;
+ const reply = getReplyById(replyId);
+ return !!reply && reply.sender_id === options.userId();
+ },
+ keyDownHandler: () => {
+ const replyId = options.replySelection.selectedId();
+ if (!replyId) return false;
+ const reply = getReplyById(replyId);
+ if (!reply) return false;
+ const actions = options.getMessageActions(reply);
+ actions?.onEdit?.({ message: reply });
+ return true;
+ },
+ }).withGroup(group);
+
+ const [attachReplyInputRef, replyInputScope] = useHotkeyDOMScope(
+ 'channel-reply-input'
+ );
+
+ registerHotkey({
+ scopeId: replyInputScope,
+ hotkey: 'escape',
+ hotkeyToken: TOKENS.channel.cancelReply,
+ description: 'Cancel reply',
+ runWithInputFocused: true,
+ keyDownHandler: () => {
+ options.setIsReplying(false);
+ return true;
+ },
+ }).withGroup(group);
+
+ onCleanup(() => group.dispose());
+
+ return { attachReplyInputRef };
+}
diff --git a/js/app/packages/channel/Thread/types.ts b/js/app/packages/channel/Thread/types.ts
index be26cd545e..c4a8de1dcb 100644
--- a/js/app/packages/channel/Thread/types.ts
+++ b/js/app/packages/channel/Thread/types.ts
@@ -35,4 +35,6 @@ export type ThreadProps = {
threadActions?: ThreadActions;
messageEditor?: MessageEditor;
isNewMessage?: (reply: NewMessageCheckable) => boolean;
+ selectedMessageId?: Accessor<string | undefined>;
+ messageListScopeId?: string;
} & ThreadState;
diff --git a/js/app/packages/core/hotkey/hotkeys.ts b/js/app/packages/core/hotkey/hotkeys.ts
index 321726b9d6..ff96301fae 100644
--- a/js/app/packages/core/hotkey/hotkeys.ts
+++ b/js/app/packages/core/hotkey/hotkeys.ts
@@ -257,7 +257,6 @@ export function registerHotkey(
// Register each hotkey with the same command
if (hotkeys) {
hotkeys.forEach((h) => {
- // Add to existing handlers array
const existingHandlers = scopeNode.hotkeyCommands.get(h) || [];
if (registrationType === 'add') {
scopeNode.hotkeyCommands.set(h, [...existingHandlers, command]);
diff --git a/js/app/packages/core/hotkey/tokens.ts b/js/app/packages/core/hotkey/tokens.ts
index 1499aff774..12cec1da3b 100644
--- a/js/app/packages/core/hotkey/tokens.ts
+++ b/js/app/packages/core/hotkey/tokens.ts
@@ -213,6 +213,14 @@ export const TOKENS = {
focusNextMessage: 'channel.focusNextMessage',
focusInput: 'channel.focusInput',
sendMessage: 'channel.sendMessage',
+ clearSelection: 'channel.clearSelection',
+ cancelReply: 'channel.cancelReply',
+ threadPreviousReply: 'channel.threadPreviousReply',
+ threadNextReply: 'channel.threadNextReply',
+ threadExit: 'channel.threadExit',
+ threadCollapse: 'channel.threadCollapse',
+ threadReply: 'channel.threadReply',
+ threadEditReply: 'channel.threadEditReply',
},
// drawer