diff --git a/web/chat/tooltip-utils.js b/web/chat/tooltip-utils.js --- a/web/chat/tooltip-utils.js +++ b/web/chat/tooltip-utils.js @@ -3,6 +3,15 @@ import invariant from 'invariant'; import * as React from 'react'; +import type { ChatMessageInfoItem } from 'lib/selectors/chat-selectors'; +import { useSidebarExistsOrCanBeCreated } from 'lib/shared/thread-utils'; +import type { ThreadInfo } from 'lib/types/thread-types'; + +import CommIcon from '../CommIcon.react'; +import { + useOnClickPendingSidebar, + useOnClickThread, +} from '../selectors/nav-selectors'; import { calculateMaxTextWidth } from '../utils/text-utils'; import type { PositionInfo } from './position-types'; @@ -298,9 +307,50 @@ }; } +function useMessageTooltipSidebarAction( + item: ChatMessageInfoItem, + threadInfo: ThreadInfo, +): ?MessageTooltipAction { + const { threadCreatedFromMessage, messageInfo } = item; + const sidebarExists = !!threadCreatedFromMessage; + const sidebarExistsOrCanBeCreated = useSidebarExistsOrCanBeCreated( + threadInfo, + item, + ); + const onThreadOpen = useOnClickThread(threadCreatedFromMessage); + const onPendingSidebarOpen = useOnClickPendingSidebar( + messageInfo, + threadInfo, + ); + return React.useMemo(() => { + if (!sidebarExistsOrCanBeCreated) { + return null; + } + const buttonContent = ; + const onClick = (event: SyntheticEvent) => { + if (threadCreatedFromMessage) { + onThreadOpen(event); + } else { + onPendingSidebarOpen(event); + } + }; + return { + actionButtonContent: buttonContent, + onClick, + label: sidebarExists ? 'Go to thread' : 'Create thread', + }; + }, [ + onPendingSidebarOpen, + onThreadOpen, + sidebarExists, + sidebarExistsOrCanBeCreated, + threadCreatedFromMessage, + ]); +} export { findTooltipPosition, calculateTooltipSize, getMessageActionTooltipStyle, + useMessageTooltipSidebarAction, sizeOfTooltipArrow, };