diff --git a/src/pages/workspace/WorkspaceInitialPage.js b/src/pages/workspace/WorkspaceInitialPage.js index 9fbada95c549..bedf7f6ba945 100644 --- a/src/pages/workspace/WorkspaceInitialPage.js +++ b/src/pages/workspace/WorkspaceInitialPage.js @@ -1,6 +1,6 @@ import lodashGet from 'lodash/get'; import PropTypes from 'prop-types'; -import React, {useCallback, useEffect, useState} from 'react'; +import React, {useCallback, useEffect, useMemo, useState} from 'react'; import {ScrollView, View} from 'react-native'; import {withOnyx} from 'react-native-onyx'; import _ from 'underscore'; @@ -15,10 +15,10 @@ import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeed import ScreenWrapper from '@components/ScreenWrapper'; import Text from '@components/Text'; import Tooltip from '@components/Tooltip'; -import withLocalize, {withLocalizePropTypes} from '@components/withLocalize'; -import withWindowDimensions from '@components/withWindowDimensions'; +import useLocalize from '@hooks/useLocalize'; import useSingleExecution from '@hooks/useSingleExecution'; import useWaitForNavigation from '@hooks/useWaitForNavigation'; +import useWindowDimensions from '@hooks/useWindowDimensions'; import compose from '@libs/compose'; import Navigation from '@libs/Navigation/Navigation'; import * as PolicyUtils from '@libs/PolicyUtils'; @@ -37,7 +37,6 @@ import withPolicyAndFullscreenLoading from './withPolicyAndFullscreenLoading'; const propTypes = { ...policyPropTypes, - ...withLocalizePropTypes, /** All reports shared with the user (coming from Onyx) */ reports: PropTypes.objectOf(reportPropTypes), @@ -75,16 +74,46 @@ function WorkspaceInitialPage(props) { const waitForNavigate = useWaitForNavigation(); const {singleExecution, isExecuting} = useSingleExecution(); + const {translate} = useLocalize(); + const {windowWidth} = useWindowDimensions(); + + const policyID = useMemo(() => policy.id, [policy]); + const [policyReports, adminsRoom, announceRoom] = useMemo(() => { + const reports = []; + let admins; + let announce; + _.each(props.reports, (report) => { + if (!report || report.policyID !== policyID) { + return; + } + + reports.push(report); + + if (!report.reportID || ReportUtils.isThread(report)) { + return; + } + + if (report.chatType === CONST.REPORT.CHAT_TYPE.POLICY_ADMINS) { + admins = report; + return; + } + + if (report.chatType === CONST.REPORT.CHAT_TYPE.POLICY_ANNOUNCE) { + announce = report; + } + }); + return [reports, admins, announce]; + }, [policyID, props.reports]); + /** * Call the delete policy and hide the modal */ const confirmDeleteAndHideModal = useCallback(() => { - const policyReports = _.filter(props.reports, (report) => report && report.policyID === policy.id); - Policy.deleteWorkspace(policy.id, policyReports, policy.name); + Policy.deleteWorkspace(policyID, policyReports, policy.name); setIsDeleteModalOpen(false); // Pop the deleted workspace page before opening workspace settings. Navigation.goBack(ROUTES.SETTINGS_WORKSPACES); - }, [props.reports, policy]); + }, [policyID, policy.name, policyReports]); useEffect(() => { const policyDraftId = lodashGet(props.policyDraft, 'id', null); @@ -108,22 +137,10 @@ function WorkspaceInitialPage(props) { * Call update workspace currency and hide the modal */ const confirmCurrencyChangeAndHideModal = useCallback(() => { - Policy.updateGeneralSettings(policy.id, policy.name, CONST.CURRENCY.USD); + Policy.updateGeneralSettings(policyID, policy.name, CONST.CURRENCY.USD); setIsCurrencyModalOpen(false); - ReimbursementAccount.navigateToBankAccountRoute(policy.id); - }, [policy]); - - /** - * Navigates to workspace rooms - * @param {String} chatType - */ - const goToRoom = useCallback( - (type) => { - const room = _.find(props.reports, (report) => report && report.policyID === policy.id && report.chatType === type && !ReportUtils.isThread(report)); - Navigation.dismissModal(room.reportID); - }, - [props.reports, policy], - ); + ReimbursementAccount.navigateToBankAccountRoute(policyID); + }, [policyID, policy.name]); const policyName = lodashGet(policy, 'name', ''); const hasMembersError = PolicyUtils.hasPolicyMemberError(props.policyMembers); @@ -179,23 +196,30 @@ function WorkspaceInitialPage(props) { }, ]; - const threeDotsMenuItems = [ - { - icon: Expensicons.Trashcan, - text: props.translate('workspace.common.delete'), - onSelected: () => setIsDeleteModalOpen(true), - }, - { - icon: Expensicons.Hashtag, - text: props.translate('workspace.common.goToRoom', {roomName: CONST.REPORT.WORKSPACE_CHAT_ROOMS.ADMINS}), - onSelected: () => goToRoom(CONST.REPORT.CHAT_TYPE.POLICY_ADMINS), - }, - { - icon: Expensicons.Hashtag, - text: props.translate('workspace.common.goToRoom', {roomName: CONST.REPORT.WORKSPACE_CHAT_ROOMS.ANNOUNCE}), - onSelected: () => goToRoom(CONST.REPORT.CHAT_TYPE.POLICY_ANNOUNCE), - }, - ]; + const threeDotsMenuItems = useMemo(() => { + const items = [ + { + icon: Expensicons.Trashcan, + text: translate('workspace.common.delete'), + onSelected: () => setIsDeleteModalOpen(true), + }, + ]; + if (adminsRoom) { + items.push({ + icon: Expensicons.Hashtag, + text: translate('workspace.common.goToRoom', {roomName: CONST.REPORT.WORKSPACE_CHAT_ROOMS.ADMINS}), + onSelected: () => Navigation.dismissModal(adminsRoom.reportID), + }); + } + if (announceRoom) { + items.push({ + icon: Expensicons.Hashtag, + text: translate('workspace.common.goToRoom', {roomName: CONST.REPORT.WORKSPACE_CHAT_ROOMS.ANNOUNCE}), + onSelected: () => Navigation.dismissModal(announceRoom.reportID), + }); + } + return items; + }, [adminsRoom, announceRoom, translate]); return ( Navigation.goBack(ROUTES.SETTINGS_WORKSPACES)} /> @@ -230,12 +254,12 @@ function WorkspaceInitialPage(props) { - + openEditor(policy.id)))} - accessibilityLabel={props.translate('workspace.common.settings')} + accessibilityLabel={translate('workspace.common.settings')} role={CONST.ACCESSIBILITY_ROLE.BUTTON} > {!_.isEmpty(policy.name) && ( - + openEditor(policy.id)))} - accessibilityLabel={props.translate('workspace.common.settings')} + accessibilityLabel={translate('workspace.common.settings')} role={CONST.ACCESSIBILITY_ROLE.BUTTON} > setIsCurrencyModalOpen(false)} - prompt={props.translate('workspace.bankAccount.updateCurrencyPrompt')} - confirmText={props.translate('workspace.bankAccount.updateToUSD')} - cancelText={props.translate('common.cancel')} + prompt={translate('workspace.bankAccount.updateCurrencyPrompt')} + confirmText={translate('workspace.bankAccount.updateToUSD')} + cancelText={translate('common.cancel')} danger /> setIsDeleteModalOpen(false)} - prompt={props.translate('workspace.common.deleteConfirmation')} - confirmText={props.translate('common.delete')} - cancelText={props.translate('common.cancel')} + prompt={translate('workspace.common.deleteConfirmation')} + confirmText={translate('common.delete')} + cancelText={translate('common.cancel')} danger /> @@ -320,9 +344,7 @@ WorkspaceInitialPage.defaultProps = defaultProps; WorkspaceInitialPage.displayName = 'WorkspaceInitialPage'; export default compose( - withLocalize, withPolicyAndFullscreenLoading, - withWindowDimensions, withOnyx({ reports: { key: ONYXKEYS.COLLECTION.REPORT,