Skip to content

Commit

Permalink
Merge pull request #2320 from MahtabBukhari/resolved_cutting_off_the_…
Browse files Browse the repository at this point in the history
…tooltip_at_AI_answer

Ensure hover modal is NOT cut off
  • Loading branch information
Rassl authored Oct 17, 2024
2 parents 881e6df + b9233ee commit 413a9df
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 38 deletions.
35 changes: 35 additions & 0 deletions src/components/App/SideBar/AiSummary/AiAnswer/__tests__/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import React from 'react'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import { AiAnswer } from '../index'
import { ExtractedEntity } from '~/types/index'

describe('AiAnswer Component', () => {
const mockHandleLoaded = jest.fn()

const renderComponent = (answer, entities) => {
render(<AiAnswer answer={answer} entities={entities} handleLoaded={mockHandleLoaded} hasBeenRendered={false} />)
}

it('should display shows tooltip on hover over entity', async () => {
const entities: ExtractedEntity[] = [
{
entity: 'React',
description: 'A JavaScript library for building user interfaces.',
},
]

renderComponent('Learn about React and its features.', entities)

await waitFor(() => expect(screen.getByText(/React/)).toBeInTheDocument())

const reactEntity = screen.getByText(
(content, element) => content.startsWith('React') && element.tagName === 'SPAN',
)

fireEvent.mouseOver(reactEntity)

const tooltip = await screen.findByText('A JavaScript library for building user interfaces.')

expect(tooltip).toBeVisible()
})
})
5 changes: 1 addition & 4 deletions src/components/App/SideBar/AiSummary/AiAnswer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export const AiAnswer = ({ answer, entities, handleLoaded, hasBeenRendered }: Pr
const { setBudget } = useUserStore((s) => s)
const [displayedText, setDisplayedText] = useState('')
const [highlightedEntities, setHighlightedEntities] = useState<ExtractedEntity[] | undefined>(entities)
const [mousePosition, setMousePosition] = useState(0)
const [isDescriptionComplete, setIsDescriptionComplete] = useState(true)

useEffect(() => {
Expand Down Expand Up @@ -78,15 +77,13 @@ export const AiAnswer = ({ answer, entities, handleLoaded, hasBeenRendered }: Pr
}
}, [entities, highlightedEntities])

const handleMouseMove = (event: React.MouseEvent<HTMLDivElement, MouseEvent>) => {
const handleMouseMove = () => {
setIsDescriptionComplete(false)
setMousePosition(event.clientX)
}

const responseTextDisplay = highlightAiSummary(
displayedText,
handleSubmit,
mousePosition,
highlightedEntities,
isDescriptionComplete,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Tooltip from '@mui/material/Tooltip'
import styled, { keyframes } from 'styled-components'
import { Tooltip } from '~/components/common/ToolTip'
import { ExtractedEntity } from '~/types'
import { colors } from '~/utils'

Expand Down Expand Up @@ -41,7 +41,6 @@ const Highlight = styled.span<{ animate: boolean }>`
export function highlightAiSummary(
sDescription: string,
handleSubmit: (search: string) => void,
mousePosition: number,
entities?: ExtractedEntity[],
isDescriptionComplete?: boolean,
) {
Expand All @@ -59,16 +58,6 @@ export function highlightAiSummary(

const parts = sDescription.split(regex)

let positionLeft: string

if (mousePosition <= 180) {
positionLeft = '145%'
} else if (mousePosition >= 250) {
positionLeft = '10%'
} else {
positionLeft = '50%'
}

return (
<>
{parts.map((part, index) => {
Expand All @@ -78,7 +67,7 @@ export function highlightAiSummary(
const uniqueKey = `${entity.entity}-${index}`

return (
<StyledTooltip key={uniqueKey} content={entity.description} positionLeft={positionLeft}>
<StyledTooltip key={uniqueKey} title={entity.description}>
<Highlight animate={!!isDescriptionComplete} onClick={() => handleSubmit(part)}>
{part}
</Highlight>
Expand All @@ -96,23 +85,17 @@ function escapeRegExp(string: string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}

const StyledTooltip = styled(({ className, positionLeft, ...props }) => (
<Tooltip
{...props}
backgroundColor={colors.BG2}
borderRadius="6px"
className={className}
color="white"
fontSize="12px"
fontWeight="500"
minWidth="160px"
mrLeft={positionLeft}
padding="10px"
textAlign="start"
whiteSpace="normal"
/>
))`
& .tooltip-content {
const StyledTooltip = styled((props) => <Tooltip classes={{ popper: props.className }} {...props} />)`
& .MuiTooltip-tooltip {
background-color: ${colors.BG4};
color: white;
font-family: Barlow;
font-size: 14px;
font-weight: 500;
max-width: 180px;
padding: 10px;
transition: opacity 0.3s;
text-align: start;
white-space: normal;
}
`
4 changes: 0 additions & 4 deletions src/components/common/ToolTip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ interface TooltipProps {
minWidth?: string
whiteSpace?: string
textAlign?: string
mrLeft?: string
}

const TooltipContainer = styled.div`
Expand All @@ -35,7 +34,6 @@ const TooltipText = styled.div<{
minWidth?: string
whiteSpace?: string
textAlign?: string
mrLeft?: string
}>`
visibility: hidden;
width: auto;
Expand Down Expand Up @@ -86,7 +84,6 @@ export const Tooltip = ({
whiteSpace,
position,
textAlign,
mrLeft,
}: TooltipProps) => (
<TooltipContainer>
{children}
Expand All @@ -98,7 +95,6 @@ export const Tooltip = ({
fontWeight={fontWeight}
margin={margin}
minWidth={minWidth}
mrLeft={mrLeft}
padding={padding}
position={position}
textAlign={textAlign}
Expand Down
1 change: 1 addition & 0 deletions src/utils/colors/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const colors = {
BG2: 'rgba(22, 23, 29, 1)',
BG2_ACTIVE_INPUT: 'rgba(16, 17, 22, 1)',
BG3: 'rgba(28, 30, 38, 1)',
BG4: 'rgba(30, 30, 35, 0.9)',
GRAY3: 'rgba(186, 193, 198, 1)',
GRAY6: '#909BAA',
GRAY7: 'rgba(107, 122, 141, 1)',
Expand Down

0 comments on commit 413a9df

Please sign in to comment.