-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45112 from Expensify/Rory-DRYReportActionsSorting
Create usePaginatedReportActions hook (round 2)
- Loading branch information
Showing
3 changed files
with
54 additions
and
57 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import {useMemo} from 'react'; | ||
import {useOnyx} from 'react-native-onyx'; | ||
import PaginationUtils from '@libs/PaginationUtils'; | ||
import * as ReportActionsUtils from '@libs/ReportActionsUtils'; | ||
import ONYXKEYS from '@src/ONYXKEYS'; | ||
|
||
/** | ||
* Get the longest continuous chunk of reportActions including the linked reportAction. If not linking to a specific action, returns the continuous chunk of newest reportActions. | ||
*/ | ||
function usePaginatedReportActions(reportID?: string, reportActionID?: string) { | ||
// Use `||` instead of `??` to handle empty string. | ||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing | ||
const reportIDWithDefault = reportID || '-1'; | ||
|
||
const [sortedAllReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportIDWithDefault}`, { | ||
canEvict: false, | ||
selector: (allReportActions) => ReportActionsUtils.getSortedReportActionsForDisplay(allReportActions, true), | ||
}); | ||
const [reportActionPages] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS_PAGES}${reportIDWithDefault}`); | ||
|
||
const reportActions = useMemo(() => { | ||
if (!sortedAllReportActions?.length) { | ||
return []; | ||
} | ||
return PaginationUtils.getContinuousChain(sortedAllReportActions, reportActionPages ?? [], (reportAction) => reportAction.reportActionID, reportActionID); | ||
}, [reportActionID, reportActionPages, sortedAllReportActions]); | ||
|
||
const linkedAction = useMemo( | ||
() => sortedAllReportActions?.find((reportAction) => String(reportAction.reportActionID) === String(reportActionID)), | ||
[reportActionID, sortedAllReportActions], | ||
); | ||
|
||
return { | ||
reportActions, | ||
linkedAction, | ||
}; | ||
} | ||
|
||
export default usePaginatedReportActions; |
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