Skip to content
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

SearchPage list refactor #33080

Merged
merged 23 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
97fbe84
refactor to functional component
lukemorawski Dec 5, 2023
a999180
list refactor
lukemorawski Dec 14, 2023
71b5056
moved useDebouncedState hook to an independent file
lukemorawski Dec 14, 2023
186bfb2
removed comment and unused param
lukemorawski Dec 14, 2023
e859ff1
removed old SearchPage component
lukemorawski Dec 14, 2023
ba39519
Merge branch 'main' into searchpage_list_refactor
lukemorawski Dec 14, 2023
d4c611f
Merge branch 'main' into searchpage_list_refactor
lukemorawski Dec 14, 2023
ad06aff
fixed theme hooks import
lukemorawski Dec 18, 2023
f3eceb7
Merge remote-tracking branch 'origin/searchpage_list_refactor' into s…
lukemorawski Dec 18, 2023
d6a29a5
Merge branch 'main' into searchpage_list_refactor
lukemorawski Dec 18, 2023
ef63c51
move to usePersonalDetails hook
lukemorawski Dec 18, 2023
af685f8
moved footer to seperate component
lukemorawski Dec 18, 2023
aba386f
moved searchRendered function outside of component scope
lukemorawski Dec 18, 2023
f013c6b
changed debouncedstate default delay value to val from const
lukemorawski Dec 19, 2023
3407d73
rename perf function
lukemorawski Dec 19, 2023
c0c09a3
fallback to global empty array
lukemorawski Dec 19, 2023
2c80e11
search page footer instance
lukemorawski Dec 19, 2023
b10fb90
Merge branch 'main' into searchpage_list_refactor
lukemorawski Dec 19, 2023
cd60062
removed old component
lukemorawski Dec 19, 2023
4d09449
const migration
lukemorawski Dec 19, 2023
ad41ffb
SectionList wrapper opacity fix to show skeleton
lukemorawski Jan 5, 2024
1a9907c
Merge branch 'main' into searchpage_list_refactor
lukemorawski Jan 5, 2024
6c2aa51
merge with main fixes
lukemorawski Jan 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/components/SelectionList/BaseSelectionList.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function BaseSelectionList({
textInputLabel = '',
textInputPlaceholder = '',
textInputValue = '',
textInputHint = '',
textInputMaxLength,
inputMode = CONST.INPUT_MODE.TEXT,
onChangeText,
Expand Down Expand Up @@ -413,6 +414,7 @@ function BaseSelectionList({
}}
label={textInputLabel}
accessibilityLabel={textInputLabel}
hint={textInputHint}
role={CONST.ROLE.PRESENTATION}
value={textInputValue}
placeholder={textInputPlaceholder}
Expand Down
35 changes: 35 additions & 0 deletions src/hooks/useDebouncedState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import debounce from 'lodash/debounce';
import {useEffect, useRef, useState} from 'react';
import CONST from '@src/CONST';

/**
* A React hook that provides a state and its debounced version.
*
* @param initialValue - The initial value of the state.
* @param delay - The debounce delay in milliseconds. Defaults to SEARCH_OPTION_LIST_DEBOUNCE_TIME = 300ms.
* @returns A tuple containing:
* - The current state value.
* - The debounced state value.
* - A function to set both the current and debounced state values.
*
* @template T The type of the state value.
*
* @example
* const [value, debouncedValue, setValue] = useDebouncedState<string>("", 300);
*/
function useDebouncedState<T>(initialValue: T, delay = CONST.TIMING.SEARCH_OPTION_LIST_DEBOUNCE_TIME): [T, T, (value: T) => void] {
const [value, setValue] = useState(initialValue);
const [debouncedValue, setDebouncedValue] = useState(initialValue);
const debouncedSetDebouncedValue = useRef(debounce(setDebouncedValue, delay)).current;

useEffect(() => () => debouncedSetDebouncedValue.cancel(), [debouncedSetDebouncedValue]);

const handleSetValue = (newValue: T) => {
setValue(newValue);
debouncedSetDebouncedValue(newValue);
};

return [value, debouncedValue, handleSetValue];
}

export default useDebouncedState;
59 changes: 59 additions & 0 deletions src/pages/SearchPage/SearchPageFooter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React from 'react';
import {View} from 'react-native';
import Icon from '@components/Icon';
import {Info} from '@components/Icon/Expensicons';
import {PressableWithoutFeedback} from '@components/Pressable';
import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';

function SearchPageFooter() {
const themeStyles = useThemeStyles();
const theme = useTheme();
const {translate} = useLocalize();

return (
<View style={[themeStyles.pb5, themeStyles.flexShrink0]}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The footer has a larger padding than what staging has

image

<PressableWithoutFeedback
onPress={() => {
Navigation.navigate(ROUTES.REFERRAL_DETAILS_MODAL.getRoute(CONST.REFERRAL_PROGRAM.CONTENT_TYPES.REFER_FRIEND));
}}
style={[
themeStyles.p5,
themeStyles.w100,
themeStyles.br2,
themeStyles.highlightBG,
themeStyles.flexRow,
themeStyles.justifyContentBetween,
themeStyles.alignItemsCenter,
{gap: 10},
]}
accessibilityLabel="referral"
role={CONST.ACCESSIBILITY_ROLE.BUTTON}
>
<Text>
{translate(`referralProgram.${CONST.REFERRAL_PROGRAM.CONTENT_TYPES.REFER_FRIEND}.buttonText1`)}
<Text
color={theme.success}
style={themeStyles.textStrong}
>
{translate(`referralProgram.${CONST.REFERRAL_PROGRAM.CONTENT_TYPES.REFER_FRIEND}.buttonText2`)}
</Text>
</Text>
<Icon
src={Info}
height={20}
width={20}
/>
</PressableWithoutFeedback>
</View>
);
}

SearchPageFooter.displayName = 'SearchPageFooter';

export default SearchPageFooter;
183 changes: 183 additions & 0 deletions src/pages/SearchPage/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import PropTypes from 'prop-types';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting here as well @rushatgabhane @adhorodyski @lukemorawski This page was added but the original SearchPage.js was not removed from the /pages folder so the app is actually using code from the original file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mountiny Actually I did remove the original file but probably it slipped in when merging this branch with main.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will prepare a quick PR removing the old file.

import React, {useEffect, useMemo, useState} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import {usePersonalDetails} from '@components/OnyxProvider';
import ScreenWrapper from '@components/ScreenWrapper';
import SelectionList from '@components/SelectionList';
import useDebouncedState from '@hooks/useDebouncedState';
import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import Performance from '@libs/Performance';
import * as ReportUtils from '@libs/ReportUtils';
import reportPropTypes from '@pages/reportPropTypes';
import * as Report from '@userActions/Report';
import Timing from '@userActions/Timing';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import SearchPageFooter from './SearchPageFooter';

const propTypes = {
/* Onyx Props */

/** Beta features list */
betas: PropTypes.arrayOf(PropTypes.string),

/** All reports shared with the user */
reports: PropTypes.objectOf(reportPropTypes),
};

const defaultProps = {
betas: [],
reports: {},
};

const setPerformanceTimersEnd = () => {
Timing.end(CONST.TIMING.SEARCH_RENDER);
Performance.markEnd(CONST.TIMING.SEARCH_RENDER);
};

const SearchPageFooterInstance = <SearchPageFooter />;

function SearchPage({betas, reports}) {
lukemorawski marked this conversation as resolved.
Show resolved Hide resolved
const [isScreenTransitionEnd, setIsScreenTransitionEnd] = useState(false);
const {translate} = useLocalize();
const {isOffline} = useNetwork();
const themeStyles = useThemeStyles();
const personalDetails = usePersonalDetails();

const offlineMessage = isOffline ? `${translate('common.youAppearToBeOffline')} ${translate('search.resultsAreLimited')}` : '';

const [searchValue, debouncedSearchValue, setSearchValue] = useDebouncedState('');

useEffect(() => {
Timing.start(CONST.TIMING.SEARCH_RENDER);
Performance.markStart(CONST.TIMING.SEARCH_RENDER);
}, []);

const onChangeText = (text = '') => {
Report.searchInServer(text);
setSearchValue(text);
};

const {
recentReports,
personalDetails: localPersonalDetails,
userToInvite,
headerMessage,
} = useMemo(() => {
if (!isScreenTransitionEnd) {
return {
recentReports: {},
personalDetails: {},
userToInvite: {},
headerMessage: '',
};
}
const options = OptionsListUtils.getSearchOptions(reports, personalDetails, debouncedSearchValue.trim(), betas);
const header = OptionsListUtils.getHeaderMessage(options.recentReports.length + options.personalDetails.length !== 0, Boolean(options.userToInvite), debouncedSearchValue);
return {...options, headerMessage: header};
}, [debouncedSearchValue, reports, personalDetails, betas, isScreenTransitionEnd]);

const sections = useMemo(() => {
const newSections = [];
let indexOffset = 0;

if (recentReports.length > 0) {
newSections.push({
data: recentReports,
shouldShow: true,
indexOffset,
});
indexOffset += recentReports.length;
}

if (localPersonalDetails.length > 0) {
newSections.push({
data: localPersonalDetails,
shouldShow: true,
indexOffset,
});
indexOffset += recentReports.length;
}

if (userToInvite) {
newSections.push({
data: [userToInvite],
shouldShow: true,
indexOffset,
});
}

return newSections;
}, [localPersonalDetails, recentReports, userToInvite]);

const selectReport = (option) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is the performance/renders situation without these 2 being memoed in any way? Are you able to compare the two?

Copy link
Contributor Author

@lukemorawski lukemorawski Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adhorodyski checked it and compared it to previous (class based) version of this component and it's in the same place, which means - renders (depends on edge case). Also measured performance on web, though not on HTA and it looks good. If you have an access to an HT account I would gladly check against it too!

if (!option) {
return;
}

if (option.reportID) {
setSearchValue('');
Navigation.dismissModal(option.reportID);
} else {
Report.navigateToAndOpenReport([option.login]);
}
};

const handleScreenTransitionEnd = () => {
setIsScreenTransitionEnd(true);
};

const isOptionsDataReady = useMemo(() => ReportUtils.isReportDataReady() && OptionsListUtils.isPersonalDetailsReady(personalDetails), [personalDetails]);

return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
testID={SearchPage.displayName}
onEntryTransitionEnd={handleScreenTransitionEnd}
>
{({didScreenTransitionEnd, safeAreaPaddingBottomStyle}) => (
<>
<HeaderWithBackButton title={translate('common.search')} />
<View style={[themeStyles.flex1, themeStyles.w100, safeAreaPaddingBottomStyle]}>
<SelectionList
sections={didScreenTransitionEnd && isOptionsDataReady ? sections : CONST.EMPTY_ARRAY}
textInputValue={searchValue}
textInputLabel={translate('optionsSelector.nameEmailOrPhoneNumber')}
textInputHint={offlineMessage}
onChangeText={onChangeText}
headerMessage={headerMessage}
onLayout={setPerformanceTimersEnd}
autoFocus
onSelectRow={selectReport}
showLoadingPlaceholder={!didScreenTransitionEnd || !isOptionsDataReady}
footerContent={SearchPageFooterInstance}
/>
</View>
</>
)}
</ScreenWrapper>
);
}

SearchPage.propTypes = propTypes;
SearchPage.defaultProps = defaultProps;
SearchPage.displayName = 'SearchPage';

export default withOnyx({
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
},
betas: {
key: ONYXKEYS.BETAS,
},
isSearchingForReports: {
key: ONYXKEYS.IS_SEARCHING_FOR_REPORTS,
initWithStoredValues: false,
},
})(SearchPage);
Loading