Repository navigation
fix: Workspace - The new WS names appear in English when Spanish is set up. #55850
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
Changes from all commits
f2356e7
61faaac
04956f8
cce2332
d0c0a57
a777e8f
570944a
72934b8
8aa5ad6
b4d7bd4
6050236
d8f6fa0
ab7e0f8
5cbcb2d
f710ed2
fcd009d
84acf45
1b6dd9b
1cc31c6
5dbcd85
bd32149
4dcbb66
c871736
3e84282
2b02548
b1f617c
7d9787e
349e5f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,7 @@ import * as ErrorUtils from '@libs/ErrorUtils'; | |
| import {createFile} from '@libs/fileDownload/FileUtils'; | ||
| import getIsNarrowLayout from '@libs/getIsNarrowLayout'; | ||
| import GoogleTagManager from '@libs/GoogleTagManager'; | ||
| import {translate, translateLocal} from '@libs/Localize'; | ||
| import Log from '@libs/Log'; | ||
| import * as NetworkStore from '@libs/Network/NetworkStore'; | ||
| import * as NumberUtils from '@libs/NumberUtils'; | ||
|
|
@@ -1579,40 +1580,46 @@ function removeWorkspace(policyID: string) { | |
| */ | ||
| function generateDefaultWorkspaceName(email = ''): string { | ||
| const emailParts = email ? email.split('@') : sessionEmail.split('@'); | ||
| let defaultWorkspaceName = ''; | ||
| if (!emailParts || emailParts.length !== 2) { | ||
| return defaultWorkspaceName; | ||
| return ''; | ||
| } | ||
| const username = emailParts.at(0) ?? ''; | ||
| const domain = emailParts.at(1) ?? ''; | ||
| const userDetails = PersonalDetailsUtils.getPersonalDetailByEmail(sessionEmail); | ||
| const displayName = userDetails?.displayName?.trim(); | ||
| let displayNameForWorkspace = ''; | ||
|
|
||
| if (!PUBLIC_DOMAINS.some((publicDomain) => publicDomain === domain.toLowerCase())) { | ||
| defaultWorkspaceName = `${Str.UCFirst(domain.split('.').at(0) ?? '')}'s Workspace`; | ||
| displayNameForWorkspace = Str.UCFirst(domain.split('.').at(0) ?? ''); | ||
| } else if (displayName) { | ||
| defaultWorkspaceName = `${Str.UCFirst(displayName)}'s Workspace`; | ||
| displayNameForWorkspace = Str.UCFirst(displayName); | ||
| } else if (PUBLIC_DOMAINS.some((publicDomain) => publicDomain === domain.toLowerCase())) { | ||
| defaultWorkspaceName = `${Str.UCFirst(username)}'s Workspace`; | ||
| displayNameForWorkspace = Str.UCFirst(username); | ||
| } else { | ||
| defaultWorkspaceName = userDetails?.phoneNumber ?? ''; | ||
| displayNameForWorkspace = userDetails?.phoneNumber ?? ''; | ||
| } | ||
|
|
||
| if (`@${domain.toLowerCase()}` === CONST.SMS.DOMAIN) { | ||
| defaultWorkspaceName = 'My Group Workspace'; | ||
| if (`@${domain}` === CONST.SMS.DOMAIN) { | ||
| return translateLocal('workspace.new.myGroupWorkspace'); | ||
| } | ||
|
|
||
| if (isEmptyObject(allPolicies)) { | ||
| return defaultWorkspaceName; | ||
| return translateLocal('workspace.new.workspaceName', {userName: displayNameForWorkspace}); | ||
| } | ||
|
|
||
| // find default named workspaces and increment the last number | ||
| const numberRegEx = new RegExp(`${escapeRegExp(defaultWorkspaceName)} ?(\\d*)`, 'i'); | ||
| const parsedWorkspaceNumbers = Object.values(allPolicies ?? {}) | ||
| .filter((policy) => policy?.name && numberRegEx.test(policy.name)) | ||
| .map((policy) => Number(numberRegEx.exec(policy?.name ?? '')?.[1] ?? '1')); // parse the number at the end | ||
| const lastWorkspaceNumber = Math.max(...parsedWorkspaceNumbers); | ||
| return lastWorkspaceNumber !== -Infinity ? `${defaultWorkspaceName} ${lastWorkspaceNumber + 1}` : defaultWorkspaceName; | ||
| const escapedName = escapeRegExp(displayNameForWorkspace); | ||
| const workspaceTranslations = CONST.LANGUAGES.map((lang) => translate(lang, 'workspace.common.workspace')).join('|'); | ||
|
|
||
| const workspaceRegex = new RegExp(`^(?=.*${escapedName})(?:.*(?:${workspaceTranslations})\\s*(\\d+)?)`, 'i'); | ||
|
|
||
| const workspaceNumbers = Object.values(allPolicies) | ||
| .map((policy) => workspaceRegex.exec(policy?.name ?? '')) | ||
| .filter(Boolean) // Remove null matches | ||
| .map((match) => Number(match?.[1] ?? '0')); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@eVoloshchak, I mistakenly removed the default value "0" when mapping over workspace number and that resulted in
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Works well now, thanks! Could you resolve the conflicts please?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. conflicts resolved. |
||
| const lastWorkspaceNumber = workspaceNumbers.length > 0 ? Math.max(...workspaceNumbers) : undefined; | ||
|
|
||
| return translateLocal('workspace.new.workspaceName', {userName: displayNameForWorkspace, workspaceNumber: lastWorkspaceNumber !== undefined ? lastWorkspaceNumber + 1 : undefined}); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -1,5 +1,10 @@ | ||||
| import {Str} from 'expensify-common'; | ||||
| import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; | ||||
| import Onyx from 'react-native-onyx'; | ||||
| import {translateLocal} from '@libs/Localize'; | ||||
| import BaseLocaleListener from '@libs/Localize/LocaleListener/BaseLocaleListener'; | ||||
| // eslint-disable-next-line no-restricted-syntax | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Will deleting this line result in a linter error?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes |
||||
| import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; | ||||
| import CONST from '@src/CONST'; | ||||
| import OnyxUpdateManager from '@src/libs/actions/OnyxUpdateManager'; | ||||
| import * as Policy from '@src/libs/actions/Policy/Policy'; | ||||
|
|
@@ -414,6 +419,119 @@ describe('actions/Policy', () => { | |||
| }); | ||||
| }); | ||||
|
|
||||
| const TEST_EMAIL = 'esh@gmail.com'; | ||||
| const TEST_EMAIL_2 = 'eshofficial@gmail.com'; | ||||
| const TEST_ACCOUNT_ID = 1; | ||||
| const TEST_DISPLAY_NAME = 'Esh Gupta'; | ||||
| const TEST_PHONE_NUMBER = '1234567890'; | ||||
| const TEST_NON_PUBLIC_DOMAIN_EMAIL = 'esh@example.com'; | ||||
| const TEST_SMS_DOMAIN_EMAIL = 'esh@expensify.sms'; | ||||
|
|
||||
| describe('generateDefaultWorkspaceName', () => { | ||||
| beforeAll(() => { | ||||
| Onyx.set(ONYXKEYS.COLLECTION.POLICY, {}); | ||||
| }); | ||||
|
|
||||
| it('should generate a workspace name based on the email domain when the domain is not public', () => { | ||||
| const domain = 'example.com'; | ||||
| const displayNameForWorkspace = Str.UCFirst(domain.split('.').at(0) ?? ''); | ||||
|
|
||||
| jest.spyOn(PersonalDetailsUtils, 'getPersonalDetailByEmail').mockReturnValue({ | ||||
| displayName: TEST_DISPLAY_NAME, | ||||
| phoneNumber: TEST_PHONE_NUMBER, | ||||
| accountID: TEST_ACCOUNT_ID, | ||||
| }); | ||||
|
|
||||
| const workspaceName = Policy.generateDefaultWorkspaceName(TEST_NON_PUBLIC_DOMAIN_EMAIL); | ||||
|
|
||||
| expect(workspaceName).toBe(translateLocal('workspace.new.workspaceName', {userName: displayNameForWorkspace})); | ||||
| }); | ||||
|
|
||||
| it('should generate a workspace name based on the display name when the domain is public and display name is available', () => { | ||||
| const displayNameForWorkspace = Str.UCFirst(TEST_DISPLAY_NAME); | ||||
|
|
||||
| jest.spyOn(PersonalDetailsUtils, 'getPersonalDetailByEmail').mockReturnValue({ | ||||
| displayName: TEST_DISPLAY_NAME, | ||||
| phoneNumber: TEST_PHONE_NUMBER, | ||||
| accountID: TEST_ACCOUNT_ID, | ||||
| }); | ||||
|
|
||||
| const workspaceName = Policy.generateDefaultWorkspaceName(TEST_EMAIL); | ||||
|
|
||||
| expect(workspaceName).toBe(translateLocal('workspace.new.workspaceName', {userName: displayNameForWorkspace})); | ||||
| }); | ||||
|
|
||||
| it('should generate a workspace name based on the username when the domain is public and display name is not available', () => { | ||||
| const emailParts = TEST_EMAIL_2.split('@'); | ||||
| const username = emailParts.at(0) ?? ''; | ||||
| const displayNameForWorkspace = Str.UCFirst(username); | ||||
|
|
||||
| jest.spyOn(PersonalDetailsUtils, 'getPersonalDetailByEmail').mockReturnValue({ | ||||
| displayName: '', | ||||
| phoneNumber: TEST_PHONE_NUMBER, | ||||
| accountID: TEST_ACCOUNT_ID, | ||||
| }); | ||||
|
|
||||
| const workspaceName = Policy.generateDefaultWorkspaceName(TEST_EMAIL_2); | ||||
|
|
||||
| expect(workspaceName).toBe(translateLocal('workspace.new.workspaceName', {userName: displayNameForWorkspace})); | ||||
| }); | ||||
|
|
||||
| it('should generate a workspace name with an incremented number when there are existing policies with similar names', async () => { | ||||
| const existingPolicies = { | ||||
| ...createRandomPolicy(0, CONST.POLICY.TYPE.PERSONAL, `${TEST_DISPLAY_NAME}'s Workspace`), | ||||
| ...createRandomPolicy(0, CONST.POLICY.TYPE.PERSONAL, `${TEST_DISPLAY_NAME}'s Workspace 1`), | ||||
| }; | ||||
|
|
||||
| jest.spyOn(PersonalDetailsUtils, 'getPersonalDetailByEmail').mockReturnValue({ | ||||
| displayName: TEST_DISPLAY_NAME, | ||||
| phoneNumber: TEST_PHONE_NUMBER, | ||||
| accountID: TEST_ACCOUNT_ID, | ||||
| }); | ||||
|
|
||||
| await Onyx.set(ONYXKEYS.COLLECTION.POLICY, existingPolicies); | ||||
|
|
||||
| const workspaceName = Policy.generateDefaultWorkspaceName(TEST_EMAIL); | ||||
|
|
||||
| expect(workspaceName).toBe(translateLocal('workspace.new.workspaceName', {userName: TEST_DISPLAY_NAME, workspaceNumber: 2})); | ||||
| }); | ||||
|
|
||||
| it('should return "My Group Workspace" when the domain is SMS', () => { | ||||
| jest.spyOn(PersonalDetailsUtils, 'getPersonalDetailByEmail').mockReturnValue({ | ||||
| displayName: TEST_DISPLAY_NAME, | ||||
| phoneNumber: TEST_PHONE_NUMBER, | ||||
| accountID: TEST_ACCOUNT_ID, | ||||
| }); | ||||
|
|
||||
| const workspaceName = Policy.generateDefaultWorkspaceName(TEST_SMS_DOMAIN_EMAIL); | ||||
|
|
||||
| expect(workspaceName).toBe(translateLocal('workspace.new.myGroupWorkspace')); | ||||
| }); | ||||
|
|
||||
| it('should generate a workspace name with an incremented number even if previous workspaces were created in english lang', async () => { | ||||
| await Onyx.set(ONYXKEYS.COLLECTION.POLICY, {}); | ||||
| jest.spyOn(BaseLocaleListener, 'getPreferredLocale').mockReturnValue('es'); | ||||
| const existingPolicies = { | ||||
| ...createRandomPolicy(0, CONST.POLICY.TYPE.PERSONAL, `${TEST_DISPLAY_NAME}'s Workspace`), | ||||
| ...createRandomPolicy(0, CONST.POLICY.TYPE.PERSONAL, `${TEST_DISPLAY_NAME}'s Workspace 1`), | ||||
| }; | ||||
|
|
||||
| jest.spyOn(PersonalDetailsUtils, 'getPersonalDetailByEmail').mockReturnValue({ | ||||
| displayName: TEST_DISPLAY_NAME, | ||||
| phoneNumber: TEST_PHONE_NUMBER, | ||||
| accountID: TEST_ACCOUNT_ID, | ||||
| }); | ||||
|
|
||||
| jest.spyOn(Str, 'UCFirst').mockReturnValue(TEST_DISPLAY_NAME); | ||||
|
|
||||
| await Onyx.set(ONYXKEYS.COLLECTION.POLICY, existingPolicies); | ||||
|
|
||||
| const workspaceName = Policy.generateDefaultWorkspaceName(TEST_EMAIL); | ||||
|
|
||||
| expect(workspaceName).toBe(translateLocal('workspace.new.workspaceName', {userName: TEST_DISPLAY_NAME, workspaceNumber: 2})); | ||||
| }); | ||||
| }); | ||||
|
|
||||
| describe('enablePolicyWorkflows', () => { | ||||
| it('should update delayed submission to instant when disabling the workflows feature', async () => { | ||||
| (fetch as MockFetch)?.pause?.(); | ||||
|
|
||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to work too, could you double check please?
Then workspaceRegex could lose the
departregex can be simplified further of course
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eVoloshchak
thanks for the suggestion. I have verified it and it works well. It has been updated now.EDIT: The suggested regex wasn't working in Spanish language so I had to update it to:
It doesn’t consume characters; it just verifies the condition.
This ensures the regex targets the correct phrases.
\s*(\d+)?:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, thanks!