1
- import React from 'react' ;
1
+ import React , { useEffect , useRef , useState } from 'react' ;
2
2
import { View } from 'react-native' ;
3
3
import { withOnyx } from 'react-native-onyx' ;
4
4
import type { OnyxEntry } from 'react-native-onyx' ;
5
5
import OfflineWithFeedback from '@components/OfflineWithFeedback' ;
6
6
import useStyleUtils from '@hooks/useStyleUtils' ;
7
7
import useThemeStyles from '@hooks/useThemeStyles' ;
8
8
import useWindowDimensions from '@hooks/useWindowDimensions' ;
9
- import * as ReportActionsUtils from '@libs/ReportActionsUtils' ;
9
+ import Navigation from '@libs/Navigation/Navigation' ;
10
+ import onyxSubscribe from '@libs/onyxSubscribe' ;
11
+ import * as ReportUtils from '@libs/ReportUtils' ;
10
12
import * as Report from '@userActions/Report' ;
11
13
import ONYXKEYS from '@src/ONYXKEYS' ;
14
+ import ROUTES from '@src/ROUTES' ;
12
15
import type * as OnyxTypes from '@src/types/onyx' ;
13
16
import AnimatedEmptyStateBackground from './AnimatedEmptyStateBackground' ;
14
17
import ReportActionItem from './ReportActionItem' ;
15
18
16
19
type ReportActionItemParentActionOnyxProps = {
17
- /** The report currently being looked at */
20
+ /** The current report is displayed */
18
21
report : OnyxEntry < OnyxTypes . Report > ;
19
-
20
- /** The actions from the parent report */
21
- parentReportActions : OnyxEntry < OnyxTypes . ReportActions > ;
22
22
} ;
23
23
24
24
type ReportActionItemParentActionProps = ReportActionItemParentActionOnyxProps & {
25
25
/** Flag to show, hide the thread divider line */
26
26
shouldHideThreadDividerLine ?: boolean ;
27
27
28
- /** Flag to display the new marker on top of the comment */
29
- shouldDisplayNewMarker : boolean ;
30
-
31
28
/** Position index of the report parent action in the overall report FlatList view */
32
29
index : number ;
33
30
34
31
/** The id of the report */
35
32
// eslint-disable-next-line react/no-unused-prop-types
36
33
reportID : string ;
37
-
38
- /** The id of the parent report */
39
- // eslint-disable-next-line react/no-unused-prop-types
40
- parentReportID : string ;
41
34
} ;
42
35
43
- function ReportActionItemParentAction ( { report, parentReportActions = { } , index = 0 , shouldHideThreadDividerLine = false , shouldDisplayNewMarker } : ReportActionItemParentActionProps ) {
36
+ function ReportActionItemParentAction ( { report, index = 0 , shouldHideThreadDividerLine = false } : ReportActionItemParentActionProps ) {
44
37
const styles = useThemeStyles ( ) ;
45
38
const StyleUtils = useStyleUtils ( ) ;
46
39
const { isSmallScreenWidth} = useWindowDimensions ( ) ;
47
- const parentReportAction = parentReportActions ?. [ `${ report ?. parentReportActionID ?? '' } ` ] ?? null ;
40
+ const ancestorIDs = useRef ( ReportUtils . getAllAncestorReportActionIDs ( report ) ) ;
41
+ const [ allAncestors , setAllAncestors ] = useState < ReportUtils . Ancestor [ ] > ( [ ] ) ;
42
+
43
+ useEffect ( ( ) => {
44
+ const unsubscribeReports : Array < ( ) => void > = [ ] ;
45
+ const unsubscribeReportActions : Array < ( ) => void > = [ ] ;
46
+ ancestorIDs . current . reportIDs . forEach ( ( ancestorReportID ) => {
47
+ unsubscribeReports . push (
48
+ onyxSubscribe ( {
49
+ key : `${ ONYXKEYS . COLLECTION . REPORT } ${ ancestorReportID } ` ,
50
+ callback : ( ) => {
51
+ setAllAncestors ( ReportUtils . getAllAncestorReportActions ( report , shouldHideThreadDividerLine ) ) ;
52
+ } ,
53
+ } ) ,
54
+ ) ;
55
+ unsubscribeReportActions . push (
56
+ onyxSubscribe ( {
57
+ key : `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ ancestorReportID } ` ,
58
+ callback : ( ) => {
59
+ setAllAncestors ( ReportUtils . getAllAncestorReportActions ( report , shouldHideThreadDividerLine ) ) ;
60
+ } ,
61
+ } ) ,
62
+ ) ;
63
+ } ) ;
64
+
65
+ return ( ) => {
66
+ unsubscribeReports . forEach ( ( unsubscribeReport ) => unsubscribeReport ( ) ) ;
67
+ unsubscribeReportActions . forEach ( ( unsubscribeReportAction ) => unsubscribeReportAction ( ) ) ;
68
+ } ;
69
+ // eslint-disable-next-line react-hooks/exhaustive-deps
70
+ } , [ ] ) ;
48
71
49
- // In case of transaction threads, we do not want to render the parent report action.
50
- if ( ReportActionsUtils . isTransactionThread ( parentReportAction ) ) {
51
- return null ;
52
- }
53
72
return (
54
- < OfflineWithFeedback
55
- shouldDisableOpacity = { Boolean ( parentReportAction ?. pendingAction ?? false ) }
56
- pendingAction = { report ?. pendingFields ?. addWorkspaceRoom ?? report ?. pendingFields ?. createChat }
57
- errors = { report ?. errorFields ?. addWorkspaceRoom ?? report ?. errorFields ?. createChat }
58
- errorRowStyles = { [ styles . ml10 , styles . mr2 ] }
59
- onClose = { ( ) => Report . navigateToConciergeChatAndDeleteReport ( report ?. reportID ?? '0' ) }
60
- >
61
- < View style = { StyleUtils . getReportWelcomeContainerStyle ( isSmallScreenWidth ) } >
73
+ < >
74
+ < View style = { [ StyleUtils . getReportWelcomeContainerStyle ( isSmallScreenWidth ) , styles . justifyContentEnd ] } >
62
75
< AnimatedEmptyStateBackground />
63
76
< View style = { [ styles . p5 , StyleUtils . getReportWelcomeTopMarginStyle ( isSmallScreenWidth ) ] } />
64
- { parentReportAction && (
65
- < ReportActionItem
66
- // @ts -expect-error TODO: Remove the comment after ReportActionItem is migrated to TypeScript.
67
- report = { report }
68
- action = { parentReportAction }
69
- displayAsGroup = { false }
70
- isMostRecentIOUReportAction = { false }
71
- shouldDisplayNewMarker = { shouldDisplayNewMarker }
72
- index = { index }
73
- />
74
- ) }
77
+ { allAncestors . map ( ( ancestor ) => (
78
+ < OfflineWithFeedback
79
+ key = { ancestor . reportAction . reportActionID }
80
+ shouldDisableOpacity = { Boolean ( ancestor . reportAction ?. pendingAction ) }
81
+ pendingAction = { ancestor . report ?. pendingFields ?. addWorkspaceRoom ?? ancestor . report ?. pendingFields ?. createChat }
82
+ errors = { ancestor . report ?. errorFields ?. addWorkspaceRoom ?? ancestor . report ?. errorFields ?. createChat }
83
+ errorRowStyles = { [ styles . ml10 , styles . mr2 ] }
84
+ onClose = { ( ) => Report . navigateToConciergeChatAndDeleteReport ( ancestor . report . reportID ) }
85
+ >
86
+ < ReportActionItem
87
+ // @ts -expect-error TODO: Remove this once ReportActionItem (https://github.com/Expensify/App/issues/31982) is migrated to TypeScript.
88
+ onPress = { ( ) => Navigation . navigate ( ROUTES . REPORT_WITH_ID . getRoute ( ancestor . report . reportID ) ) }
89
+ report = { ancestor . report }
90
+ action = { ancestor . reportAction }
91
+ displayAsGroup = { false }
92
+ isMostRecentIOUReportAction = { false }
93
+ shouldDisplayNewMarker = { ancestor . shouldDisplayNewMarker }
94
+ index = { index }
95
+ />
96
+ { ! ancestor . shouldHideThreadDividerLine && < View style = { [ styles . threadDividerLine ] } /> }
97
+ </ OfflineWithFeedback >
98
+ ) ) }
75
99
</ View >
76
- { ! shouldHideThreadDividerLine && < View style = { [ styles . threadDividerLine ] } /> }
77
- </ OfflineWithFeedback >
100
+ </ >
78
101
) ;
79
102
}
80
103
@@ -84,8 +107,4 @@ export default withOnyx<ReportActionItemParentActionProps, ReportActionItemParen
84
107
report : {
85
108
key : ( { reportID} ) => `${ ONYXKEYS . COLLECTION . REPORT } ${ reportID } ` ,
86
109
} ,
87
- parentReportActions : {
88
- key : ( { parentReportID} ) => `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ parentReportID } ` ,
89
- canEvict : false ,
90
- } ,
91
110
} ) ( ReportActionItemParentAction ) ;
0 commit comments