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.

fix(comments): release a comment link's highlight when the linked comment is clicked (#6945)

gbirmand2c640ee9b5c
diff --git a/apps/web/src/features/block-md/comments/CommentMargin.tsx b/apps/web/src/features/block-md/comments/CommentMargin.tsx
index f9b275b1f6..80cc848d68 100644
--- a/apps/web/src/features/block-md/comments/CommentMargin.tsx
+++ b/apps/web/src/features/block-md/comments/CommentMargin.tsx
@@ -95,6 +95,8 @@ const useCommentsContext = (
...operations,
inComment: true,
highlightedCommentId: () => commentState.highlightedCommentId,
+ clearHighlightedComment: () =>
+ setCommentState('highlightedCommentId', null),
};
return commentsContext;
};
diff --git a/apps/web/src/features/block-md/component/DocumentDiscussion.tsx b/apps/web/src/features/block-md/component/DocumentDiscussion.tsx
index ad561b8582..2d30eeb731 100644
--- a/apps/web/src/features/block-md/component/DocumentDiscussion.tsx
+++ b/apps/web/src/features/block-md/component/DocumentDiscussion.tsx
@@ -23,7 +23,14 @@ import { isTouchDevice } from '@core/mobile/isTouchDevice';
import { virtualKeyboardVisible } from '@core/mobile/virtualKeyboard';
import { buildSimpleEntityUrl } from '@core/util/url';
import type { MessageParent } from '@service-storage/messages';
-import { createEffect, createMemo, on, onCleanup, Show } from 'solid-js';
+import {
+ createEffect,
+ createMemo,
+ createSignal,
+ on,
+ onCleanup,
+ Show,
+} from 'solid-js';
import { createDocumentDiscussionSource } from '../comments/documentDiscussionSource';
import { URL_PARAMS } from '../constants';
import { useMarkdownDocument } from '../context/markdown-document-context';
@@ -100,6 +107,13 @@ export function MessageDocumentDiscussion(props: {
undefined,
{ equals: (a, b) => a?.key === b?.key }
);
+ // Clicking the linked message releases its highlight until the next
+ // navigation to a comment.
+ const [clearedKey, setClearedKey] = createSignal<string>();
+ const targetCleared = () => {
+ const key = scrollRequest()?.key;
+ return key !== undefined && key === clearedKey();
+ };
let scrolledKey: string | undefined;
createEffect(
on(scrollRequest, (request) => {
@@ -115,6 +129,8 @@ export function MessageDocumentDiscussion(props: {
parent={parent}
canWrite={permissions.canComment()}
targetId={params.commentId()}
+ targetCleared={targetCleared()}
+ onClearTarget={() => setClearedKey(scrollRequest()?.key)}
label={props.label}
buildLink={(message) =>
buildSimpleEntityUrl(
diff --git a/apps/web/src/features/block-md/component/MessageDocumentDiscussion.test.tsx b/apps/web/src/features/block-md/component/MessageDocumentDiscussion.test.tsx
index 7aa4beec4b..b977ca698b 100644
--- a/apps/web/src/features/block-md/component/MessageDocumentDiscussion.test.tsx
+++ b/apps/web/src/features/block-md/component/MessageDocumentDiscussion.test.tsx
@@ -46,6 +46,8 @@ vi.mock('@core/messages/DocumentConversation', () => ({
buildLink: (message: { id: string }) => string;
hideComposer?: boolean;
hideWhenEmpty?: boolean;
+ targetCleared?: boolean;
+ onClearTarget?: () => void;
}) => {
mocks.conversation(props);
return (
@@ -212,6 +214,23 @@ describe('scrolling to a linked Discussion message', () => {
expect(scrolledIds()).toEqual(['m1', 'm1']);
});
+ it('releases the linked message highlight until the next navigation', () => {
+ setRenderedMessageIds(['m1']);
+ setup();
+ navigate('m1');
+ const conversation = mocks.conversation.mock.calls[0][0] as {
+ targetCleared?: boolean;
+ onClearTarget: () => void;
+ };
+ expect(conversation.targetCleared).toBe(false);
+
+ conversation.onClearTarget();
+ expect(conversation.targetCleared).toBe(true);
+
+ navigate('m1');
+ expect(conversation.targetCleared).toBe(false);
+ });
+
it('does not steal scroll on later renders or when the value shows through again', async () => {
setRenderedMessageIds(['m1']);
setup();
diff --git a/apps/web/src/lib/core/comments/Thread.test.tsx b/apps/web/src/lib/core/comments/Thread.test.tsx
index 900952ee85..67c510aceb 100644
--- a/apps/web/src/lib/core/comments/Thread.test.tsx
+++ b/apps/web/src/lib/core/comments/Thread.test.tsx
@@ -58,6 +58,8 @@ vi.mock('@channel/Input/message-payload', () => ({
vi.mock('@core/messages/MessageThread', () => ({
MessageThreadById: (props: {
buildLink: (message: { id: string }) => string;
+ targetId?: string | null;
+ onClearTarget?: () => void;
}) => {
// Stands in for the message actions' delete confirmation, which the
// thread renders into a portal outside the card.
@@ -65,6 +67,12 @@ vi.mock('@core/messages/MessageThread', () => ({
return (
<>
<a href={props.buildLink({ id: 'comment-root' })}>Copy link</a>
+ <button
+ data-target={props.targetId ?? undefined}
+ onClick={() => props.onClearTarget?.()}
+ >
+ Linked comment
+ </button>
<button onClick={() => setConfirming(true)}>Delete</button>
<Dialog open={confirming()} onOpenChange={setConfirming}>
<Dialog.Portal>
@@ -188,6 +196,38 @@ describe('anchored comment links', () => {
}
);
+ it('releases the comment-link highlight when the highlighted comment is clicked', () => {
+ const [highlighted, setHighlighted] = createSignal<string | null>(
+ 'comment-root'
+ );
+ const view = render(() => (
+ <CommentsContext.Provider
+ value={{
+ documentId: 'document',
+ documentType: 'md',
+ canComment: () => true,
+ isDocumentOwner: () => true,
+ highlightedCommentId: highlighted,
+ clearHighlightedComment: () => setHighlighted(null),
+ setActiveThread: () => {},
+ setThreadHeight: () => {},
+ getCommentById: () => undefined,
+ ownedComment: () => false,
+ inComment: true,
+ commentOperations: noopCommentOperations,
+ messageOperations: { createComment: async () => null },
+ }}
+ >
+ <ThreadBody comment={comment} isActive />
+ </CommentsContext.Provider>
+ ));
+ const linked = view.getByRole('button', { name: 'Linked comment' });
+ expect(linked.dataset.target).toBe('comment-root');
+ fireEvent.click(linked);
+ expect(highlighted()).toBeNull();
+ expect(linked.dataset.target).toBeUndefined();
+ });
+
it('renders a PDF on the message API through the message thread', () => {
const view = renderThreadBody('pdf');
const url = new URL(view.getByRole('link').getAttribute('href')!);
diff --git a/apps/web/src/lib/core/comments/Thread.tsx b/apps/web/src/lib/core/comments/Thread.tsx
index f3d1a18cb9..7e233292e5 100644
--- a/apps/web/src/lib/core/comments/Thread.tsx
+++ b/apps/web/src/lib/core/comments/Thread.tsx
@@ -96,6 +96,8 @@ export type CommentsContextType = {
ownedComment: (id: CommentId) => boolean;
inComment: boolean;
highlightedCommentId: Accessor<CommentId | null>;
+ /** Releases the comment-link highlight when the highlighted comment is clicked. */
+ clearHighlightedComment?: () => void;
/**
* When set (the touch drawer), messages report their inline-edit state so
* the host can hide its pinned reply composer while an edit is open.
@@ -229,6 +231,7 @@ function MessageThreadBody(props: ThreadBodyProps) {
hideReplyInput={props.hideReplyInput}
onEditingChange={context.setMessageEditing}
targetId={targetId()}
+ onClearTarget={context.clearHighlightedComment}
buildLink={(message) =>
buildSimpleEntityUrl(
{ type: context.documentType, id: context.documentId },
diff --git a/apps/web/src/lib/core/messages/DocumentConversation.test.tsx b/apps/web/src/lib/core/messages/DocumentConversation.test.tsx
index 60b4900d2e..2295bce3d5 100644
--- a/apps/web/src/lib/core/messages/DocumentConversation.test.tsx
+++ b/apps/web/src/lib/core/messages/DocumentConversation.test.tsx
@@ -48,8 +48,15 @@ vi.mock('@queries/messages/timeline', () => ({
useMessageTimelineQuery: mocks.timeline,
}));
vi.mock('./MessageThread', () => ({
- MessageThread: (props: { data: MessageListItem }) => (
- <article>
+ MessageThread: (props: {
+ data: MessageListItem;
+ targetId?: string | null;
+ onClearTarget?: () => void;
+ }) => (
+ <article
+ data-target={props.targetId ?? undefined}
+ onClick={() => props.onClearTarget?.()}
+ >
{props.data.content}
<For each={props.data.thread.preview}>
{(reply) => <p>{reply.content}</p>}
@@ -227,3 +234,28 @@ describe('DocumentConversation placement', () => {
]);
});
});
+
+describe('DocumentConversation linked message highlight', () => {
+ it('releases the highlight while keeping the linked view around its root', () => {
+ const [cleared, setCleared] = createSignal(false);
+ mocks.timeline.mockReturnValue({
+ isSuccess: true,
+ data: { pages: [{ items: [thread('root-of-reply', null)] }] },
+ });
+ const view = render(() => (
+ <DocumentConversation
+ parent={{ type: 'document', id: 'document' }}
+ canWrite={false}
+ targetId="reply"
+ targetCleared={cleared()}
+ onClearTarget={() => setCleared(true)}
+ />
+ ));
+ const article = view.getByRole('article');
+ expect(article.dataset.target).toBe('reply');
+ article.click();
+ expect(article.dataset.target).toBeUndefined();
+ const [, around] = mocks.timeline.mock.calls.at(-1)!;
+ expect(around()).toBe('root-of-reply');
+ });
+});
diff --git a/apps/web/src/lib/core/messages/DocumentConversation.tsx b/apps/web/src/lib/core/messages/DocumentConversation.tsx
index 65d6d53f7d..32bc246a34 100644
--- a/apps/web/src/lib/core/messages/DocumentConversation.tsx
+++ b/apps/web/src/lib/core/messages/DocumentConversation.tsx
@@ -54,6 +54,9 @@ export function DocumentConversation(props: {
parent: MessageParent;
canWrite: boolean;
targetId?: string | null;
+ /** The linked view stays around `targetId`, but its message is no longer highlighted. */
+ targetCleared?: boolean;
+ onClearTarget?: () => void;
buildLink?: (message: MessageData) => string;
label?: string;
/** The composer is rendered elsewhere, such as a floating mobile accessory. */
@@ -121,7 +124,12 @@ export function DocumentConversation(props: {
<MessageThread
data={messagesById().get(id)!}
canWrite={props.canWrite}
- targetId={target.rootId() === id ? target.messageId() : null}
+ targetId={
+ target.rootId() === id && !props.targetCleared
+ ? target.messageId()
+ : null
+ }
+ onClearTarget={props.onClearTarget}
buildLink={props.buildLink}
/>
)}
diff --git a/apps/web/src/lib/core/messages/MessageThread.test.tsx b/apps/web/src/lib/core/messages/MessageThread.test.tsx
index 2a5f13a18c..f002e0e49e 100644
--- a/apps/web/src/lib/core/messages/MessageThread.test.tsx
+++ b/apps/web/src/lib/core/messages/MessageThread.test.tsx
@@ -5,7 +5,7 @@ import type {
MessageThread as ThreadData,
} from '@service-storage/messages';
import { cleanup, fireEvent, render } from '@solidjs/testing-library';
-import { Show } from 'solid-js';
+import { createSignal, Show } from 'solid-js';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { MessageThread, threadListItem } from './MessageThread';
@@ -70,6 +70,12 @@ vi.mock('@channel/Thread/ChannelThread', () => ({
>
Long press message
</button>
+ <button
+ onClick={() => props.targetNavigation?.onClearTarget(props.data().id)}
+ >
+ Click linked message
+ </button>
+ <p>{props.isExpanded() ? 'expanded' : 'collapsed'}</p>
</>
);
},
@@ -179,6 +185,26 @@ describe('document discussion controls', () => {
});
});
+describe('linked message highlight', () => {
+ it('releases the highlight when the linked message is clicked, keeping the thread expanded', () => {
+ const [targetId, setTargetId] = createSignal<string | null>('root');
+ const clearTarget = vi.fn(() => setTargetId(null));
+ const view = render(() => (
+ <MessageThread
+ data={message}
+ canWrite
+ targetId={targetId()}
+ onClearTarget={clearTarget}
+ />
+ ));
+ expect(view.getByText('expanded')).toBeTruthy();
+ fireEvent.click(view.getByRole('button', { name: 'Click linked message' }));
+ expect(clearTarget).toHaveBeenCalledOnce();
+ expect(targetId()).toBeNull();
+ expect(view.getByText('expanded')).toBeTruthy();
+ });
+});
+
describe('threadListItem', () => {
const reply = (id: string, createdAt: string) =>
({
diff --git a/apps/web/src/lib/core/messages/MessageThread.tsx b/apps/web/src/lib/core/messages/MessageThread.tsx
index 3843835470..ad15c74100 100644
--- a/apps/web/src/lib/core/messages/MessageThread.tsx
+++ b/apps/web/src/lib/core/messages/MessageThread.tsx
@@ -42,6 +42,8 @@ type ThreadOptions = {
canWrite: boolean;
buildLink?: (message: MessageData) => string;
targetId?: string | null;
+ /** Releases the highlight on `targetId`; called when the linked message is clicked. */
+ onClearTarget?: () => void;
expanded?: boolean;
hideReplyInput?: boolean;
onEditingChange?: (id: string, editing: boolean) => void;
@@ -138,7 +140,12 @@ export function MessageThread(
},
onTargetMessageScrolled: () => {},
onTargetReplyScrolled: () => {},
- onClearTarget: () => {},
+ onClearTarget: () => {
+ // The link expanded the thread; releasing its highlight keeps
+ // the thread open rather than collapsing the replies in view.
+ setExpanded(true);
+ props.onClearTarget?.();
+ },
}}
/>
</div>