diff --git a/src/components/LHNOptionsList/OptionRowLHN/OptionRow/Avatar.tsx b/src/components/LHNOptionsList/OptionRowLHN/OptionRow/Avatar.tsx index 482f682199be..a92e3c869e7c 100644 --- a/src/components/LHNOptionsList/OptionRowLHN/OptionRow/Avatar.tsx +++ b/src/components/LHNOptionsList/OptionRowLHN/OptionRow/Avatar.tsx @@ -1,23 +1,34 @@ import React from 'react'; -import type {ColorValue, ViewStyle} from 'react-native'; +import type {ColorValue} from 'react-native'; +import type {ValueOf} from 'type-fest'; import LHNAvatar from '@components/LHNOptionsList/LHNAvatar'; import {usePersonalDetails} from '@components/OnyxListItemProvider'; +import useThemeStyles from '@hooks/useThemeStyles'; import {shouldOptionShowTooltip} from '@libs/OptionsListUtils'; import {getDelegateAccountIDFromReportAction} from '@libs/ReportActionsUtils'; import type {OptionData} from '@libs/ReportUtils'; import CONST from '@src/CONST'; +type OptionMode = ValueOf; + type AvatarProps = { + /** Option data for the row. Source of avatar icons, subscript flag, tooltip eligibility, and delegate metadata. */ optionItem: OptionData; - isInFocusMode: boolean; - subscriptAvatarBorderColor: ColorValue; - secondaryAvatarBackgroundColor: ColorValue; - singleAvatarContainerStyle: ViewStyle[]; + + /** Display density mode. `COMPACT` switches the avatar size to `SMALL`. */ + viewMode: OptionMode; + + /** Background color used for both the subscript icon border and the secondary avatar background. Matches the row background so the chrome blends in. */ + avatarBackgroundColor: ColorValue; }; -function AvatarInner({optionItem, isInFocusMode, subscriptAvatarBorderColor, secondaryAvatarBackgroundColor, singleAvatarContainerStyle}: AvatarProps) { +function AvatarInner({optionItem, viewMode, avatarBackgroundColor}: AvatarProps) { + const styles = useThemeStyles(); const personalDetails = usePersonalDetails(); + const isInFocusMode = viewMode === CONST.OPTION_MODE.COMPACT; + const singleAvatarContainerStyle = [styles.actionAvatar, styles.mr3]; + const delegateAccountID = getDelegateAccountIDFromReportAction(optionItem?.parentReportAction); // Match the header's delegate avatar logic: when a delegate exists on the @@ -53,9 +64,9 @@ function AvatarInner({optionItem, isInFocusMode, subscriptAvatarBorderColor, sec icons={icons} shouldShowSubscript={!!optionItem.shouldShowSubscript} size={isInFocusMode ? CONST.AVATAR_SIZE.SMALL : CONST.AVATAR_SIZE.DEFAULT} - subscriptAvatarBorderColor={subscriptAvatarBorderColor} + subscriptAvatarBorderColor={avatarBackgroundColor} useMidSubscriptSize={isInFocusMode} - secondaryAvatarBackgroundColor={secondaryAvatarBackgroundColor} + secondaryAvatarBackgroundColor={avatarBackgroundColor} singleAvatarContainerStyle={singleAvatarContainerStyle} shouldShowTooltip={shouldOptionShowTooltip(optionItem)} delegateAccountID={skipDelegate ? undefined : delegateAccountID} @@ -66,7 +77,7 @@ function AvatarInner({optionItem, isInFocusMode, subscriptAvatarBorderColor, sec AvatarInner.displayName = 'OptionRow.AvatarInner'; -function Avatar({optionItem, isInFocusMode, subscriptAvatarBorderColor, secondaryAvatarBackgroundColor, singleAvatarContainerStyle}: AvatarProps) { +function Avatar({optionItem, viewMode, avatarBackgroundColor}: AvatarProps) { // Bail out before subscribing to personal details when the row has no avatar to render. if (!optionItem.icons?.length || !optionItem.icons.at(0)) { return null; @@ -74,10 +85,8 @@ function Avatar({optionItem, isInFocusMode, subscriptAvatarBorderColor, secondar return ( ); } diff --git a/src/components/LHNOptionsList/OptionRowLHN/OptionRow/DescriptiveText.tsx b/src/components/LHNOptionsList/OptionRowLHN/OptionRow/DescriptiveText.tsx index edb0a4c7f8a8..6518ebb876ea 100644 --- a/src/components/LHNOptionsList/OptionRowLHN/OptionRow/DescriptiveText.tsx +++ b/src/components/LHNOptionsList/OptionRowLHN/OptionRow/DescriptiveText.tsx @@ -6,6 +6,7 @@ import FS from '@libs/Fullstory'; import type {OptionData} from '@libs/ReportUtils'; type DescriptiveTextProps = { + /** Option data for the row. Renders `optionItem.descriptiveText` when present; component returns null otherwise. */ optionItem: OptionData; }; diff --git a/src/components/LHNOptionsList/OptionRowLHN/OptionRow/DraftIndicator.tsx b/src/components/LHNOptionsList/OptionRowLHN/OptionRow/DraftIndicator.tsx index aad0f26a3452..f2ac472d3e39 100644 --- a/src/components/LHNOptionsList/OptionRowLHN/OptionRow/DraftIndicator.tsx +++ b/src/components/LHNOptionsList/OptionRowLHN/OptionRow/DraftIndicator.tsx @@ -8,7 +8,10 @@ import useThemeStyles from '@hooks/useThemeStyles'; import variables from '@styles/variables'; type DraftIndicatorProps = { + /** Whether the report has an unsent draft comment. Required for the pencil icon to render. */ hasDraftComment: boolean; + + /** Whether the current user can comment in the report. The icon is hidden in read-only rows even when a draft exists. */ isAllowedToComment: boolean | null | undefined; }; diff --git a/src/components/LHNOptionsList/OptionRowLHN/OptionRow/ErrorBadge.tsx b/src/components/LHNOptionsList/OptionRowLHN/OptionRow/ErrorBadge.tsx index 39f663314053..c5bcd4f645cd 100644 --- a/src/components/LHNOptionsList/OptionRowLHN/OptionRow/ErrorBadge.tsx +++ b/src/components/LHNOptionsList/OptionRowLHN/OptionRow/ErrorBadge.tsx @@ -11,7 +11,10 @@ import type {OptionData} from '@libs/ReportUtils'; import CONST from '@src/CONST'; type ErrorBadgeProps = { + /** Brick road indicator for the row. The badge only renders when this equals ERROR (RBR). */ brickRoadIndicator: OptionData['brickRoadIndicator']; + + /** Action badge key used to derive the badge label (translated locally, debug builds only). */ actionBadge: OptionData['actionBadge']; }; diff --git a/src/components/LHNOptionsList/OptionRowLHN/OptionRowPressable.tsx b/src/components/LHNOptionsList/OptionRowLHN/OptionRow/Pressable.tsx similarity index 59% rename from src/components/LHNOptionsList/OptionRowLHN/OptionRowPressable.tsx rename to src/components/LHNOptionsList/OptionRowLHN/OptionRow/Pressable.tsx index e88e8daf840e..632bb73b6ce1 100644 --- a/src/components/LHNOptionsList/OptionRowLHN/OptionRowPressable.tsx +++ b/src/components/LHNOptionsList/OptionRowLHN/OptionRow/Pressable.tsx @@ -1,8 +1,14 @@ -import type {ReactNode, RefObject} from 'react'; -import React, {useState} from 'react'; +import type {ReactNode} from 'react'; +import React, {useRef, useState} from 'react'; import type {GestureResponderEvent, LayoutChangeEvent, View} from 'react-native'; import Hoverable from '@components/Hoverable'; +import {useLHNTooltipContext} from '@components/LHNOptionsList/LHNTooltipContext'; +import useLHNRowProductTrainingTooltip from '@components/LHNOptionsList/OptionRowLHN/useLHNRowProductTrainingTooltip'; import PressableWithSecondaryInteraction from '@components/PressableWithSecondaryInteraction'; +import getContextMenuAccessibilityHint from '@components/utils/getContextMenuAccessibilityHint'; +import getContextMenuAccessibilityProps from '@components/utils/getContextMenuAccessibilityProps'; +import useEnvironment from '@hooks/useEnvironment'; +import useLocalize from '@hooks/useLocalize'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useStyleUtils from '@hooks/useStyleUtils'; import useTheme from '@hooks/useTheme'; @@ -14,35 +20,72 @@ import {startSpan} from '@libs/telemetry/activeSpans'; import {showContextMenu} from '@pages/inbox/report/ContextMenu/ReportActionContextMenu'; import variables from '@styles/variables'; import CONST from '@src/CONST'; -import useLHNRowProductTrainingTooltip from './useLHNRowProductTrainingTooltip'; -type OptionRowPressableProps = { +type PressableProps = { + /** Option data for the row. Source of accessibility text and the report ID used by press/context-menu actions. */ optionItem: OptionData; + + /** Whether the row is the currently focused/active option. Drives the focused background and accessibility metadata. */ isOptionFocused: boolean; - isScreenFocused: boolean; - popoverAnchor: RefObject; - onSelectRow: (optionItem: OptionData, popoverAnchor: RefObject) => void; + + /** Press handler invoked with the option data and the popover anchor ref. */ + onSelectRow: (optionItem: OptionData, popoverAnchor: React.RefObject) => void; + + /** Layout handler forwarded to the underlying pressable. */ onLayout?: (event: LayoutChangeEvent) => void; - accessibilityLabel: string; - accessibilityHint?: string; - testID?: string; - children: (hovered: boolean) => ReactNode; + + /** Fires when the mouse enters the row. Hover state lives in the parent so leaves like Avatar can react. */ + onHoverIn?: () => void; + + /** Fires when the mouse leaves the row. */ + onHoverOut?: () => void; + + /** Row content. */ + children: ReactNode; }; -function OptionRowPressable({ - optionItem, - isOptionFocused, - isScreenFocused, - popoverAnchor, - onSelectRow, - onLayout, - accessibilityLabel, - accessibilityHint, - testID, - children, -}: OptionRowPressableProps) { +function Pressable({optionItem, isOptionFocused, onSelectRow, onLayout, onHoverIn, onHoverOut, children}: PressableProps) { + const theme = useTheme(); + const styles = useThemeStyles(); + const StyleUtils = useStyleUtils(); + const {translate} = useLocalize(); + const {isProduction} = useEnvironment(); + const {isScreenFocused} = useLHNTooltipContext(); + const {shouldUseNarrowLayout} = useResponsiveLayout(); const {hideProductTrainingTooltip} = useLHNRowProductTrainingTooltip(); + + const popoverAnchor = useRef(null); + const [isContextMenuActive, setIsContextMenuActive] = useState(false); + const reportID = optionItem.reportID; + const brickRoadIndicator = optionItem.brickRoadIndicator; + const actionBadgeText = !isProduction && optionItem.actionBadge ? translate(`common.actionBadge.${optionItem.actionBadge}`) : ''; + + let accessibilityLabelForBadge = ''; + if (brickRoadIndicator) { + accessibilityLabelForBadge = [translate('common.yourReviewIsRequired'), actionBadgeText].filter(Boolean).join(', '); + } else if (optionItem.isPinned) { + accessibilityLabelForBadge = translate('common.pinned'); + } + + const accessibilityLabel = [ + `${translate('accessibilityHints.navigatesToChat')} ${optionItem.text}`, + optionItem.isUnread ? translate('common.unread') : '', + optionItem.alternateText ?? '', + accessibilityLabelForBadge, + ] + .filter(Boolean) + .join('. '); + const contextMenuHint = getContextMenuAccessibilityHint({translate}); + const {accessibilityLabel: accessibilityLabelWithContextMenuHint, accessibilityHint} = getContextMenuAccessibilityProps({ + accessibilityLabel, + nativeAccessibilityHint: accessibilityLabel, + contextMenuHint, + }); + + // reportID may be a number contrary to the type definition + const testID = typeof reportID === 'number' ? String(reportID) : reportID; + const onPress = (event: GestureResponderEvent | KeyboardEvent | undefined) => { hideProductTrainingTooltip(); startSpan(`${CONST.TELEMETRY.SPAN_OPEN_REPORT}_${reportID}`, { @@ -55,11 +98,6 @@ function OptionRowPressable({ ReportActionComposeFocusManager.focus(); onSelectRow(optionItem, popoverAnchor); }; - const theme = useTheme(); - const styles = useThemeStyles(); - const StyleUtils = useStyleUtils(); - const {shouldUseNarrowLayout} = useResponsiveLayout(); - const [isContextMenuActive, setIsContextMenuActive] = useState(false); const showPopover = (event: MouseEvent | GestureResponderEvent) => { if (!isScreenFocused && shouldUseNarrowLayout) { @@ -86,7 +124,10 @@ function OptionRowPressable({ }; return ( - + {(hovered) => ( = 2} sentryLabel={CONST.SENTRY_LABEL.LHN.OPTION_ROW} > - {children(hovered)} + {children} )} ); } -OptionRowPressable.displayName = 'OptionRowPressable'; +Pressable.displayName = 'OptionRow.Pressable'; -export default OptionRowPressable; +export default Pressable; diff --git a/src/components/LHNOptionsList/OptionRowLHN/OptionRow/ProductTrainingTooltip.tsx b/src/components/LHNOptionsList/OptionRowLHN/OptionRow/ProductTrainingTooltip.tsx index cc33a9b92bcf..63484349684d 100644 --- a/src/components/LHNOptionsList/OptionRowLHN/OptionRow/ProductTrainingTooltip.tsx +++ b/src/components/LHNOptionsList/OptionRowLHN/OptionRow/ProductTrainingTooltip.tsx @@ -21,6 +21,7 @@ type ProductTrainingTooltipProps = { }; type ProductTrainingTooltipInnerProps = { + /** Row content the tooltip anchors to. Mounted only when the row is eligible to show a tooltip. */ children: ReactElement; }; diff --git a/src/components/LHNOptionsList/OptionRowLHN/OptionRow/Subtitle.tsx b/src/components/LHNOptionsList/OptionRowLHN/OptionRow/Subtitle.tsx index 8205b0fc04ce..71f8ffa73100 100644 --- a/src/components/LHNOptionsList/OptionRowLHN/OptionRow/Subtitle.tsx +++ b/src/components/LHNOptionsList/OptionRowLHN/OptionRow/Subtitle.tsx @@ -1,5 +1,4 @@ import React from 'react'; -import type {StyleProp, TextStyle} from 'react-native'; import type {ValueOf} from 'type-fest'; import Text from '@components/Text'; import useLocalize from '@hooks/useLocalize'; @@ -13,13 +12,17 @@ import CONST from '@src/CONST'; type OptionMode = ValueOf; type SubtitleProps = { + /** Option data for the row. Source of `alternateText` and emoji rendering hints. */ optionItem: OptionData; + + /** Display density mode. Compact rows render the subtitle with compact styles; the subtitle is hidden only when `optionItem.alternateText` is empty. */ viewMode: OptionMode; + + /** Whether the row is the currently focused/active option. Drives the active text style. */ isOptionFocused: boolean; - style?: StyleProp; }; -function Subtitle({optionItem, viewMode, isOptionFocused, style}: SubtitleProps) { +function Subtitle({optionItem, viewMode, isOptionFocused}: SubtitleProps) { const {translate} = useLocalize(); const styles = useThemeStyles(); @@ -31,8 +34,8 @@ function Subtitle({optionItem, viewMode, isOptionFocused, style}: SubtitleProps) const isInFocusMode = viewMode === CONST.OPTION_MODE.COMPACT; const textStyle = isOptionFocused ? styles.sidebarLinkActiveText : styles.sidebarLinkText; const alternateTextStyle = isInFocusMode - ? [textStyle, styles.textLabelSupporting, styles.optionAlternateTextCompact, styles.ml2, style] - : [textStyle, styles.optionAlternateText, styles.textLabelSupporting, style]; + ? [textStyle, styles.textLabelSupporting, styles.optionAlternateTextCompact, styles.ml2] + : [textStyle, styles.optionAlternateText, styles.textLabelSupporting]; const alternateTextFSClass = FS.getChatFSClass(optionItem); const containsCustomEmojiWithText = containsCustomEmojiUtils(alternateText) && !containsOnlyCustomEmoji(alternateText); diff --git a/src/components/LHNOptionsList/OptionRowLHN/OptionRow/Title.tsx b/src/components/LHNOptionsList/OptionRowLHN/OptionRow/Title.tsx index e558622dcf18..9b887d3fb7dc 100644 --- a/src/components/LHNOptionsList/OptionRowLHN/OptionRow/Title.tsx +++ b/src/components/LHNOptionsList/OptionRowLHN/OptionRow/Title.tsx @@ -1,19 +1,30 @@ import React from 'react'; -import type {StyleProp, TextStyle} from 'react-native'; import DisplayNames from '@components/DisplayNames'; import useLocalize from '@hooks/useLocalize'; +import useThemeStyles from '@hooks/useThemeStyles'; +import {shouldUseBoldText} from '@libs/OptionsListUtils'; import type {OptionData} from '@libs/ReportUtils'; import {isGroupChat, isSystemChat} from '@libs/ReportUtils'; import CONST from '@src/CONST'; type TitleProps = { + /** Option data for the row. Source of `text`, `displayNamesWithTooltips`, chat-type flags, parse-mode hints, and unread/bold derivation. */ optionItem: OptionData; - displayNameStyle: StyleProp; + + /** Whether the row is the currently focused/active option. Drives the active text style. */ + isOptionFocused: boolean; + + /** Numeric testID for the title node. This is the FlashList row index passed in from the renderItem callback, not the report ID. */ testID: number; }; -function Title({optionItem, displayNameStyle, testID}: TitleProps) { +function Title({optionItem, isOptionFocused, testID}: TitleProps) { const {translate} = useLocalize(); + const styles = useThemeStyles(); + + const textStyle = isOptionFocused ? styles.sidebarLinkActiveText : styles.sidebarLinkText; + const textUnreadStyle = shouldUseBoldText(optionItem) ? [textStyle, styles.sidebarLinkTextBold] : [textStyle]; + const displayNameStyle = [styles.optionDisplayName, styles.optionDisplayNameCompact, styles.pre, textUnreadStyle, styles.flexShrink0]; const shouldParseFullTitle = optionItem?.parentReportAction?.actionName !== CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT && !isGroupChat(optionItem); const shouldUseFullTitle = diff --git a/src/components/LHNOptionsList/OptionRowLHN/OptionRow/index.ts b/src/components/LHNOptionsList/OptionRowLHN/OptionRow/index.ts new file mode 100644 index 000000000000..9ddea46008cc --- /dev/null +++ b/src/components/LHNOptionsList/OptionRowLHN/OptionRow/index.ts @@ -0,0 +1,31 @@ +import Avatar from './Avatar'; +import DescriptiveText from './DescriptiveText'; +import DraftIndicator from './DraftIndicator'; +import ErrorBadge from './ErrorBadge'; +import InfoBadge from './InfoBadge'; +import OfflineWrapper from './OfflineWrapper'; +import OnboardingBadge from './OnboardingBadge'; +import PinIndicator from './PinIndicator'; +import Pressable from './Pressable'; +import ProductTrainingTooltip from './ProductTrainingTooltip'; +import Status from './Status'; +import Subtitle from './Subtitle'; +import Title from './Title'; + +const OptionRow = { + Avatar, + DescriptiveText, + DraftIndicator, + ErrorBadge, + InfoBadge, + OfflineWrapper, + OnboardingBadge, + PinIndicator, + Pressable, + ProductTrainingTooltip, + Status, + Subtitle, + Title, +}; + +export default OptionRow; diff --git a/src/components/LHNOptionsList/OptionRowLHN/OptionRowLHN.tsx b/src/components/LHNOptionsList/OptionRowLHN/OptionRowLHN.tsx new file mode 100644 index 000000000000..44e8b25fabe2 --- /dev/null +++ b/src/components/LHNOptionsList/OptionRowLHN/OptionRowLHN.tsx @@ -0,0 +1,83 @@ +import React from 'react'; +import {View} from 'react-native'; +import type {OptionRowLHNProps} from '@components/LHNOptionsList/types'; +import useThemeStyles from '@hooks/useThemeStyles'; +import OptionRow from './OptionRow'; +import useOptionRowChrome from './useOptionRowChrome'; + +function OptionRowLHN({isOptionFocused = false, onSelectRow = () => {}, optionItem, viewMode = 'default', onLayout = () => {}, hasDraftComment, testID}: OptionRowLHNProps) { + const styles = useThemeStyles(); + const {setHovered, sidebarInnerRowStyle, contentContainerStyles, avatarBackgroundColor} = useOptionRowChrome({ + isOptionFocused, + viewMode, + }); + + const brickRoadIndicator = optionItem.brickRoadIndicator; + + return ( + + + setHovered(true)} + onHoverOut={() => setHovered(false)} + > + + + + + + + + + + + + + + + + + + + + + + + + ); +} + +OptionRowLHN.displayName = 'OptionRowLHN'; + +export default OptionRowLHN; diff --git a/src/components/LHNOptionsList/OptionRowLHN/OptionRowLHNCore.tsx b/src/components/LHNOptionsList/OptionRowLHN/OptionRowLHNCore.tsx deleted file mode 100644 index 8bdb1b948538..000000000000 --- a/src/components/LHNOptionsList/OptionRowLHN/OptionRowLHNCore.tsx +++ /dev/null @@ -1,166 +0,0 @@ -import React, {useRef} from 'react'; -import type {ViewStyle} from 'react-native'; -import {StyleSheet, View} from 'react-native'; -import {useLHNTooltipContext} from '@components/LHNOptionsList/LHNTooltipContext'; -import type {OptionRowLHNProps} from '@components/LHNOptionsList/types'; -import getContextMenuAccessibilityHint from '@components/utils/getContextMenuAccessibilityHint'; -import getContextMenuAccessibilityProps from '@components/utils/getContextMenuAccessibilityProps'; -import useEnvironment from '@hooks/useEnvironment'; -import useLocalize from '@hooks/useLocalize'; -import useStyleUtils from '@hooks/useStyleUtils'; -import useTheme from '@hooks/useTheme'; -import useThemeStyles from '@hooks/useThemeStyles'; -import {shouldUseBoldText} from '@libs/OptionsListUtils'; -import CONST from '@src/CONST'; -import Avatar from './OptionRow/Avatar'; -import DescriptiveText from './OptionRow/DescriptiveText'; -import DraftIndicator from './OptionRow/DraftIndicator'; -import ErrorBadge from './OptionRow/ErrorBadge'; -import InfoBadge from './OptionRow/InfoBadge'; -import OfflineWrapper from './OptionRow/OfflineWrapper'; -import OnboardingBadge from './OptionRow/OnboardingBadge'; -import PinIndicator from './OptionRow/PinIndicator'; -import ProductTrainingTooltip from './OptionRow/ProductTrainingTooltip'; -import Status from './OptionRow/Status'; -import Subtitle from './OptionRow/Subtitle'; -import Title from './OptionRow/Title'; -import OptionRowPressable from './OptionRowPressable'; - -function OptionRowLHN({isOptionFocused = false, onSelectRow = () => {}, optionItem, viewMode = 'default', style, onLayout = () => {}, hasDraftComment, testID}: OptionRowLHNProps) { - const {isProduction} = useEnvironment(); - const theme = useTheme(); - const styles = useThemeStyles(); - const popoverAnchor = useRef(null); - const StyleUtils = useStyleUtils(); - - const {isScreenFocused} = useLHNTooltipContext(); - - const {translate} = useLocalize(); - const isInFocusMode = viewMode === CONST.OPTION_MODE.COMPACT; - const sidebarInnerRowStyle = StyleSheet.flatten( - isInFocusMode - ? [styles.chatLinkRowPressable, styles.flexGrow1, styles.optionItemAvatarNameWrapper, styles.optionRowCompact, styles.justifyContentCenter] - : [styles.chatLinkRowPressable, styles.flexGrow1, styles.optionItemAvatarNameWrapper, styles.optionRow, styles.justifyContentCenter], - ); - - const singleAvatarContainerStyle = [styles.actionAvatar, styles.mr3]; - - const brickRoadIndicator = optionItem.brickRoadIndicator; - const actionBadgeText = !isProduction && optionItem.actionBadge ? translate(`common.actionBadge.${optionItem.actionBadge}`) : ''; - let accessibilityLabelForBadge = ''; - if (brickRoadIndicator) { - accessibilityLabelForBadge = [translate('common.yourReviewIsRequired'), actionBadgeText].filter(Boolean).join(', '); - } else if (optionItem.isPinned) { - accessibilityLabelForBadge = translate('common.pinned'); - } - const textStyle = isOptionFocused ? styles.sidebarLinkActiveText : styles.sidebarLinkText; - const textUnreadStyle = shouldUseBoldText(optionItem) ? [textStyle, styles.sidebarLinkTextBold] : [textStyle]; - const displayNameStyle = [styles.optionDisplayName, styles.optionDisplayNameCompact, styles.pre, textUnreadStyle, styles.flexShrink0, style]; - - const contentContainerStyles = isInFocusMode ? [styles.flex1, styles.flexRow, styles.overflowHidden, StyleUtils.getCompactContentContainerStyles()] : [styles.flex1]; - const hoveredBackgroundColor = !!styles.sidebarLinkHover && 'backgroundColor' in styles.sidebarLinkHover ? styles.sidebarLinkHover.backgroundColor : theme.sidebar; - const focusedBackgroundColor = styles.sidebarLinkActive.backgroundColor; - - const subscriptAvatarBorderColor = isOptionFocused ? focusedBackgroundColor : theme.sidebar; - - const accessibilityLabel = [ - `${translate('accessibilityHints.navigatesToChat')} ${optionItem.text}`, - optionItem.isUnread ? translate('common.unread') : '', - optionItem.alternateText ?? '', - accessibilityLabelForBadge, - ] - .filter(Boolean) - .join('. '); - const contextMenuHint = getContextMenuAccessibilityHint({translate}); - const {accessibilityLabel: accessibilityLabelWithContextMenuHint, accessibilityHint} = getContextMenuAccessibilityProps({ - accessibilityLabel, - nativeAccessibilityHint: accessibilityLabel, - contextMenuHint, - }); - - return ( - - - - {(hovered) => { - let secondaryAvatarBgColor = theme.sidebar; - if (isOptionFocused) { - secondaryAvatarBgColor = focusedBackgroundColor; - } else if (hovered) { - secondaryAvatarBgColor = hoveredBackgroundColor; - } - return ( - <> - - - - - - - <OnboardingBadge optionItem={optionItem} /> - <Status optionItem={optionItem} /> - </View> - <Subtitle - optionItem={optionItem} - viewMode={viewMode} - isOptionFocused={isOptionFocused} - style={style} - /> - </View> - <DescriptiveText optionItem={optionItem} /> - <ErrorBadge - brickRoadIndicator={brickRoadIndicator} - actionBadge={optionItem.actionBadge} - /> - </View> - </View> - <View style={[styles.flexRow, styles.alignItemsCenter]}> - <InfoBadge - brickRoadIndicator={brickRoadIndicator} - actionBadge={optionItem.actionBadge} - /> - <DraftIndicator - hasDraftComment={hasDraftComment} - isAllowedToComment={optionItem.isAllowedToComment} - /> - <PinIndicator - isPinned={optionItem.isPinned} - brickRoadIndicator={brickRoadIndicator} - /> - </View> - </> - ); - }} - </OptionRowPressable> - </ProductTrainingTooltip> - </OfflineWrapper> - ); -} - -OptionRowLHN.displayName = 'OptionRowLHN'; - -export default React.memo(OptionRowLHN); diff --git a/src/components/LHNOptionsList/OptionRowLHN/OptionRowLHNData.tsx b/src/components/LHNOptionsList/OptionRowLHN/OptionRowLHNData.tsx index 5f0f5e9b3059..672e4e5ec2dd 100644 --- a/src/components/LHNOptionsList/OptionRowLHN/OptionRowLHNData.tsx +++ b/src/components/LHNOptionsList/OptionRowLHN/OptionRowLHNData.tsx @@ -20,13 +20,13 @@ import ONYXKEYS from '@src/ONYXKEYS'; import type {ReportActions as ReportActionsType} from '@src/types/onyx'; import type {VisibleReportActionsDerivedValue} from '@src/types/onyx/DerivedValues'; import type {Icon} from '@src/types/onyx/OnyxCommon'; -import OptionRowLHN from './OptionRowLHNCore'; +import OptionRowLHN from './OptionRowLHN'; /* * This component gets the data from onyx for the actual * OptionRowLHN component. - * The OptionRowLHN component is memoized, so it will only - * re-render if the data really changed. + * OptionRowLHN is auto-memoized by React Compiler, so it will + * only re-render when the inputs derived from this wrapper change. */ function OptionRowLHNData({ isOptionFocused = false, diff --git a/src/components/LHNOptionsList/OptionRowLHN/useLHNRowProductTrainingTooltip.ts b/src/components/LHNOptionsList/OptionRowLHN/useLHNRowProductTrainingTooltip.ts index e2a400aa4b19..d28bf1f589a6 100644 --- a/src/components/LHNOptionsList/OptionRowLHN/useLHNRowProductTrainingTooltip.ts +++ b/src/components/LHNOptionsList/OptionRowLHN/useLHNRowProductTrainingTooltip.ts @@ -6,7 +6,7 @@ import CONST from '@src/CONST'; /** * Resolves the product-training tooltip state (CONCIERGE_LHN_GBR) for an LHN row. - * Used by both ProductTrainingTooltipInner (render the tooltip) and OptionRowPressable (hide on press). + * Used by both ProductTrainingTooltipInner (render the tooltip) and OptionRow/Pressable (hide on press). */ function useLHNRowProductTrainingTooltip() { const {shouldUseNarrowLayout} = useResponsiveLayout(); diff --git a/src/components/LHNOptionsList/OptionRowLHN/useOptionRowChrome.ts b/src/components/LHNOptionsList/OptionRowLHN/useOptionRowChrome.ts new file mode 100644 index 000000000000..65e481fee47e --- /dev/null +++ b/src/components/LHNOptionsList/OptionRowLHN/useOptionRowChrome.ts @@ -0,0 +1,61 @@ +import {useState} from 'react'; +import type {ColorValue, ViewStyle} from 'react-native'; +import {StyleSheet} from 'react-native'; +import type {OptionRowLHNProps} from '@components/LHNOptionsList/types'; +import useStyleUtils from '@hooks/useStyleUtils'; +import useTheme from '@hooks/useTheme'; +import useThemeStyles from '@hooks/useThemeStyles'; +import CONST from '@src/CONST'; + +type UseOptionRowChromeParams = { + /** Whether the row is the currently focused/active option. Drives the focused color path. */ + isOptionFocused: boolean; + + /** Display density mode. `COMPACT` switches to the compact layout styles. */ + viewMode: NonNullable<OptionRowLHNProps['viewMode']>; +}; + +/** + * Row-level state and layout-skeleton styles for `OptionRowLHN`. + * + * Owns the pieces that cannot be pushed into leaves: the hover `useState`, the wrapper + * `<View>` styles (`sidebarInnerRowStyle`, `contentContainerStyles`) that wrap multiple + * leaves, and the single `avatarBackgroundColor` derived from hover and focus state. + * + * Per-leaf derivations (`isInFocusMode`, `singleAvatarContainerStyle`, `displayNameStyle`) + * live inside their consuming leaf component (`Avatar`, `Title`) and are not returned here. + */ +function useOptionRowChrome({isOptionFocused, viewMode}: UseOptionRowChromeParams) { + const theme = useTheme(); + const styles = useThemeStyles(); + const StyleUtils = useStyleUtils(); + const [hovered, setHovered] = useState(false); + + const isInFocusMode = viewMode === CONST.OPTION_MODE.COMPACT; + + const sidebarInnerRowStyle = StyleSheet.flatten<ViewStyle>( + isInFocusMode + ? [styles.chatLinkRowPressable, styles.flexGrow1, styles.optionItemAvatarNameWrapper, styles.optionRowCompact, styles.justifyContentCenter] + : [styles.chatLinkRowPressable, styles.flexGrow1, styles.optionItemAvatarNameWrapper, styles.optionRow, styles.justifyContentCenter], + ); + const contentContainerStyles = isInFocusMode ? [styles.flex1, styles.flexRow, styles.overflowHidden, StyleUtils.getCompactContentContainerStyles()] : [styles.flex1]; + + const hoveredBackgroundColor = !!styles.sidebarLinkHover && 'backgroundColor' in styles.sidebarLinkHover ? styles.sidebarLinkHover.backgroundColor : theme.sidebar; + const focusedBackgroundColor = styles.sidebarLinkActive.backgroundColor; + + let avatarBackgroundColor: ColorValue = theme.sidebar; + if (isOptionFocused) { + avatarBackgroundColor = focusedBackgroundColor; + } else if (hovered) { + avatarBackgroundColor = hoveredBackgroundColor; + } + + return { + setHovered, + sidebarInnerRowStyle, + contentContainerStyles, + avatarBackgroundColor, + }; +} + +export default useOptionRowChrome; diff --git a/src/components/LHNOptionsList/types.ts b/src/components/LHNOptionsList/types.ts index cae7909e523d..829de863508a 100644 --- a/src/components/LHNOptionsList/types.ts +++ b/src/components/LHNOptionsList/types.ts @@ -1,5 +1,5 @@ import type {RefObject} from 'react'; -import type {LayoutChangeEvent, StyleProp, TextStyle, View, ViewStyle} from 'react-native'; +import type {LayoutChangeEvent, StyleProp, View, ViewStyle} from 'react-native'; import type {OnyxEntry} from 'react-native-onyx'; import type {ValueOf} from 'type-fest'; import type CONST from '@src/CONST'; @@ -85,9 +85,6 @@ type OptionRowLHNProps = { /** Toggle between compact and default view */ viewMode?: OptionMode; - /** Additional style props */ - style?: StyleProp<TextStyle>; - /** The item that should be rendered */ optionItem: OptionData; diff --git a/tests/unit/hooks/useOptionRowChrome.test.ts b/tests/unit/hooks/useOptionRowChrome.test.ts new file mode 100644 index 000000000000..33576d96d0e6 --- /dev/null +++ b/tests/unit/hooks/useOptionRowChrome.test.ts @@ -0,0 +1,106 @@ +import {act, renderHook} from '@testing-library/react-native'; +import useOptionRowChrome from '@components/LHNOptionsList/OptionRowLHN/useOptionRowChrome'; +import CONST from '@src/CONST'; + +const FOCUSED_COLOR = '#focused'; +const HOVERED_COLOR = '#hovered'; +const DEFAULT_SIDEBAR = '#sidebar'; + +const MOCK_STYLES = { + sidebarLinkActive: {backgroundColor: FOCUSED_COLOR}, + sidebarLinkHover: {backgroundColor: HOVERED_COLOR}, + chatLinkRowPressable: {chatLinkRowPressable: true}, + flexGrow1: {flexGrow1: true}, + optionItemAvatarNameWrapper: {optionItemAvatarNameWrapper: true}, + optionRow: {optionRow: true}, + optionRowCompact: {optionRowCompact: true}, + justifyContentCenter: {justifyContentCenter: true}, + flex1: {flex1: true}, + flexRow: {flexRow: true}, + overflowHidden: {overflowHidden: true}, +}; + +const MOCK_THEME = {sidebar: DEFAULT_SIDEBAR}; + +const MOCK_STYLE_UTILS = { + getCompactContentContainerStyles: () => ({getCompactContentContainerStyles: true}), +}; + +jest.mock('@hooks/useTheme', () => ({ + __esModule: true, + default: jest.fn(() => MOCK_THEME), +})); + +jest.mock('@hooks/useThemeStyles', () => ({ + __esModule: true, + default: jest.fn(() => MOCK_STYLES), +})); + +jest.mock('@hooks/useStyleUtils', () => ({ + __esModule: true, + default: jest.fn(() => MOCK_STYLE_UTILS), +})); + +describe('useOptionRowChrome', () => { + describe('avatarBackgroundColor precedence', () => { + it('returns the default sidebar color when neither focused nor hovered', () => { + const {result} = renderHook(() => useOptionRowChrome({isOptionFocused: false, viewMode: CONST.OPTION_MODE.DEFAULT})); + + expect(result.current.avatarBackgroundColor).toBe(DEFAULT_SIDEBAR); + }); + + it('returns the focused color when isOptionFocused is true', () => { + const {result} = renderHook(() => useOptionRowChrome({isOptionFocused: true, viewMode: CONST.OPTION_MODE.DEFAULT})); + + expect(result.current.avatarBackgroundColor).toBe(FOCUSED_COLOR); + }); + + it('returns the hovered color when hovered and not focused', () => { + const {result} = renderHook(() => useOptionRowChrome({isOptionFocused: false, viewMode: CONST.OPTION_MODE.DEFAULT})); + + act(() => { + result.current.setHovered(true); + }); + + expect(result.current.avatarBackgroundColor).toBe(HOVERED_COLOR); + }); + + it('focused beats hovered when both are true', () => { + const {result} = renderHook(() => useOptionRowChrome({isOptionFocused: true, viewMode: CONST.OPTION_MODE.DEFAULT})); + + act(() => { + result.current.setHovered(true); + }); + + expect(result.current.avatarBackgroundColor).toBe(FOCUSED_COLOR); + }); + }); + + describe('layout styles by viewMode', () => { + it('default viewMode uses optionRow in sidebarInnerRowStyle', () => { + const {result} = renderHook(() => useOptionRowChrome({isOptionFocused: false, viewMode: CONST.OPTION_MODE.DEFAULT})); + + expect(result.current.sidebarInnerRowStyle).toMatchObject({optionRow: true}); + expect(result.current.sidebarInnerRowStyle).not.toMatchObject({optionRowCompact: true}); + }); + + it('compact viewMode uses optionRowCompact in sidebarInnerRowStyle', () => { + const {result} = renderHook(() => useOptionRowChrome({isOptionFocused: false, viewMode: CONST.OPTION_MODE.COMPACT})); + + expect(result.current.sidebarInnerRowStyle).toMatchObject({optionRowCompact: true}); + expect(result.current.sidebarInnerRowStyle).not.toMatchObject({optionRow: true}); + }); + + it('default viewMode returns flex1 contentContainerStyles', () => { + const {result} = renderHook(() => useOptionRowChrome({isOptionFocused: false, viewMode: CONST.OPTION_MODE.DEFAULT})); + + expect(result.current.contentContainerStyles).toEqual([MOCK_STYLES.flex1]); + }); + + it('compact viewMode returns the compact content container set', () => { + const {result} = renderHook(() => useOptionRowChrome({isOptionFocused: false, viewMode: CONST.OPTION_MODE.COMPACT})); + + expect(result.current.contentContainerStyles).toEqual([MOCK_STYLES.flex1, MOCK_STYLES.flexRow, MOCK_STYLES.overflowHidden, MOCK_STYLE_UTILS.getCompactContentContainerStyles()]); + }); + }); +});