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(web): render document comments without block context (#6703)

gbirmanbeb969afae43
diff --git a/apps/web/src/features/block-md/comments/CommentMargin.tsx b/apps/web/src/features/block-md/comments/CommentMargin.tsx
index c3ba67ecbe..2e4bae51d3 100644
--- a/apps/web/src/features/block-md/comments/CommentMargin.tsx
+++ b/apps/web/src/features/block-md/comments/CommentMargin.tsx
@@ -57,7 +57,8 @@ function useCommentOperations(): Pick<
const useCommentsContext = (
setThreadHeight: CommentsContextType['setThreadHeight']
): CommentsContextType => {
- const { documentId, permissions, state } = useMarkdownDocument();
+ const { documentId, kind, permissions, state } = useMarkdownDocument();
+ const documentKind = kind();
const { comments: commentState, setCommentState } = state;
const ownedCommentIds = createMemo(() => {
const userId = useUserId()();
@@ -88,6 +89,7 @@ const useCommentsContext = (
isDocumentOwner: permissions.isOwner,
getCommentById,
documentId: documentId(),
+ documentType: documentKind === 'document' ? 'md' : documentKind,
ownedComment: ownedCommentSelector,
...operations,
inComment: true,
diff --git a/apps/web/src/features/block-pdf/component/RightMarginLayout.tsx b/apps/web/src/features/block-pdf/component/RightMarginLayout.tsx
index e749055576..5bfb1b956e 100644
--- a/apps/web/src/features/block-pdf/component/RightMarginLayout.tsx
+++ b/apps/web/src/features/block-pdf/component/RightMarginLayout.tsx
@@ -73,6 +73,7 @@ const useCommentsContext = (
isDocumentOwner: pdf.permissions.isOwner,
getCommentById,
documentId: pdf.documentId(),
+ documentType: 'pdf',
ownedComment,
commentOperations: {
createComment,
diff --git a/apps/web/src/lib/core/comments/Comment.tsx b/apps/web/src/lib/core/comments/Comment.tsx
index a2a33ab083..d326d5d1a8 100644
--- a/apps/web/src/lib/core/comments/Comment.tsx
+++ b/apps/web/src/lib/core/comments/Comment.tsx
@@ -1,5 +1,4 @@
import { URL_PARAMS as MD_URL_PARAMS } from '@block-md/constants';
-import { useMaybeBlockAliasedName } from '@core/block';
import { StaticMarkdown } from '@core/component/LexicalMarkdown/component/core/StaticMarkdown';
import { toast } from '@core/component/Toast/Toast';
import { useAuthor } from '@core/context/user';
@@ -75,7 +74,6 @@ export function Comment(
actionsDropdown?: boolean;
}>
) {
- const maybeBlockName = useMaybeBlockAliasedName();
const commentsContext = useContext(CommentsContext);
const {
@@ -112,24 +110,21 @@ export function Comment(
// drawer paging to another thread with an edit open).
onCleanup(() => setMessageEditing?.(props.comment.id, false));
- const copyLink = () => {
- if (!maybeBlockName) return;
- return async () => {
- const params: Record<string, string> = {};
- if (maybeBlockName === 'task' || maybeBlockName === 'md') {
- params[MD_URL_PARAMS.commentId] = props.comment.id.toString();
- }
- try {
- const url = buildSimpleEntityUrl(
- { type: maybeBlockName, id: commentsContext.documentId },
- params
- );
- await navigator.clipboard.writeText(url);
- toast.success('Link copied to clipboard');
- } catch (_) {
- toast.failure('Could not copy link');
- }
- };
+ const copyLink = async () => {
+ const params: Record<string, string> = {};
+ if (commentsContext.documentType !== 'pdf') {
+ params[MD_URL_PARAMS.commentId] = props.comment.id.toString();
+ }
+ try {
+ const url = buildSimpleEntityUrl(
+ { type: commentsContext.documentType, id: commentsContext.documentId },
+ params
+ );
+ await navigator.clipboard.writeText(url);
+ toast.success('Link copied to clipboard');
+ } catch (_) {
+ toast.failure('Could not copy link');
+ }
};
const mentionsSignal = useContext(ThreadContext).mentionsSignal;
@@ -164,7 +159,7 @@ export function Comment(
setActiveThread(props.comment.threadId);
setIsEditing(true);
}}
- copyLink={copyLink()}
+ copyLink={copyLink}
/>
<CommentText text={props.comment.text} />
{props.children}
@@ -228,32 +223,29 @@ export function CommentReply(
getCommentById,
highlightedCommentId,
documentId,
+ documentType,
setMessageEditing,
} = useContext(CommentsContext);
- const maybeBlockName = useMaybeBlockAliasedName();
const reply = createMemo(() => getCommentById(props.replyId));
const isHighlighted = createMemo(
() => highlightedCommentId() === props.replyId
);
- const copyLink = () => {
- if (!maybeBlockName) return;
- return async () => {
- const params: Record<string, string> = {};
- if (maybeBlockName === 'task' || maybeBlockName === 'md') {
- params[MD_URL_PARAMS.commentId] = props.replyId.toString();
- }
- try {
- const url = buildSimpleEntityUrl(
- { type: maybeBlockName, id: documentId },
- params
- );
- await navigator.clipboard.writeText(url);
- toast.success('Link copied to clipboard');
- } catch (_) {
- toast.failure('Could not copy link');
- }
- };
+ const copyLink = async () => {
+ const params: Record<string, string> = {};
+ if (documentType !== 'pdf') {
+ params[MD_URL_PARAMS.commentId] = props.replyId.toString();
+ }
+ try {
+ const url = buildSimpleEntityUrl(
+ { type: documentType, id: documentId },
+ params
+ );
+ await navigator.clipboard.writeText(url);
+ toast.success('Link copied to clipboard');
+ } catch (_) {
+ toast.failure('Could not copy link');
+ }
};
const [isEditing, setIsEditingSignal] = createSignal<boolean>(false);
@@ -292,7 +284,7 @@ export function CommentReply(
isResolved={false}
deleteMessage={props.deleteReply}
enableEditing={() => setIsEditing(true)}
- copyLink={copyLink()}
+ copyLink={copyLink}
hideBottomMargin
isOwned={props.isOwned}
isActive={props.isActive}
diff --git a/apps/web/src/lib/core/comments/Thread.test.tsx b/apps/web/src/lib/core/comments/Thread.test.tsx
index 4b7d7de2d9..fb26a48f72 100644
--- a/apps/web/src/lib/core/comments/Thread.test.tsx
+++ b/apps/web/src/lib/core/comments/Thread.test.tsx
@@ -1,15 +1,17 @@
import { URL_PARAMS as markdownParams } from '@block-md/constants';
-import { cleanup, render } from '@solidjs/testing-library';
+import { cleanup, fireEvent, render, waitFor } from '@solidjs/testing-library';
import type { ParentProps } from 'solid-js';
-import { afterEach, describe, expect, it, vi } from 'vitest';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { Root } from './commentType';
import { MinimizedThread } from './MinimizedThreads';
-import { CommentsContext, noopCommentOperations, ThreadBody } from './Thread';
+import {
+ CommentsContext,
+ type CommentsContextType,
+ noopCommentOperations,
+ ThreadBody,
+} from './Thread';
-const mocks = vi.hoisted(() => ({ blockName: 'md' }));
-vi.mock('@core/block', () => ({
- useBlockAliasedName: () => mocks.blockName,
-}));
+const mocks = vi.hoisted(() => ({ unifiedDiscussions: true }));
vi.mock('@core/util/url', () => ({
buildSimpleEntityUrl: (
entity: { type: string; id: string },
@@ -17,9 +19,14 @@ vi.mock('@core/util/url', () => ({
) =>
`https://macro.test/app/${entity.type}/${entity.id}?${new URLSearchParams(params)}`,
}));
-vi.mock('./Comment', () => ({
- Comment: () => null,
- CommentReply: () => null,
+vi.mock('@core/context/user', () => ({ useAuthor: () => () => 'user' }));
+vi.mock('@core/component/Toast/Toast', () => ({
+ toast: { success: vi.fn(), failure: vi.fn() },
+}));
+vi.mock('./MessageTopRow', () => ({
+ MessageTopRow: (props: { copyLink?: () => Promise<void> }) => (
+ <button onClick={props.copyLink}>Copy comment link</button>
+ ),
}));
vi.mock('./Inputs', () => ({
EditInput: () => null,
@@ -27,7 +34,7 @@ vi.mock('./Inputs', () => ({
}));
vi.mock('@core/constant/featureFlags', async (importOriginal) => ({
...(await importOriginal<typeof import('@core/constant/featureFlags')>()),
- isFeatureEnabled: () => true,
+ isFeatureEnabled: () => mocks.unifiedDiscussions,
}));
vi.mock('@channel/Input', () => ({ ChannelInput: () => null }));
vi.mock('@queries/contacts/contacts', () => ({ useContacts: () => () => [] }));
@@ -43,6 +50,7 @@ vi.mock(
'@core/component/LexicalMarkdown/component/core/StaticMarkdown',
() => ({
StaticMarkdownContext: (props: ParentProps) => props.children,
+ StaticMarkdown: (props: { markdown: string }) => <p>{props.markdown}</p>,
})
);
vi.mock('@ui', () => ({
@@ -53,7 +61,16 @@ vi.mock('./MeasureContainer', () => ({
MeasureContainer: (props: ParentProps) => props.children,
}));
-afterEach(cleanup);
+const writeText = vi.fn();
+beforeEach(() => {
+ mocks.unifiedDiscussions = true;
+ writeText.mockReset();
+ vi.stubGlobal('navigator', { clipboard: { writeText } });
+});
+afterEach(() => {
+ cleanup();
+ vi.unstubAllGlobals();
+});
const comment: Root = {
id: 'comment-root',
@@ -85,34 +102,45 @@ describe('anchored comment links', () => {
expect(view.getByText('9')).toBeTruthy();
expect(view.queryByText('4')).toBeNull();
});
- const renderThreadBody = () =>
+ const renderThreadBody = (
+ documentType: CommentsContextType['documentType'] = 'md',
+ minimized = false
+ ) =>
render(() => (
<CommentsContext.Provider
value={{
documentId: 'document',
+ documentType,
canComment: () => true,
isDocumentOwner: () => true,
highlightedCommentId: () => null,
setActiveThread: () => {},
setThreadHeight: () => {},
- getCommentById: () => undefined,
+ getCommentById: () => ({ ...comment, id: 'reply', text: 'Reply' }),
ownedComment: () => false,
inComment: true,
commentOperations: noopCommentOperations,
messageOperations: { createComment: async () => null },
}}
>
- <ThreadBody comment={comment} isActive />
+ {minimized ? (
+ <MinimizedThread
+ comment={comment}
+ layout={{ calculatedYPos: 0 }}
+ isActive={false}
+ />
+ ) : (
+ <ThreadBody comment={{ ...comment, children: ['reply'] }} isActive />
+ )}
</CommentsContext.Provider>
));
- it.each(['md', 'task'])(
- 'uses the parameter read by document navigation for %s',
- (blockName) => {
- mocks.blockName = blockName;
- const view = renderThreadBody();
+ it.each(['md', 'task', 'snippet', 'skill'] as const)(
+ 'renders %s comment links without a block provider',
+ (documentType) => {
+ const view = renderThreadBody(documentType);
const url = new URL(view.getByRole('link').getAttribute('href')!);
- expect(url.pathname).toBe(`/app/${blockName}/document`);
+ expect(url.pathname).toBe(`/app/${documentType}/document`);
expect(url.searchParams.get(markdownParams.commentId)).toBe(
'comment-root'
);
@@ -121,9 +149,39 @@ describe('anchored comment links', () => {
);
it('keeps PDF on the legacy path while its flag-on discussion is deferred', () => {
- mocks.blockName = 'pdf';
- const view = renderThreadBody();
+ const view = renderThreadBody('pdf');
// The message thread (mocked as the copy-link anchor) is markdown-only.
expect(view.queryByRole('link')).toBeNull();
+ expect(view.getByText('Comment')).toBeTruthy();
+ });
+
+ it('expands a minimized comment in a document detail without a block provider', () => {
+ const view = renderThreadBody('md', true);
+ expect(view.queryByRole('link')).toBeNull();
+ fireEvent.click(view.getByText('1'));
+ expect(view.getByRole('link')).toBeTruthy();
});
+
+ it.each(['md', 'task', 'snippet', 'skill', 'pdf'] as const)(
+ 'copies legacy %s root and reply links without a block provider',
+ async (documentType) => {
+ mocks.unifiedDiscussions = false;
+ const view = renderThreadBody(documentType);
+ expect(view.getByText('Comment')).toBeTruthy();
+ expect(view.getByText('Reply')).toBeTruthy();
+
+ const buttons = view.getAllByRole('button', {
+ name: 'Copy comment link',
+ });
+ for (const [index, id] of ['comment-root', 'reply'].entries()) {
+ fireEvent.click(buttons[index]);
+ await waitFor(() => expect(writeText).toHaveBeenCalledTimes(index + 1));
+ const url = new URL(writeText.mock.calls[index][0]);
+ expect(url.pathname).toBe(`/app/${documentType}/document`);
+ expect(url.searchParams.get(markdownParams.commentId)).toBe(
+ documentType === 'pdf' ? null : id
+ );
+ }
+ }
+ );
});
diff --git a/apps/web/src/lib/core/comments/Thread.tsx b/apps/web/src/lib/core/comments/Thread.tsx
index de30c5083a..347e5b2e8e 100644
--- a/apps/web/src/lib/core/comments/Thread.tsx
+++ b/apps/web/src/lib/core/comments/Thread.tsx
@@ -1,7 +1,6 @@
import { URL_PARAMS as MD_URL_PARAMS } from '@block-md/constants';
import { ChannelInput } from '@channel/Input';
import { buildPostMessageSendPayload } from '@channel/Input/message-payload';
-import { useBlockAliasedName } from '@core/block';
import { StaticMarkdownContext } from '@core/component/LexicalMarkdown/component/core/StaticMarkdown';
import { createTheme } from '@core/component/LexicalMarkdown/theme';
import type { UserMentionRecord } from '@core/component/LexicalMarkdown/utils/mentionsUtils';
@@ -88,6 +87,7 @@ export type CommentsContextType = {
messageOperations?: MessageCommentOperations;
getCommentById: (id: CommentId) => Root | Reply | undefined;
documentId: string;
+ documentType: 'md' | 'task' | 'snippet' | 'skill' | 'pdf';
ownedComment: (id: CommentId) => boolean;
inComment: boolean;
highlightedCommentId: Accessor<CommentId | null>;
@@ -113,6 +113,7 @@ export const CommentsContext = createContext<CommentsContextType>({
commentOperations: noopCommentOperations,
getCommentById: (_id) => undefined,
documentId: '',
+ documentType: 'md',
ownedComment: () => false,
inComment: false,
highlightedCommentId: () => null,
@@ -143,9 +144,9 @@ type ThreadBodyProps = {
export function ThreadBody(props: ThreadBodyProps) {
// PDF flag-on discussions are deferred, so PDF stays on the legacy path even
// when the flag is on; only markdown documents use the message thread.
- const blockName = useBlockAliasedName();
+ const context = useContext(CommentsContext);
return isFeatureEnabled(enableUnifiedDocumentDiscussions) &&
- blockName !== 'pdf' ? (
+ context.documentType !== 'pdf' ? (
<MessageThreadBody {...props} />
) : (
<LegacyThreadBody {...props} />
@@ -155,7 +156,6 @@ export function ThreadBody(props: ThreadBodyProps) {
/** Document threads render the shared message thread; a draft composes its root. */
function MessageThreadBody(props: ThreadBodyProps) {
const context = useContext(CommentsContext);
- const blockName = useBlockAliasedName();
// Workspace users for @-mentions, matching the legacy comment composer.
const participants = useContacts();
const parent = () => ({ type: 'document' as const, id: context.documentId });
@@ -197,7 +197,7 @@ function MessageThreadBody(props: ThreadBodyProps) {
targetId={targetId()}
buildLink={(message) =>
buildSimpleEntityUrl(
- { type: blockName, id: context.documentId },
+ { type: context.documentType, id: context.documentId },
{ [MD_URL_PARAMS.commentId]: message.id }
)
}
diff --git a/docs/AGENT_GUIDE/documents.md b/docs/AGENT_GUIDE/documents.md
index 8f7f2a98b0..f4408ff1ae 100644
--- a/docs/AGENT_GUIDE/documents.md
+++ b/docs/AGENT_GUIDE/documents.md
@@ -432,6 +432,12 @@ keeps at least 16px of
bottom clearance above the drawer's curve, including while the keyboard is open,
and accounts for the home-indicator safe area when the keyboard is closed.
+Also verify anchored comments in Drive's detail pane: open a document with
+existing text anchors, then click a numbered comment badge to expand it. The
+document should stay visible and the thread should open; loading the document
+with its badges still collapsed does not exercise thread rendering. Comment
+copy links should retain the document/task route and the selected comment.
+
### Unified document discussions (`enable-unified-document-discussions`)
With the PostHog flag `enable-unified-document-discussions` on (locally