-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathOfflineWithFeedback.tsx
166 lines (139 loc) · 6.71 KB
/
OfflineWithFeedback.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
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
import React, {useCallback} from 'react';
import type {StyleProp, 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 {isEmptyObject} from '@src/types/utils/EmptyObject';
import CustomStylesForChildrenProvider from './CustomStylesForChildrenProvider';
import ErrorMessageRow from './ErrorMessageRow';
/**
* 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 = 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 | 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 after local styles. Applied to the parent container */
style?: StyleProp<ViewStyle>;
/** Additional styles to add after local styles. Applied to the children wrapper container */
contentContainerStyle?: StyleProp<ViewStyle>;
/** Additional style object for the error row */
errorRowStyles?: StyleProp<ViewStyle>;
/** Whether applying strikethrough to the children should be disabled */
shouldDisableStrikeThrough?: boolean;
/** Whether to apply needsOffscreenAlphaCompositing prop to the children */
needsOffscreenAlphaCompositing?: boolean;
/** Whether we can dismiss the error message */
canDismissError?: boolean;
/** Whether we should render the error message above the children */
shouldDisplayErrorAbove?: boolean;
/** Whether we should force opacity */
shouldForceOpacity?: boolean;
};
type StrikethroughProps = Partial<ChildrenProps> & {style: AllStyles[]};
function OfflineWithFeedback({
pendingAction,
canDismissError = true,
contentContainerStyle,
errorRowStyles,
errors,
needsOffscreenAlphaCompositing = false,
onClose = () => {},
shouldDisableOpacity = false,
shouldDisableStrikeThrough = false,
shouldHideOnDelete = true,
shouldShowErrorMessages = true,
style,
shouldDisplayErrorAbove = false,
shouldForceOpacity = false,
...rest
}: OfflineWithFeedbackProps) {
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {isOffline} = useNetwork();
const hasErrors = !isEmptyObject(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 = rest.children;
/**
* This method applies the strikethrough to all the children passed recursively
*/
const applyStrikeThrough = useCallback(
(childrenProp: React.ReactNode): React.ReactNode => {
const strikedThroughChildren = mapChildrenFlat(childrenProp, (child) => {
if (!React.isValidElement(child)) {
return child;
}
type ChildComponentProps = ChildrenProps & {style?: AllStyles};
const childProps = child.props as ChildComponentProps;
const props: StrikethroughProps = {
style: StyleUtils.combineStyles(childProps.style ?? [], styles.offlineFeedback.deleted, styles.userSelectNone),
};
if (childProps.children) {
props.children = applyStrikeThrough(childProps.children);
}
return React.cloneElement(child, props);
});
return strikedThroughChildren;
},
[StyleUtils, styles],
);
// Apply strikethrough to children if needed, but skip it if we are not going to render them
if (needsStrikeThrough && !hideChildren) {
children = applyStrikeThrough(children);
}
return (
<View style={style}>
{shouldShowErrorMessages && shouldDisplayErrorAbove && (
<ErrorMessageRow
errors={errors}
errorRowStyles={errorRowStyles}
onClose={onClose}
canDismissError={canDismissError}
/>
)}
{!hideChildren && (
<View
style={[needsOpacity ? styles.offlineFeedback.pending : styles.offlineFeedback.default, contentContainerStyle]}
needsOffscreenAlphaCompositing={shouldRenderOffscreen ? needsOpacity && needsOffscreenAlphaCompositing : undefined}
>
<CustomStylesForChildrenProvider style={needsStrikeThrough ? [styles.offlineFeedback.deleted, styles.userSelectNone] : null}>{children}</CustomStylesForChildrenProvider>
</View>
)}
{shouldShowErrorMessages && !shouldDisplayErrorAbove && (
<ErrorMessageRow
errors={errors}
errorRowStyles={errorRowStyles}
onClose={onClose}
canDismissError={canDismissError}
/>
)}
</View>
);
}
OfflineWithFeedback.displayName = 'OfflineWithFeedback';
export default OfflineWithFeedback;
export type {OfflineWithFeedbackProps};