Skip to content

Commit

Permalink
Merge pull request #31097 from eh2077/feat-30778-drop-user-to-last-ch…
Browse files Browse the repository at this point in the history
…at-after-leaving-room

Feat: drop user to last chat after leaving a room
  • Loading branch information
luacmartins authored Jan 26, 2024
2 parents 3f702b7 + 41aac82 commit e485d9c
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Route} from '@src/ROUTES';
import ROUTES from '@src/ROUTES';
import type {PersonalDetails, PersonalDetailsList, ReportActionReactions, ReportUserIsTyping} from '@src/types/onyx';
import type {PersonalDetails, PersonalDetailsList, ReportActionReactions, ReportMetadata, ReportUserIsTyping} from '@src/types/onyx';
import type {Decision, OriginalMessageIOU} from '@src/types/onyx/OriginalMessage';
import type {NotificationPreference, WriteCapability} from '@src/types/onyx/Report';
import type Report from '@src/types/onyx/Report';
Expand Down Expand Up @@ -125,6 +125,13 @@ Onyx.connect({
},
});

let reportMetadata: OnyxCollection<ReportMetadata> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_METADATA,
waitForCollectionCallback: true,
callback: (value) => (reportMetadata = value),
});

const allReports: OnyxCollection<Report> = {};
let conciergeChatReportID: string | undefined;
const typingWatchTimers: Record<string, NodeJS.Timeout> = {};
Expand Down Expand Up @@ -2172,7 +2179,30 @@ function leaveRoom(reportID: string, isWorkspaceMemberLeavingWorkspaceRoom = fal

API.write('LeaveRoom', parameters, {optimisticData, successData, failureData});

if (isWorkspaceMemberLeavingWorkspaceRoom) {
const sortedReportsByLastRead = ReportUtils.sortReportsByLastRead(Object.values(allReports ?? {}) as Report[], reportMetadata);

// We want to filter out the current report, hidden reports and empty chats
const filteredReportsByLastRead = sortedReportsByLastRead.filter(
(sortedReport) =>
sortedReport?.reportID !== reportID &&
sortedReport?.notificationPreference !== CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN &&
ReportUtils.shouldReportBeInOptionList({
report: sortedReport,
currentReportId: '',
isInGSDMode: false,
betas: [],
policies: {},
excludeEmptyChats: true,
doesReportHaveViolations: false,
}),
);
const lastAccessedReportID = filteredReportsByLastRead.at(-1)?.reportID;

if (lastAccessedReportID) {
// We should call Navigation.goBack to pop the current route first before navigating to Concierge.
Navigation.goBack(ROUTES.HOME);
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(lastAccessedReportID));
} else {
const participantAccountIDs = PersonalDetailsUtils.getAccountIDsByLogins([CONST.EMAIL.CONCIERGE]);
const chat = ReportUtils.getChatByParticipants(participantAccountIDs);
if (chat?.reportID) {
Expand Down

0 comments on commit e485d9c

Please sign in to comment.