-
Notifications
You must be signed in to change notification settings - Fork 4k
[Search Router] Add Create actions to navigation suggestions #97086
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
Open
nabi-ebrahimi
wants to merge
13
commits into
Expensify:main
Choose a base branch
from
nabi-ebrahimi:92752-search-router-create-actions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7bf8951
Add Create actions to Search Router navigation
nabi-ebrahimi 88d4fd6
Merge branch 'main' into 92752-search-router-create-actions
nabi-ebrahimi 8ac691f
Fix Create navigation hook mock in Search Router tests
nabi-ebrahimi 3ad4660
Fix Search Router performance test navigation mock
nabi-ebrahimi 455e6cc
Merge branch 'main' into 92752-search-router-create-actions
nabi-ebrahimi 1ca043d
Add Create navigation hook file header
nabi-ebrahimi b02b3a9
Replace Create policy limit magic value
nabi-ebrahimi 44efed7
Use eligible report policies for Create suggestions
nabi-ebrahimi 90e4f11
Remove unsafe Create policy assertion
nabi-ebrahimi 19da82e
Document Create report policy eligibility
nabi-ebrahimi 4a0f22a
Document Create report navigation ordering
nabi-ebrahimi 68ed8d4
Document Create modal replacement behavior
nabi-ebrahimi 413d46a
Add Create navigation hook coverage
nabi-ebrahimi 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
207 changes: 207 additions & 0 deletions
207
src/components/Search/SearchRouter/useCreateNavigationSuggestions.ts
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,207 @@ | ||
| /** | ||
| * Builds the Create/FAB navigation suggestions shown in the Search Router. | ||
| */ | ||
| import type {SearchQueryItem} from '@components/Search/SearchList/ListItem/SearchQueryListItem'; | ||
|
|
||
| import useCreateReport from '@hooks/useCreateReport'; | ||
| import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; | ||
| import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset'; | ||
| import useLocalize from '@hooks/useLocalize'; | ||
| import useNetwork from '@hooks/useNetwork'; | ||
| import useOnyx from '@hooks/useOnyx'; | ||
| import usePermissions from '@hooks/usePermissions'; | ||
| import usePreferredPolicy from '@hooks/usePreferredPolicy'; | ||
|
|
||
| import {startDistanceRequest, startMoneyRequest} from '@libs/actions/IOU/MoneyRequest'; | ||
| import {createNewReport, startNewChat} from '@libs/actions/Report'; | ||
| import getIconForAction from '@libs/getIconForAction'; | ||
| import interceptAnonymousUser from '@libs/interceptAnonymousUser'; | ||
| import createDynamicRoute from '@libs/Navigation/helpers/dynamicRoutesUtils/createDynamicRoute'; | ||
| import getCreateReportRoute, {getReportsRootRoute, navigateToCreateReportWorkspaceSelection} from '@libs/Navigation/helpers/getCreateReportRoute'; | ||
| import Navigation from '@libs/Navigation/Navigation'; | ||
| import {canSendInvoice, getDefaultChatEnabledPolicy, getGroupPoliciesWhereReportCanBeCreated, shouldShowPolicy} from '@libs/PolicyUtils'; | ||
| import {generateReportID, hasViolations as hasViolationsReportUtils} from '@libs/ReportUtils'; | ||
|
|
||
| import isOnSearchMoneyRequestReportPage from '@navigation/helpers/isOnSearchMoneyRequestReportPage'; | ||
|
|
||
| import {clearLastSearchParams} from '@userActions/ReportNavigation'; | ||
|
|
||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import {DYNAMIC_ROUTES} from '@src/ROUTES'; | ||
| import {isTrackIntentUserSelector} from '@src/selectors/Onboarding'; | ||
| import {emailSelector, sessionEmailAndAccountIDSelector} from '@src/selectors/Session'; | ||
| import {validTransactionDraftIDsSelector} from '@src/selectors/TransactionDraft'; | ||
| import type * as OnyxTypes from '@src/types/onyx'; | ||
| import getEmptyArray from '@src/types/utils/getEmptyArray'; | ||
| import type IconAsset from '@src/types/utils/IconAsset'; | ||
|
|
||
| import type {OnyxCollection} from 'react-native-onyx'; | ||
|
|
||
| import {useState} from 'react'; | ||
|
|
||
| import type {NavigationSuggestionSourceItem} from './SearchRouterHelpers'; | ||
|
|
||
| type CreateNavigationItem = { | ||
| visible: boolean; | ||
| text: string; | ||
| icon: IconAsset; | ||
| action: () => void; | ||
| keyForList: string; | ||
| }; | ||
|
|
||
| function buildCreateNavigationItems(items: CreateNavigationItem[]): NavigationSuggestionSourceItem[] { | ||
| return items | ||
| .filter((item) => item.visible) | ||
| .map(({text, icon, action, keyForList}) => ({ | ||
| text, | ||
| singleIcon: icon, | ||
| action, | ||
| keyForList, | ||
| matchTerms: [text], | ||
| })); | ||
| } | ||
|
|
||
| // Search Router is already hidden when this runs, so the topmost modal is an underlying RHP. | ||
| // Wait for it to close before opening the Create flow to avoid stacking modal routes. | ||
| function replaceTopmostModalWithAction(action: () => void) { | ||
| if (!Navigation.isTopmostRouteModalScreen()) { | ||
| action(); | ||
| return; | ||
| } | ||
|
|
||
| Navigation.dismissModal({afterTransition: action}); | ||
| } | ||
|
nabi-ebrahimi marked this conversation as resolved.
|
||
|
|
||
| function useCreateNavigationSuggestions(): SearchQueryItem[] { | ||
| const {translate} = useLocalize(); | ||
| const icons = useMemoizedLazyExpensifyIcons(['Coins', 'Receipt', 'Cash', 'Transfer', 'MoneyCircle', 'Location', 'Document', 'ChatBubble', 'InvoiceGeneric', 'NewWorkspace']); | ||
| const currentUserPersonalDetails = useCurrentUserPersonalDetails(); | ||
| const {isBetaEnabled} = usePermissions(); | ||
| const isSubmit2026BetaEnabled = isBetaEnabled(CONST.BETAS.SUBMIT_2026); | ||
| const {isOffline} = useNetwork(); | ||
| const {isRestrictedPolicyCreation} = usePreferredPolicy(); | ||
| const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY); | ||
| const [reportID] = useState(() => generateReportID()); | ||
| const [draftTransactionIDs] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_DRAFT, {selector: validTransactionDraftIDsSelector}); | ||
| const [lastDistanceExpenseType] = useOnyx(ONYXKEYS.NVP_LAST_DISTANCE_EXPENSE_TYPE); | ||
| const [sessionEmail] = useOnyx(ONYXKEYS.SESSION, {selector: emailSelector}); | ||
| const [session] = useOnyx(ONYXKEYS.SESSION, {selector: sessionEmailAndAccountIDSelector}); | ||
| const [allBetas] = useOnyx(ONYXKEYS.BETAS); | ||
| const [transactionViolations] = useOnyx(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS); | ||
| const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID); | ||
| const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`); | ||
| // Use the shared report eligibility rules so Submit workspaces are only included for beta users. | ||
| const [groupPoliciesWithChatEnabled = getEmptyArray<OnyxTypes.Policy>()] = useOnyx(ONYXKEYS.COLLECTION.POLICY, { | ||
| selector: (policies: OnyxCollection<OnyxTypes.Policy>) => getGroupPoliciesWhereReportCanBeCreated(policies, isSubmit2026BetaEnabled, session?.email), | ||
|
nabi-ebrahimi marked this conversation as resolved.
|
||
| }); | ||
| const [isTrackIntentUser] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {selector: isTrackIntentUserSelector}); | ||
| const [isLoading = false] = useOnyx(ONYXKEYS.IS_LOADING_APP); | ||
|
|
||
| const defaultChatEnabledPolicy = getDefaultChatEnabledPolicy([...groupPoliciesWithChatEnabled], activePolicy); | ||
|
nabi-ebrahimi marked this conversation as resolved.
|
||
| const hasViolations = hasViolationsReportUtils(undefined, transactionViolations, session?.accountID ?? CONST.DEFAULT_NUMBER_ID, session?.email ?? ''); | ||
| const isReportInSearch = isOnSearchMoneyRequestReportPage(); | ||
| const isInvoiceVisible = canSendInvoice(allPolicies ?? null, sessionEmail); | ||
|
|
||
| const {createReport, isVisible: isCreateReportVisible} = useCreateReport({ | ||
| onCreateReport: (shouldDismissEmptyReportsConfirmation?: boolean) => { | ||
| if (!defaultChatEnabledPolicy?.id) { | ||
| return; | ||
| } | ||
|
|
||
| if (isReportInSearch) { | ||
| clearLastSearchParams(); | ||
| } | ||
|
|
||
| const {reportID: createdReportID} = createNewReport( | ||
| currentUserPersonalDetails, | ||
| hasViolations, | ||
| isBetaEnabled(CONST.BETAS.ASAP_SUBMIT), | ||
| defaultChatEnabledPolicy, | ||
| allBetas, | ||
| isTrackIntentUser, | ||
| false, | ||
| shouldDismissEmptyReportsConfirmation, | ||
| ); | ||
| // Navigate to the Reports page first so getCreateReportRoute() resolves against | ||
| // the Search/Reports fullscreen context before opening the created report modal. | ||
| Navigation.navigate(getReportsRootRoute(), {forceReplace: isReportInSearch}); | ||
|
nabi-ebrahimi marked this conversation as resolved.
|
||
| Navigation.setNavigationActionToMicrotaskQueue(() => { | ||
| Navigation.navigate(getCreateReportRoute({reportID: createdReportID}), {forceReplace: isReportInSearch}); | ||
| }); | ||
| }, | ||
| groupPoliciesWithChatEnabled, | ||
| onNavigateToWorkspaceSelection: () => navigateToCreateReportWorkspaceSelection({forceReplace: isReportInSearch}), | ||
| shouldHandleNavigationBack: false, | ||
| }); | ||
|
|
||
| const shouldShowNewWorkspaceButton = | ||
| !isRestrictedPolicyCreation && !isLoading && Object.values(allPolicies ?? {}).every((policy) => !shouldShowPolicy(policy, !!isOffline, sessionEmail)); | ||
|
|
||
| return buildCreateNavigationItems([ | ||
| { | ||
| visible: true, | ||
| text: translate('iou.createExpense'), | ||
| icon: getIconForAction(CONST.IOU.TYPE.CREATE, icons), | ||
| action: () => | ||
| replaceTopmostModalWithAction(() => { | ||
| interceptAnonymousUser(() => { | ||
| startMoneyRequest(CONST.IOU.TYPE.CREATE, reportID, draftTransactionIDs, undefined, undefined, undefined, true); | ||
| }); | ||
| }), | ||
| keyForList: 'create_expense', | ||
| }, | ||
| { | ||
| visible: isCreateReportVisible, | ||
| text: translate('report.newReport.createReport'), | ||
| icon: icons.Document, | ||
| action: () => replaceTopmostModalWithAction(createReport), | ||
| keyForList: 'create_report', | ||
| }, | ||
| { | ||
| visible: true, | ||
| text: translate('iou.trackDistance'), | ||
| icon: icons.Location, | ||
| action: () => | ||
| replaceTopmostModalWithAction(() => { | ||
| interceptAnonymousUser(() => { | ||
| startDistanceRequest(CONST.IOU.TYPE.CREATE, reportID, draftTransactionIDs, lastDistanceExpenseType, undefined, undefined, true); | ||
| }); | ||
| }), | ||
| keyForList: 'create_trackDistance', | ||
| }, | ||
| { | ||
| visible: true, | ||
| text: translate('sidebarScreen.fabNewChat'), | ||
| icon: icons.ChatBubble, | ||
| action: () => replaceTopmostModalWithAction(() => interceptAnonymousUser(startNewChat)), | ||
| keyForList: 'create_chat', | ||
| }, | ||
| { | ||
| visible: isInvoiceVisible, | ||
| text: translate('workspace.invoices.sendInvoice'), | ||
| icon: icons.InvoiceGeneric, | ||
| action: () => | ||
| replaceTopmostModalWithAction(() => { | ||
| interceptAnonymousUser(() => { | ||
| startMoneyRequest(CONST.IOU.TYPE.INVOICE, reportID, draftTransactionIDs, undefined, undefined, undefined, true); | ||
| }); | ||
| }), | ||
| keyForList: 'create_invoice', | ||
| }, | ||
| { | ||
| visible: shouldShowNewWorkspaceButton, | ||
| text: translate('workspace.new.newWorkspace'), | ||
| icon: icons.NewWorkspace, | ||
| action: () => | ||
| replaceTopmostModalWithAction(() => { | ||
| interceptAnonymousUser(() => Navigation.navigate(createDynamicRoute(DYNAMIC_ROUTES.WORKSPACE_CONFIRMATION.path))); | ||
| }), | ||
| keyForList: 'create_workspace', | ||
| }, | ||
| ]); | ||
| } | ||
|
|
||
| export default useCreateNavigationSuggestions; | ||
| export {buildCreateNavigationItems, replaceTopmostModalWithAction}; | ||
| export type {CreateNavigationItem}; | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.