Skip to content

Commit

Permalink
Merge pull request #53847 from callstack-internal/fix/header-when-cre…
Browse files Browse the repository at this point in the history
…ating-group-chat

fix: displaying header for group chat
  • Loading branch information
cristipaval authored Dec 20, 2024
2 parents 78337bd + 05e49db commit 2d7d05d
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 19 deletions.
6 changes: 5 additions & 1 deletion src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,11 @@ function getMostRecentReportActionLastModified(): string {
/**
* @returns The report preview action or `null` if one couldn't be found
*/
function getReportPreviewAction(chatReportID: string, iouReportID: string): OnyxEntry<ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW>> {
function getReportPreviewAction(chatReportID: string | undefined, iouReportID: string | undefined): OnyxEntry<ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW>> {
if (!chatReportID || !iouReportID) {
return;
}

return Object.values(allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReportID}`] ?? {}).find(
(reportAction): reportAction is ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW> =>
reportAction && isActionOfType(reportAction, CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW) && getOriginalMessage(reportAction)?.linkedReportID === iouReportID,
Expand Down
14 changes: 11 additions & 3 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ function openReport(
value: {[optimisticCreatedAction.reportActionID]: optimisticCreatedAction},
},
{
onyxMethod: Onyx.METHOD.MERGE,
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.REPORT_METADATA}${reportID}`,
value: {
isOptimisticReport: true,
Expand Down Expand Up @@ -1208,7 +1208,11 @@ function navigateToAndOpenChildReport(childReportID = '-1', parentReportAction:
* Gets the older actions that have not been read yet.
* Normally happens when you scroll up on a chat, and the actions have not been read yet.
*/
function getOlderActions(reportID: string, reportActionID: string) {
function getOlderActions(reportID: string | undefined, reportActionID: string | undefined) {
if (!reportID || !reportActionID) {
return;
}

const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down Expand Up @@ -1262,7 +1266,11 @@ function getOlderActions(reportID: string, reportActionID: string) {
* Gets the newer actions that have not been read yet.
* Normally happens when you are not located at the bottom of the list and scroll down on a chat.
*/
function getNewerActions(reportID: string, reportActionID: string) {
function getNewerActions(reportID: string | undefined, reportActionID: string | undefined) {
if (!reportID || !reportActionID) {
return;
}

const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
Expand Down
5 changes: 2 additions & 3 deletions src/libs/shouldFetchReport.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type {OnyxEntry} from 'react-native-onyx';
import type Report from '@src/types/onyx/Report';
import * as ReportUtils from './ReportUtils';
import type ReportMetadata from '@src/types/onyx/ReportMetadata';

export default function shouldFetchReport(report: OnyxEntry<Report>) {
const reportMetadata = ReportUtils.getReportMetadata(report?.reportID);
export default function shouldFetchReport(report: OnyxEntry<Report>, reportMetadata: OnyxEntry<ReportMetadata>) {
// If the report is optimistic, there's no need to fetch it. The original action should create it.
// If there is an error for creating the chat, there's no need to fetch it since it doesn't exist
return !reportMetadata?.isOptimisticReport && !report?.errorFields?.createChat;
Expand Down
4 changes: 2 additions & 2 deletions src/pages/home/ReportScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro
return;
}

if (!shouldFetchReport(report)) {
if (!shouldFetchReport(report, reportMetadata)) {
return;
}
// When creating an optimistic report that already exists, we need to skip openReport
Expand All @@ -501,7 +501,7 @@ function ReportScreen({route, currentReportID = '', navigation}: ReportScreenPro
}

fetchReport();
}, [report, fetchReport, reportIDFromRoute, isLoadingApp]);
}, [reportIDFromRoute, isLoadingApp, report, reportMetadata, fetchReport]);

const dismissBanner = useCallback(() => {
setIsBannerVisible(false);
Expand Down
25 changes: 15 additions & 10 deletions src/pages/home/report/ReportActionsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,12 @@ function ReportActionsView({
const reactionListRef = useContext(ReactionListContext);
const route = useRoute<PlatformStackRouteProp<AuthScreensParamList, typeof SCREENS.REPORT>>();
const [session] = useOnyx(ONYXKEYS.SESSION);
const [transactionThreadReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID ?? -1}`, {
const [transactionThreadReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID ?? CONST.DEFAULT_NUMBER_ID}`, {
selector: (reportActions: OnyxEntry<OnyxTypes.ReportActions>) =>
ReportActionsUtils.getSortedReportActionsForDisplay(reportActions, ReportUtils.canUserPerformWriteAction(report), true),
});
const [transactionThreadReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID ?? -1}`);
const [transactionThreadReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID ?? CONST.DEFAULT_NUMBER_ID}`);
const [reportMetadata] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_METADATA}${report.reportID}`);
const prevTransactionThreadReport = usePrevious(transactionThreadReport);
const reportActionID = route?.params?.reportActionID;
const prevReportActionID = usePrevious(reportActionID);
Expand All @@ -116,7 +117,7 @@ function ReportActionsView({
const reportID = report.reportID;
const isReportFullyVisible = useMemo((): boolean => getIsReportFullyVisible(isFocused), [isFocused]);
const openReportIfNecessary = () => {
if (!shouldFetchReport(report)) {
if (!shouldFetchReport(report, reportMetadata)) {
return;
}

Expand Down Expand Up @@ -167,7 +168,7 @@ function ReportActionsView({
actions.push(optimisticCreatedAction);
}

const reportPreviewAction = ReportActionsUtils.getReportPreviewAction(report.chatReportID ?? '', report.reportID);
const reportPreviewAction = ReportActionsUtils.getReportPreviewAction(report.chatReportID, report.reportID);
const moneyRequestActions = allReportActions.filter((action) => {
const originalMessage = ReportActionsUtils.isMoneyRequestAction(action) ? ReportActionsUtils.getOriginalMessage(action) : undefined;
return (
Expand Down Expand Up @@ -283,11 +284,11 @@ function ReportActionsView({
if (!isEmptyObject(transactionThreadReport)) {
// Get newer actions based on the newest reportAction for the current report
const newestActionCurrentReport = reportActionIDMap.find((item) => item.reportID === reportID);
Report.getNewerActions(newestActionCurrentReport?.reportID ?? '-1', newestActionCurrentReport?.reportActionID ?? '-1');
Report.getNewerActions(newestActionCurrentReport?.reportID, newestActionCurrentReport?.reportActionID);

// Get newer actions based on the newest reportAction for the transaction thread report
const newestActionTransactionThreadReport = reportActionIDMap.find((item) => item.reportID === transactionThreadReport.reportID);
Report.getNewerActions(newestActionTransactionThreadReport?.reportID ?? '-1', newestActionTransactionThreadReport?.reportActionID ?? '-1');
Report.getNewerActions(newestActionTransactionThreadReport?.reportID, newestActionTransactionThreadReport?.reportActionID);
} else {
Report.getNewerActions(reportID, newestReportAction.reportActionID);
}
Expand Down Expand Up @@ -329,7 +330,11 @@ function ReportActionsView({
}, []);

const handleReportActionPagination = useCallback(
({firstReportActionID}: {firstReportActionID: string}) => {
({firstReportActionID}: {firstReportActionID: string | undefined}) => {
if (!firstReportActionID) {
return;
}

// This function is a placeholder as the actual pagination is handled by visibleReportActions
if (!hasMoreCached && !hasNewestReportAction) {
isFirstLinkedActionRender.current = false;
Expand Down Expand Up @@ -373,11 +378,11 @@ function ReportActionsView({
if (!isEmptyObject(transactionThreadReport)) {
// Get older actions based on the oldest reportAction for the current report
const oldestActionCurrentReport = reportActionIDMap.findLast((item) => item.reportID === reportID);
Report.getOlderActions(oldestActionCurrentReport?.reportID ?? '-1', oldestActionCurrentReport?.reportActionID ?? '-1');
Report.getOlderActions(oldestActionCurrentReport?.reportID, oldestActionCurrentReport?.reportActionID);

// Get older actions based on the oldest reportAction for the transaction thread report
const oldestActionTransactionThreadReport = reportActionIDMap.findLast((item) => item.reportID === transactionThreadReport.reportID);
Report.getOlderActions(oldestActionTransactionThreadReport?.reportID ?? '-1', oldestActionTransactionThreadReport?.reportActionID ?? '-1');
Report.getOlderActions(oldestActionTransactionThreadReport?.reportID, oldestActionTransactionThreadReport?.reportActionID);
} else {
// Retrieve the next REPORT.ACTIONS.LIMIT sized page of comments
Report.getOlderActions(reportID, oldestReportAction.reportActionID);
Expand Down Expand Up @@ -415,7 +420,7 @@ function ReportActionsView({
didLoadNewerChats.current = true;

if ((reportActionID && indexOfLinkedAction > -1) || !reportActionID) {
handleReportActionPagination({firstReportActionID: newestReportAction?.reportActionID ?? '-1'});
handleReportActionPagination({firstReportActionID: newestReportAction?.reportActionID});
}
},
[
Expand Down

0 comments on commit 2d7d05d

Please sign in to comment.