Skip to content

Commit

Permalink
Move turning on selection mode logic on resize to main SelectionListW…
Browse files Browse the repository at this point in the history
…ithModal component
  • Loading branch information
filip-solecki committed Jul 30, 2024
1 parent e981e36 commit 7a0e5a6
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 140 deletions.
24 changes: 21 additions & 3 deletions src/components/SelectionListWithModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
import React, {forwardRef, useEffect, useState} from 'react';
import type {ForwardedRef} from 'react';
import {useOnyx} from 'react-native-onyx';
import * as Expensicons from '@components/Icon/Expensicons';
import MenuItem from '@components/MenuItem';
import Modal from '@components/Modal';
import SelectionList from '@components/SelectionList';
import type {BaseSelectionListProps, ListItem, SelectionListHandle} from '@components/SelectionList/types';
import useLocalize from '@hooks/useLocalize';
import useWindowDimensions from '@hooks/useWindowDimensions';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import {turnOffMobileSelectionMode, turnOnMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';

type SelectionListWithModalProps<TItem extends ListItem> = BaseSelectionListProps<TItem> & {
turnOnSelectionModeOnLongPress?: boolean;
onTurnOnSelectionMode?: (item: TItem | null) => void;
};

function SelectionListWithModal<TItem extends ListItem>(
{turnOnSelectionModeOnLongPress, onTurnOnSelectionMode, onLongPressRow, ...rest}: SelectionListWithModalProps<TItem>,
{turnOnSelectionModeOnLongPress, onTurnOnSelectionMode, onLongPressRow, sections, ...rest}: SelectionListWithModalProps<TItem>,
ref: ForwardedRef<SelectionListHandle>,
) {
const [isModalVisible, setIsModalVisible] = useState(false);
const [longPressedItem, setLongPressedItem] = useState<TItem | null>(null);
const {translate} = useLocalize();
const {isSmallScreenWidth} = useWindowDimensions();
const {isSmallScreenWidth} = useResponsiveLayout();
const [selectionMode] = useOnyx(ONYXKEYS.MOBILE_SELECTION_MODE);

useEffect(() => {
// We can access 0 index safely as we are not displaying multiple sections in table view
const selectedItems = sections[0].data.filter((item) => item.isSelected);
if (!isSmallScreenWidth) {
if (selectedItems.length === 0) {
turnOffMobileSelectionMode();
}
return;
}
if (selectedItems.length > 0 && !selectionMode?.isEnabled) {
turnOnMobileSelectionMode();
}
}, [sections, selectionMode, isSmallScreenWidth]);

const handleLongPressRow = (item: TItem) => {
if (!turnOnSelectionModeOnLongPress || !isSmallScreenWidth) {
Expand Down Expand Up @@ -51,6 +68,7 @@ function SelectionListWithModal<TItem extends ListItem>(
<>
<SelectionList
ref={ref}
sections={sections}
onLongPressRow={handleLongPressRow}
// eslint-disable-next-line react/jsx-props-no-spreading
{...rest}
Expand Down
14 changes: 1 addition & 13 deletions src/pages/ReportParticipantsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import useLocalize from '@hooks/useLocalize';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import {turnOffMobileSelectionMode, turnOnMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import {turnOffMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import * as Report from '@libs/actions/Report';
import Log from '@libs/Log';
import Navigation from '@libs/Navigation/Navigation';
Expand Down Expand Up @@ -62,18 +62,6 @@ function ReportParticipantsPage({report}: WithReportOrNotFoundProps) {
setSelectedMembers([]);
}, [isFocused]);

useEffect(() => {
if (!isSmallScreenWidth) {
if (selectedMembers.length === 0) {
turnOffMobileSelectionMode();
}
return;
}
if (selectedMembers.length > 0 && !selectionMode?.isEnabled) {
turnOnMobileSelectionMode();
}
}, [isSmallScreenWidth, selectedMembers, selectionMode?.isEnabled]);

const getUsers = useCallback((): MemberOption[] => {
let result: MemberOption[] = [];
const shouldExcludeHiddenParticipants = !isGroupChat && !isIOUReport;
Expand Down
20 changes: 5 additions & 15 deletions src/pages/workspace/WorkspaceMembersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import usePrevious from '@hooks/usePrevious';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import {turnOffMobileSelectionMode, turnOnMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import {turnOffMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import Log from '@libs/Log';
import Navigation from '@libs/Navigation/Navigation';
Expand Down Expand Up @@ -91,18 +91,6 @@ function WorkspaceMembersPage({personalDetails, route, policy, currentUserPerson

const canSelectMultiple = isPolicyAdmin && (shouldUseNarrowLayout ? selectionMode?.isEnabled : true);

useEffect(() => {
if (!shouldUseNarrowLayout) {
if (selectedEmployees.length === 0) {
turnOffMobileSelectionMode();
}
return;
}
if (isPolicyAdmin && selectedEmployees.length > 0 && !selectionMode?.isEnabled) {
turnOnMobileSelectionMode();
}
}, [isPolicyAdmin, shouldUseNarrowLayout, selectedEmployees.length, selectionMode?.isEnabled]);

const confirmModalPrompt = useMemo(() => {
const approverAccountID = selectedEmployees.find((selectedEmployee) => Member.isApprover(policy, selectedEmployee));
if (!approverAccountID) {
Expand Down Expand Up @@ -544,12 +532,14 @@ function WorkspaceMembersPage({personalDetails, route, policy, currentUserPerson
);
};

const selectionModeHeader = selectionMode?.isEnabled && shouldUseNarrowLayout;

return (
<WorkspacePageWithSections
headerText={selectionMode?.isEnabled ? translate('common.selectMultiple') : translate('workspace.common.members')}
headerText={selectionModeHeader ? translate('common.selectMultiple') : translate('workspace.common.members')}
route={route}
guidesCallTaskID={CONST.GUIDES_CALL_TASK_IDS.WORKSPACE_MEMBERS}
icon={!selectionMode?.isEnabled ? Illustrations.ReceiptWrangler : undefined}
icon={!selectionModeHeader ? Illustrations.ReceiptWrangler : undefined}
headerContent={!shouldUseNarrowLayout && getHeaderButtons()}
testID={WorkspaceMembersPage.displayName}
shouldShowLoading={false}
Expand Down
21 changes: 5 additions & 16 deletions src/pages/workspace/categories/WorkspaceCategoriesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import useNetwork from '@hooks/useNetwork';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import {turnOffMobileSelectionMode, turnOnMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import {turnOffMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import localeCompare from '@libs/LocaleCompare';
import Navigation from '@libs/Navigation/Navigation';
Expand Down Expand Up @@ -68,19 +68,6 @@ function WorkspaceCategoriesPage({route}: WorkspaceCategoriesPageProps) {

const canSelectMultiple = shouldUseNarrowLayout ? selectionMode?.isEnabled : true;

useEffect(() => {
const selectedKeys = Object.keys(selectedCategories).filter((key) => selectedCategories[key]);
if (!shouldUseNarrowLayout) {
if (selectedKeys.length === 0) {
turnOffMobileSelectionMode();
}
return;
}
if (selectedKeys.length > 0 && !selectionMode?.isEnabled) {
turnOnMobileSelectionMode();
}
}, [shouldUseNarrowLayout, selectedCategories, selectionMode?.isEnabled]);

const fetchCategories = useCallback(() => {
Category.openPolicyCategoriesPage(policyId);
}, [policyId]);
Expand Down Expand Up @@ -303,6 +290,8 @@ function WorkspaceCategoriesPage({route}: WorkspaceCategoriesPageProps) {
</View>
);

const selectionModeHeader = selectionMode?.isEnabled && shouldUseNarrowLayout;

return (
<AccessOrNotFoundWrapper
accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]}
Expand All @@ -318,8 +307,8 @@ function WorkspaceCategoriesPage({route}: WorkspaceCategoriesPageProps) {
>
<HeaderWithBackButton
shouldShowBackButton={shouldUseNarrowLayout}
title={selectionMode?.isEnabled ? translate('common.selectMultiple') : translate('workspace.common.categories')}
icon={!selectionMode?.isEnabled ? Illustrations.FolderOpen : undefined}
title={selectionModeHeader ? translate('common.selectMultiple') : translate('workspace.common.categories')}
icon={!selectionModeHeader ? Illustrations.FolderOpen : undefined}
onBackButtonPress={() => {
if (selectionMode?.isEnabled) {
setSelectedCategories({});
Expand Down
20 changes: 5 additions & 15 deletions src/pages/workspace/distanceRates/PolicyDistanceRatesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import useNetwork from '@hooks/useNetwork';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import {turnOffMobileSelectionMode, turnOnMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import {turnOffMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import Navigation from '@libs/Navigation/Navigation';
Expand Down Expand Up @@ -56,18 +56,6 @@ function PolicyDistanceRatesPage({

const canSelectMultiple = shouldUseNarrowLayout ? selectionMode?.isEnabled : true;

useEffect(() => {
if (!shouldUseNarrowLayout) {
if (selectedDistanceRates.length === 0) {
turnOffMobileSelectionMode();
}
return;
}
if (selectedDistanceRates.length > 0 && !selectionMode?.isEnabled) {
turnOnMobileSelectionMode();
}
}, [shouldUseNarrowLayout, selectedDistanceRates.length, selectionMode?.isEnabled]);

const customUnit: CustomUnit | undefined = useMemo(
() => (policy?.customUnits !== undefined ? policy?.customUnits[Object.keys(policy?.customUnits)[0]] : undefined),
[policy?.customUnits],
Expand Down Expand Up @@ -288,6 +276,8 @@ function PolicyDistanceRatesPage({
</View>
);

const selectionModeHeader = selectionMode?.isEnabled && shouldUseNarrowLayout;

return (
<AccessOrNotFoundWrapper
accessVariants={[CONST.POLICY.ACCESS_VARIANTS.ADMIN, CONST.POLICY.ACCESS_VARIANTS.PAID]}
Expand All @@ -301,8 +291,8 @@ function PolicyDistanceRatesPage({
shouldShowOfflineIndicatorInWideScreen
>
<HeaderWithBackButton
icon={!selectionMode?.isEnabled ? Illustrations.CarIce : undefined}
title={translate(!selectionMode?.isEnabled ? 'workspace.common.distanceRates' : 'common.selectMultiple')}
icon={!selectionModeHeader ? Illustrations.CarIce : undefined}
title={translate(!selectionModeHeader ? 'workspace.common.distanceRates' : 'common.selectMultiple')}
shouldShowBackButton={shouldUseNarrowLayout}
onBackButtonPress={() => {
if (selectionMode?.isEnabled) {
Expand Down
22 changes: 4 additions & 18 deletions src/pages/workspace/reportFields/ReportFieldsListValuesPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useEffect, useMemo, useState} from 'react';
import React, {useMemo, useState} from 'react';
import {View} from 'react-native';
import {useOnyx} from 'react-native-onyx';
import Button from '@components/Button';
Expand All @@ -19,7 +19,7 @@ import WorkspaceEmptyStateSection from '@components/WorkspaceEmptyStateSection';
import useLocalize from '@hooks/useLocalize';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';
import {turnOffMobileSelectionMode, turnOnMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import {turnOffMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import * as ReportField from '@libs/actions/Policy/ReportField';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import Navigation from '@libs/Navigation/Navigation';
Expand Down Expand Up @@ -66,20 +66,6 @@ function ReportFieldsListValuesPage({

const canSelectMultiple = !hasAccountingConnections && (isSmallScreenWidth ? selectionMode?.isEnabled : true);

useEffect(() => {
const selectedKeys = Object.keys(selectedValues).filter((key) => selectedValues[key]);

if (!isSmallScreenWidth) {
if (selectedKeys.length === 0) {
turnOffMobileSelectionMode();
}
return;
}
if (selectedKeys.length > 0 && !selectionMode?.isEnabled) {
turnOnMobileSelectionMode();
}
}, [isSmallScreenWidth, selectedValues, selectionMode?.isEnabled]);

const [listValues, disabledListValues] = useMemo(() => {
let reportFieldValues: string[];
let reportFieldDisabledValues: boolean[];
Expand Down Expand Up @@ -287,7 +273,7 @@ function ReportFieldsListValuesPage({
);
};

const selectionHeader = selectionMode?.isEnabled && isSmallScreenWidth;
const selectionModeHeader = selectionMode?.isEnabled && isSmallScreenWidth;

return (
<AccessOrNotFoundWrapper
Expand All @@ -302,7 +288,7 @@ function ReportFieldsListValuesPage({
shouldEnableMaxHeight
>
<HeaderWithBackButton
title={translate(selectionHeader ? 'common.selectMultiple' : 'workspace.reportFields.listValues')}
title={translate(selectionModeHeader ? 'common.selectMultiple' : 'workspace.reportFields.listValues')}
onBackButtonPress={() => {
if (selectionMode?.isEnabled) {
setSelectedValues({});
Expand Down
14 changes: 1 addition & 13 deletions src/pages/workspace/reportFields/WorkspaceReportFieldsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import useNetwork from '@hooks/useNetwork';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import {turnOffMobileSelectionMode, turnOnMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import {turnOffMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import Navigation from '@libs/Navigation/Navigation';
import type {FullScreenNavigatorParamList} from '@libs/Navigation/types';
Expand Down Expand Up @@ -77,18 +77,6 @@ function WorkspaceReportFieldsPage({

const canSelectMultiple = !hasAccountingConnections && shouldUseNarrowLayout ? selectionMode?.isEnabled : true;

useEffect(() => {
if (!shouldUseNarrowLayout) {
if (selectedReportFields.length === 0) {
turnOffMobileSelectionMode();
}
return;
}
if (selectedReportFields.length > 0 && !selectionMode?.isEnabled) {
turnOnMobileSelectionMode();
}
}, [shouldUseNarrowLayout, selectedReportFields, selectionMode?.isEnabled]);

const fetchReportFields = useCallback(() => {
ReportField.openPolicyReportFieldsPage(policyID);
}, [policyID]);
Expand Down
21 changes: 5 additions & 16 deletions src/pages/workspace/tags/WorkspaceTagsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import useNetwork from '@hooks/useNetwork';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import {turnOffMobileSelectionMode, turnOnMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import {turnOffMobileSelectionMode} from '@libs/actions/MobileSelectionMode';
import * as DeviceCapabilities from '@libs/DeviceCapabilities';
import localeCompare from '@libs/LocaleCompare';
import Navigation from '@libs/Navigation/Navigation';
Expand Down Expand Up @@ -69,19 +69,6 @@ function WorkspaceTagsPage({route}: WorkspaceTagsPageProps) {

useFocusEffect(fetchTags);

useEffect(() => {
const selectedKeys = Object.keys(selectedTags).filter((key) => selectedTags[key]);
if (!shouldUseNarrowLayout) {
if (selectedKeys.length === 0) {
turnOffMobileSelectionMode();
}
return;
}
if (selectedKeys.length > 0 && !selectionMode?.isEnabled) {
turnOnMobileSelectionMode();
}
}, [shouldUseNarrowLayout, selectedTags, selectionMode?.isEnabled]);

useEffect(() => {
if (isFocused) {
return;
Expand Down Expand Up @@ -317,6 +304,8 @@ function WorkspaceTagsPage({route}: WorkspaceTagsPageProps) {

const hasVisibleTag = tagList.some((tag) => tag.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE || isOffline);

const selectionModeHeader = selectionMode?.isEnabled && shouldUseNarrowLayout;

return (
<AccessOrNotFoundWrapper
policyID={policyID}
Expand All @@ -331,8 +320,8 @@ function WorkspaceTagsPage({route}: WorkspaceTagsPageProps) {
offlineIndicatorStyle={styles.mtAuto}
>
<HeaderWithBackButton
icon={!selectionMode?.isEnabled ? Illustrations.Tag : undefined}
title={translate(selectionMode?.isEnabled ? 'common.selectMultiple' : 'workspace.common.tags')}
icon={!selectionModeHeader ? Illustrations.Tag : undefined}
title={translate(selectionModeHeader ? 'common.selectMultiple' : 'workspace.common.tags')}
shouldShowBackButton={shouldUseNarrowLayout}
onBackButtonPress={() => {
if (selectionMode?.isEnabled) {
Expand Down
Loading

0 comments on commit 7a0e5a6

Please sign in to comment.