|
| 1 | +import React from 'react'; |
| 2 | +import {View} from 'react-native'; |
| 3 | +import Icon from '@components/Icon'; |
| 4 | +import Switch from '@components/Switch'; |
| 5 | +import Table from '@components/Table'; |
| 6 | +import type {TableData} from '@components/Table'; |
| 7 | +import TextWithTooltip from '@components/TextWithTooltip'; |
| 8 | +import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset'; |
| 9 | +import useLocalize from '@hooks/useLocalize'; |
| 10 | +import useTheme from '@hooks/useTheme'; |
| 11 | +import useThemeStyles from '@hooks/useThemeStyles'; |
| 12 | +import variables from '@styles/variables'; |
| 13 | +import CONST from '@src/CONST'; |
| 14 | +import type * as OnyxCommon from '@src/types/onyx/OnyxCommon'; |
| 15 | + |
| 16 | +type WorkspaceTaxTableRowData = TableData & { |
| 17 | + name: string; |
| 18 | + alternateText: string; |
| 19 | + enabled: boolean; |
| 20 | + isLocked: boolean; |
| 21 | + isSwitchDisabled?: boolean; |
| 22 | + errors?: OnyxCommon.Errors; |
| 23 | + pendingAction?: OnyxCommon.PendingAction; |
| 24 | + action: () => void; |
| 25 | + onToggleEnabled: (enabled: boolean) => void; |
| 26 | + onDisabledSwitchPress?: () => void; |
| 27 | + onClose: () => void; |
| 28 | +}; |
| 29 | + |
| 30 | +type WorkspaceTaxesTableRowProps = { |
| 31 | + /** Data about the tax rate */ |
| 32 | + item: WorkspaceTaxTableRowData; |
| 33 | + |
| 34 | + /** The index of the row relative to all other rows */ |
| 35 | + rowIndex: number; |
| 36 | + |
| 37 | + /** Whether to use narrow table row layout */ |
| 38 | + shouldUseNarrowTableLayout: boolean; |
| 39 | +}; |
| 40 | + |
| 41 | +function WorkspaceTaxesTableRow({item, rowIndex, shouldUseNarrowTableLayout}: WorkspaceTaxesTableRowProps) { |
| 42 | + const theme = useTheme(); |
| 43 | + const styles = useThemeStyles(); |
| 44 | + const {translate} = useLocalize(); |
| 45 | + const icons = useMemoizedLazyExpensifyIcons(['ArrowRight']); |
| 46 | + |
| 47 | + const isDeleting = item.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE; |
| 48 | + const enabledStatusLabel = item.enabled ? translate('common.enabled') : translate('common.disabled'); |
| 49 | + |
| 50 | + const accessibilityLabel = [item.name, item.alternateText, enabledStatusLabel].filter(Boolean).join(', '); |
| 51 | + |
| 52 | + return ( |
| 53 | + <Table.Row |
| 54 | + interactive |
| 55 | + rowIndex={rowIndex} |
| 56 | + disabled={isDeleting} |
| 57 | + accessibilityLabel={accessibilityLabel} |
| 58 | + skeletonReasonAttributes={{context: 'workspaceTaxesTableRow'}} |
| 59 | + sentryLabel={CONST.SENTRY_LABEL.WORKSPACE.TAXES.ROW} |
| 60 | + onPress={item.action} |
| 61 | + offlineWithFeedback={{ |
| 62 | + errors: item.errors, |
| 63 | + pendingAction: item.pendingAction, |
| 64 | + onClose: item.onClose, |
| 65 | + }} |
| 66 | + > |
| 67 | + {({hovered}) => ( |
| 68 | + <> |
| 69 | + <View style={[styles.flex1, styles.flexRow, styles.alignItemsCenter, shouldUseNarrowTableLayout && styles.gap1]}> |
| 70 | + <View style={[styles.flex1, styles.gap1]}> |
| 71 | + <TextWithTooltip |
| 72 | + shouldShowTooltip |
| 73 | + numberOfLines={1} |
| 74 | + text={item.name} |
| 75 | + style={styles.optionDisplayName} |
| 76 | + /> |
| 77 | + {!!item.alternateText && ( |
| 78 | + <TextWithTooltip |
| 79 | + shouldShowTooltip |
| 80 | + numberOfLines={1} |
| 81 | + text={item.alternateText} |
| 82 | + style={[styles.textLabelSupporting, styles.lh16, styles.pre]} |
| 83 | + /> |
| 84 | + )} |
| 85 | + </View> |
| 86 | + </View> |
| 87 | + |
| 88 | + <View style={[styles.justifyContentCenter, styles.alignItemsEnd]}> |
| 89 | + <Switch |
| 90 | + isOn={item.enabled} |
| 91 | + showLockIcon={item.isLocked} |
| 92 | + disabled={item.isSwitchDisabled} |
| 93 | + disabledAction={item.onDisabledSwitchPress} |
| 94 | + accessibilityLabel={translate('workspace.taxes.actions.enable')} |
| 95 | + onToggle={item.onToggleEnabled} |
| 96 | + isNested |
| 97 | + /> |
| 98 | + </View> |
| 99 | + |
| 100 | + <Icon |
| 101 | + src={icons.ArrowRight} |
| 102 | + fill={theme.icon} |
| 103 | + additionalStyles={[styles.justifyContentCenter, styles.alignItemsCenter, (!hovered || isDeleting) && styles.opacitySemiTransparent]} |
| 104 | + width={variables.iconSizeNormal} |
| 105 | + height={variables.iconSizeNormal} |
| 106 | + /> |
| 107 | + </> |
| 108 | + )} |
| 109 | + </Table.Row> |
| 110 | + ); |
| 111 | +} |
| 112 | + |
| 113 | +export default WorkspaceTaxesTableRow; |
| 114 | +export type {WorkspaceTaxTableRowData}; |
0 commit comments