-
Notifications
You must be signed in to change notification settings - Fork 4
/
Header.js
201 lines (188 loc) · 5.74 KB
/
Header.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2023 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import * as React from "react";
import {Dimensions, StyleSheet, View} from "react-native";
import {Appbar, Avatar, Menu, Text, TouchableRipple} from "react-native-paper";
import {useNotifications} from "react-native-notificated";
import Icon from "react-native-vector-icons/MaterialCommunityIcons";
import CasdoorLoginPage, {CasdoorLogout} from "./CasdoorLoginPage";
import useStore from "./useStorage";
import {useAccountSync} from "./useAccountStore";
import LoginMethodSelector from "./LoginMethodSelector";
const {width} = Dimensions.get("window");
const Header = () => {
const {userInfo, clearAll} = useStore();
const {isSyncing, syncError, clearSyncError} = useAccountSync();
const [showLoginPage, setShowLoginPage] = React.useState(false);
const [menuVisible, setMenuVisible] = React.useState(false);
const [loginMethod, setLoginMethod] = React.useState(null);
const {notify} = useNotifications();
const openMenu = () => setMenuVisible(true);
const closeMenu = () => setMenuVisible(false);
const handleMenuLogoutClicked = () => {
handleCasdoorLogout();
closeMenu();
};
const {openActionSheet} = LoginMethodSelector({
onSelectMethod: (method) => {
setLoginMethod(method);
setShowLoginPage(true);
},
});
const handleCasdoorLogin = () => {
openActionSheet();
};
const handleHideLoginPage = () => {
setShowLoginPage(false);
};
const handleCasdoorLogout = () => {
CasdoorLogout();
clearAll();
clearSyncError();
};
const handleSyncErrorPress = () => {
notify("error", {
params: {
title: "Error",
description: syncError || "An unknown error occurred during synchronization.",
},
});
};
return (
<Appbar.Header mode="small" style={styles.header}>
<Appbar.Content
title={
<View style={styles.titleContainer}>
<Text style={styles.titleTextCasdoor}>Casdoor</Text>
</View>
}
style={styles.titleWrapper}
/>
<View style={styles.rightContainer}>
{userInfo !== null && (
<Icon
name={
isSyncing
? "cloud-sync-outline"
: syncError
? "cloud-off-outline"
: "cloud-check-outline"
}
size={22}
color={isSyncing ? "#FFC107" : syncError ? "#E53935" : "#4CAF50"}
style={styles.syncIcon}
onPress={(isSyncing || syncError === null) ? null : handleSyncErrorPress}
/>
)}
<Menu
visible={menuVisible}
onDismiss={closeMenu}
contentStyle={styles.menuContent}
anchorPosition="bottom"
mode="elevated"
anchor={
<TouchableRipple
onPress={userInfo === null ? handleCasdoorLogin : openMenu}
style={styles.buttonContainer}
>
<View style={styles.buttonContent}>
{userInfo !== null && (
<Avatar.Image
size={28}
source={{uri: userInfo.avatar}}
style={styles.avatar}
/>
)}
<Text style={[
styles.buttonText,
userInfo === null && {marginLeft: 0},
]}>
{userInfo === null ? "Login" : userInfo.name}
</Text>
</View>
</TouchableRipple>
}
>
<Menu.Item onPress={handleMenuLogoutClicked} title="Logout" />
</Menu>
</View>
{showLoginPage && (
<CasdoorLoginPage
onWebviewClose={handleHideLoginPage}
initialMethod={loginMethod}
/>
)}
</Appbar.Header>
);
};
const styles = StyleSheet.create({
header: {
backgroundColor: "#F2F2F2",
height: 56,
},
rightContainer: {
flexDirection: "row",
alignItems: "center",
paddingRight: width * 0.04,
},
titleWrapper: {
alignItems: "flex-start",
},
titleContainer: {
flexDirection: "row",
alignItems: "baseline",
},
titleTextCasdoor: {
fontSize: Math.max(24, width * 0.05),
fontWeight: "bold",
color: "#212121",
fontFamily: "Lato_700Bold",
},
buttonContainer: {
borderRadius: 24,
overflow: "hidden",
borderWidth: 0.5,
borderColor: "#DDDDDD",
},
buttonContent: {
flexDirection: "row",
alignItems: "center",
justifyContent: "center",
paddingVertical: 6,
paddingHorizontal: 14,
},
buttonText: {
fontSize: Math.max(14, width * 0.042),
fontWeight: "600",
marginLeft: 8,
color: "#424242",
fontFamily: "Roboto_500Medium",
},
menuContent: {
backgroundColor: "#FAFAFA",
borderRadius: 8,
elevation: 2,
shadowColor: "#000000",
shadowOffset: {width: 0, height: 1},
shadowOpacity: 0.1,
shadowRadius: 2,
},
avatar: {
backgroundColor: "transparent",
},
syncIcon: {
marginRight: 12,
},
});
export default Header;