Skip to content
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

fix unsubscribe is typing event if not focus on screen #39347

Merged
merged 16 commits into from
Apr 15, 2024
8 changes: 3 additions & 5 deletions src/pages/home/report/ReportActionsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -348,12 +348,10 @@ function ReportActionsList({
const unsubscribe = Report.subscribeToNewActionEvent(report.reportID, scrollToBottomForCurrentUserAction);

const cleanup = () => {
if (unsubscribe) {
unsubscribe();
if (!unsubscribe) {
return;
}
InteractionManager.runAfterInteractions(() => {
Report.unsubscribeFromReportChannel(report.reportID);
});
unsubscribe();
};

newActionUnsubscribeMap[report.reportID] = cleanup;
Expand Down
42 changes: 28 additions & 14 deletions src/pages/home/report/ReportActionsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import usePrevious from '@hooks/usePrevious';
import useWindowDimensions from '@hooks/useWindowDimensions';
import DateUtils from '@libs/DateUtils';
import getIsReportFullyVisible from '@libs/getIsReportFullyVisible';
import Navigation from '@libs/Navigation/Navigation';
import type {CentralPaneNavigatorParamList} from '@libs/Navigation/types';
import * as NumberUtils from '@libs/NumberUtils';
import {generateNewRandomInt} from '@libs/NumberUtils';
Expand Down Expand Up @@ -324,21 +325,34 @@ function ReportActionsView({
if (route?.params?.reportID !== reportID) {
return;
}
// Ensures subscription event succeeds when the report/workspace room is created optimistically.
// Check if the optimistic `OpenReport` or `AddWorkspaceRoom` has succeeded by confirming
// any `pendingFields.createChat` or `pendingFields.addWorkspaceRoom` fields are set to null.
// Existing reports created will have empty fields for `pendingFields`.
const didCreateReportSuccessfully = !report.pendingFields || (!report.pendingFields.addWorkspaceRoom && !report.pendingFields.createChat);
if (!didSubscribeToReportTypingEvents.current && didCreateReportSuccessfully) {
const interactionTask = InteractionManager.runAfterInteractions(() => {
Report.subscribeToReportTypingEvents(reportID);
didSubscribeToReportTypingEvents.current = true;
});
return () => {
interactionTask.cancel();
};

if (isFocused) {
// Ensures subscription event succeeds when the report/workspace room is created optimistically.
// Check if the optimistic `OpenReport` or `AddWorkspaceRoom` has succeeded by confirming
// any `pendingFields.createChat` or `pendingFields.addWorkspaceRoom` fields are set to null.
// Existing reports created will have empty fields for `pendingFields`.
const didCreateReportSuccessfully = !report.pendingFields || (!report.pendingFields.addWorkspaceRoom && !report.pendingFields.createChat);

if (!didSubscribeToReportTypingEvents.current && didCreateReportSuccessfully) {
const interactionTask = InteractionManager.runAfterInteractions(() => {
Report.subscribeToReportTypingEvents(reportID);
didSubscribeToReportTypingEvents.current = true;
});
return () => {
interactionTask.cancel();
};
}
} else {
const topmostReportId = Navigation.getTopmostReportId();

if (topmostReportId !== report.reportID && didSubscribeToReportTypingEvents.current) {
Beamanator marked this conversation as resolved.
Show resolved Hide resolved
didSubscribeToReportTypingEvents.current = false;
InteractionManager.runAfterInteractions(() => {
Report.unsubscribeFromReportChannel(report.reportID);
});
}
}
}, [report.pendingFields, didSubscribeToReportTypingEvents, route, reportID]);
}, [isFocused, report.reportID, report.pendingFields, didSubscribeToReportTypingEvents, route, reportID]);

const onContentSizeChange = useCallback((w: number, h: number) => {
contentListHeight.current = h;
Expand Down
Loading