-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHeader.tsx
More file actions
135 lines (123 loc) · 4.42 KB
/
Header.tsx
File metadata and controls
135 lines (123 loc) · 4.42 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
import { useRouter } from 'next/router';
import React, { useEffect, useState } from 'react';
import '../../public/images/logo.png';
import { RiQuestionLine } from 'react-icons/ri';
import { BiLogIn, BiLogOut, BiSync } from 'react-icons/bi';
import './Header.less';
import {
COMPLIANCE_TESTING_RESULT_PAGE,
API_TESTING_RESULT_PAGE,
DATA_PROTECTION_NOTICE_PAGE,
IMPRINT_PAGE
} from '../../service/constants';
import useTranslations from '../../hooks/useTranslation';
import HeaderMenuButton from './HeaderMenuButton';
import { syncGitBookBBRequirements as syncGitBookBBRequirementsAPI } from '../../service/serviceAPI';
const Header = () => {
const router = useRouter();
const { format } = useTranslations();
const [isLoggedIn, setIsLoggedIn] = useState(false);
const [isSyncing, setIsSyncing] = useState(false);
useEffect(() => {
const token = sessionStorage.getItem('accessToken');
setIsLoggedIn(!!token);
});
const handleBackToHomePage = () => {
router.push('/');
};
const goToDataProtectionNoticePage = () => router.push(DATA_PROTECTION_NOTICE_PAGE);
const goToImprintPage = () => router.push(IMPRINT_PAGE);
const handleLogin = () => {
const apiUrl = process.env.API_URL;
window.location.href = `${apiUrl}/auth/github`;
};
const handleLogout = () => {
sessionStorage.clear();
setIsLoggedIn(false);
router.push('/');
};
const syncGitBookBBRequirements = async () => {
if (isSyncing) return; // Prevent multiple simultaneous requests
setIsSyncing(true);
try {
await syncGitBookBBRequirementsAPI();
console.log('Building block requirements synced successfully');
// Optionally refresh the page or show a success notification
} catch (error) {
console.error('Failed to sync building block requirements:', error);
// You could add a toast notification here for better UX
} finally {
setIsSyncing(false);
}
};
const handleHelpClick = () => {
const slackChannelUrl = process.env.SLACK_CHANNEL_URL;
window.open(slackChannelUrl, '_blank');
};
const currentPath = router.pathname;
return (
<div className="header">
<div
className="header-logo"
onClick={handleBackToHomePage}
data-testid="logo"
>
<img src="/images/logo.png" alt="logo" />
</div>
<div className="header-right-section">
<div className="header-link-button-section">
<HeaderMenuButton
buttonTitle={format('app.software_requirements_compliance.label')}
href={COMPLIANCE_TESTING_RESULT_PAGE}
active={currentPath?.includes(COMPLIANCE_TESTING_RESULT_PAGE)}
/>
<HeaderMenuButton
buttonTitle={format('app.api-testing.label')}
href={API_TESTING_RESULT_PAGE}
active={currentPath?.includes(API_TESTING_RESULT_PAGE)}
/>
</div>
<div className="action-buttons">
<div className="header-login">
{isLoggedIn ? (
<>
<button onClick={handleLogout} className="header-menu-button">
<BiLogOut className="header-icon" />
{format('app.logout.label')}
</button>
<button
onClick={syncGitBookBBRequirements}
className="header-menu-button"
disabled={isSyncing}
>
<BiSync className={`header-icon ${isSyncing ? 'syncing' : ''}`} />
{format('app.sync.label')}
</button>
</>
) : (
<>
<button onClick={handleLogin} className="header-menu-button">
<BiLogIn className="header-icon" />
{format('app.login.label')}
</button>
</>
)}
</div>
<div className="header-help">
<button onClick={handleHelpClick} className="header-menu-button">
<RiQuestionLine className="header-icon" />
{format('app.help.label')}
</button>
</div>
<button onClick={goToImprintPage} className="header-menu-button">
{format('app.imprint')}
</button>
<button onClick={goToDataProtectionNoticePage} className="header-menu-button">
{format('app.data_protection_notice')}
</button>
</div>
</div>
</div>
);
};
export default Header;