-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
20354 task share destination selector refactor #35413
Merged
cristipaval
merged 7 commits into
Expensify:main
from
lukemorawski:20354-task_share_destination_selector_refactor
Feb 29, 2024
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7beb521
added isLoadingNewOptions prop to BaseSelectionList
lukemorawski 4c45c2a
component refactor
lukemorawski a01ada2
brought back displayName
lukemorawski 618a41f
Merge branch 'main' into 20354-task_share_destination_selector_refactor
lukemorawski 2a269b5
offline text input hint
lukemorawski ecef93b
Merge branch 'main' into 20354-task_share_destination_selector_refactor
lukemorawski b028c51
crash fix
lukemorawski 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
This file contains 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 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 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 |
---|---|---|
@@ -1,154 +1,103 @@ | ||
/* eslint-disable es/no-optional-chaining */ | ||
import keys from 'lodash/keys'; | ||
import reduce from 'lodash/reduce'; | ||
import PropTypes from 'prop-types'; | ||
import React, {useCallback, useEffect, useMemo, useState} from 'react'; | ||
import React, {useEffect, useMemo} from 'react'; | ||
import {View} from 'react-native'; | ||
import {withOnyx} from 'react-native-onyx'; | ||
import _ from 'underscore'; | ||
import HeaderWithBackButton from '@components/HeaderWithBackButton'; | ||
import OptionsSelector from '@components/OptionsSelector'; | ||
import {usePersonalDetails} from '@components/OnyxProvider'; | ||
import ScreenWrapper from '@components/ScreenWrapper'; | ||
import withLocalize, {withLocalizePropTypes} from '@components/withLocalize'; | ||
import useAutoFocusInput from '@hooks/useAutoFocusInput'; | ||
import useNetwork from '@hooks/useNetwork'; | ||
import SelectionList from '@components/SelectionList'; | ||
import useDebouncedState from '@hooks/useDebouncedState'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import * as Report from '@libs/actions/Report'; | ||
import compose from '@libs/compose'; | ||
import Navigation from '@libs/Navigation/Navigation'; | ||
import * as OptionsListUtils from '@libs/OptionsListUtils'; | ||
import * as ReportUtils from '@libs/ReportUtils'; | ||
import personalDetailsPropType from '@pages/personalDetailsPropType'; | ||
import reportPropTypes from '@pages/reportPropTypes'; | ||
import * as Task from '@userActions/Task'; | ||
import CONST from '@src/CONST'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import ROUTES from '@src/ROUTES'; | ||
|
||
const propTypes = { | ||
/* Onyx Props */ | ||
|
||
/** Beta features list */ | ||
betas: PropTypes.arrayOf(PropTypes.string), | ||
|
||
/** All of the personal details for everyone */ | ||
personalDetails: PropTypes.objectOf(personalDetailsPropType), | ||
|
||
/** All reports shared with the user */ | ||
reports: PropTypes.objectOf(reportPropTypes), | ||
|
||
/** Whether we are searching for reports in the server */ | ||
/** Whether or not we are searching for reports on the server */ | ||
isSearchingForReports: PropTypes.bool, | ||
|
||
...withLocalizePropTypes, | ||
}; | ||
|
||
const defaultProps = { | ||
betas: [], | ||
personalDetails: {}, | ||
reports: {}, | ||
isSearchingForReports: false, | ||
}; | ||
|
||
function TaskShareDestinationSelectorModal(props) { | ||
const styles = useThemeStyles(); | ||
const [searchValue, setSearchValue] = useState(''); | ||
const [headerMessage, setHeaderMessage] = useState(''); | ||
const [filteredRecentReports, setFilteredRecentReports] = useState([]); | ||
const selectReportHandler = (option) => { | ||
if (!option || !option.reportID) { | ||
return; | ||
} | ||
|
||
const {inputCallbackRef} = useAutoFocusInput(); | ||
const {isSearchingForReports} = props; | ||
const {isOffline} = useNetwork(); | ||
Task.setShareDestinationValue(option.reportID); | ||
Navigation.goBack(ROUTES.NEW_TASK); | ||
}; | ||
|
||
const filteredReports = useMemo(() => { | ||
const reports = {}; | ||
_.keys(props.reports).forEach((reportKey) => { | ||
if ( | ||
!ReportUtils.canUserPerformWriteAction(props.reports[reportKey]) || | ||
!ReportUtils.canCreateTaskInReport(props.reports[reportKey]) || | ||
ReportUtils.isCanceledTaskReport(props.reports[reportKey]) | ||
) { | ||
return; | ||
const reportFilter = (reports) => | ||
reduce( | ||
keys(reports), | ||
(filtered, reportKey) => { | ||
const report = reports[reportKey]; | ||
if (ReportUtils.canUserPerformWriteAction(report) && ReportUtils.canCreateTaskInReport(report) && !ReportUtils.isCanceledTaskReport(report)) { | ||
return {...filtered, [reportKey]: report}; | ||
} | ||
reports[reportKey] = props.reports[reportKey]; | ||
}); | ||
return reports; | ||
}, [props.reports]); | ||
const updateOptions = useCallback(() => { | ||
const {recentReports} = OptionsListUtils.getShareDestinationOptions(filteredReports, props.personalDetails, props.betas, searchValue.trim(), [], CONST.EXPENSIFY_EMAILS, true); | ||
|
||
setHeaderMessage(OptionsListUtils.getHeaderMessage(recentReports?.length !== 0, false, searchValue)); | ||
|
||
setFilteredRecentReports(recentReports); | ||
}, [props, searchValue, filteredReports]); | ||
|
||
useEffect(() => { | ||
const debouncedSearch = _.debounce(updateOptions, 150); | ||
debouncedSearch(); | ||
return () => { | ||
debouncedSearch.cancel(); | ||
}; | ||
}, [updateOptions]); | ||
return filtered; | ||
}, | ||
{}, | ||
); | ||
|
||
const getSections = () => { | ||
const sections = []; | ||
let indexOffset = 0; | ||
function TaskShareDestinationSelectorModal({reports, isSearchingForReports}) { | ||
const styles = useThemeStyles(); | ||
const [searchValue, debouncedSearchValue, setSearchValue] = useDebouncedState(''); | ||
const {translate} = useLocalize(); | ||
const personalDetails = usePersonalDetails(); | ||
|
||
if (filteredRecentReports?.length > 0) { | ||
sections.push({ | ||
data: filteredRecentReports, | ||
shouldShow: true, | ||
indexOffset, | ||
}); | ||
indexOffset += filteredRecentReports?.length; | ||
} | ||
const options = useMemo(() => { | ||
const filteredReports = reportFilter(reports); | ||
|
||
return sections; | ||
}; | ||
const {recentReports} = OptionsListUtils.getShareDestinationOptions(filteredReports, personalDetails, [], debouncedSearchValue.trim(), [], CONST.EXPENSIFY_EMAILS, true); | ||
|
||
const selectReport = (option) => { | ||
if (!option) { | ||
return; | ||
} | ||
const headerMessage = OptionsListUtils.getHeaderMessage(recentReports && recentReports.length !== 0, false, debouncedSearchValue); | ||
|
||
if (option.reportID) { | ||
Task.setShareDestinationValue(option.reportID); | ||
Navigation.goBack(ROUTES.NEW_TASK); | ||
} | ||
}; | ||
const sections = recentReports && recentReports.length > 0 ? [{data: recentReports, shouldShow: true}] : []; | ||
|
||
// When search term updates we will fetch any reports | ||
const setSearchTermAndSearchInServer = useCallback((text = '') => { | ||
Report.searchInServer(text); | ||
setSearchValue(text); | ||
}, []); | ||
return {sections, headerMessage}; | ||
}, [personalDetails, reports, debouncedSearchValue]); | ||
|
||
const sections = getSections(); | ||
useEffect(() => { | ||
Report.searchInServer(debouncedSearchValue); | ||
}, [debouncedSearchValue]); | ||
|
||
return ( | ||
<ScreenWrapper | ||
includeSafeAreaPaddingBottom={false} | ||
testID={TaskShareDestinationSelectorModal.displayName} | ||
testID="TaskShareDestinationSelectorModal" | ||
> | ||
{({didScreenTransitionEnd, safeAreaPaddingBottomStyle}) => ( | ||
<> | ||
<HeaderWithBackButton | ||
title={props.translate('newTaskPage.shareSomewhere')} | ||
title={translate('newTaskPage.shareSomewhere')} | ||
onBackButtonPress={() => Navigation.goBack(ROUTES.NEW_TASK)} | ||
/> | ||
<View style={[styles.flex1, styles.w100, styles.pRelative]}> | ||
<OptionsSelector | ||
sections={sections} | ||
onSelectRow={selectReport} | ||
onChangeText={setSearchTermAndSearchInServer} | ||
headerMessage={headerMessage} | ||
hideSection | ||
Headers | ||
showTitleTooltip | ||
shouldShowOptions={didScreenTransitionEnd} | ||
textInputLabel={props.translate('optionsSelector.nameEmailOrPhoneNumber')} | ||
textInputAlert={isOffline ? `${props.translate('common.youAppearToBeOffline')} ${props.translate('search.resultsAreLimited')}` : ''} | ||
<SelectionList | ||
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. @lukemorawski on this branch, no offline message appears. was it intentionally removed? |
||
sections={didScreenTransitionEnd ? options.sections : CONST.EMPTY_ARRAY} | ||
onSelectRow={selectReportHandler} | ||
onChangeText={setSearchValue} | ||
textInputValue={searchValue} | ||
headerMessage={options.headerMessage} | ||
textInputLabel={translate('optionsSelector.nameEmailOrPhoneNumber')} | ||
safeAreaPaddingBottomStyle={safeAreaPaddingBottomStyle} | ||
autoFocus={false} | ||
ref={inputCallbackRef} | ||
showLoadingPlaceholder={!didScreenTransitionEnd} | ||
isLoadingNewOptions={isSearchingForReports} | ||
/> | ||
</View> | ||
|
@@ -162,21 +111,12 @@ TaskShareDestinationSelectorModal.displayName = 'TaskShareDestinationSelectorMod | |
TaskShareDestinationSelectorModal.propTypes = propTypes; | ||
TaskShareDestinationSelectorModal.defaultProps = defaultProps; | ||
|
||
export default compose( | ||
withLocalize, | ||
withOnyx({ | ||
reports: { | ||
key: ONYXKEYS.COLLECTION.REPORT, | ||
}, | ||
personalDetails: { | ||
key: ONYXKEYS.PERSONAL_DETAILS_LIST, | ||
}, | ||
betas: { | ||
key: ONYXKEYS.BETAS, | ||
}, | ||
isSearchingForReports: { | ||
key: ONYXKEYS.IS_SEARCHING_FOR_REPORTS, | ||
initWithStoredValues: false, | ||
}, | ||
}), | ||
)(TaskShareDestinationSelectorModal); | ||
export default withOnyx({ | ||
reports: { | ||
key: ONYXKEYS.COLLECTION.REPORT, | ||
}, | ||
isSearchingForReports: { | ||
key: ONYXKEYS.IS_SEARCHING_FOR_REPORTS, | ||
initWithStoredValues: false, | ||
}, | ||
})(TaskShareDestinationSelectorModal); |
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.
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.
The task share page has a stuttering animation.
Screen.Recording.2024-02-14.at.08.32.26.mov