Repository navigation
Fix carousel auto scroll problem #83925
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
ae17af1
b94dabb
8642c4a
f819e16
3245265
7f5c45b
7aa8892
24572cb
6969c53
6a8d1bc
6dea5ce
14b6646
469bbb1
c59bbce
e960031
2d27dba
9109868
f50fec1
75ca17f
5137dd4
5e8482c
0598f7a
392fce6
cfe9aa1
45d6c26
66c35f9
97bcd61
a464919
01368c2
b9d8524
78b0cd9
c7643f8
ac0923f
5947290
a768daa
50b89b2
e29c53d
e5422a0
12698a3
0750f48
1e3b892
3195132
90d2d5a
5e3edab
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 |
|---|---|---|
| @@ -1,29 +1,77 @@ | ||
| import {useEffect, useMemo, useRef} from 'react'; | ||
| import {deletePendingNewTransactionIDs} from '@libs/actions/IOU/PendingNewTransactions'; | ||
| import CONST from '@src/CONST'; | ||
| import type {Transaction} from '@src/types/onyx'; | ||
| import {isEmptyObject} from '@src/types/utils/EmptyObject'; | ||
| import usePrevious from './usePrevious'; | ||
|
|
||
| /** | ||
| * This hook returns new transactions that have been added since the last transactions update. | ||
| * This hook should be used only in the context of highlighting the new transactions on the Report table view. | ||
| * | ||
| * When `pendingNewTransactionIDs` is provided, those transactions will be treated as new even on the | ||
| * first load. This handles the case where a transaction was created before the component mounts | ||
| * (e.g., submitting a tracked expense from Self DM to a workspace on Web). | ||
| */ | ||
| function useNewTransactions(hasOnceLoadedReportActions: boolean | undefined, transactions: Transaction[] | undefined) { | ||
| // If we haven't loaded report yet we set previous transactions to undefined. | ||
| function useNewTransactions( | ||
| hasOnceLoadedReportActions: boolean | undefined, | ||
| transactions: Transaction[] | undefined, | ||
| pendingNewTransactionIDs?: Record<string, true | null>, | ||
| reportID?: string, | ||
| isFocused?: boolean, | ||
| ) { | ||
| // If we haven't loaded report yet we set previous transaction ids to undefined. | ||
| const prevTransactions = usePrevious(hasOnceLoadedReportActions ? transactions : undefined); | ||
|
|
||
| // We need to skip the first transactions change, to avoid highlighting transactions on the first load. | ||
| const skipFirstTransactionsChange = useRef(!hasOnceLoadedReportActions); | ||
|
|
||
| const newTransactions = useMemo(() => { | ||
| // If isFocused is not passed (=undefined) we will not return empty. | ||
| if (isFocused === false) { | ||
| return CONST.EMPTY_ARRAY as unknown as Transaction[]; | ||
| } | ||
|
|
||
| if (transactions === undefined || prevTransactions === undefined || transactions.length <= prevTransactions.length) { | ||
| // When a transaction is submitted from another report (e.g., Self DM → workspace), it is | ||
| // already in the transactions list by the time this component mounts. | ||
| // So we use pendingNewTransactionIDs from report metadata to identify these transactions on first load. | ||
| if (isFocused && reportID && !isEmptyObject(pendingNewTransactionIDs) && transactions?.length) { | ||
| const pendingSet = new Set(Object.keys(pendingNewTransactionIDs)); | ||
| const pendingTransactions = transactions.filter(({transactionID}) => pendingSet.has(transactionID) && pendingNewTransactionIDs[transactionID]); | ||
| return pendingTransactions; | ||
| } | ||
| return CONST.EMPTY_ARRAY as unknown as Transaction[]; | ||
| } | ||
| if (skipFirstTransactionsChange.current) { | ||
| skipFirstTransactionsChange.current = false; | ||
| return CONST.EMPTY_ARRAY as unknown as Transaction[]; | ||
| } | ||
| return transactions.filter((transaction) => !prevTransactions?.some((prevTransaction) => prevTransaction.transactionID === transaction.transactionID)); | ||
| }, [transactions, prevTransactions]); | ||
|
|
||
| // We don't need to recalculate on change of prevTransactions or pendingNewTransactionIDs as it will make the value | ||
| // disappear quickly which will break the scroll and highlight on slower devices like mobile app. | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [transactions, reportID, isFocused]); | ||
|
Comment on lines
+52
to
+55
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.
This memo reads Useful? React with 👍 / 👎. |
||
|
|
||
| useEffect(() => { | ||
| if (!pendingNewTransactionIDs) { | ||
| return; | ||
| } | ||
| const pendingSet = new Set(Object.keys(pendingNewTransactionIDs)); | ||
| const pendingTransactions = newTransactions.filter(({transactionID}) => pendingSet.has(transactionID) && pendingNewTransactionIDs[transactionID]); | ||
| if (!pendingTransactions.length) { | ||
| return; | ||
| } | ||
|
|
||
| // We deletePendingNewTransactionIDs after the scroll and highlight has occurred. | ||
| setTimeout(() => { | ||
| deletePendingNewTransactionIDs( | ||
| reportID, | ||
| pendingTransactions.map((transaction) => transaction.transactionID), | ||
| ); | ||
| }, CONST.PENDING_TRANSACTION_DELETION_DELAY); | ||
| }, [pendingNewTransactionIDs, newTransactions, reportID]); | ||
|
|
||
| // In case when we have loaded the report, but there were no transactions in it, then we need to explicitly set skipFirstTransactionsChange to false, as it will be not set in the useMemo above. | ||
| useEffect(() => { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import Onyx from 'react-native-onyx'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
|
|
||
| function addPendingNewTransactionIDs(reportID: string | undefined, transactionID: string | undefined) { | ||
| if (!reportID || !transactionID) { | ||
| return; | ||
| } | ||
|
|
||
| // We are saving in object form so that consecutive onyx merge will not reset previous value. | ||
| Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_METADATA}${reportID}`, {pendingNewTransactionIDs: {[transactionID]: true}}); | ||
| } | ||
|
|
||
| function deletePendingNewTransactionIDs(reportID: string | undefined, transactionIDs: string[]) { | ||
| if (!reportID) { | ||
| return; | ||
| } | ||
|
|
||
| const pendingNewTransactionIDs: Record<string, null> = {}; | ||
| for (const transactionID of transactionIDs) { | ||
| Object.assign(pendingNewTransactionIDs, {[transactionID]: null}); | ||
| } | ||
| Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_METADATA}${reportID}`, {pendingNewTransactionIDs}); | ||
| } | ||
|
|
||
| export {addPendingNewTransactionIDs, deletePendingNewTransactionIDs}; |
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.
From #98308: Don't pass the transactions list to the hook until it's a complete snapshot. useNewTransactions already skips diffing when given undefined, so the key is to check isDeliveryComplete before passing transactions to it.