-
Notifications
You must be signed in to change notification settings - Fork 3k
/
ReportActionItemFragment.js
161 lines (142 loc) · 6.39 KB
/
ReportActionItemFragment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import React, {memo} from 'react';
import {ActivityIndicator, View} from 'react-native';
import PropTypes from 'prop-types';
import Str from 'expensify-common/lib/str';
import reportActionFragmentPropTypes from './reportActionFragmentPropTypes';
import styles from '../../../styles/styles';
import variables from '../../../styles/variables';
import themeColors from '../../../styles/themes/default';
import RenderHTML from '../../../components/RenderHTML';
import Text from '../../../components/Text';
import Tooltip from '../../../components/Tooltip';
import * as EmojiUtils from '../../../libs/EmojiUtils';
import withWindowDimensions, {windowDimensionsPropTypes} from '../../../components/withWindowDimensions';
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import canUseTouchScreen from '../../../libs/canUseTouchscreen';
import compose from '../../../libs/compose';
const propTypes = {
/** The message fragment needing to be displayed */
fragment: reportActionFragmentPropTypes.isRequired,
/** Text to be shown for tooltip When Fragment is report Actor */
tooltipText: PropTypes.string,
/** Is this fragment an attachment? */
isAttachment: PropTypes.bool,
/** If this fragment is attachment than has info? */
attachmentInfo: PropTypes.shape({
/** The file name of attachment */
name: PropTypes.string,
/** The file size of the attachment in bytes. */
size: PropTypes.number,
/** The MIME type of the attachment. */
type: PropTypes.string,
/** Attachment's URL represents the specified File object or Blob object */
source: PropTypes.string,
}),
/** Does this fragment belong to a reportAction that has not yet loaded? */
loading: PropTypes.bool,
/** Should this fragment be contained in a single line? */
isSingleLine: PropTypes.bool,
...windowDimensionsPropTypes,
/** localization props */
...withLocalizePropTypes,
};
const defaultProps = {
isAttachment: false,
attachmentInfo: {
name: '',
size: 0,
type: '',
source: '',
},
loading: false,
isSingleLine: false,
tooltipText: '',
};
const ReportActionItemFragment = (props) => {
switch (props.fragment.type) {
case 'COMMENT': {
// If this is an attachment placeholder, return the placeholder component
if (props.isAttachment && props.loading) {
return (
Str.isImage(props.attachmentInfo.name)
? (
<RenderHTML html={`<comment><img src="${props.attachmentInfo.source}" data-expensify-preview-modal-disabled="true"/></comment>`} />
) : (
<View style={[styles.chatItemAttachmentPlaceholder]}>
<ActivityIndicator
size="large"
color={themeColors.textSupporting}
style={[styles.flex1]}
/>
</View>
)
);
}
// If the only difference between fragment.text and fragment.html is <br /> tags
// we replace them with line breaks and render it as text, not as html.
// This is done to render emojis with line breaks between them as text.
const differByLineBreaksOnly = Str.replaceAll(props.fragment.html, '<br />', ' ') === props.fragment.text;
if (differByLineBreaksOnly) {
const textWithLineBreaks = Str.replaceAll(props.fragment.html, '<br />', '\n');
// eslint-disable-next-line no-param-reassign
props.fragment = {...props.fragment, text: textWithLineBreaks, html: textWithLineBreaks};
}
// Only render HTML if we have html in the fragment
return props.fragment.html !== props.fragment.text
? (
<RenderHTML
html={`<comment>${props.fragment.html + (props.fragment.isEdited ? '<edited></edited>' : '')}</comment>`}
/>
) : (
<Text
selectable={!canUseTouchScreen() || !props.isSmallScreenWidth}
style={EmojiUtils.containsOnlyEmojis(props.fragment.text) ? styles.onlyEmojisText : undefined}
>
{Str.htmlDecode(props.fragment.text)}
{props.fragment.isEdited && (
<Text
fontSize={variables.fontSizeSmall}
color={themeColors.textSupporting}
>
{` ${props.translate('reportActionCompose.edited')}`}
</Text>
)}
</Text>
);
}
case 'TEXT':
return (
<Tooltip text={props.tooltipText}>
<Text
selectable
numberOfLines={props.isSingleLine ? 1 : undefined}
style={[styles.chatItemMessageHeaderSender]}
>
{Str.htmlDecode(props.fragment.text)}
</Text>
</Tooltip>
);
case 'LINK':
return <Text>LINK</Text>;
case 'INTEGRATION_COMMENT':
return <Text>REPORT_LINK</Text>;
case 'REPORT_LINK':
return <Text>REPORT_LINK</Text>;
case 'POLICY_LINK':
return <Text>POLICY_LINK</Text>;
// If we have a message fragment type of OLD_MESSAGE this means we have not yet converted this over to the
// new data structure. So we simply set this message as inner html and render it like we did before.
// This wil allow us to convert messages over to the new structure without needing to do it all at once.
case 'OLD_MESSAGE':
return <Text>OLD_MESSAGE</Text>;
default:
return <Text>fragment.text</Text>;
}
};
ReportActionItemFragment.propTypes = propTypes;
ReportActionItemFragment.defaultProps = defaultProps;
ReportActionItemFragment.displayName = 'ReportActionItemFragment';
export default compose(
withWindowDimensions,
withLocalize,
)(memo(ReportActionItemFragment));