-
Notifications
You must be signed in to change notification settings - Fork 87
/
NavBar.jsx
205 lines (187 loc) · 7.82 KB
/
NavBar.jsx
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
202
203
204
205
import React, { PureComponent } from "react";
import PropTypes from "prop-types";
import classNames from "classnames";
import { Link, withRouter } from "react-router-dom";
import { compose, withApollo } from "react-apollo";
import { Utilities } from "@src/utilities/utilities";
import { listClusters } from "@src/queries/ClusterQueries";
import Avatar from "../shared/Avatar";
import "@src/scss/components/shared/NavBar.scss";
export class NavBar extends PureComponent {
constructor(props) {
super(props);
this.state = {
selectedTab: ""
};
}
static propTypes = {
refetchListApps: PropTypes.func.isRequired,
history: PropTypes.object.isRequired
}
handleLogOut = async (e) => {
e.preventDefault();
try {
const res = await fetch(`${window.env.API_ENDPOINT}/logout`, {
headers: {
"Authorization": Utilities.getToken(),
"Content-Type": "application/json",
},
method: "POST",
});
if (res.ok && res.status === 204) {
Utilities.logoutUser();
}
} catch(err) {
console.log(err);
}
}
componentDidUpdate(lastProps) {
const { pathname } = this.props.location;
if (pathname !== lastProps.location.pathname) {
this.setSelectedTab();
}
}
componentDidMount() {
this.setSelectedTab();
}
setSelectedTab = () => {
const { pathname } = this.props.location;
let selectedTab = "";
if (pathname === "/gitops") {
selectedTab = "gitops";
} else if (pathname === "/cluster/manage") {
selectedTab = "cluster_management";
} else if (pathname.startsWith("/app")) {
selectedTab = "dashboard"
} else if (pathname === "/snapshots") {
selectedTab = "snapshots";
}
this.setState({ selectedTab });
}
handleGoToGitOps = () => {
if (this.props.location.pathname === "/gitops") {
this.props.client.query({
query: listClusters,
fetchPolicy: "network-only",
});
} else {
this.props.history.push("/gitops");
}
}
handleGoToClusterManagement = () => {
this.props.history.push("/cluster/manage");
}
handleAddNewApplication = () => {
this.props.history.push("/upload-license");
}
handleGoToSnapshots = () => {
this.props.history.push("/snapshots");
}
redirectToDashboard = () => {
const { refetchListApps, history } = this.props;
refetchListApps().then(() => {
history.push("/");
});
}
render() {
const { className, fetchingMetadata, isKurlEnabled, isGitOpsSupported, listApps, logo, location, isSnapshotsSupported } = this.props;
const { user, selectedTab } = this.state;
const pathname = location.pathname.split("/");
let selectedApp;
let appLogo;
let licenseType;
if (pathname.length > 2 && pathname[1] === "app") {
selectedApp = listApps.find(app => app.slug === pathname[2]);
appLogo = selectedApp?.iconUri;
licenseType = selectedApp?.licenseType;
} else {
appLogo = logo;
licenseType = "";
}
const isClusterScope = this.props.location.pathname.includes("/clusterscope");
return (
<div className={classNames("NavBarWrapper flex flex-auto", className, {
"cluster-scope": isClusterScope
})}>
<div className="container flex flex1">
<div className="flex1 justifyContent--flexStart">
<div className="flex1 flex u-height--full">
<div className="flex flex-auto">
<div className="flex alignItems--center flex1 flex-verticalCenter u-position--relative u-marginRight--20">
<div className="HeaderLogo">
<Link to={isClusterScope ? "/clusterscope" : "/"} tabIndex="-1">
{appLogo
? <span className="nav-logo clickable" style={{ backgroundImage: `url(${appLogo})` }} />
: !fetchingMetadata ? <span className="logo icon clickable" />
: <span style={{ width: "30px", height: "30px" }} />
}
{licenseType === "community" && <span className="flag flex"> <span className="flagText">Community Edition</span> </span>}
</Link>
</div>
</div>
{Utilities.isLoggedIn() && listApps.length > 0 && (
<div className="flex flex-auto left-items">
<div className={classNames("NavItem u-position--relative flex", { "is-active": selectedTab === "dashboard" })}>
<span className="HeaderLink flex flex1 u-cursor--pointer" onClick={this.redirectToDashboard}>
<span className="text u-fontSize--normal u-fontWeight--medium flex-column justifyContent--center">
<span>Dashboard</span>
</span>
</span>
</div>
{isGitOpsSupported &&
<div className={classNames("NavItem u-position--relative flex", { "is-active": selectedTab === "gitops" })}>
<span className="HeaderLink flex flex1 u-cursor--pointer" onClick={this.handleGoToGitOps}>
<span className="text u-fontSize--normal u-fontWeight--medium flex-column justifyContent--center">
<span>GitOps</span>
</span>
</span>
</div>
}
{isKurlEnabled &&
<div className={classNames("NavItem u-position--relative flex", { "is-active": selectedTab === "cluster_management" })}>
<span className="HeaderLink flex flex1 u-cursor--pointer" onClick={this.handleGoToClusterManagement}>
<span className="text u-fontSize--normal u-fontWeight--medium flex-column justifyContent--center">
<span>Cluster Management</span>
</span>
</span>
</div>
}
{isSnapshotsSupported &&
<div className={classNames("NavItem u-position--relative flex", { "is-active": selectedTab === "snapshots" })}>
<span className="HeaderLink flex flex1 u-cursor--pointer" onClick={this.handleGoToSnapshots}>
<span className="text u-fontSize--normal u-fontWeight--medium flex-column justifyContent--center">
<span>Snapshot Settings</span>
</span>
</span>
</div>
}
</div>
)}
</div>
{Utilities.isLoggedIn() ?
<div className="flex flex1 justifyContent--flexEnd right-items">
{pathname[1] === "upload-license" || pathname[2] === "airgap" ?
null :
<div className="flex-column flex-auto u-marginRight--20 justifyContent--center">
<Link className="btn secondary blue rounded" to="/upload-license">
Add a new application
</Link>
</div>}
<div className="flex-column flex-auto justifyContent--center">
<p data-qa="Navbar--logOutButton" className="NavItem" onClick={this.handleLogOut}>Log out</p>
</div>
{user && user.avatarUrl !== "" ?
<div className="flex-column flex-auto justifyContent--center u-marginLeft--10">
<Avatar imageUrl={this.state.user && this.state.user.avatarUrl} />
</div>
: null}
</div>
: null}
</div>
</div>
</div>
</div>
);
}
}
export default compose(withRouter, withApollo)(NavBar);