Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ibm
GitHub Repository: ibm/watson-machine-learning-samples
Path: blob/master/cloud/ai-service-apps/nextjs-carbon-react-ui/src/components/AppHeader/ProfilePanel.js
6408 views
1
import React, { useState } from "react";
2
import { HeaderGlobalAction, HeaderPanel } from "@carbon/react";
3
import { useAppContext } from "@/contexts/app-context";
4
import { useOutsideClick } from "../../hooks/useOutsideClick";
5
import { getUserInitials, mapUserColor } from "../../utils/user-util";
6
import ThemeToggle from "../ThemeToggle/ThemeToggle";
7
8
const profilePanelContentId = "profile-panel-content";
9
10
const ProfilePanel = () => {
11
const [isExpanded, setIsExpanded] = useState(false);
12
const { userData } = useAppContext();
13
const { givenName, familyName } = userData;
14
15
const userName = userData?.name;
16
const refHeaderProfileAction = useOutsideClick(
17
() => setIsExpanded(false),
18
[profilePanelContentId]
19
);
20
const userInitials = getUserInitials({ givenName, familyName });
21
const userColor = mapUserColor(userName);
22
23
return (
24
<>
25
{userName && (
26
<>
27
<HeaderGlobalAction
28
aria-label="User profile"
29
className="profile-panel__action"
30
onClick={() => {
31
setIsExpanded((currentValue) => !currentValue);
32
}}
33
ref={refHeaderProfileAction}
34
>
35
<div className="profile-panel__actionUserAvatar" style={{ backgroundColor: userColor }}>
36
{userInitials}
37
</div>
38
</HeaderGlobalAction>
39
<HeaderPanel className="profile-panel__content" expanded={isExpanded}>
40
<div id={profilePanelContentId} className="profile-panel__contentWrapper">
41
<div className="profile-panel__appSection">
42
<div className="profile-panel__version">Version {process.env.version}</div>
43
<div className="profile-panel__themeSection">
44
<ThemeToggle isExpanded={isExpanded} />
45
</div>
46
</div>
47
<div className="profile-panel__userSection">
48
<div className="profile-panel__userAvatar" style={{ backgroundColor: userColor }}>
49
{userInitials}
50
</div>
51
<div className="profile-panel__info">
52
<div className="profile-panel__userName">{userName}</div>
53
</div>
54
</div>
55
</div>
56
</HeaderPanel>
57
</>
58
)}
59
</>
60
);
61
};
62
63
export default ProfilePanel;
64
65