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
4 changes: 3 additions & 1 deletion src/components/KYCWall/BaseKYCWall.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,14 @@ function KYCWall({
return;
}

const lastWorkspaceNumber = lastWorkspaceNumberSelector(policies, currentUserEmail);
const lastWorkspaceNumber = lastWorkspaceNumberSelector(policies, currentUserEmail, currentUserDetails.displayName);
const {policyID, workspaceChatReportID, adminsChatReportID} =
createWorkspaceFromIOUPayment({
iouReport,
reportPreviewAction,
currentUserAccountID,
currentUserEmail,
currentUserDisplayName: currentUserDetails.displayName,
iouReportOwnerEmail: employeeLogin ?? '',
currentUserLocalCurrency: localCurrency,
lastWorkspaceNumber,
Expand Down Expand Up @@ -277,6 +278,7 @@ function KYCWall({
reportPreviewAction,
currentUserAccountID,
currentUserEmail,
currentUserDetails.displayName,
employeeLogin,
doesSubmitterPersonalDetailExist,
introSelected,
Expand Down
13 changes: 9 additions & 4 deletions src/components/WorkspaceConfirmationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'
import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import usePersonalDetailByLogin from '@hooks/usePersonalDetailByLogin';
import useThemeStyles from '@hooks/useThemeStyles';

import {clearDraftValues} from '@libs/actions/FormActions';
import type {PolicyOwner} from '@libs/actions/Policy/Policy';
import {generateDefaultWorkspaceName, generatePolicyID} from '@libs/actions/Policy/Policy';
import type {CustomRNImageManipulatorResult} from '@libs/cropOrRotateImage/types';
import {addErrorMessage} from '@libs/ErrorUtils';
Expand All @@ -18,6 +20,7 @@ import {isRequiredFulfilled} from '@libs/ValidationUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {DYNAMIC_ROUTES} from '@src/ROUTES';
import {accountIDSelector, displayNameSelector} from '@src/selectors/PersonalDetails';
import {lastWorkspaceNumberSelector} from '@src/selectors/Policy';
import type {PolicyType} from '@src/types/form/WorkspaceConfirmationForm';
import INPUT_IDS from '@src/types/form/WorkspaceConfirmationForm';
Expand Down Expand Up @@ -47,7 +50,7 @@ type WorkspaceConfirmationSubmitFunctionParams = {
name: string;
currency: string;
planType?: PolicyType;
owner?: string;
owner?: PolicyOwner;
makeMeAdmin: boolean;
avatarFile: File | CustomRNImageManipulatorResult | undefined;
policyID: string;
Expand Down Expand Up @@ -129,8 +132,9 @@ function WorkspaceConfirmationForm({
const [draftValues] = useOnyx(ONYXKEYS.FORMS.WORKSPACE_CONFIRMATION_FORM_DRAFT);

const email = policyOwnerEmail || (session?.email ?? '');
const lastWorkspaceNumber = lastWorkspaceNumberSelector(policies, email);
const defaultWorkspaceName = generateDefaultWorkspaceName(email, lastWorkspaceNumber, translate);
const userDisplayName = usePersonalDetailByLogin(email, displayNameSelector);
const lastWorkspaceNumber = lastWorkspaceNumberSelector(policies, email, userDisplayName);
const defaultWorkspaceName = generateDefaultWorkspaceName(email, lastWorkspaceNumber, translate, userDisplayName);
const [workspaceNameFirstCharacter, setWorkspaceNameFirstCharacter] = useState(defaultWorkspaceName ?? '');

const userCurrency = draftValues?.currency ?? currentUserPersonalDetails?.localCurrencyCode ?? CONST.CURRENCY.USD;
Expand All @@ -140,6 +144,7 @@ function WorkspaceConfirmationForm({

const userOwner = draftValues?.owner ?? defaultOwner;
const ownerDisplayName = userOwner;
const ownerAccountID = usePersonalDetailByLogin(userOwner, accountIDSelector);

const [makeMeAdmin, setMakeMeAdmin] = useState(true);
const currentUserEmail = session?.email ?? '';
Expand Down Expand Up @@ -218,7 +223,7 @@ function WorkspaceConfirmationForm({
name: val[INPUT_IDS.NAME],
currency: val[INPUT_IDS.CURRENCY],
planType: isApprovedAccountant ? val[INPUT_IDS.PLAN_TYPE] : undefined,
owner: isApprovedAccountant ? val[INPUT_IDS.OWNER] : '',
owner: isApprovedAccountant ? {email: val[INPUT_IDS.OWNER], accountID: ownerAccountID} : undefined,
makeMeAdmin: isApprovedAccountant && isOwnerDifferentFromCurrentUser ? makeMeAdmin : false,
avatarFile,
policyID,
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useAutoCreateSubmitWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function useAutoCreateSubmitWorkspace() {

const {adminsChatReportID: newAdminsChatReportID, policyID: newPolicyID} = shouldCreateWorkspace
? createWorkspace({
policyOwnerEmail: undefined,
policyOwner: undefined,
makeMeAdmin: true,
policyName: generateDefaultWorkspaceName(currentUserEmail, lastWorkspaceNumber, translate, displayName),
policyID: generatePolicyID(),
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useAutoCreateTrackWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function useAutoCreateTrackWorkspace() {

const {adminsChatReportID: newAdminsChatReportID, policyID: newPolicyID} = shouldCreateWorkspace
? createWorkspace({
policyOwnerEmail: undefined,
policyOwner: undefined,
makeMeAdmin: true,
policyName: generateDefaultWorkspaceName(currentUserEmail, lastWorkspaceNumber, translate, displayName),
policyID: generatePolicyID(),
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useCompleteOnboarding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ function useCompleteOnboarding() {

const {adminsChatReportID, policyID} = shouldCreateWorkspace
? createWorkspace({
policyOwnerEmail: undefined,
policyOwner: undefined,
makeMeAdmin: true,
policyName: generateDefaultWorkspaceName(email, lastWorkspaceNumber, translate),
policyName: generateDefaultWorkspaceName(email, lastWorkspaceNumber, translate, currentUserPersonalDetails.displayName),
policyID: generatePolicyID(),
engagementChoice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM,
currency: currentUserPersonalDetails?.localCurrencyCode ?? '',
Expand Down
9 changes: 6 additions & 3 deletions src/hooks/useLastWorkspaceNumber.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ONYXKEYS from '@src/ONYXKEYS';
import {displayNameSelector} from '@src/selectors/PersonalDetails';
import {lastWorkspaceNumberSelector} from '@src/selectors/Policy';
import {emailSelector} from '@src/selectors/Session';
import type {Policy} from '@src/types/onyx';
Expand All @@ -8,13 +9,15 @@ import type {OnyxCollection} from 'react-native-onyx';
import {useMemo} from 'react';

import useOnyx from './useOnyx';
import usePersonalDetailByLogin from './usePersonalDetailByLogin';

function useLastWorkspaceNumber(email?: string) {
const [sessionEmail] = useOnyx(ONYXKEYS.SESSION, {selector: emailSelector});
const resolvedEmail = email ?? sessionEmail ?? '';
// Memoize so the POLICY-collection regex scan only re-runs when the resolved email changes, not on every render
// of consumers (e.g. the expense header renders this on every expense).
const selector = useMemo(() => (policies: OnyxCollection<Policy>) => lastWorkspaceNumberSelector(policies, resolvedEmail), [resolvedEmail]);
const userDisplayName = usePersonalDetailByLogin(resolvedEmail, displayNameSelector);
// Memoize so the POLICY-collection regex scan only re-runs when the resolved email or display name changes, not on
// every render of consumers (e.g. the expense header renders this on every expense).
const selector = useMemo(() => (policies: OnyxCollection<Policy>) => lastWorkspaceNumberSelector(policies, resolvedEmail, userDisplayName), [resolvedEmail, userDisplayName]);
const [lastWorkspaceNumber] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector});
return lastWorkspaceNumber;
}
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useParticipantSubmission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,10 @@ function useParticipantSubmission({

if ((isCategorizing || isShareAction) && numberOfParticipants.current === 0) {
const email = userDetails.email ?? '';
const lastWorkspaceNumber = lastWorkspaceNumberSelector(policies, email);
const lastWorkspaceNumber = lastWorkspaceNumberSelector(policies, email, userDetails.displayName);
const {expenseChatReportID, policyID, policyName} = createDraftWorkspace({
introSelected: intro,
workspaceName: generateDefaultWorkspaceName(email, lastWorkspaceNumber, translate),
workspaceName: generateDefaultWorkspaceName(email, lastWorkspaceNumber, translate, userDetails.displayName),
currentUserAccountID: userDetails.accountID,
currentUserEmail: email,
currency: userDetails.localCurrencyCode ?? CONST.CURRENCY.USD,
Expand Down
5 changes: 2 additions & 3 deletions src/libs/Navigation/AppNavigator/AuthScreens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ function AuthScreens() {
};

return (
<>
<PersonalDetailsByLoginProvider>
<AuthScreensInitHandler />
<SearchRouterWarmup />
<SubmitIntentDeeplinkHandler />
Expand All @@ -174,7 +174,6 @@ function AuthScreens() {
<DelegatorConnectGuard>
<ComposeProviders
components={[
PersonalDetailsByLoginProvider,
AttachmentModalContextProvider,
PlaybackContextProvider,
VolumeContextProvider,
Expand Down Expand Up @@ -401,7 +400,7 @@ function AuthScreens() {
<PriorityModeController />
</ComposeProviders>
</DelegatorConnectGuard>
</>
</PersonalDetailsByLoginProvider>
);
}

Expand Down
15 changes: 11 additions & 4 deletions src/libs/Navigation/AppNavigator/AuthScreensInitHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import useLastWorkspaceNumber from '@hooks/useLastWorkspaceNumber';
import useLocalize from '@hooks/useLocalize';
import useOneTransactionThreadReportID from '@hooks/useOneTransactionThreadReportID';
import useOnyx from '@hooks/useOnyx';
import usePersonalDetailByLogin from '@hooks/usePersonalDetailByLogin';
import useReconcileHighContrastIntent from '@hooks/useReconcileHighContrastIntent';
import useReportAttributes from '@hooks/useReportAttributes';
import useRootNavigationState from '@hooks/useRootNavigationState';
Expand Down Expand Up @@ -40,6 +41,7 @@ import ROUTES from '@src/ROUTES';
import type {ReportAttributesDerivedValue} from '@src/types/onyx';

import {guidedSetupAndTourStatusSelector} from '@selectors/Onboarding';
import {accountIDSelector, displayNameSelector} from '@selectors/PersonalDetails';
import {useEffect, useRef} from 'react';

function initializePusher(
Expand Down Expand Up @@ -93,6 +95,9 @@ function AuthScreensInitHandler() {
const [conciergeReportID] = useOnyx(ONYXKEYS.CONCIERGE_REPORT_ID);
const [conciergeChat] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${conciergeReportID}`);
const lastWorkspaceNumber = useLastWorkspaceNumber(ownerEmail ?? undefined);
const policyOwnerLogin = ownerEmail ?? session?.email;
const policyOwnerAccountID = usePersonalDetailByLogin(policyOwnerLogin, accountIDSelector);
const policyOwnerDisplayName = usePersonalDetailByLogin(policyOwnerLogin, displayNameSelector);
Comment thread
bernhardoj marked this conversation as resolved.
Resolved
const activePolicy = useActivePolicy();
const currentUserPersonalDetails = useCurrentUserPersonalDetails();

Expand Down Expand Up @@ -200,18 +205,20 @@ function AuthScreensInitHandler() {
App.reconnectApp(initialLastUpdateIDAppliedToClient);
}

App.setUpPoliciesAndNavigate(
App.setUpPoliciesAndNavigate({
session,
introSelected,
currentUserPersonalDetails.localCurrencyCode ?? CONST.CURRENCY.USD,
currency: currentUserPersonalDetails.localCurrencyCode ?? CONST.CURRENCY.USD,
activePolicy,
guidedSetupAndTourStatus?.isSelfTourViewed,
isSelfTourViewed: guidedSetupAndTourStatus?.isSelfTourViewed,
betas,
hasActiveAdminPolicies,
lastWorkspaceNumber,
translate,
conciergeChat,
);
policyOwnerAccountID,
policyOwnerDisplayName,
});

Download.clearDownloads();
clearStaleExportDownloads();
Expand Down
61 changes: 39 additions & 22 deletions src/libs/actions/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {Str} from 'expensify-common';
import {AppState} from 'react-native';
import Onyx from 'react-native-onyx';

import type {PolicyOwner} from './Policy/Policy';

import clearOnyxAndSeedFullReconnect from './clearOnyxAndSeedFullReconnect';
import {setShouldForceOffline} from './Network';
import {getAll, rollbackOngoingRequest, save} from './PersistedRequests';
Expand Down Expand Up @@ -594,7 +596,7 @@ type PolicyType = typeof CONST.POLICY.TYPE.TEAM | typeof CONST.POLICY.TYPE.CORPO
type CreateWorkspaceWithPolicyDraftParams = {
isSelfTourViewed: boolean | undefined;
introSelected: OnyxEntry<OnyxTypes.IntroSelected>;
policyOwnerEmail?: string;
policyOwner?: PolicyOwner;
policyName: string;
transitionFromOldDot?: boolean;
makeMeAdmin?: boolean;
Expand All @@ -621,7 +623,7 @@ type CreateWorkspaceWithPolicyDraftParams = {
function createWorkspaceWithPolicyDraftAndNavigateToIt(params: CreateWorkspaceWithPolicyDraftParams) {
const {
introSelected,
policyOwnerEmail = '',
policyOwner,
policyName,
transitionFromOldDot = false,
makeMeAdmin = false,
Expand Down Expand Up @@ -664,7 +666,7 @@ function createWorkspaceWithPolicyDraftAndNavigateToIt(params: CreateWorkspaceWi
savePolicyDraftByNewWorkspace({
policyID: policyIDWithDefault,
policyName,
policyOwnerEmail,
policyOwner,
makeMeAdmin,
currency,
file,
Expand Down Expand Up @@ -706,7 +708,6 @@ function createWorkspaceWithPolicyDraftAndNavigateToIt(params: CreateWorkspaceWi
function createWorkspaceWithPolicyDraft(params: CreateWorkspaceWithPolicyDraftParams) {
const {
introSelected,
policyOwnerEmail = '',
policyName,
makeMeAdmin = false,
policyID = '',
Expand Down Expand Up @@ -736,7 +737,6 @@ function createWorkspaceWithPolicyDraft(params: CreateWorkspaceWithPolicyDraftPa
savePolicyDraftByNewWorkspace({
policyID,
policyName,
policyOwnerEmail,
makeMeAdmin,
currency,
file,
Expand All @@ -758,7 +758,7 @@ type SavePolicyDraftByNewWorkspaceParams = {
isSelfTourViewed: boolean | undefined;
policyID?: string;
policyName: string;
policyOwnerEmail?: string;
policyOwner?: PolicyOwner;
makeMeAdmin?: boolean;
currency?: string;
file?: File;
Expand All @@ -782,7 +782,7 @@ type SavePolicyDraftByNewWorkspaceParams = {
function savePolicyDraftByNewWorkspace({
policyID,
policyName,
policyOwnerEmail = '',
policyOwner,
makeMeAdmin = false,
currency = '',
file,
Expand All @@ -801,7 +801,7 @@ function savePolicyDraftByNewWorkspace({
isAnnualSubscription = false,
}: SavePolicyDraftByNewWorkspaceParams) {
createWorkspace({
policyOwnerEmail,
policyOwner,
makeMeAdmin,
policyName,
policyID,
Expand Down Expand Up @@ -839,18 +839,35 @@ function savePolicyDraftByNewWorkspace({
* When the exitTo route is 'workspace/new', we create a new
* workspace and navigate to it
*/
function setUpPoliciesAndNavigate(
session: OnyxEntry<OnyxTypes.Session>,
introSelected: OnyxEntry<OnyxTypes.IntroSelected>,
currency: string,
activePolicy: OnyxEntry<OnyxTypes.Policy>,
isSelfTourViewed: boolean | undefined,
betas: OnyxEntry<OnyxTypes.Beta[]>,
hasActiveAdminPolicies: boolean,
lastWorkspaceNumber: number | undefined,
translate: LocalizedTranslate,
conciergeChat: OnyxEntry<OnyxTypes.Report>,
) {
type SetUpPoliciesAndNavigateParams = {
session: OnyxEntry<OnyxTypes.Session>;
introSelected: OnyxEntry<OnyxTypes.IntroSelected>;
currency: string;
activePolicy: OnyxEntry<OnyxTypes.Policy>;
isSelfTourViewed: boolean | undefined;
betas: OnyxEntry<OnyxTypes.Beta[]>;
hasActiveAdminPolicies: boolean;
lastWorkspaceNumber: number | undefined;
translate: LocalizedTranslate;
conciergeChat: OnyxEntry<OnyxTypes.Report>;
policyOwnerAccountID: number | undefined;
policyOwnerDisplayName: string | undefined;
};

function setUpPoliciesAndNavigate({
session,
introSelected,
currency,
activePolicy,
isSelfTourViewed,
betas,
hasActiveAdminPolicies,
lastWorkspaceNumber,
translate,
conciergeChat,
policyOwnerAccountID,
policyOwnerDisplayName,
}: SetUpPoliciesAndNavigateParams) {
const currentUrl = getCurrentUrl();
if (!session || !currentUrl?.includes('exitTo')) {
return;
Expand All @@ -874,8 +891,8 @@ function setUpPoliciesAndNavigate(
createWorkspaceWithPolicyDraftAndNavigateToIt({
introSelected,
currency,
policyOwnerEmail,
policyName: policyName || generateDefaultWorkspaceName(policyOwnerEmail, lastWorkspaceNumber, translate),
policyOwner: {email: policyOwnerEmail, accountID: policyOwnerAccountID},
policyName: policyName || generateDefaultWorkspaceName(policyOwnerEmail, lastWorkspaceNumber, translate, policyOwnerDisplayName),
transitionFromOldDot: true,
makeMeAdmin,
activePolicy,
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/IOU/PayMoneyRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ function getPayMoneyRequestParams({
successData: policySuccessData,
params,
} = buildPolicyData({
policyOwnerEmail: currentUserEmailParam,
policyOwner: {email: currentUserEmailParam, accountID: currentUserAccountIDParam},
policyName: defaultWorkspaceName,
makeMeAdmin: true,
policyID: payerPolicyID,
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/IOU/TrackExpense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ function getTrackExpenseInformation(params: GetTrackExpenseInformationParams): T

if (isDraftReportLocal) {
const workspaceData = buildPolicyData({
policyOwnerEmail: undefined,
policyOwner: undefined,
makeMeAdmin: policy?.makeMeAdmin,
policyName: policy?.name ?? defaultWorkspaceName ?? '',
policyID: policy?.id,
Expand Down
Loading
Loading