|
| 1 | +import React, {useMemo} from 'react'; |
| 2 | +import {View} from 'react-native'; |
| 3 | +import Checkbox from '@components/Checkbox'; |
| 4 | +import type {ListItem, TransactionCardGroupListItemType} from '@components/SelectionList/types'; |
| 5 | +import SubscriptAvatar from '@components/SubscriptAvatar'; |
| 6 | +import type {SubIcon} from '@components/SubscriptAvatar'; |
| 7 | +import TextWithTooltip from '@components/TextWithTooltip'; |
| 8 | +import useLocalize from '@hooks/useLocalize'; |
| 9 | +import useStyleUtils from '@hooks/useStyleUtils'; |
| 10 | +import useTheme from '@hooks/useTheme'; |
| 11 | +import useThemeIllustrations from '@hooks/useThemeIllustrations'; |
| 12 | +import useThemeStyles from '@hooks/useThemeStyles'; |
| 13 | +import {getCardFeedIcon} from '@libs/CardUtils'; |
| 14 | +import {formatPhoneNumber} from '@libs/LocalePhoneNumber'; |
| 15 | +import {getDisplayNameOrDefault} from '@libs/PersonalDetailsUtils'; |
| 16 | +import variables from '@styles/variables'; |
| 17 | +import CONST from '@src/CONST'; |
| 18 | +import type {CompanyCardFeed} from '@src/types/onyx/CardFeeds'; |
| 19 | +import type {Icon} from '@src/types/onyx/OnyxCommon'; |
| 20 | + |
| 21 | +type CardListItemHeaderProps<TItem extends ListItem> = { |
| 22 | + /** The card currently being looked at */ |
| 23 | + card: TransactionCardGroupListItemType; |
| 24 | + |
| 25 | + /** Callback to fire when a checkbox is pressed */ |
| 26 | + onCheckboxPress?: (item: TItem) => void; |
| 27 | + |
| 28 | + /** Whether this section items disabled for selection */ |
| 29 | + isDisabled?: boolean | null; |
| 30 | + |
| 31 | + /** Whether the item is hovered */ |
| 32 | + isHovered?: boolean; |
| 33 | + |
| 34 | + /** Whether the item is focused */ |
| 35 | + isFocused?: boolean; |
| 36 | + |
| 37 | + /** Whether selecting multiple transactions at once is allowed */ |
| 38 | + canSelectMultiple: boolean | undefined; |
| 39 | +}; |
| 40 | + |
| 41 | +function CardListItemHeader<TItem extends ListItem>({card: cardItem, onCheckboxPress, isDisabled, isHovered, isFocused, canSelectMultiple}: CardListItemHeaderProps<TItem>) { |
| 42 | + const theme = useTheme(); |
| 43 | + const styles = useThemeStyles(); |
| 44 | + const StyleUtils = useStyleUtils(); |
| 45 | + const {translate} = useLocalize(); |
| 46 | + const illustrations = useThemeIllustrations(); |
| 47 | + |
| 48 | + const formattedDisplayName = useMemo(() => formatPhoneNumber(getDisplayNameOrDefault(cardItem)), [cardItem]); |
| 49 | + |
| 50 | + const [memberAvatar, cardIcon] = useMemo(() => { |
| 51 | + const avatar: Icon = { |
| 52 | + source: cardItem.avatar, |
| 53 | + type: CONST.ICON_TYPE_AVATAR, |
| 54 | + name: formattedDisplayName, |
| 55 | + id: cardItem.accountID, |
| 56 | + }; |
| 57 | + |
| 58 | + const icon: SubIcon = { |
| 59 | + source: getCardFeedIcon(cardItem.bank as CompanyCardFeed, illustrations), |
| 60 | + width: variables.cardAvatarWidth, |
| 61 | + height: variables.cardAvatarHeight, |
| 62 | + }; |
| 63 | + |
| 64 | + return [avatar, icon]; |
| 65 | + }, [formattedDisplayName, illustrations, cardItem]); |
| 66 | + |
| 67 | + const backgroundColor = |
| 68 | + StyleUtils.getItemBackgroundColorStyle(!!cardItem.isSelected, !!isFocused || !!isHovered, !!isDisabled, theme.activeComponentBG, theme.hoverComponentBG)?.backgroundColor ?? |
| 69 | + theme.highlightBG; |
| 70 | + |
| 71 | + // s77rt add total cell, action cell and collapse/expand button |
| 72 | + |
| 73 | + return ( |
| 74 | + <View> |
| 75 | + <View style={[styles.pv1Half, styles.ph3, styles.flexRow, styles.alignItemsCenter, styles.justifyContentStart]}> |
| 76 | + <View style={[styles.flexRow, styles.alignItemsCenter, styles.mnh40, styles.flex1, styles.gap3]}> |
| 77 | + {!!canSelectMultiple && ( |
| 78 | + <Checkbox |
| 79 | + onPress={() => onCheckboxPress?.(cardItem as unknown as TItem)} |
| 80 | + isChecked={cardItem.isSelected} |
| 81 | + disabled={!!isDisabled || cardItem.isDisabledCheckbox} |
| 82 | + accessibilityLabel={translate('common.select')} |
| 83 | + /> |
| 84 | + )} |
| 85 | + <View style={[styles.flexRow, styles.gap3]}> |
| 86 | + <SubscriptAvatar |
| 87 | + mainAvatar={memberAvatar} |
| 88 | + subscriptIcon={cardIcon} |
| 89 | + backgroundColor={backgroundColor} |
| 90 | + noMargin |
| 91 | + /> |
| 92 | + <View style={[styles.gapHalf]}> |
| 93 | + <TextWithTooltip |
| 94 | + text={formattedDisplayName} |
| 95 | + style={[styles.optionDisplayName, styles.sidebarLinkTextBold, styles.pre]} |
| 96 | + /> |
| 97 | + <TextWithTooltip |
| 98 | + text={`${cardItem.cardName} • ${cardItem.lastFourPAN}`} |
| 99 | + style={[styles.textLabelSupporting, styles.lh16, styles.pre]} |
| 100 | + /> |
| 101 | + </View> |
| 102 | + </View> |
| 103 | + </View> |
| 104 | + </View> |
| 105 | + <View style={[styles.pv2, styles.ph3]}> |
| 106 | + <View style={[styles.borderBottom]} /> |
| 107 | + </View> |
| 108 | + </View> |
| 109 | + ); |
| 110 | +} |
| 111 | + |
| 112 | +CardListItemHeader.displayName = 'CardListItemHeader'; |
| 113 | + |
| 114 | +export default CardListItemHeader; |
0 commit comments