-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Create a new generateDefaultWorkspaceName #85789
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
Merged
cristipaval
merged 12 commits into
Expensify:main
from
bernhardoj:chore/66574-refactor-policy-to-use-useonyx-9
Apr 2, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3f5ac7d
create a new generateDefaultWorkspaceName that doesn't use deprecated…
bernhardoj d2efed7
Merge branch 'main' into chore/66574-refactor-policy-to-use-useonyx-9
bernhardoj 80a6e20
use the new generateDefaultWorkspaceName
bernhardoj 8d2a524
int
bernhardoj e14eb14
remove unused param (yet)
bernhardoj 880936c
Merge branch 'main' into chore/66574-refactor-policy-to-use-useonyx-9
bernhardoj e0d8ed1
lint
bernhardoj c4002b6
fix test
bernhardoj d0dfb98
Merge branch 'main' into chore/66574-refactor-policy-to-use-useonyx-9
bernhardoj 96e4c67
Merge branch 'main' into chore/66574-refactor-policy-to-use-useonyx-9
bernhardoj b304ce8
pass translate from param
bernhardoj 8ee3b6f
fix redeclaring issue
bernhardoj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import type {OnyxCollection} from 'react-native-onyx'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import {lastWorkspaceNumberSelector} from '@src/selectors/Policy'; | ||
| import {emailSelector} from '@src/selectors/Session'; | ||
| import type {Policy} from '@src/types/onyx'; | ||
| import useOnyx from './useOnyx'; | ||
|
|
||
| function useLastWorkspaceNumber() { | ||
| const [sessionEmail] = useOnyx(ONYXKEYS.SESSION, {selector: emailSelector}); | ||
| const lastWorkspaceNumberSelectorWithEmail = (policies: OnyxCollection<Policy>) => lastWorkspaceNumberSelector(policies, sessionEmail ?? ''); | ||
| const [lastWorkspaceNumber] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {selector: lastWorkspaceNumberSelectorWithEmail}); | ||
| return lastWorkspaceNumber; | ||
| } | ||
|
|
||
| export default useLastWorkspaceNumber; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |
| import Onyx from 'react-native-onyx'; | ||
| import type {TupleToUnion, ValueOf} from 'type-fest'; | ||
| import type {ReportExportType} from '@components/ButtonWithDropdownMenu/types'; | ||
| import type {LocaleContextProps} from '@components/LocaleContextProvider'; | ||
| import type {LocaleContextProps, LocalizedTranslate} from '@components/LocaleContextProvider'; | ||
| import type PolicyData from '@hooks/usePolicyData/types'; | ||
| import * as API from '@libs/API'; | ||
| import type { | ||
|
|
@@ -252,7 +252,7 @@ | |
| }; | ||
|
|
||
| const deprecatedAllPolicies: OnyxCollection<Policy> = {}; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.COLLECTION.POLICY, | ||
| callback: (val, key) => { | ||
| if (!key) { | ||
|
|
@@ -268,7 +268,7 @@ | |
| }); | ||
|
|
||
| let deprecatedAllReportActions: OnyxCollection<ReportActions>; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, | ||
| waitForCollectionCallback: true, | ||
| callback: (actions) => { | ||
|
|
@@ -278,7 +278,7 @@ | |
|
|
||
| let deprecatedSessionEmail = ''; | ||
| let deprecatedSessionAccountID = 0; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.SESSION, | ||
| callback: (val) => { | ||
| deprecatedSessionEmail = val?.email ?? ''; | ||
|
|
@@ -287,7 +287,7 @@ | |
| }); | ||
|
|
||
| let deprecatedAllPersonalDetails: OnyxEntry<PersonalDetailsList>; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.PERSONAL_DETAILS_LIST, | ||
| callback: (val) => (deprecatedAllPersonalDetails = val), | ||
| }); | ||
|
|
@@ -2150,6 +2150,51 @@ | |
| Onyx.set(ONYXKEYS.DUPLICATE_WORKSPACE, {}); | ||
| } | ||
|
|
||
| function getDisplayNameForWorkspace(email: string) { | ||
| const emailParts = email.split('@'); | ||
| const domain = emailParts.at(1) ?? ''; | ||
| const isSMSDomain = `@${domain}` === CONST.SMS.DOMAIN; | ||
| if (isSMSDomain) { | ||
| // eslint-disable-next-line @typescript-eslint/no-deprecated | ||
| return translateLocal('workspace.new.myGroupWorkspace', {}); | ||
| } | ||
|
|
||
| if (!PUBLIC_DOMAINS_SET.has(domain.toLowerCase())) { | ||
| return Str.UCFirst(domain.split('.').at(0) ?? ''); | ||
| } | ||
|
|
||
| const userDetails = PersonalDetailsUtils.getPersonalDetailByEmail(email); | ||
| const displayName = userDetails?.displayName?.trim(); | ||
| if (displayName) { | ||
| return Str.UCFirst(displayName); | ||
| } | ||
|
|
||
| const username = emailParts.at(0) ?? ''; | ||
| return Str.UCFirst(username); | ||
| } | ||
|
|
||
| /** | ||
| * Generate a policy name based on an email and policy list. | ||
| * @param [email] the email to base the workspace name on. If not passed, will use the logged-in user's email instead | ||
| * @param [lastWorkspaceNumber] the last workspace number | ||
| */ | ||
| function newGenerateDefaultWorkspaceName(email: string, lastWorkspaceNumber: number | undefined, localeTranslate: LocalizedTranslate): string { | ||
|
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. I'll rename localeTranslate back to translate after we completely remove |
||
| const emailParts = email ? email.split('@') : deprecatedSessionEmail.split('@'); | ||
| if (!emailParts || emailParts.length !== 2) { | ||
| return ''; | ||
| } | ||
| const domain = emailParts.at(1) ?? ''; | ||
| const isSMSDomain = `@${domain}` === CONST.SMS.DOMAIN; | ||
|
|
||
| if (isSMSDomain) { | ||
| return localeTranslate('workspace.new.myGroupWorkspace', {workspaceNumber: lastWorkspaceNumber !== undefined ? lastWorkspaceNumber + 1 : undefined}); | ||
| } | ||
|
|
||
| const displayNameForWorkspace = getDisplayNameForWorkspace(email || deprecatedSessionEmail); | ||
|
|
||
| return localeTranslate('workspace.new.workspaceName', displayNameForWorkspace, lastWorkspaceNumber !== undefined ? lastWorkspaceNumber + 1 : undefined); | ||
| } | ||
|
|
||
| /** | ||
| * Generate a policy name based on an email and policy list. | ||
| * @param [email] the email to base the workspace name on. If not passed, will use the logged-in user's email instead | ||
|
|
@@ -7081,6 +7126,8 @@ | |
| updateLastAccessedWorkspace, | ||
| clearDeleteWorkspaceError, | ||
| setWorkspaceDefaultSpendCategory, | ||
| getDisplayNameForWorkspace, | ||
| newGenerateDefaultWorkspaceName, | ||
| generateDefaultWorkspaceName, | ||
| updateGeneralSettings, | ||
| deleteWorkspaceAvatar, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import CONST from '@src/CONST'; | ||
| import IntlStore from '@src/languages/IntlStore'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import {lastWorkspaceNumberSelector} from '@src/selectors/Policy'; | ||
| import type {Policy} from '@src/types/onyx'; | ||
|
|
||
| describe('lastWorkspaceNumberSelector', () => { | ||
| const email = 'jdoe@expensify.com'; | ||
| const displayName = 'Expensify'; | ||
| const workspaceName = `${displayName} Workspace`; | ||
|
|
||
| beforeAll(() => IntlStore.load(CONST.LOCALES.DEFAULT)); | ||
|
|
||
| it('should return undefined when there are no policies', () => { | ||
| expect(lastWorkspaceNumberSelector({}, email)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return undefined when email is invalid', () => { | ||
| expect(lastWorkspaceNumberSelector({}, 'invalid-email')).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should return 0 when there is a matching workspace without a number', () => { | ||
| const policies = { | ||
| [`${ONYXKEYS.COLLECTION.POLICY}1`]: {name: workspaceName} as Policy, | ||
| }; | ||
| expect(lastWorkspaceNumberSelector(policies, email)).toBe(0); | ||
| }); | ||
|
|
||
| it('should return the number when there is a matching workspace with a number', () => { | ||
| const policies = { | ||
| [`${ONYXKEYS.COLLECTION.POLICY}1`]: {name: `${workspaceName} 2`} as Policy, | ||
| }; | ||
| expect(lastWorkspaceNumberSelector(policies, email)).toBe(2); | ||
| }); | ||
|
|
||
| it('should return the maximum number when there are multiple matching workspaces', () => { | ||
| const policies = { | ||
| [`${ONYXKEYS.COLLECTION.POLICY}1`]: {name: workspaceName} as Policy, | ||
| [`${ONYXKEYS.COLLECTION.POLICY}2`]: {name: `${workspaceName} 2`} as Policy, | ||
| [`${ONYXKEYS.COLLECTION.POLICY}3`]: {name: `${workspaceName} 5`} as Policy, | ||
| [`${ONYXKEYS.COLLECTION.POLICY}4`]: {name: 'Other Workspace'} as Policy, | ||
| }; | ||
| expect(lastWorkspaceNumberSelector(policies, email)).toBe(5); | ||
| }); | ||
|
|
||
| it('should handle SMS domain correctly', () => { | ||
| const smsEmail = `+15555555555${CONST.SMS.DOMAIN}`; | ||
| const smsDisplayName = 'My Group Workspace'; | ||
| const policies = { | ||
| [`${ONYXKEYS.COLLECTION.POLICY}1`]: {name: smsDisplayName} as Policy, | ||
| [`${ONYXKEYS.COLLECTION.POLICY}2`]: {name: `${smsDisplayName} 3`} as Policy, | ||
| }; | ||
| expect(lastWorkspaceNumberSelector(policies, smsEmail)).toBe(3); | ||
| }); | ||
|
|
||
| it('should ignore case when matching workspace names', () => { | ||
| const policies = { | ||
| [`${ONYXKEYS.COLLECTION.POLICY}1`]: {name: workspaceName.toLowerCase()} as Policy, | ||
| [`${ONYXKEYS.COLLECTION.POLICY}2`]: {name: `${workspaceName.toUpperCase()} 4`} as Policy, | ||
| }; | ||
| expect(lastWorkspaceNumberSelector(policies, email)).toBe(4); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Where do we manage this part from old function?
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.
In line 2147-2149, we already return early if
PUBLIC_DOMAINS_SET.has(domain.toLowerCase())is false,So
} else if (PUBLIC_DOMAINS_SET.has(domain.toLowerCase())) {will always betrue