Repository navigation
Add sticky group headers to Spend table (two-phase approach) #92067
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
Changes from all commits
d8c1da2
bea81da
c93a49a
e148a5b
248eecf
5980a0a
1a393fa
34adab1
05c4306
97c9a10
c2a32ec
87cbc9b
d3d7d63
912eaef
c7ca12d
438e40b
f69688b
a09c8e2
1b93219
2b6d025
bb880dc
ad4792e
44cf02a
9b8417e
903e5ed
335c5bf
1b37fe3
30f174c
01de445
7b2a8e6
a901b0e
c74da45
455f534
188e027
d0263ce
8efed92
26e8d7d
c2bd800
a648786
284bad9
a98eb7a
b2a8b73
e3446df
05b6d29
c03de11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| import React from 'react'; | ||
| import {View} from 'react-native'; | ||
| import Animated from 'react-native-reanimated'; | ||
| import useExpandCollapseAnimation from '@hooks/useExpandCollapseAnimation'; | ||
| import useTheme from '@hooks/useTheme'; | ||
| import useThemeStyles from '@hooks/useThemeStyles'; | ||
| import GroupChildrenContent from './GroupChildrenContent'; | ||
| import type {GroupChildrenContentProps} from './types'; | ||
|
|
||
| type GroupChildrenContainerProps = GroupChildrenContentProps & { | ||
| isLastItem?: boolean; | ||
| isSelected?: boolean; | ||
| }; | ||
|
|
||
| function GroupChildrenContainer({ | ||
| item, | ||
| isExpanded, | ||
| groupBy, | ||
| searchType, | ||
| columns, | ||
| canSelectMultiple, | ||
| onSelectRow, | ||
| onCheckboxPress, | ||
| onLongPressRow, | ||
| nonPersonalAndWorkspaceCards, | ||
| onUndelete, | ||
| isLastItem, | ||
| isSelected, | ||
| newTransactionID, | ||
| bankAccountList, | ||
| cardFeeds, | ||
| conciergeReportID, | ||
| }: GroupChildrenContainerProps) { | ||
| const theme = useTheme(); | ||
| const styles = useThemeStyles(); | ||
| const {isRendered, animatedStyle, onLayout} = useExpandCollapseAnimation(isExpanded); | ||
|
|
||
| if (!isExpanded && !isRendered) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <View style={[styles.mh5, {backgroundColor: isSelected ? theme.activeComponentBG : theme.highlightBG}, isLastItem && [styles.tableBottomRadius, styles.overflowHidden]]}> | ||
|
Contributor
Author
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. |
||
| <Animated.View style={animatedStyle}> | ||
| <Animated.View | ||
| style={[styles.stickToTop, styles.pb1]} | ||
| onLayout={onLayout} | ||
| > | ||
| <GroupChildrenContent | ||
| item={item} | ||
| isExpanded={isExpanded} | ||
| groupBy={groupBy} | ||
| searchType={searchType} | ||
| columns={columns} | ||
| canSelectMultiple={canSelectMultiple} | ||
| onSelectRow={onSelectRow} | ||
| onCheckboxPress={onCheckboxPress} | ||
| onLongPressRow={onLongPressRow} | ||
| nonPersonalAndWorkspaceCards={nonPersonalAndWorkspaceCards} | ||
| onUndelete={onUndelete} | ||
| newTransactionID={newTransactionID} | ||
| bankAccountList={bankAccountList} | ||
| cardFeeds={cardFeeds} | ||
| conciergeReportID={conciergeReportID} | ||
| /> | ||
| </Animated.View> | ||
| </Animated.View> | ||
| </View> | ||
| ); | ||
| } | ||
|
|
||
| export default GroupChildrenContainer; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| import {useIsFocused} from '@react-navigation/native'; | ||
| import React, {useEffect, useMemo, useState} from 'react'; | ||
| import {useSearchSelectionContext} from '@components/Search/SearchContext'; | ||
| import useActionLoadingReportIDs from '@hooks/useActionLoadingReportIDs'; | ||
| import {useCurrencyListActions} from '@hooks/useCurrencyList'; | ||
| import useCurrentUserPersonalDetails from '@hooks/useCurrentUserPersonalDetails'; | ||
| import useLocalize from '@hooks/useLocalize'; | ||
| import useNetwork from '@hooks/useNetwork'; | ||
| import useOnyx from '@hooks/useOnyx'; | ||
| import {search} from '@libs/actions/Search'; | ||
| import {getSections} from '@libs/SearchUIUtils'; | ||
| import {mergeProhibitedViolations, shouldShowViolation} from '@libs/TransactionUtils'; | ||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {Transaction, TransactionViolation, TransactionViolations} from '@src/types/onyx'; | ||
| import TransactionGroupListExpandedItem from './TransactionGroupListExpanded'; | ||
| import type {GroupChildrenContentProps, TransactionListItemType} from './types'; | ||
|
|
||
| function GroupChildrenContent({ | ||
| item, | ||
| isExpanded, | ||
| groupBy, | ||
| searchType, | ||
| columns, | ||
| canSelectMultiple, | ||
| onSelectRow, | ||
| onCheckboxPress, | ||
| onLongPressRow, | ||
| nonPersonalAndWorkspaceCards, | ||
| onUndelete, | ||
| newTransactionID, | ||
| bankAccountList, | ||
| cardFeeds, | ||
| conciergeReportID, | ||
| }: GroupChildrenContentProps) { | ||
| const {translate, formatPhoneNumber} = useLocalize(); | ||
| const {selectedTransactions} = useSearchSelectionContext(); | ||
| const currentUserDetails = useCurrentUserPersonalDetails(); | ||
| const isScreenFocused = useIsFocused(); | ||
| const {convertToDisplayString} = useCurrencyListActions(); | ||
| const {isOffline} = useNetwork(); | ||
|
|
||
| const groupItem = item; | ||
| const isExpenseReportType = searchType === CONST.SEARCH.DATA_TYPES.EXPENSE_REPORT; | ||
|
|
||
| const [transactionsSnapshot] = useOnyx(`${ONYXKEYS.COLLECTION.SNAPSHOT}${groupItem.transactionsQueryJSON?.hash}`); | ||
| const [transactionsVisibleLimit, setTransactionsVisibleLimit] = useState<number>(CONST.TRANSACTION.RESULTS_PAGE_SIZE); | ||
| const isActionLoadingSet = useActionLoadingReportIDs(); | ||
| const snapshotData = transactionsSnapshot?.data; | ||
|
|
||
| const selectedTransactionIDsSet = useMemo(() => new Set(Object.keys(selectedTransactions)), [selectedTransactions]); | ||
|
|
||
| const transactions: TransactionListItemType[] = useMemo(() => { | ||
| if (isExpenseReportType) { | ||
| return groupItem.transactions; | ||
| } | ||
| if (!snapshotData) { | ||
| return []; | ||
| } | ||
| const [sectionData] = getSections({ | ||
| type: CONST.SEARCH.DATA_TYPES.EXPENSE, | ||
| data: snapshotData, | ||
| currentAccountID: currentUserDetails.accountID, | ||
| currentUserEmail: currentUserDetails.email ?? '', | ||
| translate, | ||
| formatPhoneNumber, | ||
| bankAccountList, | ||
| isActionLoadingSet, | ||
| cardFeeds, | ||
| conciergeReportID, | ||
| convertToDisplayString, | ||
| }) as [TransactionListItemType[], number, boolean]; | ||
| return sectionData.map((transactionItem) => ({ | ||
| ...transactionItem, | ||
| isSelected: selectedTransactionIDsSet.has(transactionItem.transactionID), | ||
| })); | ||
| }, [ | ||
| isExpenseReportType, | ||
| groupItem.transactions, | ||
| snapshotData, | ||
| currentUserDetails.accountID, | ||
| currentUserDetails.email, | ||
| translate, | ||
| formatPhoneNumber, | ||
| bankAccountList, | ||
| isActionLoadingSet, | ||
| cardFeeds, | ||
| conciergeReportID, | ||
| convertToDisplayString, | ||
| selectedTransactionIDsSet, | ||
| ]); | ||
|
|
||
| const isEmpty = transactions.length === 0; | ||
| const shouldDisplayEmptyView = isEmpty && isExpenseReportType; | ||
|
|
||
| const refreshTransactions = () => { | ||
| if (!groupItem.transactionsQueryJSON) { | ||
| return; | ||
| } | ||
| search({ | ||
| queryJSON: groupItem.transactionsQueryJSON, | ||
| searchKey: undefined, | ||
| offset: 0, | ||
| shouldCalculateTotals: false, | ||
| isLoading: !!transactionsSnapshot?.search?.isLoading, | ||
| isOffline, | ||
| }); | ||
| }; | ||
|
|
||
| const searchTransactions = (pageSize = 0) => { | ||
| if (!groupItem.transactionsQueryJSON) { | ||
| return; | ||
| } | ||
| search({ | ||
| queryJSON: groupItem.transactionsQueryJSON, | ||
| searchKey: undefined, | ||
| offset: (transactionsSnapshot?.search?.offset ?? 0) + pageSize, | ||
| shouldCalculateTotals: false, | ||
| isLoading: !!transactionsSnapshot?.search?.isLoading, | ||
| isOffline, | ||
| }); | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| if (!newTransactionID || !isExpanded) { | ||
| return; | ||
| } | ||
| refreshTransactions(); | ||
| // Only refresh when a new transaction is created in this group — refreshTransactions is excluded to avoid infinite loops | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
|
Krishna2323 marked this conversation as resolved.
Resolved
|
||
| }, [newTransactionID, isExpanded]); | ||
|
|
||
| useEffect(() => { | ||
| if (!isExpanded || isExpenseReportType) { | ||
| return; | ||
| } | ||
| refreshTransactions(); | ||
|
Krishna2323 marked this conversation as resolved.
Resolved
|
||
| // Only fetch on expand — refreshTransactions and isExpenseReportType are excluded to prevent re-fetching on every render | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [isExpanded]); | ||
|
|
||
| const wasScreenFocusedRef = React.useRef(isScreenFocused); | ||
| useEffect(() => { | ||
| const didReturnToScreen = wasScreenFocusedRef.current === false && isScreenFocused === true; | ||
| wasScreenFocusedRef.current = isScreenFocused; | ||
| if (!didReturnToScreen || !isExpanded || isExpenseReportType) { | ||
| return; | ||
|
Krishna2323 marked this conversation as resolved.
Resolved
|
||
| } | ||
| refreshTransactions(); | ||
| // Only refresh when returning to this screen while expanded — other deps excluded to avoid redundant fetches | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [isScreenFocused]); | ||
|
|
||
| const filteredViolations: Record<string, TransactionViolation[]> = useMemo(() => { | ||
| if (!snapshotData) { | ||
| return {}; | ||
| } | ||
|
|
||
| const groupViolations: Record<string, TransactionViolations | undefined> = {}; | ||
| for (const [key, value] of Object.entries(snapshotData)) { | ||
| if (key.startsWith(ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS)) { | ||
| groupViolations[key] = value as TransactionViolations; | ||
| } | ||
| } | ||
|
|
||
| const result: Record<string, TransactionViolation[]> = {}; | ||
| for (const key of Object.keys(snapshotData)) { | ||
| if (!key.startsWith(ONYXKEYS.COLLECTION.TRANSACTION)) { | ||
| continue; | ||
| } | ||
| const snapshotTransaction = snapshotData[key as keyof typeof snapshotData] as Transaction; | ||
|
Check failure on line 171 in src/components/Search/SearchList/ListItem/GroupChildrenContent.tsx
|
||
| if (!snapshotTransaction || typeof snapshotTransaction !== 'object' || !('transactionID' in snapshotTransaction) || !('reportID' in snapshotTransaction)) { | ||
| continue; | ||
| } | ||
|
|
||
| const report = snapshotData[`${ONYXKEYS.COLLECTION.REPORT}${snapshotTransaction.reportID}`]; | ||
| const policy = snapshotData[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`]; | ||
|
|
||
| if (report && policy) { | ||
| const transactionViolations = groupViolations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${snapshotTransaction.transactionID}`]; | ||
| if (transactionViolations) { | ||
| const merged = mergeProhibitedViolations( | ||
| transactionViolations.filter((violation) => shouldShowViolation(report, policy, violation.name, currentUserDetails?.email ?? '', true, snapshotTransaction)), | ||
| ); | ||
| if (merged.length > 0) { | ||
| result[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${snapshotTransaction.transactionID}`] = merged; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return result; | ||
| }, [snapshotData, currentUserDetails.email]); | ||
|
|
||
| const onExpandedRowLongPress = (transaction: TransactionListItemType) => { | ||
| onLongPressRow?.(transaction); | ||
| }; | ||
|
|
||
| return ( | ||
| <TransactionGroupListExpandedItem | ||
| showTooltip | ||
| canSelectMultiple={canSelectMultiple} | ||
| onSelectionButtonPress={onCheckboxPress} | ||
| onSelectRow={onSelectRow} | ||
| columns={columns} | ||
| groupBy={groupBy} | ||
| accountID={currentUserDetails.accountID} | ||
| isOffline={isOffline} | ||
| violations={filteredViolations} | ||
| transactions={transactions} | ||
| transactionsVisibleLimit={transactionsVisibleLimit} | ||
| setTransactionsVisibleLimit={setTransactionsVisibleLimit} | ||
| isEmpty={isEmpty} | ||
| shouldDisplayEmptyView={shouldDisplayEmptyView} | ||
| isExpenseReportType={isExpenseReportType} | ||
| transactionsSnapshot={transactionsSnapshot} | ||
| transactionsQueryJSON={groupItem.transactionsQueryJSON} | ||
| searchTransactions={searchTransactions} | ||
| isInSingleTransactionReport={groupItem.transactions.length === 1} | ||
| onLongPress={onExpandedRowLongPress} | ||
| nonPersonalAndWorkspaceCards={nonPersonalAndWorkspaceCards} | ||
| onUndelete={onUndelete} | ||
| hideSearchTableHeader | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| export default GroupChildrenContent; | ||
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.
These files parse Onyx snapshot data using the same
snapshotData[key as keyof typeof snapshotData] as Transactionandvalue as TransactionViolationspatterns already present inTransactionGroupListItem.tsxandTransactionGroupListExpanded.tsx.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.
Should we add these exceptions here? Or should we disable lint for those lines?
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.
I went with seatbelt since it's the same pattern used by
TransactionGroupListItem.tsxandTransactionGroupListExpanded.tsx— the files this code was extracted from. Happy to switch to inline// eslint-disable-next-lineif that's preferred for new files.