Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import {isUserValidatedSelector} from '@selectors/Account';
import {tierNameSelector} from '@selectors/UserWallet';
import isEmpty from 'lodash/isEmpty';
import React, {useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState} from 'react';
import React, {useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState} from 'react';
import type {NativeScrollEvent, NativeSyntheticEvent} from 'react-native';
import {DeviceEventEmitter, InteractionManager, View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
Expand Down Expand Up @@ -60,6 +60,7 @@
import {isTransactionPendingDelete} from '@libs/TransactionUtils';
import Visibility from '@libs/Visibility';
import isSearchTopmostFullScreenRoute from '@navigation/helpers/isSearchTopmostFullScreenRoute';
import {ActionListContext} from '@pages/inbox/ReportScreenContext';
import FloatingMessageCounter from '@pages/inbox/report/FloatingMessageCounter';
import getInitialNumToRender from '@pages/inbox/report/getInitialNumReportActionsToRender';
import ReportActionsListItemRenderer from '@pages/inbox/report/ReportActionsListItemRenderer';
Expand Down Expand Up @@ -293,6 +294,7 @@
const lastActionIndex = lastAction?.reportActionID;
const previousLastIndex = useRef(lastActionIndex);

const {scrollOffsetRef} = useContext(ActionListContext);
const scrollingVerticalBottomOffset = useRef(0);
const scrollingVerticalTopOffset = useRef(0);
const wrapperViewRef = useRef<View>(null);
Expand Down Expand Up @@ -481,6 +483,7 @@
* Diff == (height of all items in the list) - (height of the layout with the list) - (how far user scrolled)
*/
scrollingVerticalBottomOffset.current = fullContentHeight - layoutMeasurement.height - contentOffset.y;
scrollOffsetRef.current = scrollingVerticalBottomOffset.current;

// We additionally track the top offset to be able to scroll to the new transaction when it's added
scrollingVerticalTopOffset.current = contentOffset.y;
Expand Down Expand Up @@ -671,7 +674,7 @@
reportScrollManager.scrollToEnd();
readActionSkipped.current = false;
readNewestAction(report.reportID);
}, [setIsFloatingMessageCounterVisible, hasNewestReportAction, reportScrollManager, report.reportID]);

Check warning on line 677 in src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

React Hook useCallback has a missing dependency: 'introSelected'. Either include it or remove the dependency array

Check warning on line 677 in src/components/MoneyRequestReportView/MoneyRequestReportActionsList.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

React Hook useCallback has a missing dependency: 'introSelected'. Either include it or remove the dependency array

