-
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
31
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
31 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 c046aa6
Merge branch 'main' into 92752-search-router-create-actions
nabi-ebrahimi e50c880
Merge branch 'main' into 92752-search-router-create-actions
nabi-ebrahimi 3dc5b82
Fix Create report navigation from native Search Router
nabi-ebrahimi fbdbad5
Remove redundant violation subscription from Create suggestions
nabi-ebrahimi 7ca67db
Remove duplicate session subscription from Create suggestions
nabi-ebrahimi 13ce056
Remove redundant navigation value normalization
nabi-ebrahimi 0ad477b
Preserve Create navigation source item type
nabi-ebrahimi cc7f946
Document Create report eligibility behavior
nabi-ebrahimi fb6b852
Add Create suggestion source integration coverage
nabi-ebrahimi 2fd1bcc
Add localized aliases for Create suggestions
nabi-ebrahimi 463d3e6
Remove stale Create suggestions violation mock
nabi-ebrahimi 4a10180
Defer Create report confirmation scan in Search Router
nabi-ebrahimi 6050523
Remove duplicate policy subscription from Create suggestions
nabi-ebrahimi 69b620c
Remove redundant navigation match trimming
nabi-ebrahimi 91a0be8
Remove redundant Create whitespace test
nabi-ebrahimi b6328dd
Merge branch 'main' into 92752-search-router-create-actions
nabi-ebrahimi af2b8fc
Merge branch 'main' into 92752-search-router-create-actions
nabi-ebrahimi 789c795
Pass currency decimals to Create report action
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
224 changes: 224 additions & 0 deletions
224
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,224 @@ | ||
| /** | ||
| * Builds the Create/FAB navigation suggestions shown in the Search Router. | ||
| */ | ||
| import useCreateReport from '@hooks/useCreateReport'; | ||
| import {useCurrencyListActions} from '@hooks/useCurrencyList'; | ||
| 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} 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} from '@src/selectors/Session'; | ||
| import {validTransactionDraftIDsSelector} from '@src/selectors/TransactionDraft'; | ||
| import type IconAsset from '@src/types/utils/IconAsset'; | ||
|
|
||
| import {useState} from 'react'; | ||
|
|
||
| import type {NavigationSuggestionSourceItem} from './SearchRouterHelpers'; | ||
|
|
||
| import {isNavigationIntentOnlyQuery, matchesNavigationQuery, stripNavigationIntentPrefix} from './SearchRouterHelpers'; | ||
|
|
||
| type CreateNavigationItem = { | ||
| visible: boolean; | ||
| text: string; | ||
| icon: IconAsset; | ||
| action: () => void; | ||
| keyForList: string; | ||
| matchTerms?: string[]; | ||
| }; | ||
|
|
||
| function buildCreateNavigationItems(items: CreateNavigationItem[]): NavigationSuggestionSourceItem[] { | ||
| return items | ||
| .filter((item) => item.visible) | ||
| .map(({text, icon, action, keyForList, matchTerms}) => ({ | ||
| text, | ||
| singleIcon: icon, | ||
| action, | ||
| keyForList, | ||
| matchTerms: 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}); | ||
| } | ||
|
|
||
| function useCreateNavigationSuggestions(query = ''): NavigationSuggestionSourceItem[] { | ||
| const {translate} = useLocalize(); | ||
| const icons = useMemoizedLazyExpensifyIcons(['Coins', 'Receipt', 'Cash', 'Transfer', 'MoneyCircle', 'Location', 'Document', 'ChatBubble', 'InvoiceGeneric', 'NewWorkspace']); | ||
| const currentUserPersonalDetails = useCurrentUserPersonalDetails(); | ||
| const {getCurrencyDecimals} = useCurrencyListActions(); | ||
| const {isBetaEnabled} = usePermissions(); | ||
| const isSubmit2026BetaEnabled = isBetaEnabled(CONST.BETAS.SUBMIT_2026); | ||
| const {isOffline} = useNetwork(); | ||
| const {isRestrictedPolicyCreation} = usePreferredPolicy(); | ||
| const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY); | ||
|
nabi-ebrahimi marked this conversation as resolved.
|
||
| 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 [allBetas] = useOnyx(ONYXKEYS.BETAS); | ||
| const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID); | ||
| const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`); | ||
| // Match Quick Creation and Search actions by using shared report eligibility rules. Unlike the FAB's | ||
| // expense-chat filter, this intentionally includes Submit workspaces only when the beta is enabled. | ||
| const groupPoliciesWithChatEnabled = getGroupPoliciesWhereReportCanBeCreated(allPolicies ?? null, isSubmit2026BetaEnabled, sessionEmail); | ||
| 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 isInvoiceVisible = canSendInvoice(allPolicies ?? null, sessionEmail); | ||
|
|
||
| const createExpenseMatchTerms = [translate('iou.createExpense'), translate('iou.addExpense'), translate('homePage.gettingStartedSection.createExpense')]; | ||
| const createReportMatchTerms = [translate('report.newReport.createReport')]; | ||
| const trackDistanceMatchTerms = [translate('iou.trackDistance')]; | ||
| const chatMatchTerms = [translate('sidebarScreen.fabNewChat'), `${translate('common.new')} ${translate('common.chat')}`]; | ||
| const invoiceMatchTerms = [translate('workspace.invoices.sendInvoice')]; | ||
| const workspaceMatchTerms = [translate('workspace.new.newWorkspace'), translate('onboarding.workspace.createWorkspace'), translate('homePage.gettingStartedSection.createWorkspace')]; | ||
| const matchQuery = stripNavigationIntentPrefix(query); | ||
| const shouldPrepareCreateReport = | ||
| isNavigationIntentOnlyQuery(query) || | ||
| [createExpenseMatchTerms, createReportMatchTerms, trackDistanceMatchTerms, chatMatchTerms, invoiceMatchTerms, workspaceMatchTerms].some((matchTerms) => | ||
| matchesNavigationQuery(matchQuery, ...matchTerms), | ||
| ); | ||
|
|
||
| const {createReport, isVisible: isCreateReportVisible} = useCreateReport({ | ||
| onCreateReport: (shouldDismissEmptyReportsConfirmation?: boolean) => { | ||
| if (!defaultChatEnabledPolicy?.id) { | ||
| return; | ||
| } | ||
|
|
||
| // Read this after Search Router closes so native resolves the underlying route instead of the router screen itself. | ||
| const isReportInSearch = isOnSearchMoneyRequestReportPage(); | ||
| if (isReportInSearch) { | ||
| clearLastSearchParams(); | ||
| } | ||
|
|
||
| const {reportID: createdReportID} = createNewReport( | ||
| currentUserPersonalDetails, | ||
| false, | ||
|
nabi-ebrahimi marked this conversation as resolved.
|
||
| isBetaEnabled(CONST.BETAS.ASAP_SUBMIT), | ||
| defaultChatEnabledPolicy, | ||
| allBetas, | ||
| isTrackIntentUser, | ||
| getCurrencyDecimals, | ||
| 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: isOnSearchMoneyRequestReportPage()}), | ||
| shouldHandleNavigationBack: false, | ||
| shouldSkipEmptyReportConfirmation: !shouldPrepareCreateReport, | ||
| }); | ||
|
nabi-ebrahimi marked this conversation as resolved.
|
||
|
|
||
| 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), | ||
| matchTerms: createExpenseMatchTerms, | ||
| 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, | ||
| matchTerms: createReportMatchTerms, | ||
| action: () => replaceTopmostModalWithAction(createReport), | ||
| keyForList: 'create_report', | ||
| }, | ||
| { | ||
| visible: true, | ||
| text: translate('iou.trackDistance'), | ||
| icon: icons.Location, | ||
| matchTerms: trackDistanceMatchTerms, | ||
| action: () => | ||
| replaceTopmostModalWithAction(() => { | ||
| interceptAnonymousUser(() => { | ||
| startDistanceRequest(CONST.IOU.TYPE.CREATE, reportID, draftTransactionIDs, lastDistanceExpenseType, undefined, undefined, true); | ||
| }); | ||
| }), | ||
| keyForList: 'create_trackDistance', | ||
| }, | ||
| { | ||
| visible: true, | ||
| text: translate('sidebarScreen.fabNewChat'), | ||
|
nabi-ebrahimi marked this conversation as resolved.
|
||
| icon: icons.ChatBubble, | ||
| matchTerms: chatMatchTerms, | ||
| action: () => replaceTopmostModalWithAction(() => interceptAnonymousUser(startNewChat)), | ||
| keyForList: 'create_chat', | ||
| }, | ||
| { | ||
| visible: isInvoiceVisible, | ||
| text: translate('workspace.invoices.sendInvoice'), | ||
| icon: icons.InvoiceGeneric, | ||
| matchTerms: invoiceMatchTerms, | ||
| 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, | ||
| matchTerms: workspaceMatchTerms, | ||
| 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
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.