Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions src/CONST/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8669,6 +8669,7 @@ const CONST = {
CARD_LIST_ROW: 'WorkspaceExpensifyCard-CardListRow',
},
WORKSPACE: {
APPROVAL_WORKFLOW_SECTION: 'Workspace-ApprovalWorkflowSection',
Comment thread
mountiny marked this conversation as resolved.
Resolved
TOGGLE_SETTINGS_ROW: 'Workspace-ToggleSettingsRow',
DUPLICATE_SELECT_FEATURES_SELECT_ALL: 'WorkspaceDuplicate-SelectFeaturesSelectAll',
WORKSPACE_MENU_ITEM: 'Workspace-WorkspaceMenuItem',
Expand Down
9 changes: 3 additions & 6 deletions src/components/ApprovalWorkflowSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ import React from 'react';
import {View} from 'react-native';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import usePersonalDetailsByEmail from '@hooks/usePersonalDetailsByEmail';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import {sortAlphabetically} from '@libs/OptionsListUtils';
import {getApprovalLimitDescription} from '@libs/WorkflowUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {personalDetailsByEmailSelector} from '@src/selectors/PersonalDetails';
import type ApprovalWorkflow from '@src/types/onyx/ApprovalWorkflow';
import Icon from './Icon';
import MenuItem from './MenuItem';
Expand All @@ -35,9 +33,7 @@ function ApprovalWorkflowSection({approvalWorkflow, onPress, currency = CONST.CU
const theme = useTheme();
const {translate, toLocaleOrdinal, localeCompare} = useLocalize();
const {shouldUseNarrowLayout} = useResponsiveLayout();
const [personalDetailsByEmail] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {
selector: personalDetailsByEmailSelector,
});
const personalDetailsByEmail = usePersonalDetailsByEmail();

const approverTitle = (index: number) =>
approvalWorkflow.approvers.length > 1 ? `${toLocaleOrdinal(index + 1, true)} ${translate('workflowsPage.approver').toLowerCase()}` : `${translate('workflowsPage.approver')}`;
Expand All @@ -50,6 +46,7 @@ function ApprovalWorkflowSection({approvalWorkflow, onPress, currency = CONST.CU
return (
<PressableWithoutFeedback
accessibilityRole="button"
sentryLabel={CONST.SENTRY_LABEL.WORKSPACE.APPROVAL_WORKFLOW_SECTION}
style={[styles.border, shouldUseNarrowLayout ? styles.p3 : styles.p4, styles.flexRow, styles.justifyContentBetween, styles.mt6, styles.mbn3]}
onPress={onPress}
accessibilityLabel={translate('workflowsPage.accessibilityLabel', {
Expand Down
16 changes: 3 additions & 13 deletions src/components/ReportWelcomeText.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {createPersonalDetailsSelector} from '@selectors/PersonalDetails';
import React from 'react';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import useEnvironment from '@hooks/useEnvironment';
import useLocalize from '@hooks/useLocalize';
import useMappedPersonalDetails, {personalDetailMapper} from '@hooks/useMappedPersonalDetails';
import useOnyx from '@hooks/useOnyx';
import usePreferredPolicy from '@hooks/usePreferredPolicy';
import useReportIsArchived from '@hooks/useReportIsArchived';
Expand All @@ -27,7 +27,7 @@ import CONST from '@src/CONST';
import type {IOUType} from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {OnyxInputOrEntry, PersonalDetails, PersonalDetailsList, Policy, Report} from '@src/types/onyx';
import type {OnyxInputOrEntry, PersonalDetailsList, Policy, Report} from '@src/types/onyx';
import RenderHTML from './RenderHTML';
import Text from './Text';

Expand All @@ -39,21 +39,11 @@ type ReportWelcomeTextProps = {
policy: OnyxEntry<Policy>;
};

const personalDetailSelector = (personalDetail: OnyxInputOrEntry<PersonalDetails>): OnyxInputOrEntry<PersonalDetails> =>
personalDetail && {
accountID: personalDetail.accountID,
login: personalDetail.login,
avatar: personalDetail.avatar,
pronouns: personalDetail.pronouns,
};

const personalDetailsSelector = (personalDetail: OnyxEntry<PersonalDetailsList>) => createPersonalDetailsSelector(personalDetail, personalDetailSelector);

function ReportWelcomeText({report, policy}: ReportWelcomeTextProps) {
const {translate, localeCompare} = useLocalize();
const styles = useThemeStyles();
const {environmentURL} = useEnvironment();
const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: personalDetailsSelector});
const [personalDetails] = useMappedPersonalDetails(personalDetailMapper);
const {isRestrictedToPreferredPolicy} = usePreferredPolicy();
const isPolicyExpenseChat = isPolicyExpenseChatReportUtils(report);
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
Expand Down
31 changes: 31 additions & 0 deletions src/hooks/useMappedPersonalDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type {OnyxEntry} from 'react-native-onyx';
import ONYXKEYS from '@src/ONYXKEYS';
import type {OnyxInputOrEntry, PersonalDetails} from '@src/types/onyx';
import useOnyx from './useOnyx';

const personalDetailMapper = (personalDetail: OnyxInputOrEntry<PersonalDetails>): OnyxInputOrEntry<PersonalDetails> =>
personalDetail && {
accountID: personalDetail.accountID,
login: personalDetail.login,
avatar: personalDetail.avatar,
pronouns: personalDetail.pronouns,
};

/**
* Subscribes to all personal details and transforms each one via the provided mapper.
* Unlike passing a selector directly to useOnyx (which triggers expensive deepEqual
* comparisons on the entire mapped collection), this hook lets Onyx use cheap
* shallowEqual on raw personal detail references, then maps the collection inline.
*/
function useMappedPersonalDetails<T>(mapper: (personalDetail: OnyxEntry<PersonalDetails>) => T) {
const [personalDetails, metadata] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST);
const transformed = Object.entries(personalDetails ?? {}).reduce<Record<string, T>>((acc, [key, entry]) => {
acc[key] = mapper(entry ?? undefined);
return acc;
}, {});
Comment thread
TMisiukiewicz marked this conversation as resolved.
Resolved

return [transformed, metadata] as const;
}

export {personalDetailMapper};
export default useMappedPersonalDetails;
18 changes: 18 additions & 0 deletions src/hooks/usePersonalDetailsByEmail.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import lodashMapKeys from 'lodash/mapKeys';
import ONYXKEYS from '@src/ONYXKEYS';
import useOnyx from './useOnyx';

/**
* Subscribes to all personal details and remaps them by email (login) instead of account ID.
* Unlike passing a selector directly to useOnyx (which triggers expensive deepEqual
* comparisons on the entire mapped collection), this hook lets Onyx use cheap
* shallowEqual on raw personal detail references, then remaps inline.
*/
function usePersonalDetailsByEmail() {
const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST);
const result = personalDetails ? lodashMapKeys(personalDetails, (value, key) => value?.login ?? key) : undefined;
Comment thread
TMisiukiewicz marked this conversation as resolved.
Resolved

return result;
}

export default usePersonalDetailsByEmail;
15 changes: 3 additions & 12 deletions src/pages/Debug/Report/DebugReportActions.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {createPersonalDetailsSelector} from '@selectors/PersonalDetails';
import React, {useCallback, useMemo} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import Button from '@components/Button';
Expand All @@ -7,6 +6,7 @@ import SelectionList from '@components/SelectionList';
import RadioListItem from '@components/SelectionList/ListItem/RadioListItem';
import useDebouncedState from '@hooks/useDebouncedState';
import useLocalize from '@hooks/useLocalize';
import useMappedPersonalDetails, {personalDetailMapper} from '@hooks/useMappedPersonalDetails';
import useOnyx from '@hooks/useOnyx';
import useReportIsArchived from '@hooks/useReportIsArchived';
import useThemeStyles from '@hooks/useThemeStyles';
Expand All @@ -19,20 +19,11 @@ import SidebarUtils from '@libs/SidebarUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {OnyxInputOrEntry, PersonalDetails, PersonalDetailsList, ReportAction, ReportActions} from '@src/types/onyx';
import type {OnyxInputOrEntry, PersonalDetailsList, ReportAction, ReportActions} from '@src/types/onyx';

type DebugReportActionsProps = {
reportID: string;
};
const personalDetailSelector = (personalDetail: OnyxInputOrEntry<PersonalDetails>): OnyxInputOrEntry<PersonalDetails> =>
personalDetail && {
accountID: personalDetail.accountID,
login: personalDetail.login,
avatar: personalDetail.avatar,
pronouns: personalDetail.pronouns,
};

const personalDetailsSelector = (personalDetail: OnyxEntry<PersonalDetailsList>) => createPersonalDetailsSelector(personalDetail, personalDetailSelector);

function DebugReportActions({reportID}: DebugReportActionsProps) {
const {translate, datetimeToCalendarTime, localeCompare} = useLocalize();
Expand All @@ -44,7 +35,7 @@ function DebugReportActions({reportID}: DebugReportActionsProps) {
const [invoiceReceiverPolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${invoiceReceiverPolicyID}`);
const isReportArchived = useReportIsArchived(reportID);
const ifUserCanPerformWriteAction = canUserPerformWriteAction(report, isReportArchived);
const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {selector: personalDetailsSelector});
const [personalDetails] = useMappedPersonalDetails(personalDetailMapper);

const getSortedAllReportActionsSelector = useCallback(
(allReportActions: OnyxEntry<ReportActions>): ReportAction[] => {
Expand Down
2 changes: 1 addition & 1 deletion src/pages/domain/Members/DomainMemberDetailsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {domainNameSelector, selectSecurityGroupForAccount, vacationDelegateSelector} from '@selectors/Domain';
import {personalDetailsSelector} from '@selectors/PersonalDetails';
import personalDetailsSelector from '@selectors/PersonalDetails';
import React, {useState} from 'react';
import Button from '@components/Button';
import DecisionModal from '@components/DecisionModal';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {vacationDelegateSelector} from '@selectors/Domain';
import {personalDetailsSelector} from '@selectors/PersonalDetails';
import personalDetailsSelector from '@selectors/PersonalDetails';
import React from 'react';
import BaseVacationDelegateSelectionComponent from '@components/BaseVacationDelegateSelectionComponent';
import ScreenWrapper from '@components/ScreenWrapper';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ import ScrollView from '@components/ScrollView';
import Text from '@components/Text';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import usePersonalDetailsByEmail from '@hooks/usePersonalDetailsByEmail';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import {sortAlphabetically} from '@libs/OptionsListUtils';
import {isControlPolicy} from '@libs/PolicyUtils';
import {getApprovalLimitDescription} from '@libs/WorkflowUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import {personalDetailsByEmailSelector} from '@src/selectors/PersonalDetails';
import type {ApprovalWorkflowOnyx, Policy} from '@src/types/onyx';
import type {Approver} from '@src/types/onyx/ApprovalWorkflow';
import type {PendingAction} from '@src/types/onyx/OnyxCommon';
Expand All @@ -47,9 +45,7 @@ function ApprovalWorkflowEditor({approvalWorkflow, removeApprovalWorkflow, polic
const icons = useMemoizedLazyExpensifyIcons(['Trashcan']);
const styles = useThemeStyles();
const {translate, toLocaleOrdinal, localeCompare} = useLocalize();
const [personalDetailsByEmail] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {
selector: personalDetailsByEmailSelector,
});
const personalDetailsByEmail = usePersonalDetailsByEmail();
const approverCount = approvalWorkflow.approvers.length;
const currency = policy?.outputCurrency ?? CONST.CURRENCY.USD;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import useBottomSafeSafeAreaPaddingStyle from '@hooks/useBottomSafeSafeAreaPaddi
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import usePersonalDetailsByEmail from '@hooks/usePersonalDetailsByEmail';
import useThemeStyles from '@hooks/useThemeStyles';
import {convertToBackendAmount, convertToFrontendAmountAsString} from '@libs/CurrencyUtils';
import Navigation from '@libs/Navigation/Navigation';
Expand All @@ -29,7 +30,6 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import {personalDetailsByEmailSelector} from '@src/selectors/PersonalDetails';
import type {Approver} from '@src/types/onyx/ApprovalWorkflow';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

Expand All @@ -41,9 +41,7 @@ function WorkspaceWorkflowsApprovalsApprovalLimitPage({policy, isLoadingReportDa
const {translate} = useLocalize();
const icons = useMemoizedLazyExpensifyIcons(['Trashcan'] as const);
const [approvalWorkflow] = useOnyx(ONYXKEYS.APPROVAL_WORKFLOW);
const [personalDetailsByEmail] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {
selector: personalDetailsByEmailSelector,
});
const personalDetailsByEmail = usePersonalDetailsByEmail();

const policyID = route.params.policyID;
const approverIndex = Number(route.params.approverIndex) || 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Text from '@components/Text';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import usePersonalDetailsByEmail from '@hooks/usePersonalDetailsByEmail';
import useThemeStyles from '@hooks/useThemeStyles';
import {clearApprovalWorkflowApprover, clearApprovalWorkflowApprovers, setApprovalWorkflowApprover} from '@libs/actions/Workflow';
import Navigation from '@libs/Navigation/Navigation';
Expand All @@ -20,7 +21,6 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import SCREENS from '@src/SCREENS';
import {personalDetailsByEmailSelector} from '@src/selectors/PersonalDetails';
import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue';

type WorkspaceWorkflowsApprovalsApproverPageProps = WithPolicyAndFullscreenLoadingProps &
Expand All @@ -32,9 +32,7 @@ function WorkspaceWorkflowsApprovalsApproverPage({policy, personalDetails, isLoa
const icons = useMemoizedLazyExpensifyIcons(['FallbackAvatar']);
const [approvalWorkflow, approvalWorkflowMetadata] = useOnyx(ONYXKEYS.APPROVAL_WORKFLOW);
const isApprovalWorkflowLoading = isLoadingOnyxValue(approvalWorkflowMetadata);
const [personalDetailsByEmail] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {
selector: personalDetailsByEmailSelector,
});
const personalDetailsByEmail = usePersonalDetailsByEmail();
const approverIndex = Number(route.params.approverIndex) ?? 0;
const rhpRoutes = useNavigationState((state) => state.routes);
const defaultApprover = getDefaultApprover(policy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Text from '@components/Text';
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import usePersonalDetailsByEmail from '@hooks/usePersonalDetailsByEmail';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
Expand All @@ -19,7 +20,6 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import {personalDetailsByEmailSelector} from '@src/selectors/PersonalDetails';
import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue';

type WorkspaceWorkflowsApprovalsOverLimitApproverPageProps = WithPolicyAndFullscreenLoadingProps &
Expand All @@ -30,9 +30,7 @@ function WorkspaceWorkflowsApprovalsOverLimitApproverPage({policy, personalDetai
const {translate} = useLocalize();
const [approvalWorkflow, approvalWorkflowMetadata] = useOnyx(ONYXKEYS.APPROVAL_WORKFLOW);
const isApprovalWorkflowLoading = isLoadingOnyxValue(approvalWorkflowMetadata);
const [personalDetailsByEmail] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST, {
selector: personalDetailsByEmailSelector,
});
const personalDetailsByEmail = usePersonalDetailsByEmail();
const expensifyIcons = useMemoizedLazyExpensifyIcons(['FallbackAvatar'] as const);

const policyID = route.params.policyID;
Expand Down
14 changes: 2 additions & 12 deletions src/selectors/PersonalDetails.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import lodashMapKeys from 'lodash/mapKeys';
import type {OnyxEntry} from 'react-native-onyx';
import type {OnyxInputOrEntry, PersonalDetails, PersonalDetailsList} from '@src/types/onyx';
import mapOnyxCollectionItems from '@src/utils/mapOnyxCollectionItems';

type PersonalDetailsSelector<T> = (personalDetails: OnyxInputOrEntry<PersonalDetails>) => T;

const createPersonalDetailsSelector = <T>(personalDetails: OnyxEntry<PersonalDetailsList>, personalDetailsSelector: PersonalDetailsSelector<T>) =>
mapOnyxCollectionItems(personalDetails, personalDetailsSelector);

const personalDetailsByEmailSelector = (personalDetails: OnyxEntry<PersonalDetailsList>) =>
personalDetails ? lodashMapKeys(personalDetails, (value, key) => value?.login ?? key) : undefined;
import type {PersonalDetailsList} from '@src/types/onyx';

const personalDetailsSelector = (accountID: number) => (personalDetailsList: OnyxEntry<PersonalDetailsList>) => personalDetailsList?.[accountID];

export {createPersonalDetailsSelector, personalDetailsByEmailSelector, personalDetailsSelector};
export default personalDetailsSelector;
2 changes: 1 addition & 1 deletion tests/unit/PersonalDetailsSelectorTest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {personalDetailsSelector} from '@selectors/PersonalDetails';
import personalDetailsSelector from '@selectors/PersonalDetails';
import type {PersonalDetailsList} from '@src/types/onyx';

describe('PersonalDetailsSelector', () => {
Expand Down
Loading
Loading