Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 3 additions & 1 deletion src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ function getContinuousReportActionChain(sortedReportActions: ReportAction[], id?
}

if (index === -1) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@situchan In your case, it will return an empty array since we don't find the report actions that is created action or pending action is not add.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It's bug. We should create report optimistically without skeleton but grayed out.

To cover all case above I think we can do my alternative solution which will return the current sortedReportActions if the id is empty and index is -1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@roryabraham what do you think?

And this case should also be added in unit test

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@situchan Updated the solution and unit test.

return [];
// if no non-pending action is found, that means all actions on the report are optimistic
// in this case, we'll assume the whole chain of reportActions is continuous and return it in its entirety
return id ? [] : sortedReportActions;
Comment thread
roryabraham marked this conversation as resolved.
}

let startIndex = index;
Expand Down
32 changes: 18 additions & 14 deletions src/pages/home/ReportScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ function ReportScreen({

const prevReport = usePrevious(report);
const prevUserLeavingStatus = usePrevious(userLeavingStatus);
const [isLinkingToMessage, setLinkingToMessage] = useState(!!reportActionIDFromRoute);
const [isLinkingToMessage, setIsLinkingToMessage] = useState(!!reportActionIDFromRoute);
const reportActions = useMemo(() => {
if (!sortedAllReportActions.length) {
return [];
Expand All @@ -243,9 +243,11 @@ function ReportScreen({
return currentRangeOfReportActions;
}, [reportActionIDFromRoute, sortedAllReportActions]);

// Define here because reportActions are recalculated before mount, allowing data to display faster than useEffect can trigger. If we have cached reportActions, they will be shown immediately. We aim to display a loader first, then fetch relevant reportActions, and finally show them.
// Define here because reportActions are recalculated before mount, allowing data to display faster than useEffect can trigger.
// If we have cached reportActions, they will be shown immediately.
// We aim to display a loader first, then fetch relevant reportActions, and finally show them.
useLayoutEffect(() => {
setLinkingToMessage(!!reportActionIDFromRoute);
setIsLinkingToMessage(!!reportActionIDFromRoute);
}, [route, reportActionIDFromRoute]);

const [isBannerVisible, setIsBannerVisible] = useState(true);
Expand All @@ -261,8 +263,6 @@ function ReportScreen({
const {reportPendingAction, reportErrors} = ReportUtils.getReportOfflinePendingActionAndErrors(report);
const screenWrapperStyle: ViewStyle[] = [styles.appContent, styles.flex1, {marginTop: viewportOffsetTop}];
const isEmptyChat = useMemo((): boolean => reportActions.length === 0, [reportActions]);
// There are no reportActions at all to display and we are still in the process of loading the next set of actions.
const isLoadingInitialReportActions = reportActions.length === 0 && !!reportMetadata?.isLoadingInitialReportActions;
const isOptimisticDelete = report.statusNum === CONST.REPORT.STATUS_NUM.CLOSED;

// If there's a non-404 error for the report we should show it instead of blocking the screen
Expand Down Expand Up @@ -325,16 +325,20 @@ function ReportScreen({
/**
* When false the ReportActionsView will completely unmount and we will show a loader until it returns true.
*/
const isReportReadyForDisplay = useMemo((): boolean => {
const isCurrentReportLoadedFromOnyx = useMemo((): boolean => {
// This is necessary so that when we are retrieving the next report data from Onyx the ReportActionsView will remount completely
const isTransitioning = report && report.reportID !== reportIDFromRoute;
return reportIDFromRoute !== '' && !!report.reportID && !isTransitioning;
}, [report, reportIDFromRoute]);

const shouldShowSkeleton =
isLinkingToMessage || !isReportReadyForDisplay || isLoadingInitialReportActions || isLoading || (!!reportActionIDFromRoute && reportMetadata?.isLoadingInitialReportActions);
isLinkingToMessage ||
!isCurrentReportLoadedFromOnyx ||
(reportActions.length === 0 && !!reportMetadata?.isLoadingInitialReportActions) ||
isLoading ||
(!!reportActionIDFromRoute && reportMetadata?.isLoadingInitialReportActions);

const shouldShowReportActionList = isReportReadyForDisplay && !isLoading;
const shouldShowReportActionList = isCurrentReportLoadedFromOnyx && !isLoading;

const fetchReport = useCallback(() => {
Report.openReport(reportIDFromRoute, reportActionIDFromRoute);
Expand Down Expand Up @@ -544,14 +548,14 @@ function ReportScreen({
// This helps in tracking from the moment 'route' triggers useMemo until isLoadingInitialReportActions becomes true. It prevents blinking when loading reportActions from cache.
useEffect(() => {
InteractionManager.runAfterInteractions(() => {
setLinkingToMessage(false);
setIsLinkingToMessage(false);
});
}, [reportMetadata?.isLoadingInitialReportActions]);

const onLinkPress = () => {
const navigateToEndOfReport = useCallback(() => {
Navigation.setParams({reportActionID: ''});
fetchReport();
};
}, [fetchReport]);

const isLinkedReportActionDeleted = useMemo(() => {
if (!reportActionIDFromRoute || !sortedAllReportActions) {
Expand All @@ -570,7 +574,7 @@ function ReportScreen({
title={translate('notFound.notHere')}
shouldShowLink
linkKey="notFound.noAccess"
onLinkPress={onLinkPress}
onLinkPress={navigateToEndOfReport}
/>
);
}
Expand Down Expand Up @@ -618,7 +622,7 @@ function ReportScreen({
shouldShowCloseButton
/>
)}
<DragAndDropProvider isDisabled={!isReportReadyForDisplay || !ReportUtils.canUserPerformWriteAction(report)}>
<DragAndDropProvider isDisabled={!isCurrentReportLoadedFromOnyx || !ReportUtils.canUserPerformWriteAction(report)}>
<View
style={[styles.flex1, styles.justifyContentEnd, styles.overflowHidden]}
onLayout={onListLayout}
Expand All @@ -640,7 +644,7 @@ function ReportScreen({
we'll unnecessarily unmount the ReportActionsView which will clear the new marker lines initial state. */}
{shouldShowSkeleton && <ReportActionsSkeletonView />}

{isReportReadyForDisplay ? (
{isCurrentReportLoadedFromOnyx ? (
<ReportFooter
report={report}
pendingAction={reportPendingAction}
Expand Down
137 changes: 137 additions & 0 deletions tests/unit/ReportActionsUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,143 @@ describe('ReportActionsUtils', () => {
input.pop();
expect(result).toStrictEqual(expectedResult.reverse());
});

it('given an empty input ID and the report only contains pending actions, it will return all actions', () => {
const input: ReportAction[] = [
// Given these sortedReportActions
{
reportActionID: '1',
previousReportActionID: undefined,
created: '2022-11-13 22:27:01.825',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
originalMessage: {
html: 'Hello world',
whisperedTo: [],
},
message: [
{
html: 'Hello world',
type: 'Action type',
text: 'Action text',
},
],
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
{
reportActionID: '2',
previousReportActionID: '1',
created: '2022-11-13 22:27:01.825',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
originalMessage: {
html: 'Hello world',
whisperedTo: [],
},
message: [
{
html: 'Hello world',
type: 'Action type',
text: 'Action text',
},
],
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
{
reportActionID: '3',
previousReportActionID: '2',
created: '2022-11-13 22:27:01.825',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
originalMessage: {
html: 'Hello world',
whisperedTo: [],
},
message: [
{
html: 'Hello world',
type: 'Action type',
text: 'Action text',
},
],
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
{
reportActionID: '4',
previousReportActionID: '3',
created: '2022-11-13 22:27:01.825',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
originalMessage: {
html: 'Hello world',
whisperedTo: [],
},
message: [
{
html: 'Hello world',
type: 'Action type',
text: 'Action text',
},
],
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
{
reportActionID: '5',
previousReportActionID: '4',
created: '2022-11-13 22:27:01.825',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
originalMessage: {
html: 'Hello world',
whisperedTo: [],
},
message: [
{
html: 'Hello world',
type: 'Action type',
text: 'Action text',
},
],
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
{
reportActionID: '6',
previousReportActionID: '5',
created: '2022-11-13 22:27:01.825',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
originalMessage: {
html: 'Hello world',
whisperedTo: [],
},
message: [
{
html: 'Hello world',
type: 'Action type',
text: 'Action text',
},
],
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
{
reportActionID: '7',
previousReportActionID: '6',
created: '2022-11-13 22:27:01.825',
actionName: CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT,
originalMessage: {
html: 'Hello world',
whisperedTo: [],
},
message: [
{
html: 'Hello world',
type: 'Action type',
text: 'Action text',
},
],
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
];

const expectedResult = input;
// Reversing the input array to simulate descending order sorting as per our data structure
const result = ReportActionsUtils.getContinuousReportActionChain(input.reverse(), '');
expect(result).toStrictEqual(expectedResult.reverse());
});
});

describe('getLastVisibleAction', () => {
Expand Down