-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ConnectionLayout.tsx
155 lines (129 loc) · 5.25 KB
/
ConnectionLayout.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
import {isEmpty} from 'lodash';
import React, {useMemo} from 'react';
import type {StyleProp, TextStyle, ViewStyle} from 'react-native';
import {View} from 'react-native';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import * as PolicyUtils from '@libs/PolicyUtils';
import type {AccessVariant} from '@pages/workspace/AccessOrNotFoundWrapper';
import AccessOrNotFoundWrapper from '@pages/workspace/AccessOrNotFoundWrapper';
import type {TranslationPaths} from '@src/languages/types';
import type {ConnectionName, PolicyFeatureName} from '@src/types/onyx/Policy';
import HeaderWithBackButton from './HeaderWithBackButton';
import ScreenWrapper from './ScreenWrapper';
import ScrollView from './ScrollView';
import Text from './Text';
type ConnectionLayoutProps = {
/** Used to set the testID for tests */
displayName: string;
/** Header title to be translated for the connection component */
headerTitle?: TranslationPaths;
/** The subtitle to show in the header */
headerSubtitle?: string;
/** React nodes that will be shown */
children?: React.ReactNode;
/** Title to be translated for the connection component */
title?: TranslationPaths;
/** The current policyID */
policyID: string;
/** Defines which types of access should be verified */
accessVariants?: AccessVariant[];
/** The current feature name that the user tries to get access to */
featureName?: PolicyFeatureName;
/** The content container style of Scrollview */
contentContainerStyle?: StyleProp<ViewStyle> | undefined;
/** Style of the title text */
titleStyle?: StyleProp<TextStyle> | undefined;
/** Whether to include safe area padding bottom or not */
shouldIncludeSafeAreaPaddingBottom?: boolean;
/** Whether to use ScrollView or not */
shouldUseScrollView?: boolean;
/** Used for dynamic header title translation with parameters */
headerTitleAlreadyTranslated?: string;
/** Used for dynamic title translation with parameters */
titleAlreadyTranslated?: string;
/** Name of the current connection */
connectionName: ConnectionName;
/** Whether the screen should load for an empty connection */
shouldLoadForEmptyConnection?: boolean;
/** Handler for back button press */
onBackButtonPress?: () => void;
/** Whether or not to block user from accessing the page */
shouldBeBlocked?: boolean;
};
type ConnectionLayoutContentProps = Pick<ConnectionLayoutProps, 'title' | 'titleStyle' | 'children' | 'titleAlreadyTranslated'>;
function ConnectionLayoutContent({title, titleStyle, children, titleAlreadyTranslated}: ConnectionLayoutContentProps) {
const {translate} = useLocalize();
const styles = useThemeStyles();
return (
<>
{title && <Text style={[styles.pb5, titleStyle]}>{titleAlreadyTranslated ?? translate(title)}</Text>}
{children}
</>
);
}
function ConnectionLayout({
displayName,
headerTitle,
children,
title,
headerSubtitle,
policyID,
accessVariants,
featureName,
contentContainerStyle,
titleStyle,
shouldIncludeSafeAreaPaddingBottom,
connectionName,
shouldUseScrollView = true,
headerTitleAlreadyTranslated,
titleAlreadyTranslated,
shouldLoadForEmptyConnection = false,
onBackButtonPress = () => Navigation.goBack(),
shouldBeBlocked = false,
}: ConnectionLayoutProps) {
const {translate} = useLocalize();
const policy = PolicyUtils.getPolicy(policyID);
const isConnectionEmpty = isEmpty(policy?.connections?.[connectionName]);
const renderSelectionContent = useMemo(
() => (
<ConnectionLayoutContent
title={title}
titleStyle={titleStyle}
titleAlreadyTranslated={titleAlreadyTranslated}
>
{children}
</ConnectionLayoutContent>
),
[title, titleStyle, children, titleAlreadyTranslated],
);
const shouldBlockByConnection = shouldLoadForEmptyConnection ? !isConnectionEmpty : isConnectionEmpty;
return (
<AccessOrNotFoundWrapper
policyID={policyID}
accessVariants={accessVariants}
featureName={featureName}
shouldBeBlocked={!!shouldBeBlocked || shouldBlockByConnection}
>
<ScreenWrapper
includeSafeAreaPaddingBottom={!!shouldIncludeSafeAreaPaddingBottom}
shouldEnableMaxHeight
testID={displayName}
>
<HeaderWithBackButton
title={headerTitleAlreadyTranslated ?? (headerTitle ? translate(headerTitle) : '')}
subtitle={headerSubtitle}
onBackButtonPress={onBackButtonPress}
/>
{shouldUseScrollView ? (
<ScrollView contentContainerStyle={contentContainerStyle}>{renderSelectionContent}</ScrollView>
) : (
<View style={contentContainerStyle}>{renderSelectionContent}</View>
)}
</ScreenWrapper>
</AccessOrNotFoundWrapper>
);
}
ConnectionLayout.displayName = 'ConnectionLayout';
export default ConnectionLayout;