|
| 1 | +import {findFocusedRoute, useNavigationState} from '@react-navigation/native'; |
| 2 | +import React, {useCallback, useMemo} from 'react'; |
| 3 | +import {View} from 'react-native'; |
| 4 | +import type {OnyxCollection} from 'react-native-onyx'; |
| 5 | +import type {ValueOf} from 'type-fest'; |
| 6 | +import Icon from '@components/Icon'; |
| 7 | +import {PressableWithFeedback} from '@components/Pressable'; |
| 8 | +import Text from '@components/Text'; |
| 9 | +import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; |
| 10 | +import {useMemoizedLazyExpensifyIcons} from '@hooks/useLazyAsset'; |
| 11 | +import useLocalize from '@hooks/useLocalize'; |
| 12 | +import useOnyx from '@hooks/useOnyx'; |
| 13 | +import useResponsiveLayout from '@hooks/useResponsiveLayout'; |
| 14 | +import useTheme from '@hooks/useTheme'; |
| 15 | +import useThemeStyles from '@hooks/useThemeStyles'; |
| 16 | +import useWorkspacesTabIndicatorStatus from '@hooks/useWorkspacesTabIndicatorStatus'; |
| 17 | +import navigateToWorkspacesPage, {getWorkspaceNavigationRouteState} from '@libs/Navigation/helpers/navigateToWorkspacesPage'; |
| 18 | +import variables from '@styles/variables'; |
| 19 | +import CONST from '@src/CONST'; |
| 20 | +import NAVIGATORS from '@src/NAVIGATORS'; |
| 21 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 22 | +import type {DomainSplitNavigatorParamList, WorkspaceSplitNavigatorParamList} from '@navigation/types'; |
| 23 | +import type SCREENS from '@src/SCREENS'; |
| 24 | +import type {Domain, Policy} from '@src/types/onyx'; |
| 25 | +import getTabIconFill from './getTabIconFill'; |
| 26 | +import NAVIGATION_TABS from './NAVIGATION_TABS'; |
| 27 | + |
| 28 | +type WorkspacesTabButtonProps = { |
| 29 | + selectedTab: ValueOf<typeof NAVIGATION_TABS>; |
| 30 | + isWideLayout: boolean; |
| 31 | +}; |
| 32 | + |
| 33 | +function WorkspacesTabButton({selectedTab, isWideLayout}: WorkspacesTabButtonProps) { |
| 34 | + const theme = useTheme(); |
| 35 | + const styles = useThemeStyles(); |
| 36 | + const {translate, preferredLocale} = useLocalize(); |
| 37 | + const expensifyIcons = useMemoizedLazyExpensifyIcons(['Buildings']); |
| 38 | + const {indicatorColor: workspacesTabIndicatorColor, status: workspacesTabIndicatorStatus} = useWorkspacesTabIndicatorStatus(); |
| 39 | + const {shouldUseNarrowLayout} = useResponsiveLayout(); |
| 40 | + const {login: currentUserLogin} = useCurrentUserPersonalDetails(); |
| 41 | + |
| 42 | + const navigationState = useNavigationState(findFocusedRoute); |
| 43 | + const {lastWorkspacesTabNavigatorRoute, workspacesTabState} = getWorkspaceNavigationRouteState(); |
| 44 | + const params = workspacesTabState?.routes?.at(0)?.params as |
| 45 | + | WorkspaceSplitNavigatorParamList[typeof SCREENS.WORKSPACE.INITIAL] |
| 46 | + | DomainSplitNavigatorParamList[typeof SCREENS.DOMAIN.INITIAL]; |
| 47 | + |
| 48 | + const paramsPolicyID = params && 'policyID' in params ? params.policyID : undefined; |
| 49 | + const paramsDomainAccountID = params && 'domainAccountID' in params ? params.domainAccountID : undefined; |
| 50 | + |
| 51 | + const lastViewedPolicySelector = useCallback( |
| 52 | + (policies: OnyxCollection<Policy>) => { |
| 53 | + if (!lastWorkspacesTabNavigatorRoute || lastWorkspacesTabNavigatorRoute.name !== NAVIGATORS.WORKSPACE_SPLIT_NAVIGATOR || !paramsPolicyID) { |
| 54 | + return undefined; |
| 55 | + } |
| 56 | + |
| 57 | + return policies?.[`${ONYXKEYS.COLLECTION.POLICY}${paramsPolicyID}`]; |
| 58 | + }, |
| 59 | + [paramsPolicyID, lastWorkspacesTabNavigatorRoute], |
| 60 | + ); |
| 61 | + |
| 62 | + const [lastViewedPolicy] = useOnyx( |
| 63 | + ONYXKEYS.COLLECTION.POLICY, |
| 64 | + { |
| 65 | + canBeMissing: true, |
| 66 | + selector: lastViewedPolicySelector, |
| 67 | + }, |
| 68 | + [navigationState], |
| 69 | + ); |
| 70 | + |
| 71 | + const lastViewedDomainSelector = useCallback( |
| 72 | + (domains: OnyxCollection<Domain>) => { |
| 73 | + if (!lastWorkspacesTabNavigatorRoute || lastWorkspacesTabNavigatorRoute.name !== NAVIGATORS.DOMAIN_SPLIT_NAVIGATOR || !paramsDomainAccountID) { |
| 74 | + return undefined; |
| 75 | + } |
| 76 | + |
| 77 | + return domains?.[`${ONYXKEYS.COLLECTION.DOMAIN}${paramsDomainAccountID}`]; |
| 78 | + }, |
| 79 | + [paramsDomainAccountID, lastWorkspacesTabNavigatorRoute], |
| 80 | + ); |
| 81 | + |
| 82 | + const [lastViewedDomain] = useOnyx( |
| 83 | + ONYXKEYS.COLLECTION.DOMAIN, |
| 84 | + { |
| 85 | + canBeMissing: true, |
| 86 | + selector: lastViewedDomainSelector, |
| 87 | + }, |
| 88 | + [navigationState], |
| 89 | + ); |
| 90 | + |
| 91 | + const showWorkspaces = useCallback(() => { |
| 92 | + navigateToWorkspacesPage({shouldUseNarrowLayout, currentUserLogin, policy: lastViewedPolicy, domain: lastViewedDomain}); |
| 93 | + }, [shouldUseNarrowLayout, currentUserLogin, lastViewedPolicy, lastViewedDomain]); |
| 94 | + |
| 95 | + const workspacesAccessibilityState = useMemo(() => ({selected: selectedTab === NAVIGATION_TABS.WORKSPACES}), [selectedTab]); |
| 96 | + |
| 97 | + if (isWideLayout) { |
| 98 | + return ( |
| 99 | + <PressableWithFeedback |
| 100 | + onPress={showWorkspaces} |
| 101 | + role={CONST.ROLE.TAB} |
| 102 | + accessibilityLabel={`${translate('common.workspacesTabTitle')}${workspacesTabIndicatorStatus ? `. ${translate('common.yourReviewIsRequired')}` : ''}`} |
| 103 | + accessibilityState={workspacesAccessibilityState} |
| 104 | + style={({hovered}) => [styles.leftNavigationTabBarItem, hovered && styles.navigationTabBarItemHovered]} |
| 105 | + sentryLabel={CONST.SENTRY_LABEL.NAVIGATION_TAB_BAR.WORKSPACES} |
| 106 | + > |
| 107 | + {({hovered}) => ( |
| 108 | + <> |
| 109 | + <View> |
| 110 | + <Icon |
| 111 | + src={expensifyIcons.Buildings} |
| 112 | + fill={getTabIconFill(theme, {isSelected: selectedTab === NAVIGATION_TABS.WORKSPACES, isHovered: hovered})} |
| 113 | + width={variables.iconBottomBar} |
| 114 | + height={variables.iconBottomBar} |
| 115 | + /> |
| 116 | + {!!workspacesTabIndicatorStatus && ( |
| 117 | + <View |
| 118 | + style={[styles.navigationTabBarStatusIndicator, styles.statusIndicatorColor(workspacesTabIndicatorColor), hovered && {borderColor: theme.sidebarHover}]} |
| 119 | + /> |
| 120 | + )} |
| 121 | + </View> |
| 122 | + <Text |
| 123 | + numberOfLines={preferredLocale === CONST.LOCALES.DE || preferredLocale === CONST.LOCALES.NL ? 1 : 2} |
| 124 | + style={[ |
| 125 | + styles.textSmall, |
| 126 | + styles.textAlignCenter, |
| 127 | + styles.mt1Half, |
| 128 | + selectedTab === NAVIGATION_TABS.WORKSPACES ? styles.textBold : styles.textSupporting, |
| 129 | + styles.navigationTabBarLabel, |
| 130 | + ]} |
| 131 | + > |
| 132 | + {translate('common.workspacesTabTitle')} |
| 133 | + </Text> |
| 134 | + </> |
| 135 | + )} |
| 136 | + </PressableWithFeedback> |
| 137 | + ); |
| 138 | + } |
| 139 | + |
| 140 | + return ( |
| 141 | + <PressableWithFeedback |
| 142 | + onPress={showWorkspaces} |
| 143 | + role={CONST.ROLE.TAB} |
| 144 | + accessibilityLabel={`${translate('common.workspacesTabTitle')}${workspacesTabIndicatorStatus ? `. ${translate('common.yourReviewIsRequired')}` : ''}`} |
| 145 | + accessibilityState={workspacesAccessibilityState} |
| 146 | + wrapperStyle={styles.flex1} |
| 147 | + style={styles.navigationTabBarItem} |
| 148 | + sentryLabel={CONST.SENTRY_LABEL.NAVIGATION_TAB_BAR.WORKSPACES} |
| 149 | + > |
| 150 | + <View> |
| 151 | + <Icon |
| 152 | + src={expensifyIcons.Buildings} |
| 153 | + fill={selectedTab === NAVIGATION_TABS.WORKSPACES ? theme.iconMenu : theme.icon} |
| 154 | + width={variables.iconBottomBar} |
| 155 | + height={variables.iconBottomBar} |
| 156 | + /> |
| 157 | + {!!workspacesTabIndicatorStatus && <View style={[styles.navigationTabBarStatusIndicator, styles.statusIndicatorColor(workspacesTabIndicatorColor)]} />} |
| 158 | + </View> |
| 159 | + <Text |
| 160 | + numberOfLines={1} |
| 161 | + style={[ |
| 162 | + styles.textSmall, |
| 163 | + styles.textAlignCenter, |
| 164 | + styles.mt1Half, |
| 165 | + selectedTab === NAVIGATION_TABS.WORKSPACES ? styles.textBold : styles.textSupporting, |
| 166 | + styles.navigationTabBarLabel, |
| 167 | + ]} |
| 168 | + > |
| 169 | + {translate('common.workspacesTabTitle')} |
| 170 | + </Text> |
| 171 | + </PressableWithFeedback> |
| 172 | + ); |
| 173 | +} |
| 174 | + |
| 175 | +export default WorkspacesTabButton; |
0 commit comments