const scrollToNewTransaction = useCallback(
(pageY: number) => {
Expand Down
13 changes: 13 additions & 0 deletions src/hooks/useActionListContextValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {useRef} from 'react';
import type {FlatList} from 'react-native';
import type {ActionListContextType, ScrollPosition} from '@pages/inbox/ReportScreenContext';

function useActionListContextValue(): ActionListContextType {
const flatListRef = useRef<FlatList>(null);
const scrollPositionRef = useRef<ScrollPosition>({});
const scrollOffsetRef = useRef(0);

return {flatListRef, scrollPositionRef, scrollOffsetRef};
}

export default useActionListContextValue;
47 changes: 19 additions & 28 deletions src/hooks/useReportScrollManager/index.native.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
import {useCallback, useContext} from 'react';
import {useContext} from 'react';
// eslint-disable-next-line no-restricted-imports
import type {ScrollView} from 'react-native';
import {ActionListContext} from '@pages/inbox/ReportScreenContext';
import type ReportScrollManagerData from './types';

function useReportScrollManager(): ReportScrollManagerData {
const {flatListRef, setScrollPosition} = useContext(ActionListContext);
const {flatListRef, scrollPositionRef} = useContext(ActionListContext);

/**
* Scroll to the provided index.
*/
const scrollToIndex = useCallback(
(index: number) => {
if (!flatListRef?.current) {
return;
}

flatListRef.current.scrollToIndex({index});
},
[flatListRef],
);
const scrollToIndex = (index: number) => {
if (!flatListRef?.current) {
return;
}
flatListRef.current.scrollToIndex({index});
};

/**
* Scroll to the bottom of the inverted FlatList.
* When FlatList is inverted it's "bottom" is really it's top
*/
const scrollToBottom = useCallback(() => {
const scrollToBottom = () => {
if (!flatListRef?.current) {
return;
}

setScrollPosition({offset: 0});

scrollPositionRef.current = {offset: 0};
flatListRef.current?.scrollToOffset({animated: false, offset: 0});
}, [flatListRef, setScrollPosition]);
};

/**
* Scroll to the end of the FlatList.
*/
const scrollToEnd = useCallback(() => {
const scrollToEnd = () => {
if (!flatListRef?.current) {
return;
}
Expand All @@ -51,18 +46,14 @@ function useReportScrollManager(): ReportScrollManagerData {
}

flatListRef.current.scrollToEnd({animated: false});
}, [flatListRef]);
};

const scrollToOffset = useCallback(
(offset: number) => {
if (!flatListRef?.current) {
return;
}

flatListRef.current.scrollToOffset({offset, animated: false});
},
[flatListRef],
);
const scrollToOffset = (offset: number) => {
if (!flatListRef?.current) {
return;
}
flatListRef.current.scrollToOffset({offset, animated: false});
};

return {ref: flatListRef, scrollToIndex, scrollToBottom, scrollToEnd, scrollToOffset};
}
Expand Down
44 changes: 19 additions & 25 deletions src/hooks/useReportScrollManager/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {useCallback, useContext} from 'react';
import {useContext} from 'react';
import {ActionListContext} from '@pages/inbox/ReportScreenContext';
import type ReportScrollManagerData from './types';

Expand All @@ -8,50 +8,44 @@ function useReportScrollManager(): ReportScrollManagerData {
/**
* Scroll to the provided index. On non-native implementations we do not want to scroll when we are scrolling because
*/
const scrollToIndex = useCallback(
(index: number, isEditing?: boolean) => {
if (!flatListRef?.current || isEditing) {
return;
}
const scrollToIndex = (index: number, isEditing?: boolean) => {
if (!flatListRef?.current || isEditing) {
return;
}

flatListRef.current.scrollToIndex({index, animated: true});
},
[flatListRef],
);
flatListRef.current.scrollToIndex({index, animated: true});
};

/**
* Scroll to the bottom of the inverted FlatList.
* When FlatList is inverted it's "bottom" is really it's top
*/
const scrollToBottom = useCallback(() => {
const scrollToBottom = () => {
if (!flatListRef?.current) {
return;
}

flatListRef.current.scrollToOffset({animated: false, offset: 0});
}, [flatListRef]);
};

/**
* Scroll to the end of the FlatList.
*/
const scrollToEnd = useCallback(() => {
const scrollToEnd = () => {
if (!flatListRef?.current) {
return;
}

flatListRef.current.scrollToEnd({animated: false});
}, [flatListRef]);

const scrollToOffset = useCallback(
(offset: number) => {
if (!flatListRef?.current) {
return;
}

flatListRef.current.scrollToOffset({animated: true, offset});
},
[flatListRef],
);
};

const scrollToOffset = (offset: number) => {
if (!flatListRef?.current) {
return;
}

flatListRef.current.scrollToOffset({animated: true, offset});
};

return {ref: flatListRef, scrollToIndex, scrollToBottom, scrollToEnd, scrollToOffset};
}
Expand Down
32 changes: 28 additions & 4 deletions src/libs/actions/Report/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@
shouldPlaySound?: boolean;
isInSidePanel?: boolean;
pregeneratedResponseParams?: PregeneratedResponseParams;
reportActionID?: string;
};

type AddActionsParams = {
Expand All @@ -292,6 +293,7 @@
file?: FileObject;
isInSidePanel?: boolean;
pregeneratedResponseParams?: PregeneratedResponseParams;
reportActionID?: string;
};

type AddAttachmentWithCommentParams = {
Expand All @@ -312,7 +314,7 @@
/** @deprecated This value is deprecated and will be removed soon after migration. Use the email from useCurrentUserPersonalDetails hook instead. */
let deprecatedCurrentUserLogin: string | undefined;

Onyx.connect({

Check warning on line 317 in src/libs/actions/Report/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.SESSION,
callback: (value) => {
// When signed out, val is undefined
Expand All @@ -326,7 +328,7 @@
},
});

Onyx.connect({

Check warning on line 331 in src/libs/actions/Report/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.CONCIERGE_REPORT_ID,
callback: (value) => (conciergeReportIDOnyxConnect = value),
});
Expand All @@ -334,7 +336,7 @@
// map of reportID to all reportActions for that report
const allReportActions: OnyxCollection<ReportActions> = {};

Onyx.connect({

Check warning on line 339 in src/libs/actions/Report/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
callback: (actions, key) => {
if (!key || !actions) {
Expand All @@ -346,7 +348,7 @@
});

let allReports: OnyxCollection<Report>;
Onyx.connect({

Check warning on line 351 in src/libs/actions/Report/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT,
waitForCollectionCallback: true,
callback: (value) => {
Expand All @@ -355,7 +357,7 @@
});

let allPersonalDetails: OnyxEntry<PersonalDetailsList> = {};
Onyx.connect({

Check warning on line 360 in src/libs/actions/Report/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (value) => {
allPersonalDetails = value ?? {};
Expand All @@ -370,7 +372,7 @@
});

let onboarding: OnyxEntry<Onboarding>;
Onyx.connect({

Check warning on line 375 in src/libs/actions/Report/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.NVP_ONBOARDING,
callback: (val) => {
if (Array.isArray(val)) {
Expand All @@ -381,7 +383,7 @@
});

let deprecatedIntroSelected: OnyxEntry<IntroSelected> = {};
Onyx.connect({

Check warning on line 386 in src/libs/actions/Report/index.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.NVP_INTRO_SELECTED,
callback: (val) => (deprecatedIntroSelected = val),
});
Expand Down Expand Up @@ -609,7 +611,18 @@
* @param isInSidePanel - Whether the comment is being added from the side panel
* @param pregeneratedResponseParams - Optional params for pre-generated response (API only, no optimistic action - used when response display is delayed)
*/
function addActions({report, notifyReportID, ancestors, timezoneParam, currentUserAccountID, text = '', file, isInSidePanel = false, pregeneratedResponseParams}: AddActionsParams) {
function addActions({
report,
notifyReportID,
ancestors,
timezoneParam,
currentUserAccountID,
text = '',
file,
isInSidePanel = false,
pregeneratedResponseParams,
reportActionID,
Copy link
Contributor

Choose a reason for hiding this comment

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

question: Would an alternative here be to return the reportActionID for the newly created actions? that way it doesn't have to be passed in. NAB

}: AddActionsParams) {
if (!report?.reportID) {
return;
}
Expand All @@ -620,7 +633,7 @@
let commandName: typeof WRITE_COMMANDS.ADD_COMMENT | typeof WRITE_COMMANDS.ADD_ATTACHMENT | typeof WRITE_COMMANDS.ADD_TEXT_AND_ATTACHMENT = WRITE_COMMANDS.ADD_COMMENT;

if (text && !file) {
const reportComment = buildOptimisticAddCommentReportAction(text, undefined, undefined, undefined, reportID);
const reportComment = buildOptimisticAddCommentReportAction(text, undefined, undefined, undefined, reportID, reportActionID);
reportCommentAction = reportComment.reportAction;
reportCommentText = reportComment.commentText;
}
Expand Down Expand Up @@ -848,11 +861,22 @@
}

/** Add a single comment to a report */
function addComment({report, notifyReportID, ancestors, text, timezoneParam, currentUserAccountID, shouldPlaySound, isInSidePanel, pregeneratedResponseParams}: AddCommentParams) {
function addComment({
report,
notifyReportID,
ancestors,
text,
timezoneParam,
currentUserAccountID,
shouldPlaySound,
isInSidePanel,
pregeneratedResponseParams,
reportActionID,
}: AddCommentParams) {
if (shouldPlaySound) {
playSound(SOUNDS.DONE);
}
addActions({report, notifyReportID, ancestors, timezoneParam, currentUserAccountID, text, isInSidePanel, pregeneratedResponseParams});
addActions({report, notifyReportID, ancestors, timezoneParam, currentUserAccountID, text, isInSidePanel, pregeneratedResponseParams, reportActionID});
}

function reportActionsExist(reportID: string): boolean {
Expand Down
10 changes: 9 additions & 1 deletion src/libs/telemetry/activeSpans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,16 @@ function cancelAllSpans() {
}
}

function cancelSpansByPrefix(prefix: string) {
for (const [spanID] of activeSpans.entries()) {
if (spanID.startsWith(prefix)) {
cancelSpan(spanID);
}
}
}

function getSpan(spanId: string) {
return activeSpans.get(spanId);
}

export {startSpan, endSpan, getSpan, cancelSpan, cancelAllSpans};
export {startSpan, endSpan, getSpan, cancelSpan, cancelAllSpans, cancelSpansByPrefix};
15 changes: 9 additions & 6 deletions src/pages/Search/SearchMoneyRequestReportPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {PortalHost} from '@gorhom/portal';
import {useIsFocused} from '@react-navigation/native';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import type {FlatList} from 'react-native';
import React, {useCallback, useEffect, useMemo, useRef} from 'react';
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import DragAndDropProvider from '@components/DragAndDrop/Provider';
Expand All @@ -10,6 +9,7 @@
import {useSearchContext} from '@components/Search/SearchContext';
import useShowSuperWideRHPVersion from '@components/WideRHPContextProvider/useShowSuperWideRHPVersion';
import WideRHPOverlayWrapper from '@components/WideRHPOverlayWrapper';
import useActionListContextValue from '@hooks/useActionListContextValue';
import useIsReportReadyToDisplay from '@hooks/useIsReportReadyToDisplay';
import useNetwork from '@hooks/useNetwork';
import useOnyx from '@hooks/useOnyx';
Expand All @@ -33,10 +33,10 @@
isMoneyRequestAction,
} from '@libs/ReportActionsUtils';
import {isMoneyRequestReport, isValidReportIDFromPath} from '@libs/ReportUtils';
import {cancelSpansByPrefix} from '@libs/telemetry/activeSpans';
import {isDefaultAvatar, isLetterAvatar, isPresetAvatar} from '@libs/UserAvatarUtils';
import Navigation from '@navigation/Navigation';
import ReactionListWrapper from '@pages/inbox/ReactionListWrapper';
import type {ActionListContextType, ScrollPosition} from '@pages/inbox/ReportScreenContext';
import {ActionListContext} from '@pages/inbox/ReportScreenContext';
import {createTransactionThreadReport, openReport, updateLastVisitTime} from '@userActions/Report';
import CONST from '@src/CONST';
Expand Down Expand Up @@ -89,7 +89,7 @@
}
Navigation.dismissModal();
}
}, [report]);

Check warning on line 92 in src/pages/Search/SearchMoneyRequestReportPage.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

React Hook useEffect has missing dependencies: 'isFocused' and 'prevReport'. Either include them or remove the dependency array

useEffect(() => {
// Update last visit time when the expense super wide RHP report is focused
Expand Down Expand Up @@ -121,9 +121,7 @@

const {isEditingDisabled, isCurrentReportLoadedFromOnyx} = useIsReportReadyToDisplay(report, reportIDFromRoute, isReportArchived);

const [scrollPosition, setScrollPosition] = useState<ScrollPosition>({});
const flatListRef = useRef<FlatList>(null);
const actionListValue = useMemo((): ActionListContextType => ({flatListRef, scrollPosition, setScrollPosition}), [flatListRef, scrollPosition, setScrollPosition]);
const actionListValue = useActionListContextValue();

const [chatReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${report?.chatReportID}`, {canBeMissing: true});
const [introSelected] = useOnyx(ONYXKEYS.NVP_INTRO_SELECTED, {canBeMissing: true});
Expand Down Expand Up @@ -219,6 +217,11 @@

useEffect(() => {
hasCreatedLegacyThreadRef.current = false;

return () => {
// Cancel any pending send-message spans to prevent orphaned spans when navigating away
cancelSpansByPrefix(CONST.TELEMETRY.SPAN_SEND_MESSAGE);
};
}, [reportIDFromRoute]);

// Create transaction thread for legacy transactions that don't have one yet.
Expand Down
Loading
Loading