-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathDotIndicatorMessage.tsx
101 lines (88 loc) · 4.07 KB
/
DotIndicatorMessage.tsx
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
/* eslint-disable react/no-array-index-key */
import React from 'react';
import type {StyleProp, TextStyle, ViewStyle} from 'react-native';
import {View} from 'react-native';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import {isReceiptError} from '@libs/ErrorUtils';
import fileDownload from '@libs/fileDownload';
import type {MaybePhraseKey} from '@libs/Localize';
import * as Localize from '@libs/Localize';
import type {ReceiptError} from '@src/types/onyx/Transaction';
import Icon from './Icon';
import * as Expensicons from './Icon/Expensicons';
import Text from './Text';
import TextLink from './TextLink';
type DotIndicatorMessageProps = {
/**
* In most cases this should just be errors from onxyData
* if you are not passing that data then this needs to be in a similar shape like
* {
* timestamp: 'message',
* }
*/
messages: Record<string, Localize.MaybePhraseKey | ReceiptError>;
/** The type of message, 'error' shows a red dot, 'success' shows a green dot */
type: 'error' | 'success';
/** Additional styles to apply to the container */
style?: StyleProp<ViewStyle>;
/** Additional styles to apply to the text */
textStyles?: StyleProp<TextStyle>;
};
function DotIndicatorMessage({messages = {}, style, type, textStyles}: DotIndicatorMessageProps) {
const theme = useTheme();
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
if (Object.keys(messages).length === 0) {
return null;
}
// Fetch the keys, sort them, and map through each key to get the corresponding message
const sortedMessages: Array<MaybePhraseKey | ReceiptError> = Object.keys(messages)
.sort()
.map((key) => messages[key]);
// Removing duplicates using Set and transforming the result into an array
const uniqueMessages: Array<ReceiptError | string> = [...new Set(sortedMessages)].map((message) => (isReceiptError(message) ? message : Localize.translateIfPhraseKey(message)));
const isErrorMessage = type === 'error';
return (
<View style={[styles.dotIndicatorMessage, style]}>
<View style={styles.offlineFeedback.errorDot}>
<Icon
src={Expensicons.DotIndicator}
fill={isErrorMessage ? theme.danger : theme.success}
/>
</View>
<View style={styles.offlineFeedback.textContainer}>
{uniqueMessages.map((message, i) =>
isReceiptError(message) ? (
<Text
key={i}
style={styles.offlineFeedback.text}
>
<Text style={[StyleUtils.getDotIndicatorTextStyles(isErrorMessage)]}>{Localize.translateLocal('iou.error.receiptFailureMessage')}</Text>
<TextLink
style={[StyleUtils.getDotIndicatorTextStyles(), styles.link]}
onPress={() => {
fileDownload(message.source, message.filename);
}}
>
{Localize.translateLocal('iou.error.saveFileMessage')}
</TextLink>
<Text style={[StyleUtils.getDotIndicatorTextStyles(isErrorMessage)]}>{Localize.translateLocal('iou.error.loseFileMessage')}</Text>
</Text>
) : (
<Text
// eslint-disable-next-line react/no-array-index-key
key={i}
style={[StyleUtils.getDotIndicatorTextStyles(isErrorMessage), textStyles]}
>
{message}
</Text>
),
)}
</View>
</View>
);
}
DotIndicatorMessage.displayName = 'DotIndicatorMessage';
export default DotIndicatorMessage;