Skip to content

Commit

Permalink
Fix reply counter color in hovered comment indicator (#4772)
Browse files Browse the repository at this point in the history
* Fix reply counter color in hovered comment indicator

* remove avatar error handling logic
  • Loading branch information
gbalint authored Jan 25, 2024
1 parent 25260fa commit cf3c150
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
21 changes: 12 additions & 9 deletions editor/src/components/canvas/controls/comment-indicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -336,10 +336,6 @@ const CommentIndicator = React.memo(({ thread }: CommentIndicatorProps) => {
],
)

// This is a hack: when the comment is unread, we show a dark background, so we need light foreground colors.
// So we trick the Liveblocks Comment component and lie to it that the theme is dark mode.
const dataThemeProp = isRead ? {} : { 'data-theme': 'dark' }

const style = useIndicatorStyle(dragPosition ?? location, {
isRead: isRead,
resolved: thread.metadata.resolved,
Expand All @@ -366,7 +362,7 @@ const CommentIndicator = React.memo(({ thread }: CommentIndicatorProps) => {
overflow: 'auto',
background: 'transparent',
}}
{...dataThemeProp}
forceDarkMode={!isRead}
/>
</div>
)
Expand Down Expand Up @@ -572,10 +568,11 @@ function useHover() {
type CommentIndicatorWrapper = {
thread: ThreadData<ThreadMetadata>
expanded: boolean
forceDarkMode: boolean
} & CommentWrapperProps

const CommentIndicatorWrapper = React.memo((props: CommentIndicatorWrapper) => {
const { thread, expanded, user, ...commentProps } = props
const { thread, expanded, user, forceDarkMode, ...commentProps } = props

const [avatarRef, animateAvatar] = useAnimate()

Expand All @@ -594,8 +591,14 @@ const CommentIndicatorWrapper = React.memo((props: CommentIndicatorWrapper) => {
)
}, [expanded, avatarRef, animateAvatar])

// This is a hack: when the comment is unread, we show a dark background, so we need light foreground colors.
// So we trick the Liveblocks Comment component and lie to it that the theme is dark mode.
const updatedCommentProps = forceDarkMode
? { ...commentProps, 'data-theme': 'dark' }
: commentProps

if (user == null) {
return <Comment {...commentProps} />
return <Comment {...updatedCommentProps} />
}

return (
Expand Down Expand Up @@ -643,8 +646,8 @@ const CommentIndicatorWrapper = React.memo((props: CommentIndicatorWrapper) => {
}}
transition={{ duration: animDuration }}
>
<Comment {...commentProps} />
<CommentRepliesCounter thread={thread} />
<Comment {...updatedCommentProps} />
<CommentRepliesCounter thread={thread} forceDarkMode={forceDarkMode} />
</motion.div>,
)}
</AnimatePresence>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,39 @@ import React from 'react'
import type { ThreadData } from '@liveblocks/client'
import type { ThreadMetadata } from '../../../../liveblocks.config'
import { useColorTheme } from '../../../uuiui'
import { darkColorThemeCssVariables } from '../../../uuiui/styles/theme/utopia-theme'
import { dark } from '../../../uuiui/styles/theme/dark'
import { useTheme } from '@emotion/react'
import { Substores, useEditorState } from '../../editor/store/store-hook'
import { getCurrentTheme } from '../../editor/store/editor-state'

interface CommentRepliesCounterProps {
thread: ThreadData<ThreadMetadata>
forceDarkMode?: boolean
}

export const CommentRepliesCounter = React.memo((props: CommentRepliesCounterProps) => {
const colorTheme = useColorTheme()

const theme = useEditorState(
Substores.userState,
(store) => getCurrentTheme(store.userState),
'CommentRepliesCounter theme',
)

const color = React.useMemo(() => {
// If we don't force dark mode because of a blue background, we can just use foreground color
if (!props.forceDarkMode) {
return colorTheme.fg6.value
}
// In dark mode we need a lighter foreground which is visible well on blue background
if (theme === 'dark') {
return colorTheme.fg2.value
}
// In light mode we need a light background color, so it is visible on blue background
return colorTheme.bg3.value
}, [colorTheme, theme, props.forceDarkMode])

const repliesCount = props.thread.comments.filter((c) => c.deletedAt == null).length - 1

if (repliesCount <= 0) {
Expand All @@ -21,7 +46,7 @@ export const CommentRepliesCounter = React.memo((props: CommentRepliesCounterPro
style={{
paddingLeft: 44,
fontSize: 9,
color: colorTheme.fg6.value,
color: color,
marginBottom: 8,
}}
>
Expand Down
14 changes: 1 addition & 13 deletions editor/src/components/user-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -453,18 +453,7 @@ const AvatarPicture = React.memo((props: AvatarPictureProps) => {

const { initials, size } = props

const [pictureNotFound, setPictureNotFound] = React.useState(false)

React.useEffect(() => {
setPictureNotFound(false)
}, [url])

const onPictureError = React.useCallback(() => {
console.warn('cannot get picture', url)
setPictureNotFound(true)
}, [url])

if (url == null || pictureNotFound) {
if (url == null) {
return <span>{initials}</span>
}
return (
Expand All @@ -479,7 +468,6 @@ const AvatarPicture = React.memo((props: AvatarPictureProps) => {
}}
src={url}
referrerPolicy='no-referrer'
onError={onPictureError}
draggable={false}
/>
)
Expand Down

0 comments on commit cf3c150

Please sign in to comment.