-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPageHeader.tsx
More file actions
318 lines (298 loc) · 12.5 KB
/
PageHeader.tsx
File metadata and controls
318 lines (298 loc) · 12.5 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
* Copyright (c) 2024. Devtron Inc.
*
* 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 { useEffect, useState } from 'react'
import ReactGA from 'react-ga4'
import Tippy from '@tippyjs/react'
import { ReactComponent as ICCaretDownSmall } from '@Icons/ic-caret-down-small.svg'
import { ReactComponent as Close } from '@Icons/ic-close.svg'
import { ReactComponent as Question } from '@Icons/ic-help-outline.svg'
import { ReactComponent as ICMediumPaintBucket } from '@IconsV2/ic-medium-paintbucket.svg'
import { getAlphabetIcon, TippyCustomized, TippyTheme } from '../../../Common'
import { MAX_LOGIN_COUNT, POSTHOG_EVENT_ONBOARDING } from '../../../Common/Constants'
import { useMainContext, useTheme, useUserEmail } from '../../Providers'
import GettingStartedCard from '../GettingStartedCard/GettingStarted'
import { InfoIconTippy } from '../InfoIconTippy'
import LogoutCard from '../LogoutCard'
import HelpNav from './HelpNav'
import { IframePromoButton } from './IframePromoButton'
import { getServerInfo } from './service'
import { InstallationType, PageHeaderType, ServerInfo } from './types'
import { getIsShowingLicenseData, handlePostHogEventUpdate, setActionWithExpiry } from './utils'
import './pageHeader.scss'
const PageHeader = ({
headerName,
additionalHeaderInfo,
showTabs = false,
renderHeaderTabs,
isBreadcrumbs = false,
breadCrumbs,
renderActionButtons,
showCloseButton = false,
onClose,
markAsBeta,
tippyProps,
}: PageHeaderType) => {
const {
loginCount,
setLoginCount,
showGettingStartedCard,
setShowGettingStartedCard,
setGettingStartedClicked,
licenseData,
} = useMainContext()
const { showSwitchThemeLocationTippy, handleShowSwitchThemeLocationTippyChange } = useTheme()
const { isTippyCustomized, tippyRedirectLink, TippyIcon, tippyMessage, onClickTippyButton, additionalContent } =
tippyProps || {}
const [showHelpCard, setShowHelpCard] = useState(false)
const [showLogOutCard, setShowLogOutCard] = useState(false)
const { email } = useUserEmail()
const [currentServerInfo, setCurrentServerInfo] = useState<{ serverInfo: ServerInfo; fetchingServerInfo: boolean }>(
{
serverInfo: undefined,
fetchingServerInfo: false,
},
)
const [expiryDate, setExpiryDate] = useState(0)
const getCurrentServerInfo = async () => {
try {
const { result } = await getServerInfo(true, true)
setCurrentServerInfo({
serverInfo: result,
fetchingServerInfo: false,
})
} catch {
setCurrentServerInfo({
serverInfo: currentServerInfo.serverInfo,
fetchingServerInfo: false,
})
// eslint-disable-next-line no-console
console.error('Error in fetching server info')
}
}
useEffect(() => {
setExpiryDate(+localStorage.getItem('clickedOkay'))
}, [])
const hideGettingStartedCard = (count?: string) => {
setShowGettingStartedCard(false)
if (count) {
setLoginCount(+count)
}
}
const handleCloseSwitchThemeLocationTippyChange = () => {
handleShowSwitchThemeLocationTippyChange(false)
}
const onClickLogoutButton = () => {
handleCloseSwitchThemeLocationTippyChange()
setShowLogOutCard(!showLogOutCard)
if (showHelpCard) {
setShowHelpCard(false)
}
setActionWithExpiry('clickedOkay', 1)
hideGettingStartedCard()
}
const onClickHelp = async () => {
if (
!window._env_.K8S_CLIENT &&
currentServerInfo.serverInfo?.installationType !== InstallationType.ENTERPRISE
) {
await getCurrentServerInfo()
}
setShowHelpCard(!showHelpCard)
if (showLogOutCard) {
setShowLogOutCard(false)
}
setActionWithExpiry('clickedOkay', 1)
hideGettingStartedCard()
await handlePostHogEventUpdate(POSTHOG_EVENT_ONBOARDING.HELP)
ReactGA.event({
category: 'Main Navigation',
action: `Help Clicked`,
})
}
const renderThemePreferenceLocationTippyContent = () => (
<div className="px-16 pb-16 flexbox-col dc__gap-4">
<h6 className="m-0 fs-14 fw-6 lh-20">Theme Preference</h6>
<p className="m-0 fs-13 fw-4 lh-20">You can change your theme preference here</p>
</div>
)
const renderLogoutHelpSection = () => (
<>
<div className="flex left cursor dc__gap-8" onClick={onClickHelp}>
<span className="icon-dim-24 fcn-9">
<Question />
</span>
<span className="fs-13 cn-9" data-testid="go-to-get-started">
Help
</span>
<ICCaretDownSmall
style={{ ['--rotateBy' as any]: `${180 * Number(showHelpCard)}deg` }}
className="scn-7 icon-dim-16 rotate pointer"
/>
</div>
{!window._env_.K8S_CLIENT && (
<TippyCustomized
theme={TippyTheme.black}
className="w-250"
placement="bottom"
visible={showSwitchThemeLocationTippy}
Icon={ICMediumPaintBucket}
iconClass="icon-dim-40 dc__no-shrink"
iconSize={40}
additionalContent={renderThemePreferenceLocationTippyContent()}
showCloseButton
trigger="manual"
interactive
arrow
onClose={handleCloseSwitchThemeLocationTippyChange}
>
<button
type="button"
data-testid="profile-button"
onClick={onClickLogoutButton}
className="dc__transparent flex p-4 dc__gap-4 br-18 bg__secondary border__secondary"
>
{getAlphabetIcon(email, 'm-0-imp h-24 w-24-imp')}
<ICCaretDownSmall
className={`scn-7 icon-dim-16 ${showLogOutCard ? 'dc__flip-180' : ''} dc__transition--transform`}
/>
</button>
</TippyCustomized>
)}
</>
)
const getExpired = (): boolean => {
// Render Getting started tippy card if the time gets expired
const now = new Date().valueOf()
return now > expiryDate
}
const renderBetaTag = (): JSX.Element => (
<span className="fs-12 fw-4 lh-18 pt-1 pb-1 pl-6 pr-6 ml-8 cn-9 bcy-5 br-4">Beta</span>
)
const showingLicenseBar = getIsShowingLicenseData(licenseData)
const renderIframeButton = () => <IframePromoButton />
return (
<div
className={`dc__page-header dc__content-space cn-9 bg__primary pl-20 pr-20 ${
showTabs ? 'dc__page-header-tabs__height' : 'dc__page-header__height flex'
}`}
>
<h1 className="dc__page-header__title dc__content-space flex fs-16 fw-6 lh-20 h-48">
<div className="flex left">
{showCloseButton && (
<button
className="dc__transparent flex mr-8"
type="button"
aria-label="close-button"
onClick={onClose}
>
<Close className="dc__page-header__close-icon icon-dim-24 cursor" />
</button>
)}
<span className="fw-6" data-testid="main-header">
{headerName}
</span>
{additionalHeaderInfo && additionalHeaderInfo()}
{isBreadcrumbs && breadCrumbs()}
{tippyProps &&
(isTippyCustomized ? (
<InfoIconTippy
infoText={tippyMessage}
heading={headerName}
iconClassName="icon-dim-20 ml-8 fcn-5"
documentationLink={tippyRedirectLink}
documentationLinkText="Learn More"
additionalContent={additionalContent}
>
{TippyIcon && (
<div className="flex">
<TippyIcon className="icon-dim-20 ml- cursor fcn-5" />
</div>
)}
</InfoIconTippy>
) : (
<a
data-testid="learn-more-symbol"
className="dc__link flex"
target="_blank"
href={tippyRedirectLink}
onClick={onClickTippyButton}
rel="noreferrer"
aria-label="tippy-icon"
>
<Tippy
className="default-tt "
arrow={false}
placement="top"
content={<span style={{ display: 'block', width: '66px' }}> {tippyMessage} </span>}
>
<div className="flex">
<TippyIcon className="icon-dim-20 ml-8 cursor fcn-5" />
</div>
</Tippy>
</a>
))}
{markAsBeta && renderBetaTag()}
</div>
{showTabs && (
<div className="flex left dc__gap-12">
{renderIframeButton()}
{typeof renderActionButtons === 'function' && renderActionButtons()}
{renderLogoutHelpSection()}
</div>
)}
</h1>
{showTabs && renderHeaderTabs()}
{showHelpCard && (
<HelpNav
className={`help-card__more-option ${showingLicenseBar ? 'with-bar' : ''} ${window._env_.K8S_CLIENT ? 'k8s-client-view' : ''}`}
setShowHelpCard={setShowHelpCard}
serverInfo={currentServerInfo.serverInfo}
fetchingServerInfo={currentServerInfo.fetchingServerInfo}
setGettingStartedClicked={setGettingStartedClicked}
showHelpCard={showHelpCard}
/>
)}
{!window._env_.K8S_CLIENT &&
showGettingStartedCard &&
loginCount >= 0 &&
loginCount < MAX_LOGIN_COUNT &&
getExpired() && (
<GettingStartedCard
className="w-300"
showHelpCard={false}
hideGettingStartedCard={hideGettingStartedCard}
loginCount={loginCount}
/>
)}
{showLogOutCard && (
<LogoutCard
className={`logout-card__more-option ${showingLicenseBar ? 'with-bar' : ''} mt-8`}
userFirstLetter={email}
setShowLogOutCard={setShowLogOutCard}
showLogOutCard={showLogOutCard}
/>
)}
{!showTabs && (
<div className="flex left dc__gap-12">
{typeof renderActionButtons === 'function' && renderActionButtons()}
{renderIframeButton()}
{renderLogoutHelpSection()}
</div>
)}
</div>
)
}
export default PageHeader