Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/CONST/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ const CONST = {
ANIMATED_HIGHLIGHT_ENTRY_DURATION: 300,
ANIMATED_HIGHLIGHT_START_DELAY: 10,
ANIMATED_HIGHLIGHT_START_DURATION: 300,
ANIMATED_HIGHLIGHT_WORKSPACE_FEATURE_ITEM_END_DELAY: 7000,
ANIMATED_HIGHLIGHT_WORKSPACE_FEATURE_ITEM_END_DURATION: 3000,
ANIMATED_HIGHLIGHT_END_DELAY: 800,
ANIMATED_HIGHLIGHT_END_DURATION: 2000,
ANIMATED_TRANSITION: 300,
Expand Down
6 changes: 6 additions & 0 deletions src/components/HighlightableMenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import React, {forwardRef} from 'react';
import type {View} from 'react-native';
import {StyleSheet} from 'react-native';
import useAnimatedHighlightStyle from '@hooks/useAnimatedHighlightStyle';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import CONST from '@src/CONST';
import MenuItem from './MenuItem';
import type {MenuItemProps} from './MenuItem';

Expand All @@ -14,12 +16,16 @@ type Props = MenuItemProps & {

function HighlightableMenuItem({wrapperStyle, highlighted, ...restOfProps}: Props, ref: ForwardedRef<View>) {
const styles = useThemeStyles();
const theme = useTheme();

const flattenedWrapperStyles = StyleSheet.flatten(wrapperStyle);
const animatedHighlightStyle = useAnimatedHighlightStyle({
shouldHighlight: highlighted ?? false,
height: flattenedWrapperStyles?.height ? Number(flattenedWrapperStyles.height) : styles.sectionMenuItem.height,
borderRadius: flattenedWrapperStyles?.borderRadius ? Number(flattenedWrapperStyles.borderRadius) : styles.sectionMenuItem.borderRadius,
highlightColor: theme.messageHighlightBG,
highlightEndDelay: CONST.ANIMATED_HIGHLIGHT_WORKSPACE_FEATURE_ITEM_END_DELAY,
highlightEndDuration: CONST.ANIMATED_HIGHLIGHT_WORKSPACE_FEATURE_ITEM_END_DURATION,
});

return (
Expand Down
4 changes: 1 addition & 3 deletions src/components/Hoverable/index.native.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import React from 'react';
import {View} from 'react-native';
import type HoverableProps from './types';

/**
Expand All @@ -9,7 +7,7 @@ import type HoverableProps from './types';
function Hoverable({children}: HoverableProps) {
const childrenWithHoverState = typeof children === 'function' ? children(false) : children;

return <View>{childrenWithHoverState}</View>;
return childrenWithHoverState;
}

Hoverable.displayName = 'Hoverable';
Expand Down
128 changes: 107 additions & 21 deletions src/pages/workspace/WorkspaceMoreFeaturesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, {useCallback, useEffect, useState} from 'react';
import {View} from 'react-native';
import ConfirmModal from '@components/ConfirmModal';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import Hoverable from '@components/Hoverable';
import * as Illustrations from '@components/Icon/Illustrations';
import ScreenWrapper from '@components/ScreenWrapper';
import ScrollView from '@components/ScrollView';
Expand Down Expand Up @@ -67,6 +68,7 @@ type Item = {
pendingAction: PendingAction | undefined;
errors?: Errors;
onCloseError?: () => void;
onPress?: () => void;
};

type SectionObject = {
Expand Down Expand Up @@ -147,6 +149,12 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
}
enablePolicyDistanceRates(policyID, isEnabled, distanceRateCustomUnit);
},
onPress: () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ PERF-4 (docs)

The onPress function is created inline on every render, creating a new function reference each time. Since this is passed as a prop to item objects used in rendering, it will cause unnecessary re-renders.

Suggested fix: Use useCallback to memoize the function:

const navigateToDistanceRates = useCallback(() => {
    if (!policyID) {
        return;
    }
    Navigation.navigate(ROUTES.WORKSPACE_DISTANCE_RATES.getRoute(policyID));
}, [policyID]);

// Then in the item:
onPress: navigateToDistanceRates,

Note: This same issue applies to all the other onPress handlers added in this PR (lines 175, 204, 247, 271, 289, 308, 318, 338, 359, 395, 431).

if (!policyID) {
return;
}
Navigation.navigate(ROUTES.WORKSPACE_DISTANCE_RATES.getRoute(policyID));
},
},
{
icon: illustrations.HandCard,
Expand All @@ -164,6 +172,12 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
disabledAction: () => {
setIsDisableExpensifyCardWarningModalOpen(true);
},
onPress: () => {
if (!policyID) {
return;
}
Navigation.navigate(ROUTES.WORKSPACE_EXPENSIFY_CARD.getRoute(policyID));
},
},
];

Expand All @@ -183,6 +197,12 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
disabledAction: () => {
setIsDisableCompanyCardsWarningModalOpen(true);
},
onPress: () => {
if (!policyID) {
return;
}
Navigation.navigate(ROUTES.WORKSPACE_COMPANY_CARDS.getRoute(policyID));
},
});

spendItems.push({
Expand All @@ -201,6 +221,12 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
}
enablePerDiem(policyID, isEnabled, perDiemCustomUnit?.customUnitID, true, quickAction);
},
onPress: () => {
if (!policyID) {
return;
}
Navigation.navigate(ROUTES.WORKSPACE_PER_DIEM.getRoute(policyID));
},
});

const manageItems: Item[] = [
Expand All @@ -218,6 +244,12 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
},
disabled: isSmartLimitEnabled,
disabledAction: onDisabledWorkflowPress,
onPress: () => {
if (!policyID) {
return;
}
Navigation.navigate(ROUTES.WORKSPACE_WORKFLOWS.getRoute(policyID));
},
},
{
icon: illustrations.Rules,
Expand All @@ -236,6 +268,12 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
}
enablePolicyRules(policyID, isEnabled);
},
onPress: () => {
if (!policyID) {
return;
}
Navigation.navigate(ROUTES.WORKSPACE_RULES.getRoute(policyID));
},
},
];

Expand All @@ -252,6 +290,12 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
}
enablePolicyInvoicing(policyID, isEnabled);
},
onPress: () => {
if (!policyID) {
return;
}
Navigation.navigate(ROUTES.WORKSPACE_INVOICES.getRoute(policyID));
},
},
];

Expand All @@ -270,6 +314,12 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
}
enablePolicyCategories(policyID, isEnabled, policyTagLists, policyCategories, allTransactionViolations, true);
},
onPress: () => {
if (!policyID) {
return;
}
Navigation.navigate(ROUTES.WORKSPACE_CATEGORIES.getRoute(policyID));
},
},
{
icon: illustrations.Tag,
Expand All @@ -285,6 +335,12 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
}
enablePolicyTags({policyID, enabled: isEnabled, policyTags: policyTagLists});
},
onPress: () => {
if (!policyID) {
return;
}
Navigation.navigate(ROUTES.WORKSPACE_TAGS.getRoute(policyID));
},
},
{
icon: illustrations.Coins,
Expand All @@ -300,6 +356,12 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
}
enablePolicyTaxes(policyID, isEnabled);
},
onPress: () => {
if (!policyID) {
return;
}
Navigation.navigate(ROUTES.WORKSPACE_TAXES.getRoute(policyID));
},
},
];

Expand Down Expand Up @@ -330,6 +392,12 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
}
clearPolicyErrorField(policyID, CONST.POLICY.MORE_FEATURES.ARE_CONNECTIONS_ENABLED);
},
onPress: () => {
if (!policyID) {
return;
}
Navigation.navigate(ROUTES.POLICY_ACCOUNTING.getRoute(policyID));
},
},
];

Expand Down Expand Up @@ -360,6 +428,12 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
}
clearPolicyErrorField(policyID, CONST.POLICY.MORE_FEATURES.ARE_RECEIPT_PARTNERS_ENABLED);
},
onPress: () => {
if (!policyID) {
return;
}
Navigation.navigate(ROUTES.WORKSPACE_RECEIPT_PARTNERS.getRoute(policyID));
},
});
}

Expand Down Expand Up @@ -391,30 +465,42 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro
},
];

const getItemStyle = useCallback(
(item: Item, hovered: boolean) => [
styles.workspaceSectionMoreFeaturesItem,
shouldUseNarrowLayout && styles.flexBasis100,
shouldUseNarrowLayout && StyleUtils.getMinimumWidth(0),
hovered && item.isActive && !!item.onPress && styles.hoveredComponentBG,
],
[styles.workspaceSectionMoreFeaturesItem, styles.flexBasis100, styles.hoveredComponentBG, shouldUseNarrowLayout, StyleUtils],
);

const renderItem = useCallback(
(item: Item) => (
<View
key={item.titleTranslationKey}
style={[styles.workspaceSectionMoreFeaturesItem, shouldUseNarrowLayout && styles.flexBasis100, shouldUseNarrowLayout && StyleUtils.getMinimumWidth(0)]}
>
<ToggleSettingOptionRow
icon={item.icon}
disabled={item.disabled}
disabledAction={item.disabledAction}
title={translate(item.titleTranslationKey)}
titleStyle={styles.textStrong}
subtitle={translate(item.subtitleTranslationKey)}
switchAccessibilityLabel={translate(item.subtitleTranslationKey)}
isActive={item.isActive}
pendingAction={item.pendingAction}
onToggle={item.action}
showLockIcon={item.disabled}
errors={item.errors}
onCloseError={item.onCloseError}
/>
</View>
<Hoverable key={item.titleTranslationKey}>
{(hovered) => (
<View style={getItemStyle(item, hovered)}>
<ToggleSettingOptionRow
icon={item.icon}
disabled={item.disabled}
disabledAction={item.disabledAction}
title={translate(item.titleTranslationKey)}
titleStyle={styles.textStrong}
subtitle={translate(item.subtitleTranslationKey)}
switchAccessibilityLabel={translate(item.subtitleTranslationKey)}
isActive={item.isActive}
pendingAction={item.pendingAction}
onToggle={item.action}
showLockIcon={item.disabled}
errors={item.errors}
onCloseError={item.onCloseError}
onPress={item.onPress}
/>
</View>
)}
</Hoverable>
),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❌ PERF-6 (docs)

The renderItem callback should include item.onPress and item.isActive in its dependency array, or better yet, specify the individual properties used from each item parameter. Currently, the callback uses item.onPress and item.isActive from the function parameter, but these aren't tracked in the dependencies.

Suggested fix: Since renderItem is called with different items from .map(), the current dependencies are acceptable. However, if you're using this pattern, consider that hovered comes from the Hoverable's render prop and doesn't need to be in dependencies. The current implementation is actually acceptable for this use case.

[styles, StyleUtils, shouldUseNarrowLayout, translate],
[styles, StyleUtils, shouldUseNarrowLayout, translate, getItemStyle],
);

/** Used to fill row space in the Section items when there are odd number of items to create equal margins for last odd item. */
Expand Down
55 changes: 39 additions & 16 deletions src/pages/workspace/workflows/ToggleSettingsOptionRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {StyleProp, TextStyle, ViewStyle} from 'react-native';
import Accordion from '@components/Accordion';
import Icon from '@components/Icon';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback';
import RenderHTML from '@components/RenderHTML';
import Switch from '@components/Switch';
import Text from '@components/Text';
Expand Down Expand Up @@ -77,6 +78,9 @@ type ToggleSettingOptionRowProps = {

/** Callback to fire when the switch is toggled in disabled state */
disabledAction?: () => void;

/** Callback to fire when the content area is pressed (only works when isActive is true) */
onPress?: () => void;
};
const ICON_SIZE = 48;

Expand All @@ -102,6 +106,7 @@ function ToggleSettingOptionRow({
onCloseError,
disabled = false,
showLockIcon = false,
onPress,
}: ToggleSettingOptionRowProps) {
const styles = useThemeStyles();
const {isAccordionExpanded, shouldAnimateAccordionSection} = useAccordionAnimation(isActive);
Expand Down Expand Up @@ -153,6 +158,27 @@ function ToggleSettingOptionRow({
processedSubtitle,
]);

const contentArea = (
<View style={[styles.flexRow, styles.alignItemsCenter, styles.flex1]}>
{!!icon && (
<Icon
src={icon}
height={ICON_SIZE}
width={ICON_SIZE}
additionalStyles={[styles.mr3]}
/>
)}
{customTitle ?? (
<View style={[styles.flexColumn, styles.flex1]}>
<Text style={[styles.textNormal, styles.lh20, titleStyle]}>{title}</Text>
{!shouldPlaceSubtitleBelowSwitch && subtitle && subTitleView}
</View>
)}
</View>
);

const shouldMakeContentPressable = isActive && onPress;

return (
<OfflineWithFeedback
pendingAction={pendingAction}
Expand All @@ -163,22 +189,19 @@ function ToggleSettingOptionRow({
>
<View style={styles.pRelative}>
<View style={[styles.flexRow, styles.alignItemsCenter, styles.justifyContentBetween, shouldPlaceSubtitleBelowSwitch && styles.h10]}>
<View style={[styles.flexRow, styles.alignItemsCenter, styles.flex1]}>
{!!icon && (
<Icon
src={icon}
height={ICON_SIZE}
width={ICON_SIZE}
additionalStyles={[styles.mr3]}
/>
)}
{customTitle ?? (
<View style={[styles.flexColumn, styles.flex1]}>
<Text style={[styles.textNormal, styles.lh20, titleStyle]}>{title}</Text>
{!shouldPlaceSubtitleBelowSwitch && subtitle && subTitleView}
</View>
)}
</View>
{shouldMakeContentPressable ? (
<PressableWithoutFeedback
style={[styles.flexRow, styles.alignItemsCenter, styles.flex1]}
onPress={onPress}
accessibilityLabel={title}
role="button"
accessible={false}
>
{contentArea}
</PressableWithoutFeedback>
) : (
contentArea
)}
<Switch
disabledAction={disabledAction}
accessibilityLabel={switchAccessibilityLabel}
Expand Down