Skip to content

Commit

Permalink
Merge pull request #11875 from Expensify/arosiclair-workspace-naming
Browse files Browse the repository at this point in the history
Improve default workspace naming
  • Loading branch information
arosiclair authored Oct 28, 2022
2 parents 8ea6589 + 1076845 commit 84aa9c8
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Onyx from 'react-native-onyx';
import lodashGet from 'lodash/get';
import {PUBLIC_DOMAINS} from 'expensify-common/lib/CONST';
import Str from 'expensify-common/lib/str';
import {escapeRegExp} from 'lodash';
import * as DeprecatedAPI from '../deprecatedAPI';
import * as API from '../API';
import ONYXKEYS from '../../ONYXKEYS';
Expand All @@ -19,7 +20,12 @@ const allPolicies = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.POLICY,
callback: (val, key) => {
if (!val || !key) {
if (!key) {
return;
}

if (val === null || val === undefined) {
delete allPolicies[key];
return;
}

Expand Down Expand Up @@ -687,17 +693,14 @@ function generateDefaultWorkspaceName(email = '') {
return defaultWorkspaceName;
}

// Check if this name already exists in the policies
let suffix = 0;
_.forEach(allPolicies, (policy) => {
const name = lodashGet(policy, 'name', '');

if (name.toLowerCase().includes(defaultWorkspaceName.toLowerCase())) {
suffix += 1;
}
});

return suffix > 0 ? `${defaultWorkspaceName} ${suffix}` : defaultWorkspaceName;
// find default named workspaces and increment the last number
const numberRegEx = new RegExp(`${escapeRegExp(defaultWorkspaceName)} ?(\\d*)`, 'i');
const lastWorkspaceNumber = _.chain(allPolicies)
.filter(policy => policy.name && numberRegEx.test(policy.name))
.map(policy => parseInt(numberRegEx.exec(policy.name)[1] || 1, 10)) // parse the number at the end
.max()
.value();
return lastWorkspaceNumber !== -Infinity ? `${defaultWorkspaceName} ${lastWorkspaceNumber + 1}` : defaultWorkspaceName;
}

/**
Expand Down

0 comments on commit 84aa9c8

Please sign in to comment.