-
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
perf: Cache search options #38207
Merged
Merged
perf: Cache search options #38207
Changes from all commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
840ac30
add function to create all reports and personal details options
TMisiukiewicz 68e364c
use context to initialize all options
TMisiukiewicz d32dd3a
update getOptions, use options generation in search
TMisiukiewicz addd702
Merge branch 'main' into perf/cache-options
TMisiukiewicz 50c823c
update usage in search page
TMisiukiewicz 2c7d231
update options list utils
TMisiukiewicz b1bd11f
create reusable initializer for search pages
TMisiukiewicz 499eb84
add options to new chat page
TMisiukiewicz 122a6fb
update other occurencies of getoptions
TMisiukiewicz 4c0cb3e
update initializing in search page
TMisiukiewicz 0a7925e
update types for options
TMisiukiewicz 2eb21a1
Merge branch 'main' into perf/cache-options
TMisiukiewicz 47795b9
update loading in search page
TMisiukiewicz ffaee2a
update new chat page skeleton displaying
TMisiukiewicz 47fc679
update caching in all selection lists
TMisiukiewicz 623810f
update options in the background
TMisiukiewicz ef90c70
Merge remote-tracking branch 'upstream/main' into perf/cache-options
TMisiukiewicz d4621e9
update changed personal details
TMisiukiewicz 653c9cb
fix loader on new chat page
TMisiukiewicz d75cc3f
filter personal details with login
TMisiukiewicz 7ad2872
generate alternate text on the fly if needed
TMisiukiewicz 67a0ae2
freeze the option list when search is open
TMisiukiewicz 47c9e6b
remove unnecessary code
TMisiukiewicz 0377ba6
remove console logs
TMisiukiewicz f44f1f0
Merge branch 'main' into perf/cache-options
TMisiukiewicz c6e9541
fix for generating alternate text
TMisiukiewicz e7264b5
fix jest tests
TMisiukiewicz aa0c81b
Merge remote-tracking branch 'upstream/main' into perf/cache-options
TMisiukiewicz 69cd8ce
update reassure tests
TMisiukiewicz dcb9c38
fix prettier
TMisiukiewicz a694d98
Merge branch 'main' into perf/cache-options
TMisiukiewicz 6876ae7
update reassure option list utils tests
TMisiukiewicz e9fae4e
remove redundant function calls
TMisiukiewicz 02d531f
Merge branch 'main' into perf/cache-options
TMisiukiewicz 46a4fcc
speed up setting options
TMisiukiewicz 820c69a
Merge branch 'main' into perf/cache-options
TMisiukiewicz b28bd13
update money request participants selector
TMisiukiewicz 5692a8d
lint code
TMisiukiewicz ca2c10a
add comments for option list context
TMisiukiewicz ec3547e
add test for cached options in search page
TMisiukiewicz eef4d62
update displaying lists
TMisiukiewicz e13492e
Merge branch 'main' into perf/cache-options
TMisiukiewicz c28b0be
Merge remote-tracking branch 'upstream/main' into perf/cache-options
TMisiukiewicz 8e1cc45
code review updates
TMisiukiewicz 3467a73
fix displaying personal details in current user option
TMisiukiewicz cdd26e8
update caching behavior in lists
TMisiukiewicz d5b8e36
Merge remote-tracking branch 'upstream/main' into perf/cache-options
TMisiukiewicz 45efae2
Merge branch 'main' into perf/cache-options
TMisiukiewicz 40dfb81
update tagpicker types
TMisiukiewicz f971d6a
Merge remote-tracking branch 'upstream/main' into perf/cache-options
TMisiukiewicz c577094
fix failing reassure tests
TMisiukiewicz 5507fc8
Merge branch 'main' into perf/cache-options
TMisiukiewicz 304544f
lint code
TMisiukiewicz 4865582
fix typecheck
TMisiukiewicz d98ee6c
Merge remote-tracking branch 'upstream/main' into perf/cache-options
TMisiukiewicz 07d1b74
update function naming
TMisiukiewicz 50ed5e4
Merge remote-tracking branch 'upstream/main' into perf/cache-options
TMisiukiewicz 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 |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import React, {createContext, useCallback, useContext, useEffect, useMemo, useRef, useState} from 'react'; | ||
import {withOnyx} from 'react-native-onyx'; | ||
import type {OnyxCollection} from 'react-native-onyx'; | ||
import * as OptionsListUtils from '@libs/OptionsListUtils'; | ||
import type {OptionList} from '@libs/OptionsListUtils'; | ||
import * as ReportUtils from '@libs/ReportUtils'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
import type {Report} from '@src/types/onyx'; | ||
import {usePersonalDetails} from './OnyxProvider'; | ||
|
||
type OptionsListContextProps = { | ||
/** List of options for reports and personal details */ | ||
options: OptionList; | ||
/** Function to initialize the options */ | ||
initializeOptions: () => void; | ||
/** Flag to check if the options are initialized */ | ||
areOptionsInitialized: boolean; | ||
}; | ||
|
||
type OptionsListProviderOnyxProps = { | ||
/** Collection of reports */ | ||
reports: OnyxCollection<Report>; | ||
}; | ||
|
||
type OptionsListProviderProps = OptionsListProviderOnyxProps & { | ||
/** Actual content wrapped by this component */ | ||
children: React.ReactNode; | ||
}; | ||
|
||
const OptionsListContext = createContext<OptionsListContextProps>({ | ||
options: { | ||
reports: [], | ||
personalDetails: [], | ||
}, | ||
initializeOptions: () => {}, | ||
areOptionsInitialized: false, | ||
}); | ||
|
||
function OptionsListContextProvider({reports, children}: OptionsListProviderProps) { | ||
const areOptionsInitialized = useRef(false); | ||
const [options, setOptions] = useState<OptionList>({ | ||
reports: [], | ||
personalDetails: [], | ||
}); | ||
const personalDetails = usePersonalDetails(); | ||
|
||
useEffect(() => { | ||
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. Coming from #40493, we missed updating the cache when a report is deleted. |
||
// there is no need to update the options if the options are not initialized | ||
if (!areOptionsInitialized.current) { | ||
return; | ||
} | ||
|
||
const lastUpdatedReport = ReportUtils.getLastUpdatedReport(); | ||
|
||
if (!lastUpdatedReport) { | ||
return; | ||
} | ||
|
||
const newOption = OptionsListUtils.createOptionFromReport(lastUpdatedReport, personalDetails); | ||
const replaceIndex = options.reports.findIndex((option) => option.reportID === lastUpdatedReport.reportID); | ||
|
||
if (replaceIndex === -1) { | ||
return; | ||
} | ||
|
||
setOptions((prevOptions) => { | ||
const newOptions = {...prevOptions}; | ||
newOptions.reports[replaceIndex] = newOption; | ||
return newOptions; | ||
}); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [reports]); | ||
|
||
useEffect(() => { | ||
// there is no need to update the options if the options are not initialized | ||
if (!areOptionsInitialized.current) { | ||
return; | ||
} | ||
|
||
// since personal details are not a collection, we need to recreate the whole list from scratch | ||
const newPersonalDetailsOptions = OptionsListUtils.createOptionList(personalDetails).personalDetails; | ||
|
||
setOptions((prevOptions) => { | ||
const newOptions = {...prevOptions}; | ||
newOptions.personalDetails = newPersonalDetailsOptions; | ||
return newOptions; | ||
}); | ||
}, [personalDetails]); | ||
|
||
const loadOptions = useCallback(() => { | ||
const optionLists = OptionsListUtils.createOptionList(personalDetails, reports); | ||
setOptions({ | ||
reports: optionLists.reports, | ||
personalDetails: optionLists.personalDetails, | ||
}); | ||
}, [personalDetails, reports]); | ||
|
||
const initializeOptions = useCallback(() => { | ||
if (areOptionsInitialized.current) { | ||
return; | ||
} | ||
|
||
loadOptions(); | ||
areOptionsInitialized.current = true; | ||
}, [loadOptions]); | ||
|
||
return ( | ||
<OptionsListContext.Provider value={useMemo(() => ({options, initializeOptions, areOptionsInitialized: areOptionsInitialized.current}), [options, initializeOptions])}> | ||
{children} | ||
</OptionsListContext.Provider> | ||
); | ||
} | ||
|
||
const useOptionsListContext = () => useContext(OptionsListContext); | ||
|
||
// Hook to use the OptionsListContext with an initializer to load the options | ||
const useOptionsList = (options?: {shouldInitialize: boolean}) => { | ||
const {shouldInitialize = true} = options ?? {}; | ||
const {initializeOptions, options: optionsList, areOptionsInitialized} = useOptionsListContext(); | ||
|
||
useEffect(() => { | ||
if (!shouldInitialize || areOptionsInitialized) { | ||
return; | ||
} | ||
|
||
initializeOptions(); | ||
}, [shouldInitialize, initializeOptions, areOptionsInitialized]); | ||
|
||
return { | ||
initializeOptions, | ||
options: optionsList, | ||
areOptionsInitialized, | ||
}; | ||
}; | ||
|
||
export default withOnyx<OptionsListProviderProps, OptionsListProviderOnyxProps>({ | ||
reports: { | ||
key: ONYXKEYS.COLLECTION.REPORT, | ||
}, | ||
})(OptionsListContextProvider); | ||
|
||
export {useOptionsListContext, useOptionsList, OptionsListContext}; |
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
Oops, something went wrong.
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.
Placing this context provider here caused #39710 – we needed to move it inside
AuthScreens
so that it would be destroyed each time the user logs out.