-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathOfflineWithFeedback.tsx
More file actions
174 lines (146 loc) · 7.25 KB
/
OfflineWithFeedback.tsx
File metadata and controls
174 lines (146 loc) · 7.25 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
import React from 'react';
import type {StyleProp, TextStyle, ViewStyle} from 'react-native';
import {View} from 'react-native';
import useNetwork from '@hooks/useNetwork';
import useStyleUtils from '@hooks/useStyleUtils';
import useThemeStyles from '@hooks/useThemeStyles';
import mapChildrenFlat from '@libs/mapChildrenFlat';
import shouldRenderOffscreen from '@libs/shouldRenderOffscreen';
import type {AllStyles} from '@styles/utils/types';
import CONST from '@src/CONST';
import type * as OnyxCommon from '@src/types/onyx/OnyxCommon';
import type {ReceiptErrors} from '@src/types/onyx/Transaction';
import type ChildrenProps from '@src/types/utils/ChildrenProps';
import {isEmptyValueObject} from '@src/types/utils/EmptyObject';
import CustomStylesForChildrenProvider from './CustomStylesForChildrenProvider';
import ErrorMessageRow from './ErrorMessageRow';
import ImageSVG from './ImageSVG';
/**
* This component should be used when we are using the offline pattern B (offline with feedback).
* You should enclose any element that should have feedback that the action was taken offline and it will take
* care of adding the appropriate styles for pending actions and displaying the dismissible error.
*/
type OfflineWithFeedbackProps = Partial<ChildrenProps> & {
/** The type of action that's pending */
pendingAction?: OnyxCommon.PendingAction | null;
/** Determine whether to hide the component's children if deletion is pending */
shouldHideOnDelete?: boolean;
/** The errors to display */
errors?: OnyxCommon.Errors | OnyxCommon.TranslationKeyErrors | ReceiptErrors | null;
/** Whether we should show the error messages */
shouldShowErrorMessages?: boolean;
/** Whether we should disable opacity */
shouldDisableOpacity?: boolean;
/** A function to run when the X button next to the error is clicked */
onClose?: () => void;
/** Additional styles to add to the container after local styles. Applied to the parent container */
style?: StyleProp<ViewStyle>;
/** Additional styles to add to the children wrapper container after local styles. Applied to the children wrapper container */
contentContainerStyle?: StyleProp<ViewStyle>;
/** Additional style object for the error row */
errorRowStyles?: StyleProp<ViewStyle>;
/** Additional style object for the error row text */
errorRowTextStyles?: StyleProp<TextStyle>;
/** Whether applying strikethrough to the children should be disabled */
shouldDisableStrikeThrough?: boolean;
/** Whether to apply needsOffscreenAlphaCompositing prop to the children */
needsOffscreenAlphaCompositing?: boolean;
/** Whether we should render the error message above the children */
shouldDisplayErrorAbove?: boolean;
/** Whether we should force opacity */
shouldForceOpacity?: boolean;
/** A function to dismiss error */
dismissError?: () => void;
};
type StrikethroughProps = Partial<ChildrenProps> & {style: AllStyles[]};
function OfflineWithFeedback({
pendingAction,
contentContainerStyle,
errorRowStyles,
errors,
needsOffscreenAlphaCompositing = false,
onClose: onDismiss,
shouldDisableOpacity = false,
shouldDisableStrikeThrough = false,
shouldHideOnDelete = true,
shouldShowErrorMessages = true,
style,
shouldDisplayErrorAbove = false,
shouldForceOpacity = false,
dismissError = () => {},
errorRowTextStyles,
...restProps
}: OfflineWithFeedbackProps) {
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {isOffline} = useNetwork();
const hasErrors = !isEmptyValueObject(errors ?? {});
const isOfflinePendingAction = !!isOffline && !!pendingAction;
const isUpdateOrDeleteError = hasErrors && (pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE || pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE);
const isAddError = hasErrors && pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD;
const needsOpacity = (!shouldDisableOpacity && ((isOfflinePendingAction && !isUpdateOrDeleteError) || isAddError)) || shouldForceOpacity;
const needsStrikeThrough = !shouldDisableStrikeThrough && isOffline && pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE;
const hideChildren = shouldHideOnDelete && !isOffline && pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE && !hasErrors;
let children = restProps.children;
const hasChildren = !!children;
/**
* This method applies the strikethrough to all the children passed recursively
*/
const applyStrikeThrough = (childrenProp: React.ReactNode): React.ReactNode => {
const strikeThroughChildren = mapChildrenFlat(childrenProp, (child) => {
if (!React.isValidElement(child) || child.type === ImageSVG) {
return child;
}
if (child.type === React.Fragment) {
const childProps = child.props as {children?: React.ReactNode};
return React.createElement(React.Fragment, {key: child.key}, applyStrikeThrough(childProps.children));
}
type ChildComponentProps = ChildrenProps & {style?: AllStyles};
const childProps = child.props as ChildComponentProps;
const props: StrikethroughProps = {
style: StyleUtils.combineStyles(childProps.style ?? [], styles.offlineFeedbackDeleted, styles.userSelectNone),
};
if (childProps.children) {
props.children = applyStrikeThrough(childProps.children);
}
return React.cloneElement(child, props);
});
return strikeThroughChildren;
};
// Apply strikethrough to children if needed, but skip it if we are not going to render them
if (hasChildren && needsStrikeThrough && !hideChildren) {
children = applyStrikeThrough(children);
}
return (
<View style={style}>
{shouldShowErrorMessages && shouldDisplayErrorAbove && (
<ErrorMessageRow
errors={errors}
errorRowStyles={errorRowStyles}
onDismiss={onDismiss}
errorRowTextStyles={errorRowTextStyles}
dismissError={dismissError}
/>
)}
{hasChildren && !hideChildren && (
<View
style={[needsOpacity ? styles.offlineFeedbackPending : styles.offlineFeedbackDefault, contentContainerStyle]}
needsOffscreenAlphaCompositing={shouldRenderOffscreen ? needsOpacity && needsOffscreenAlphaCompositing : undefined}
>
<CustomStylesForChildrenProvider style={needsStrikeThrough ? [styles.offlineFeedbackDeleted, styles.userSelectNone] : null}>{children}</CustomStylesForChildrenProvider>
</View>
)}
{shouldShowErrorMessages && !shouldDisplayErrorAbove && (
<ErrorMessageRow
errors={errors}
errorRowStyles={errorRowStyles}
errorRowTextStyles={errorRowTextStyles}
onDismiss={onDismiss}
dismissError={dismissError}
/>
)}
</View>
);
}
export default OfflineWithFeedback;
export type {OfflineWithFeedbackProps};