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): go to message (#2015)

teoc010239dc3bc
diff --git a/js/app/packages/channel/Channel/Channel.tsx b/js/app/packages/channel/Channel/Channel.tsx
index 3e391405e2..f39f2d167c 100644
--- a/js/app/packages/channel/Channel/Channel.tsx
+++ b/js/app/packages/channel/Channel/Channel.tsx
@@ -60,10 +60,15 @@ 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';
+import { createTargetMessageController } from './create-target-message-controller';
+import { createMethodRegistration } from '@core/orchestrator';
+import { blockHandleSignal } from '@core/signal/load';
+import { URL_PARAMS } from '@block-channel/constants';
type ChannelProps = {
channelId: string;
targetMessageId?: string | undefined;
+ targetMessageReplyId?: string | undefined;
lastViewedAt?: DateValue | null;
};
@@ -74,18 +79,50 @@ export function Channel(props: ChannelProps) {
const deleteMessageMutation = useDeleteMessageMutation();
const addReactionMutation = useAddReactionMutation();
const removeReactionMutation = useRemoveReactionMutation();
+ const [threadListNavigation, setThreadListNavigation] =
+ createSignal<ThreadListNavigation>();
+ const [threadListScrollState, setThreadListScrollState] =
+ createSignal<ThreadListScrollState>();
+
+ const targetMessageController = createTargetMessageController({
+ channelId: () => props.channelId,
+ initialTargetMessageId: props.targetMessageId,
+ messageKeys: () => messageIndex().keys,
+ navigation: threadListNavigation,
+ });
- const [targetMessageId, _setTargetMessageId] = createSignal<
- string | undefined
- >(props.targetMessageId);
+ // START BLOCK COMPATIBILITY
+ const blockHandle = blockHandleSignal.get;
+ createMethodRegistration(blockHandle, {
+ goToLocationFromParams: async (params: Record<string, unknown>) => {
+ const threadId = params[URL_PARAMS.thread] as string | undefined;
+ const messageId = params[URL_PARAMS.message] as string | undefined;
+
+ // For compotibility the naming is a little strange here.
+ // New channels index by top level message and then spertately handle replies.
+ // If we have a threadId that is actually the top level message and the reply is the message id.
+ const topLevelMessageId = threadId ? threadId : messageId;
+ const messageReplyId = threadId ? messageId : threadId;
+
+ if (topLevelMessageId) {
+ targetMessageController.goToMessage(topLevelMessageId, messageReplyId);
+ }
+ },
+ });
+ // END BLOCK COMPATIBILITY
const [channelInputSnapshot, setChannelInputSnapshot] =
createSignal<InputSnapshot>();
const messagesQuery = useChannelMessagesQuery(
() => props.channelId,
- targetMessageId
+ targetMessageController.loadAroundMessageId
+ );
+ const messageIndex = createMemo(() =>
+ makeMessageIndex(messagesQuery.data as ChannelMessagesData | undefined)
);
+ const messages = createMemo(() => messageIndex().items);
+ const messageById = createMemo(() => messageIndex().byId);
const activity = useChannelActivity(props.channelId);
@@ -109,11 +146,6 @@ export function Channel(props: ChannelProps) {
});
});
- const [threadListNavigation, setThreadListNavigation] =
- createSignal<ThreadListNavigation>();
- const [threadListScrollState, setThreadListScrollState] =
- createSignal<ThreadListScrollState>();
-
const threadManager = createThreadManager();
const threadPaginator = createThreadPaginator(messagesQuery);
const messageEditor = createMessageEditor({
@@ -122,13 +154,9 @@ export function Channel(props: ChannelProps) {
});
const threadListInitialScrollTarget: Accessor<ThreadListScrollTarget> = () =>
- defaultThreadListTargetFromMessage(targetMessageId());
-
- const messageIndex = createMemo(() =>
- makeMessageIndex(messagesQuery.data as ChannelMessagesData | undefined)
- );
- const messages = createMemo(() => messageIndex().items);
- const messageById = createMemo(() => messageIndex().byId);
+ defaultThreadListTargetFromMessage(
+ targetMessageController.activeTargetMessageId()
+ );
const shift = () => threadPaginator.isShifting();
@@ -184,7 +212,7 @@ export function Channel(props: ChannelProps) {
createStickyScrollEffect({
isNearBottom: () => threadListScrollState()?.isNearBottom ?? false,
- hasMoreBelow: () => threadPaginator.hasMoreShifting(),
+ hasMoreBelow: () => threadPaginator.hasMorePrepend(),
messages,
scrollToBottom: () => threadListNavigation()?.scrollToBottom(),
});
@@ -239,6 +267,11 @@ export function Channel(props: ChannelProps) {
data={m}
channelId={() => props.channelId}
getMessageActions={getMessageActions}
+ targetReplyId={targetMessageController.activeTargetMessageReplyId()}
+ highlighted={
+ m().id ===
+ targetMessageController.highlightedMessageId()
+ }
isExpanded={state.isExpanded}
setIsExpanded={state.setIsExpanded}
isReplying={state.isReplying}
diff --git a/js/app/packages/channel/Channel/create-target-message-controller.ts b/js/app/packages/channel/Channel/create-target-message-controller.ts
new file mode 100644
index 0000000000..28ae0fa83a
--- /dev/null
+++ b/js/app/packages/channel/Channel/create-target-message-controller.ts
@@ -0,0 +1,120 @@
+import { createEffect, on, type Accessor } from 'solid-js';
+import {
+ getChannelMessagesQueryKey,
+ type ChannelMessagesData,
+} from '@queries/channel/channel-messages';
+import { queryClient } from '@queries/client';
+import type { ThreadListNavigation } from './ThreadList';
+import { createStore } from 'solid-js/store';
+
+type CreateTargetMessageControllerOptions = {
+ channelId: Accessor<string>;
+ initialTargetMessageId?: string | undefined;
+ initialTargetMessageReplyId?: string | undefined;
+ messageKeys: Accessor<string[]>;
+ navigation: Accessor<ThreadListNavigation | undefined>;
+};
+
+export type TargetMessageController = ReturnType<
+ typeof createTargetMessageController
+>;
+
+type TargetMessageData = {
+ activeTargetMessageId: string | undefined;
+ activeTargetMessageReplyId: string | undefined;
+ highlightedMessageId: string | undefined;
+ loadAroundMessageId: string | undefined;
+ pendingScrollTargetId: string | undefined;
+};
+
+export function createTargetMessageController(
+ options: CreateTargetMessageControllerOptions
+) {
+ const initialTargetMessageData: TargetMessageData = {
+ activeTargetMessageId: options.initialTargetMessageId,
+ activeTargetMessageReplyId: undefined,
+ highlightedMessageId:
+ options.initialTargetMessageReplyId ?? options.initialTargetMessageId,
+ loadAroundMessageId: options.initialTargetMessageId,
+ pendingScrollTargetId: options.initialTargetMessageId,
+ };
+
+ const [targetMessageData, setTargetMessageData] =
+ createStore<TargetMessageData>(initialTargetMessageData);
+
+ const hasMessageLoaded = (messageId: string) =>
+ options.messageKeys().includes(messageId);
+
+ const goToMessage = (messageId: string, replyId?: string) => {
+ const isSameTarget =
+ targetMessageData['activeTargetMessageId'] === messageId;
+ const isPending = targetMessageData['pendingScrollTargetId'] === messageId;
+
+ if (isSameTarget && isPending) return;
+
+ setTargetMessageData({
+ activeTargetMessageId: messageId,
+ activeTargetMessageReplyId: replyId,
+ highlightedMessageId: replyId ?? messageId,
+ loadAroundMessageId: hasMessageLoaded(messageId) ? undefined : messageId,
+ pendingScrollTargetId: messageId,
+ });
+ };
+
+ const completePendingScroll = (messageId: string) => {
+ if (targetMessageData['pendingScrollTargetId'] !== messageId) return;
+ setTargetMessageData('pendingScrollTargetId', undefined);
+ };
+
+ createEffect(
+ on(
+ [
+ options.navigation,
+ () => targetMessageData['pendingScrollTargetId'],
+ options.messageKeys,
+ ],
+ ([navigation, pendingTargetId]) => {
+ if (!navigation || !pendingTargetId) return;
+ if (!hasMessageLoaded(pendingTargetId)) return;
+ if (!navigation.scrollToId(pendingTargetId)) return;
+
+ const restoredDefaultPagination =
+ restoreDefaultChannelPaginationAfterTargetLoad(
+ options.channelId(),
+ targetMessageData['loadAroundMessageId']
+ );
+ if (restoredDefaultPagination) {
+ setTargetMessageData('loadAroundMessageId', undefined);
+ }
+ completePendingScroll(pendingTargetId);
+ }
+ )
+ );
+
+ return {
+ activeTargetMessageId: () => targetMessageData['activeTargetMessageId'],
+ activeTargetMessageReplyId: () =>
+ targetMessageData['activeTargetMessageReplyId'],
+ highlightedMessageId: () => targetMessageData['highlightedMessageId'],
+ loadAroundMessageId: () => targetMessageData['loadAroundMessageId'],
+ pendingScrollTargetId: () => targetMessageData['pendingScrollTargetId'],
+
+ goToMessage,
+ completePendingScroll,
+ };
+}
+
+export function restoreDefaultChannelPaginationAfterTargetLoad(
+ channelId: string,
+ loadAroundMessageId: string | undefined
+) {
+ if (!loadAroundMessageId) return false;
+
+ const aroundKey = getChannelMessagesQueryKey(channelId, loadAroundMessageId);
+ const defaultKey = getChannelMessagesQueryKey(channelId, null);
+ const aroundData = queryClient.getQueryData<ChannelMessagesData>(aroundKey);
+ if (!aroundData) return false;
+
+ queryClient.setQueryData(defaultKey, aroundData);
+ return true;
+}
diff --git a/js/app/packages/channel/Channel/tests/create-channel-message-actions.test.ts b/js/app/packages/channel/Channel/tests/create-channel-message-actions.test.ts
index 7cc4a2ad23..4e58edde26 100644
--- a/js/app/packages/channel/Channel/tests/create-channel-message-actions.test.ts
+++ b/js/app/packages/channel/Channel/tests/create-channel-message-actions.test.ts
@@ -89,16 +89,4 @@ describe('createChannelMessageActions', () => {
currentReactions: [],
});
});
-
- it('delegates edit handling to the provided callback', () => {
- const onEdit = vi.fn();
- const harness = buildHarness({ userId: 'user-1', onEdit });
- const message = buildMessage();
- const actions = harness.getMessageActions(message);
-
- actions.onEdit?.({ message });
-
- expect(onEdit).toHaveBeenCalledOnce();
- expect(onEdit).toHaveBeenCalledWith({ message });
- });
});
diff --git a/js/app/packages/channel/Channel/tests/create-target-message-controller.test.ts b/js/app/packages/channel/Channel/tests/create-target-message-controller.test.ts
new file mode 100644
index 0000000000..0ee54f6223
--- /dev/null
+++ b/js/app/packages/channel/Channel/tests/create-target-message-controller.test.ts
@@ -0,0 +1,117 @@
+import { createRoot, createSignal } from 'solid-js';
+import { afterEach, describe, expect, it } from 'vitest';
+import {
+ createTargetMessageController,
+ restoreDefaultChannelPaginationAfterTargetLoad,
+} from '../create-target-message-controller';
+import { queryClient } from '@queries/client';
+import {
+ getChannelMessagesQueryKey,
+ type ChannelMessagesData,
+} from '@queries/channel/channel-messages';
+
+afterEach(() => {
+ queryClient.clear();
+});
+
+function createController(
+ input?: Partial<{
+ channelId: string;
+ initialTargetMessageId: string;
+ messageKeys: string[];
+ scrollToId: (messageId: string) => boolean;
+ withNavigation: boolean;
+ }>
+) {
+ const [messageKeys, setMessageKeys] = createSignal(input?.messageKeys ?? []);
+
+ const scrollToId =
+ input?.scrollToId ??
+ (() => {
+ return true;
+ });
+
+ const controller = createTargetMessageController({
+ channelId: () => input?.channelId ?? 'channel-1',
+ initialTargetMessageId: input?.initialTargetMessageId,
+ messageKeys,
+ navigation: () =>
+ input?.withNavigation
+ ? {
+ scrollTo: () => false,
+ scrollToIndex: () => false,
+ scrollByDelta: () => false,
+ scrollToTop: () => false,
+ scrollToBottom: () => false,
+ scrollToId,
+ navigatePrevious: () => false,
+ navigateNext: () => false,
+ isNearBottom: () => true,
+ }
+ : undefined,
+ });
+
+ return {
+ controller,
+ setMessageKeys,
+ };
+}
+
+describe('createTargetMessageController', () => {
+ it('preserves the current load-around target when navigating to an already loaded message', () => {
+ createRoot((dispose) => {
+ const { controller } = createController({
+ initialTargetMessageId: 'message-1',
+ messageKeys: ['message-2'],
+ });
+
+ controller.goToMessage('message-2');
+
+ expect(controller.activeTargetMessageId()).toBe('message-2');
+ expect(controller.highlightedMessageId()).toBe('message-2');
+ expect(controller.pendingScrollTargetId()).toBe('message-2');
+ expect(controller.loadAroundMessageId()).toBeUndefined();
+ dispose();
+ });
+ });
+
+ it('switches load-around target when navigating to a missing message', () => {
+ createRoot((dispose) => {
+ const { controller } = createController({
+ initialTargetMessageId: 'message-1',
+ });
+
+ controller.goToMessage('message-9');
+
+ expect(controller.activeTargetMessageId()).toBe('message-9');
+ expect(controller.highlightedMessageId()).toBe('message-9');
+ expect(controller.pendingScrollTargetId()).toBe('message-9');
+ expect(controller.loadAroundMessageId()).toBe('message-9');
+ dispose();
+ });
+ });
+
+ it('copies around-target query data into the default query key', () => {
+ const aroundData = {
+ pageParams: [null],
+ pages: [
+ {
+ items: [],
+ next_cursor: 'next',
+ previous_cursor: 'prev',
+ },
+ ],
+ } as ChannelMessagesData;
+
+ queryClient.setQueryData(
+ getChannelMessagesQueryKey('channel-1', 'message-9'),
+ aroundData
+ );
+
+ restoreDefaultChannelPaginationAfterTargetLoad('channel-1', 'message-9');
+
+ expect(
+ queryClient.getQueryData(getChannelMessagesQueryKey('channel-1', null))
+ ).toEqual(aroundData);
+ });
+});
diff --git a/js/app/packages/channel/Thread/ChannelThread.tsx b/js/app/packages/channel/Thread/ChannelThread.tsx
index 026fa4a538..b840304942 100644
--- a/js/app/packages/channel/Thread/ChannelThread.tsx
+++ b/js/app/packages/channel/Thread/ChannelThread.tsx
@@ -1,5 +1,12 @@
import { useThreadRepliesQuery } from '@queries/channel/thread-replies';
-import { Show, Suspense, type Accessor } from 'solid-js';
+import {
+ createEffect,
+ createSignal,
+ on,
+ Show,
+ Suspense,
+ type Accessor,
+} from 'solid-js';
import { ChannelMessage } from '../Message';
import { MarkMessaageNotifications } from '@notifications/components/MarkMessageNotifications';
import { useUserId } from '@core/context/user';
@@ -7,6 +14,7 @@ import { tryMacroId, useDisplayName } from '@core/user';
import { Thread } from './Thread';
import type { ThreadProps } from './types';
import type { ApiThreadReply } from '@service-comms/client';
+import type { ThreadReplyListHandle } from './ThreadReplyList';
import {
DEFAULT_VISIBLE_REPLY_COUNT,
getCollapsedRepliesCount,
@@ -97,11 +105,39 @@ export function ChannelThread(props: ThreadProps) {
!props.isReplying() && !props.isExpanded() && collapsedRepliesCount() > 0;
const shouldShowReplyButton = () =>
hasReplies() && !props.isReplying() && !shouldShowCollapsedIndicator();
+ const [replyListHandle, setReplyListHandle] =
+ createSignal<ThreadReplyListHandle>();
const expand = () => {
props.setIsExpanded(true);
};
+ createEffect(
+ on(
+ [
+ () => props.targetReplyId,
+ activeReplies,
+ props.isExpanded,
+ replyListHandle,
+ ],
+ ([targetReplyId, replies, isExpanded, handle]) => {
+ if (!targetReplyId) return;
+
+ const targetReplyIndex = replies.findIndex(
+ (reply) => reply.id === targetReplyId
+ );
+ if (targetReplyIndex === -1) return;
+
+ if (!isExpanded && targetReplyIndex >= DEFAULT_VISIBLE_REPLY_COUNT) {
+ props.setIsExpanded(true);
+ return;
+ }
+
+ handle?.scrollToIndex(targetReplyIndex);
+ }
+ )
+ );
+
return (
<Suspense>
<Thread.Row
@@ -120,7 +156,9 @@ export function ChannelThread(props: ThreadProps) {
actions={props.getMessageActions?.(props.data())}
listMeta={props.listMeta}
messageEditor={props.messageEditor}
- highlighted={isSelected() && !isThreadFocused()}
+ highlighted={
+ props.highlighted || (isSelected() && !isThreadFocused())
+ }
selectionState={
isSelected() && !isThreadFocused()
? { isSelected: true }
@@ -146,6 +184,8 @@ export function ChannelThread(props: ThreadProps) {
getMessageActions={props.getMessageActions}
messageEditor={props.messageEditor}
isNewMessage={props.isNewMessage}
+ highlightedReplyId={props.targetReplyId}
+ onReady={setReplyListHandle}
selectedReplyId={replySelection.selectedId}
isThreadFocused={isThreadFocused}
/>
@@ -159,6 +199,8 @@ export function ChannelThread(props: ThreadProps) {
getMessageActions={props.getMessageActions}
messageEditor={props.messageEditor}
isNewMessage={props.isNewMessage}
+ highlightedReplyId={props.targetReplyId}
+ onReady={setReplyListHandle}
selectedReplyId={replySelection.selectedId}
isThreadFocused={isThreadFocused}
/>
diff --git a/js/app/packages/channel/Thread/ThreadReplyList.tsx b/js/app/packages/channel/Thread/ThreadReplyList.tsx
index dc092fba35..afda65bde7 100644
--- a/js/app/packages/channel/Thread/ThreadReplyList.tsx
+++ b/js/app/packages/channel/Thread/ThreadReplyList.tsx
@@ -11,6 +11,18 @@ import { ThreadRail } from './ThreadRail';
import type { MessageEditor } from '../Channel/create-message-editor';
import type { NewMessageCheckable } from '../Channel/util';
+export type ThreadReplyListHandle = {
+ scrollToIndex: (index: number) => boolean;
+};
+
+function getReplyElementAtIndex(
+ elements: Array<HTMLElement | undefined>,
+ index: number
+): HTMLElement | undefined {
+ if (index < 0) return undefined;
+ return elements[index];
+}
+
export function ThreadReplyList(props: {
channelId: string;
threadId: string;
@@ -18,16 +30,30 @@ export function ThreadReplyList(props: {
getMessageActions?: (message: MessageData) => MessageActions | undefined;
messageEditor?: MessageEditor;
isNewMessage?: (message: NewMessageCheckable) => boolean;
+ highlightedReplyId?: string;
+ onReady?: (handle: ThreadReplyListHandle) => void;
selectedReplyId?: Accessor<string | undefined>;
isThreadFocused?: Accessor<boolean>;
}) {
const listMetaByReplyId = createMemo(() =>
buildThreadReplyListMeta(props.replies, props.isNewMessage)
);
+ const replyElements: Array<HTMLElement | undefined> = [];
+
+ const scrollToIndex = (index: number): boolean => {
+ const element = getReplyElementAtIndex(replyElements, index);
+ if (!element) return false;
+ element.scrollIntoView({ block: 'nearest' });
+ return true;
+ };
+
+ props.onReady?.({
+ scrollToIndex,
+ });
return (
<For each={props.replies}>
- {(reply) => {
+ {(reply, index) => {
const replyMessage = () => ({
...reply,
thread_id: props.threadId,
@@ -37,7 +63,12 @@ export function ThreadReplyList(props: {
!!props.isThreadFocused?.() && props.selectedReplyId?.() === reply.id;
return (
- <div class="relative">
+ <div
+ ref={(element) => {
+ replyElements[index()] = element;
+ }}
+ class="relative"
+ >
<ThreadRail
newMessage={listMetaByReplyId()[reply.id].isNewMessage}
/>
@@ -51,7 +82,9 @@ export function ThreadReplyList(props: {
actions={props.getMessageActions?.(replyMessage())}
listMeta={listMetaByReplyId()[reply.id]}
messageEditor={props.messageEditor}
- highlighted={isReplySelected()}
+ highlighted={
+ props.highlightedReplyId === reply.id || isReplySelected()
+ }
selectionState={
isReplySelected() ? { isSelected: true } : undefined
}
diff --git a/js/app/packages/channel/Thread/types.ts b/js/app/packages/channel/Thread/types.ts
index c4a8de1dcb..53fdfb9eac 100644
--- a/js/app/packages/channel/Thread/types.ts
+++ b/js/app/packages/channel/Thread/types.ts
@@ -34,7 +34,9 @@ export type ThreadProps = {
listMeta?: ChannelMessageListMeta;
threadActions?: ThreadActions;
messageEditor?: MessageEditor;
+ targetReplyId?: string;
isNewMessage?: (reply: NewMessageCheckable) => boolean;
+ highlighted?: boolean;
selectedMessageId?: Accessor<string | undefined>;
messageListScopeId?: string;
} & ThreadState;