Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve default workspace naming #11875

Merged
merged 5 commits into from
Oct 28, 2022
Merged
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
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 @@ -741,17 +747,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