|
1 | 1 | import {emailSelector} from '@selectors/Session'; |
2 | 2 | import React from 'react'; |
3 | | -import type {OnyxEntry} from 'react-native-onyx'; |
| 3 | +import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; |
4 | 4 | import MenuItem from '@components/MenuItem'; |
5 | 5 | import useLocalize from '@hooks/useLocalize'; |
6 | 6 | import useOnyx from '@hooks/useOnyx'; |
@@ -35,37 +35,42 @@ type InvoiceSenderFieldProps = { |
35 | 35 | transaction: OnyxEntry<OnyxTypes.Transaction>; |
36 | 36 | }; |
37 | 37 |
|
| 38 | +const senderWorkspaceSelector = (policy: OnyxEntry<OnyxTypes.Policy>) => (policy ? {id: policy.id, name: policy.name, avatarURL: policy.avatarURL} : undefined); |
| 39 | + |
| 40 | +const createCanUpdateSenderWorkspaceSelector = |
| 41 | + (selectedParticipants: Participant[], currentUserLogin: string | undefined, isFromGlobalCreate: boolean) => |
| 42 | + (policies: OnyxCollection<OnyxTypes.Policy>): boolean => { |
| 43 | + const isInvoiceRoomParticipant = selectedParticipants.some((participant) => participant.isInvoiceRoom); |
| 44 | + return canSendInvoice(policies ?? null, currentUserLogin) && isFromGlobalCreate && !isInvoiceRoomParticipant; |
| 45 | + }; |
| 46 | + |
38 | 47 | function InvoiceSenderField({selectedParticipants, isReadOnly, didConfirm, iouType, reportID, transaction}: InvoiceSenderFieldProps) { |
39 | 48 | const styles = useThemeStyles(); |
40 | 49 | const {translate} = useLocalize(); |
41 | 50 |
|
42 | | - // canSendInvoice needs the full policy collection to check all admin workspaces |
43 | | - const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY); |
| 51 | + const senderPolicyID = selectedParticipants.find((participant) => participant.isSender)?.policyID; |
| 52 | + |
| 53 | + const [senderWorkspace] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${senderPolicyID}`, {selector: senderWorkspaceSelector}); |
44 | 54 | const [currentUserLogin] = useOnyx(ONYXKEYS.SESSION, {selector: emailSelector}); |
45 | 55 |
|
46 | 56 | const isFromGlobalCreate = !!transaction?.isFromGlobalCreate; |
47 | 57 |
|
48 | | - const senderWorkspace = (() => { |
49 | | - const senderWorkspaceParticipant = selectedParticipants.find((participant) => participant.isSender); |
50 | | - return allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${senderWorkspaceParticipant?.policyID}`]; |
51 | | - })(); |
52 | | - |
53 | | - const canUpdateSenderWorkspace = (() => { |
54 | | - const isInvoiceRoomParticipant = selectedParticipants.some((participant) => participant.isInvoiceRoom); |
55 | | - return canSendInvoice(allPolicies, currentUserLogin) && isFromGlobalCreate && !isInvoiceRoomParticipant; |
56 | | - })(); |
| 58 | + // canSendInvoice needs the full policy collection to check all admin workspaces |
| 59 | + const [canUpdateSenderWorkspace] = useOnyx(ONYXKEYS.COLLECTION.POLICY, { |
| 60 | + selector: createCanUpdateSenderWorkspaceSelector(selectedParticipants, currentUserLogin, isFromGlobalCreate), |
| 61 | + }); |
57 | 62 |
|
58 | 63 | return ( |
59 | 64 | <MenuItem |
60 | 65 | avatarID={senderWorkspace?.id} |
61 | | - shouldShowRightIcon={!isReadOnly && canUpdateSenderWorkspace} |
| 66 | + shouldShowRightIcon={!isReadOnly && !!canUpdateSenderWorkspace} |
62 | 67 | title={senderWorkspace?.name} |
63 | | - icon={senderWorkspace?.avatarURL ? senderWorkspace?.avatarURL : getDefaultWorkspaceAvatar(senderWorkspace?.name)} |
| 68 | + icon={senderWorkspace?.avatarURL ? senderWorkspace.avatarURL : getDefaultWorkspaceAvatar(senderWorkspace?.name)} |
64 | 69 | iconType={CONST.ICON_TYPE_WORKSPACE} |
65 | 70 | description={translate('workspace.common.workspace')} |
66 | 71 | label={translate('workspace.invoices.sendFrom')} |
67 | 72 | isLabelHoverable={false} |
68 | | - interactive={!isReadOnly && canUpdateSenderWorkspace} |
| 73 | + interactive={!isReadOnly && !!canUpdateSenderWorkspace} |
69 | 74 | onPress={() => { |
70 | 75 | if (!transaction?.transactionID) { |
71 | 76 | return; |
|
0 commit comments