From 9b57d51a5779abab8b975bdee44245ef7b67ad62 Mon Sep 17 00:00:00 2001 From: tienifr Date: Thu, 14 Dec 2023 15:24:54 +0700 Subject: [PATCH 1/5] Green line appear and remains when mark chat as unread and than mark it as read --- src/libs/actions/Report.ts | 1 + .../DeviceEventListenerRef.ts | 30 +++++++++++++ .../index.js} | 42 ++++++++++++------- 3 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 src/pages/home/report/ReportActionsList/DeviceEventListenerRef.ts rename src/pages/home/report/{ReportActionsList.js => ReportActionsList/index.js} (93%) diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index bea4ab8aed77..cbe192309625 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -933,6 +933,7 @@ function readNewestAction(reportID: string) { }; API.write('ReadNewestAction', parameters, {optimisticData}); + DeviceEventEmitter.emit(`readNewestAction_${reportID}`, lastReadTime); } /** diff --git a/src/pages/home/report/ReportActionsList/DeviceEventListenerRef.ts b/src/pages/home/report/ReportActionsList/DeviceEventListenerRef.ts new file mode 100644 index 000000000000..13dbb17ed5ae --- /dev/null +++ b/src/pages/home/report/ReportActionsList/DeviceEventListenerRef.ts @@ -0,0 +1,30 @@ +import {EmitterSubscription,DeviceEventEmitter} from 'react-native'; + +export default class DeviceEventListenerRef { + private typePrefix: string; + + private subscription: EmitterSubscription | null = null; + + constructor(typePrefix: string) { + this.typePrefix = typePrefix; + } + + static use(typePrefix: string): DeviceEventListenerRef { + return new DeviceEventListenerRef(typePrefix); + } + + add( + typeSuffix: string, // report.reportID + listener: (data: unknown) => void, + ): void { + this.subscription = DeviceEventEmitter.addListener(`${this.typePrefix}_${typeSuffix}`, listener); + } + + remove(): void { + if (!this.subscription) { + return; + } + this.subscription.remove(); + this.subscription = null; + } +} diff --git a/src/pages/home/report/ReportActionsList.js b/src/pages/home/report/ReportActionsList/index.js similarity index 93% rename from src/pages/home/report/ReportActionsList.js rename to src/pages/home/report/ReportActionsList/index.js index 46abbfc71b84..f9a97185c76b 100644 --- a/src/pages/home/report/ReportActionsList.js +++ b/src/pages/home/report/ReportActionsList/index.js @@ -2,7 +2,6 @@ import {useRoute} from '@react-navigation/native'; import lodashGet from 'lodash/get'; import PropTypes from 'prop-types'; import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react'; -import {DeviceEventEmitter} from 'react-native'; import Animated, {useAnimatedStyle, useSharedValue, withTiming} from 'react-native-reanimated'; import _ from 'underscore'; import InvertedFlatList from '@components/InvertedFlatList'; @@ -21,10 +20,11 @@ import reportPropTypes from '@pages/reportPropTypes'; import variables from '@styles/variables'; import * as Report from '@userActions/Report'; import CONST from '@src/CONST'; -import FloatingMessageCounter from './FloatingMessageCounter'; -import ListBoundaryLoader from './ListBoundaryLoader/ListBoundaryLoader'; -import reportActionPropTypes from './reportActionPropTypes'; -import ReportActionsListItemRenderer from './ReportActionsListItemRenderer'; +import FloatingMessageCounter from '@pages/home/report/FloatingMessageCounter'; +import ListBoundaryLoader from '@pages/home/report/ListBoundaryLoader/ListBoundaryLoader'; +import reportActionPropTypes from '@pages/home/report/reportActionPropTypes'; +import ReportActionsListItemRenderer from '@pages/home/report/ReportActionsListItemRenderer'; +import DeviceEventListenerRef from './DeviceEventListenerRef'; const propTypes = { /** The report currently being looked at */ @@ -143,7 +143,7 @@ function ReportActionsList({ const route = useRoute(); const opacity = useSharedValue(0); const userActiveSince = useRef(null); - const unreadActionSubscription = useRef(null); + const markerInit = () => { if (!cacheUnreadMarkers.has(report.reportID)) { return null; @@ -221,20 +221,32 @@ function ReportActionsList({ }, [report.lastReadTime, report.reportID]); useEffect(() => { - // If the reportID changes, we reset the userActiveSince to null, we need to do it because - // this component doesn't unmount when the reportID changes - if (unreadActionSubscription.current) { - unreadActionSubscription.current.remove(); - unreadActionSubscription.current = null; - } - - // Listen to specific reportID for unread event and set the marker to new message - unreadActionSubscription.current = DeviceEventEmitter.addListener(`unreadAction_${report.reportID}`, (newLastReadTime) => { + const resetUnreadMarker = (newLastReadTime) => { cacheUnreadMarkers.delete(report.reportID); lastReadTimeRef.current = newLastReadTime; setCurrentUnreadMarker(null); + }; + + // Listen to specific reportID for unread event and set the marker to new message + const unreadActionSubscription = DeviceEventListenerRef.use('unreadAction'); + unreadActionSubscription.add(report.reportID, (newLastReadTime) => { + resetUnreadMarker(newLastReadTime); setMessageManuallyMarkedUnread(new Date().getTime()); }); + + const readNewestActionSubscription = DeviceEventListenerRef.use('readNewestAction'); + // Listen to specific reportID for read newest action event and reset the marker + readNewestActionSubscription.add(report.reportID, (newLastReadTime) => { + resetUnreadMarker(newLastReadTime); + setMessageManuallyMarkedUnread(0); + }); + + return () => { + // If the reportID changes, we reset the userActiveSince to null, we need to do it because + // this component doesn't unmount when the reportID changes + unreadActionSubscription.remove(); + readNewestActionSubscription.remove(); + }; }, [report.reportID]); useEffect(() => { From b847b62502572c20e56a584ac52d0ecab7f2ab38 Mon Sep 17 00:00:00 2001 From: tienifr Date: Thu, 14 Dec 2023 15:30:17 +0700 Subject: [PATCH 2/5] run prettier --- .../report/ReportActionsList/DeviceEventListenerRef.ts | 2 +- src/pages/home/report/ReportActionsList/index.js | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/pages/home/report/ReportActionsList/DeviceEventListenerRef.ts b/src/pages/home/report/ReportActionsList/DeviceEventListenerRef.ts index 13dbb17ed5ae..973087e07c4c 100644 --- a/src/pages/home/report/ReportActionsList/DeviceEventListenerRef.ts +++ b/src/pages/home/report/ReportActionsList/DeviceEventListenerRef.ts @@ -1,4 +1,4 @@ -import {EmitterSubscription,DeviceEventEmitter} from 'react-native'; +import {DeviceEventEmitter, EmitterSubscription} from 'react-native'; export default class DeviceEventListenerRef { private typePrefix: string; diff --git a/src/pages/home/report/ReportActionsList/index.js b/src/pages/home/report/ReportActionsList/index.js index f9a97185c76b..512f96b3f629 100644 --- a/src/pages/home/report/ReportActionsList/index.js +++ b/src/pages/home/report/ReportActionsList/index.js @@ -16,14 +16,14 @@ import compose from '@libs/compose'; import DateUtils from '@libs/DateUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import * as ReportUtils from '@libs/ReportUtils'; -import reportPropTypes from '@pages/reportPropTypes'; -import variables from '@styles/variables'; -import * as Report from '@userActions/Report'; -import CONST from '@src/CONST'; import FloatingMessageCounter from '@pages/home/report/FloatingMessageCounter'; import ListBoundaryLoader from '@pages/home/report/ListBoundaryLoader/ListBoundaryLoader'; import reportActionPropTypes from '@pages/home/report/reportActionPropTypes'; import ReportActionsListItemRenderer from '@pages/home/report/ReportActionsListItemRenderer'; +import reportPropTypes from '@pages/reportPropTypes'; +import variables from '@styles/variables'; +import * as Report from '@userActions/Report'; +import CONST from '@src/CONST'; import DeviceEventListenerRef from './DeviceEventListenerRef'; const propTypes = { From 8dd161a2f97f35b8a684aef34348923c86d3e694 Mon Sep 17 00:00:00 2001 From: tienifr Date: Thu, 21 Dec 2023 16:09:13 +0700 Subject: [PATCH 3/5] revert class file --- .../index.js => ReportActionsList.js} | 22 +++++++------- .../DeviceEventListenerRef.ts | 30 ------------------- 2 files changed, 11 insertions(+), 41 deletions(-) rename src/pages/home/report/{ReportActionsList/index.js => ReportActionsList.js} (96%) delete mode 100644 src/pages/home/report/ReportActionsList/DeviceEventListenerRef.ts diff --git a/src/pages/home/report/ReportActionsList/index.js b/src/pages/home/report/ReportActionsList.js similarity index 96% rename from src/pages/home/report/ReportActionsList/index.js rename to src/pages/home/report/ReportActionsList.js index ac3d4375bcfa..9e573723619d 100644 --- a/src/pages/home/report/ReportActionsList/index.js +++ b/src/pages/home/report/ReportActionsList.js @@ -2,6 +2,7 @@ import {useRoute} from '@react-navigation/native'; import lodashGet from 'lodash/get'; import PropTypes from 'prop-types'; import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react'; +import {DeviceEventEmitter} from 'react-native'; import Animated, {useAnimatedStyle, useSharedValue, withTiming} from 'react-native-reanimated'; import _ from 'underscore'; import InvertedFlatList from '@components/InvertedFlatList'; @@ -16,16 +17,15 @@ import compose from '@libs/compose'; import DateUtils from '@libs/DateUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import * as ReportUtils from '@libs/ReportUtils'; -import FloatingMessageCounter from '@pages/home/report/FloatingMessageCounter'; -import ListBoundaryLoader from '@pages/home/report/ListBoundaryLoader/ListBoundaryLoader'; -import reportActionPropTypes from '@pages/home/report/reportActionPropTypes'; -import ReportActionsListItemRenderer from '@pages/home/report/ReportActionsListItemRenderer'; import Visibility from '@libs/Visibility'; import reportPropTypes from '@pages/reportPropTypes'; import variables from '@styles/variables'; import * as Report from '@userActions/Report'; import CONST from '@src/CONST'; -import DeviceEventListenerRef from './DeviceEventListenerRef'; +import FloatingMessageCounter from './FloatingMessageCounter'; +import ListBoundaryLoader from './ListBoundaryLoader/ListBoundaryLoader'; +import reportActionPropTypes from './reportActionPropTypes'; +import ReportActionsListItemRenderer from './ReportActionsListItemRenderer'; const propTypes = { /** The report currently being looked at */ @@ -144,6 +144,8 @@ function ReportActionsList({ const route = useRoute(); const opacity = useSharedValue(0); const userActiveSince = useRef(null); + const unreadActionSubscription = useRef(null); + const readNewestActionSubscription = useRef(null); const markerInit = () => { if (!cacheUnreadMarkers.has(report.reportID)) { @@ -229,15 +231,13 @@ function ReportActionsList({ }; // Listen to specific reportID for unread event and set the marker to new message - const unreadActionSubscription = DeviceEventListenerRef.use('unreadAction'); - unreadActionSubscription.add(report.reportID, (newLastReadTime) => { + unreadActionSubscription.current = DeviceEventEmitter.addListener(`unreadAction_${report.reportID}`, (newLastReadTime) => { resetUnreadMarker(newLastReadTime); setMessageManuallyMarkedUnread(new Date().getTime()); }); - const readNewestActionSubscription = DeviceEventListenerRef.use('readNewestAction'); // Listen to specific reportID for read newest action event and reset the marker - readNewestActionSubscription.add(report.reportID, (newLastReadTime) => { + readNewestActionSubscription.current = DeviceEventEmitter.addListener(`readNewestAction_${report.reportID}`, (newLastReadTime) => { resetUnreadMarker(newLastReadTime); setMessageManuallyMarkedUnread(0); }); @@ -245,8 +245,8 @@ function ReportActionsList({ return () => { // If the reportID changes, we reset the userActiveSince to null, we need to do it because // this component doesn't unmount when the reportID changes - unreadActionSubscription.remove(); - readNewestActionSubscription.remove(); + unreadActionSubscription.current.remove(); + readNewestActionSubscription.current.remove(); }; }, [report.reportID]); diff --git a/src/pages/home/report/ReportActionsList/DeviceEventListenerRef.ts b/src/pages/home/report/ReportActionsList/DeviceEventListenerRef.ts deleted file mode 100644 index 973087e07c4c..000000000000 --- a/src/pages/home/report/ReportActionsList/DeviceEventListenerRef.ts +++ /dev/null @@ -1,30 +0,0 @@ -import {DeviceEventEmitter, EmitterSubscription} from 'react-native'; - -export default class DeviceEventListenerRef { - private typePrefix: string; - - private subscription: EmitterSubscription | null = null; - - constructor(typePrefix: string) { - this.typePrefix = typePrefix; - } - - static use(typePrefix: string): DeviceEventListenerRef { - return new DeviceEventListenerRef(typePrefix); - } - - add( - typeSuffix: string, // report.reportID - listener: (data: unknown) => void, - ): void { - this.subscription = DeviceEventEmitter.addListener(`${this.typePrefix}_${typeSuffix}`, listener); - } - - remove(): void { - if (!this.subscription) { - return; - } - this.subscription.remove(); - this.subscription = null; - } -} From bdedd0b45b8056bcd4adfbf291833a9eafd62ec4 Mon Sep 17 00:00:00 2001 From: tienifr Date: Fri, 22 Dec 2023 16:06:18 +0700 Subject: [PATCH 4/5] refactor code --- src/pages/home/report/ReportActionsList.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/pages/home/report/ReportActionsList.js b/src/pages/home/report/ReportActionsList.js index 9e573723619d..c9d60e607ba9 100644 --- a/src/pages/home/report/ReportActionsList.js +++ b/src/pages/home/report/ReportActionsList.js @@ -144,8 +144,6 @@ function ReportActionsList({ const route = useRoute(); const opacity = useSharedValue(0); const userActiveSince = useRef(null); - const unreadActionSubscription = useRef(null); - const readNewestActionSubscription = useRef(null); const markerInit = () => { if (!cacheUnreadMarkers.has(report.reportID)) { @@ -231,22 +229,20 @@ function ReportActionsList({ }; // Listen to specific reportID for unread event and set the marker to new message - unreadActionSubscription.current = DeviceEventEmitter.addListener(`unreadAction_${report.reportID}`, (newLastReadTime) => { + const unreadActionSubscription = DeviceEventEmitter.addListener(`unreadAction_${report.reportID}`, (newLastReadTime) => { resetUnreadMarker(newLastReadTime); setMessageManuallyMarkedUnread(new Date().getTime()); }); // Listen to specific reportID for read newest action event and reset the marker - readNewestActionSubscription.current = DeviceEventEmitter.addListener(`readNewestAction_${report.reportID}`, (newLastReadTime) => { + const readNewestActionSubscription = DeviceEventEmitter.addListener(`readNewestAction_${report.reportID}`, (newLastReadTime) => { resetUnreadMarker(newLastReadTime); setMessageManuallyMarkedUnread(0); }); return () => { - // If the reportID changes, we reset the userActiveSince to null, we need to do it because - // this component doesn't unmount when the reportID changes - unreadActionSubscription.current.remove(); - readNewestActionSubscription.current.remove(); + unreadActionSubscription.remove(); + readNewestActionSubscription.remove(); }; }, [report.reportID]); From 13372331fb86876208389005e122123229c66768 Mon Sep 17 00:00:00 2001 From: tienifr Date: Fri, 29 Dec 2023 16:52:10 +0700 Subject: [PATCH 5/5] remove comment --- src/pages/home/report/ReportActionsList.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/pages/home/report/ReportActionsList.js b/src/pages/home/report/ReportActionsList.js index 406fc1cdbee4..fe054ecc462c 100644 --- a/src/pages/home/report/ReportActionsList.js +++ b/src/pages/home/report/ReportActionsList.js @@ -242,13 +242,11 @@ function ReportActionsList({ setCurrentUnreadMarker(null); }; - // Listen to specific reportID for unread event and set the marker to new message const unreadActionSubscription = DeviceEventEmitter.addListener(`unreadAction_${report.reportID}`, (newLastReadTime) => { resetUnreadMarker(newLastReadTime); setMessageManuallyMarkedUnread(new Date().getTime()); }); - // Listen to specific reportID for read newest action event and reset the marker const readNewestActionSubscription = DeviceEventEmitter.addListener(`readNewestAction_${report.reportID}`, (newLastReadTime) => { resetUnreadMarker(newLastReadTime); setMessageManuallyMarkedUnread(0);