Repository navigation
Add Chat and Copilot buttons to Agents page #91533
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
612ccbd
d12a379
9430c51
5a89b4e
ab81873
7b9f56c
4d7b73c
afc3ff2
b1dd748
98b5d6d
045140b
d3e2c18
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 |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import {hasSeenTourSelector} from '@selectors/Onboarding'; | ||
| import {navigateToAndOpenReportWithAccountIDs} from '@libs/actions/Report'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import useCurrentUserPersonalDetails from './useCurrentUserPersonalDetails'; | ||
| import useOnyx from './useOnyx'; | ||
|
|
||
| /** | ||
| * Encapsulates the data fetching and navigation logic for opening a DM chat with an agent. | ||
| * Returns a function that, given an accountID, navigates to the DM report with that agent. | ||
| */ | ||
| function useChatWithAgent() { | ||
| const currentUserPersonalDetails = useCurrentUserPersonalDetails(); | ||
| const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED); | ||
| const [isSelfTourViewed] = useOnyx(ONYXKEYS.NVP_ONBOARDING, {selector: hasSeenTourSelector}); | ||
| const [betas] = useOnyx(ONYXKEYS.BETAS); | ||
| const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST); | ||
|
|
||
| const chatWithAgent = (accountID: number) => { | ||
| navigateToAndOpenReportWithAccountIDs([accountID], currentUserPersonalDetails.accountID, introSelected, isSelfTourViewed, betas, personalDetails); | ||
| }; | ||
|
|
||
| return chatWithAgent; | ||
| } | ||
|
|
||
| export default useChatWithAgent; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import {useDelegateNoAccessActions, useDelegateNoAccessState} from '@components/DelegateNoAccessModalProvider'; | ||
| import {ModalActions} from '@components/Modal/Global/ModalContext'; | ||
| import {connect, disconnect} from '@libs/actions/Delegate'; | ||
| import {close as modalClose} from '@libs/actions/Modal'; | ||
| import {getGpsPoints, stopGpsTrip} from '@libs/GPSDraftDetailsUtils'; | ||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import {isTrackingSelector} from '@src/selectors/GPSDraftDetails'; | ||
| import type {Account} from '@src/types/onyx'; | ||
| import useConfirmModal from './useConfirmModal'; | ||
| import useLocalize from './useLocalize'; | ||
| import useNetwork from './useNetwork'; | ||
| import useOnyx from './useOnyx'; | ||
|
|
||
| /** | ||
| * Encapsulates the safety checks needed before switching to a delegator account: | ||
| * 1. Offline check – blocks the switch and shows an offline modal. | ||
| * 2. Chained delegation check – if already acting as a delegate and not returning | ||
| * to the original user, shows the "not so fast" modal. | ||
| * 3. GPS tracking check – if a GPS trip is in progress, asks the user to confirm | ||
| * stopping the trip before switching. | ||
| */ | ||
| function useSwitchToDelegator() { | ||
| const {translate} = useLocalize(); | ||
| const {isOffline} = useNetwork(); | ||
| const {showConfirmModal} = useConfirmModal(); | ||
| const {isActingAsDelegate} = useDelegateNoAccessState(); | ||
| const {showDelegateNoAccessModal} = useDelegateNoAccessActions(); | ||
|
|
||
| const [delegatedAccess] = useOnyx(ONYXKEYS.ACCOUNT, {selector: (account: Account | undefined) => account?.delegatedAccess}); | ||
| const [credentials] = useOnyx(ONYXKEYS.CREDENTIALS); | ||
| const [stashedCredentials = CONST.EMPTY_OBJECT] = useOnyx(ONYXKEYS.STASHED_CREDENTIALS); | ||
| const [session] = useOnyx(ONYXKEYS.SESSION); | ||
| const [stashedSession] = useOnyx(ONYXKEYS.STASHED_SESSION); | ||
| const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID); | ||
| const [gpsDraftDetails] = useOnyx(ONYXKEYS.GPS_DRAFT_DETAILS); | ||
| const [isTrackingGPS = false] = useOnyx(ONYXKEYS.GPS_DRAFT_DETAILS, {selector: isTrackingSelector}); | ||
|
|
||
| const showOfflineModal = () => { | ||
| showConfirmModal({ | ||
| title: translate('common.youAppearToBeOffline'), | ||
| prompt: translate('common.offlinePrompt'), | ||
| confirmText: translate('common.buttonConfirm'), | ||
| shouldShowCancelButton: false, | ||
| }); | ||
| }; | ||
|
|
||
| const showGpsInProgressModal = async (switchAccount: () => ReturnType<typeof connect | typeof disconnect>) => { | ||
| const result = await showConfirmModal({ | ||
| title: translate('gps.switchAccountWarningTripInProgress.title'), | ||
| prompt: translate('gps.switchAccountWarningTripInProgress.prompt'), | ||
| confirmText: translate('gps.switchAccountWarningTripInProgress.confirm'), | ||
| cancelText: translate('common.cancel'), | ||
| }); | ||
|
|
||
| if (result.action !== ModalActions.CONFIRM) { | ||
| return; | ||
| } | ||
|
|
||
| await stopGpsTrip(false, getGpsPoints(gpsDraftDetails), true); | ||
| switchAccount(); | ||
| }; | ||
|
|
||
| const switchToDelegator = (email: string) => { | ||
| if (isOffline) { | ||
| modalClose(() => showOfflineModal()); | ||
| return; | ||
| } | ||
| const isReturningToOriginalUser = isActingAsDelegate && email === stashedSession?.email; | ||
| // Chained delegation isn't supported by the backend — if we're already acting as a delegate, | ||
| // the only legal switch is back to the original user. Anything else triggers the "Not so fast" modal. | ||
| if (isActingAsDelegate && !isReturningToOriginalUser) { | ||
| modalClose(() => showDelegateNoAccessModal()); | ||
| return; | ||
| } | ||
| const switchAction = isReturningToOriginalUser | ||
| ? () => disconnect({stashedCredentials, stashedSession}) | ||
| : () => connect({email, delegatedAccess, credentials, session, activePolicyID}); | ||
| if (isTrackingGPS) { | ||
| modalClose(() => showGpsInProgressModal(switchAction)); | ||
| return; | ||
| } | ||
| switchAction(); | ||
| }; | ||
|
|
||
| return switchToDelegator; | ||
| } | ||
|
|
||
| export default useSwitchToDelegator; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,17 +36,31 @@ type AgentsListRowProps = { | |
|
|
||
| /** Whether to show the red error dot indicator */ | ||
| brickRoadIndicator?: typeof CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR | null; | ||
|
|
||
| /** Called when the Chat button is pressed */ | ||
| onChatPress?: (accountID: number) => void; | ||
|
|
||
| /** Called when the Copilot button is pressed */ | ||
| onCopilotPress?: (login: string) => void; | ||
| }; | ||
|
|
||
| function AgentsListRow({accountID, displayName, login, pendingAction, errors, onErrorClose, brickRoadIndicator}: AgentsListRowProps) { | ||
| function AgentsListRow({accountID, displayName, login, pendingAction, errors, onErrorClose, brickRoadIndicator, onChatPress, onCopilotPress}: AgentsListRowProps) { | ||
| const styles = useThemeStyles(); | ||
| const theme = useTheme(); | ||
| const {translate} = useLocalize(); | ||
| const {shouldUseNarrowLayout} = useResponsiveLayout(); | ||
| const icons = useMemoizedLazyExpensifyIcons(['DotIndicator']); | ||
| const icons = useMemoizedLazyExpensifyIcons(['DotIndicator', 'ChatBubble']); | ||
|
|
||
| const isPendingDeletion = pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE; | ||
| const isPendingAddOrDelete = pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD || isPendingDeletion; | ||
| const areActionsDisabled = isPendingAddOrDelete || accountID <= 0 || !login; | ||
| const navigateToEdit = () => Navigation.navigate(ROUTES.SETTINGS_AGENTS_EDIT.getRoute(accountID)); | ||
| const handleChatPress = () => { | ||
| onChatPress?.(accountID); | ||
| }; | ||
| const handleCopilotPress = () => { | ||
| onCopilotPress?.(login); | ||
| }; | ||
|
|
||
| return ( | ||
| <OfflineWithFeedback | ||
|
|
@@ -79,7 +93,7 @@ function AgentsListRow({accountID, displayName, login, pendingAction, errors, on | |
| )} | ||
| </PressableWithFeedback> | ||
| ) : ( | ||
| <View style={[styles.selectionListPressableItemWrapper, styles.mb2, styles.gap3]}> | ||
| <View style={[styles.selectionListPressableItemWrapper, styles.mb2, styles.gap2]}> | ||
| <AgentInfoRow | ||
| accountID={accountID} | ||
| displayName={displayName} | ||
|
|
@@ -92,6 +106,20 @@ function AgentsListRow({accountID, displayName, login, pendingAction, errors, on | |
| fill={theme.danger} | ||
| /> | ||
| )} | ||
| <Button | ||
| small | ||
| icon={icons.ChatBubble} | ||
| onPress={handleChatPress} | ||
| isDisabled={areActionsDisabled} | ||
| accessibilityLabel={translate('editAgentPage.chatWithAgent')} | ||
| /> | ||
|
Comment on lines
+109
to
+115
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. @MelvinBot Fix accessibility
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. Added |
||
| <Button | ||
| small | ||
| text={translate('delegate.copilot')} | ||
| onPress={handleCopilotPress} | ||
| isDisabled={areActionsDisabled} | ||
| accessibilityLabel={translate('editAgentPage.copilotIntoAccount')} | ||
| /> | ||
|
Comment on lines
+116
to
+122
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. @MelvinBot Fix accessibility
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. Added |
||
| <Button | ||
| small | ||
| text={translate('common.edit')} | ||
|
|
||
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.
❌ CONSISTENCY-3 (docs)
The
handleChatPresslogic (callingnavigateToAndOpenReportWithAccountIDswithcurrentUserPersonalDetails.accountID,introSelected,isSelfTourViewed,betas,personalDetails) is duplicated identically in bothAgentsListRow.tsxandEditAgentPage.tsx. All the supporting hooks (useCurrentUserPersonalDetails,useOnyx(NVP_INTRO_SELECTED),useOnyx(NVP_ONBOARDING),useOnyx(BETAS),useOnyx(PERSONAL_DETAILS_LIST)) are also duplicated.Extract a reusable hook (e.g.,
useChatWithAccount) that encapsulates the data fetching and returns a(accountID: number) => voidcallback, similar to howuseSwitchToDelegatorwas extracted.Reviewed at: b1dd748 | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.
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.
@MelvinBot Address this
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.
Addressed — extracted a
useChatWithAgenthook that encapsulates the data fetching (introSelected,isSelfTourViewed,betas,personalDetails,currentUserPersonalDetails) and thenavigateToAndOpenReportWithAccountIDscall. BothAgentsListRowandEditAgentPagenow use this shared hook.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.
Done — see reply above.