Skip to content

Commit

Permalink
Merge pull request #3250 from tloncorp/po/land-1409-clicking-links-in…
Browse files Browse the repository at this point in the history
…-refs-should-not-cause-navigate-to-ref

refs: fix issue with clicking links in refs
  • Loading branch information
patosullivan authored Feb 15, 2024
2 parents 19978f8 + 5a700c6 commit 9947fb2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 11 deletions.
38 changes: 36 additions & 2 deletions ui/src/chat/ChatContent/ChatContent.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect } from 'react';
import { findLastIndex } from 'lodash';
import cn from 'classnames';
import { Link } from 'react-router-dom';
Expand Down Expand Up @@ -34,6 +34,8 @@ interface ChatContentProps {
isScrolling?: boolean;
className?: string;
writId?: string;
onClick?: (e: React.MouseEvent<HTMLAnchorElement>) => void;
isInReference?: boolean;
}

interface InlineContentProps {
Expand Down Expand Up @@ -207,6 +209,8 @@ function ChatContent({
isScrolling = false,
className = '',
writId = 'not-writ',
onClick,
isInReference,
}: ChatContentProps) {
const storyInlines = (
story.filter((s) => 'inline' in s) as VerseInline[]
Expand All @@ -227,8 +231,38 @@ function ChatContent({
return 0;
});

useEffect(() => {
// If we have an onClick handler (as we would in the case of this component
// being use in a reference), we need to add a click listener to the chat
// content. This is to prevent the click handler from firing when clicking
// on a link within the chat content.
// This will *NOT* work if you just pass the onClick handler directly to the
// chat content. You need to wrap it in a function that checks the target
// element.

if (onClick) {
const handleClick = (e: MouseEvent) => {
const target = e.target as HTMLElement | null;

if (target && target.id === `${writId}-chat-content`) {
onClick(e as unknown as React.MouseEvent<HTMLAnchorElement>);
}
};
document.addEventListener('click', handleClick);
return () => {
document.removeEventListener('click', handleClick);
};
}

return () => ({});
}, [onClick, writId]);

return (
<div className={cn('leading-6', className)}>
<div
data-in-reference={isInReference}
id={`${writId}-chat-content`}
className={cn('leading-6', className)}
>
{blockLength > 0 ? (
<>
{blockContent
Expand Down
20 changes: 11 additions & 9 deletions ui/src/components/References/WritBaseReference.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,13 @@ function WritBaseReference({

if (contextApp === 'heap-comment') {
return (
<div
onClick={handleOpenReferenceClick}
className="cursor-pointer rounded-lg border-2 border-gray-50 text-base"
>
<div className="cursor-pointer rounded-lg border-2 border-gray-50 text-base">
<ReferenceInHeap type="text" contextApp={contextApp}>
<ChatContent
className="line-clamp-1 p-2"
story={content}
isScrolling={false}
onClick={handleOpenReferenceClick}
/>
{children}
<ReferenceBar
Expand All @@ -192,11 +190,15 @@ function WritBaseReference({
'mb-2': isReply,
})}
>
<div
onClick={handleOpenReferenceClick}
className={'cursor-pointer p-2 group-hover:bg-gray-50'}
>
<ChatContent className="p-2" story={content} isScrolling={false} />
<div className={'cursor-pointer p-2 group-hover:bg-gray-50'}>
<ChatContent
className="p-2"
story={content}
isScrolling={false}
onClick={handleOpenReferenceClick}
writId={noteId}
isInReference
/>
</div>
<ReferenceBar
nest={nest}
Expand Down

0 comments on commit 9947fb2

Please sign in to comment.