Skip to content

Commit eccfb9a

Browse files
committed
Add log out functionality
1 parent 46c4d63 commit eccfb9a

File tree

6 files changed

+73
-11
lines changed

6 files changed

+73
-11
lines changed

src/components/Admin/NavSidebar/index.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { usePostHog } from "posthog-js/react";
22
import { useEffect } from "react";
33
import { useTheme } from "styled-components";
4-
import { useAdminDispatch } from "../../../containers/Admin/hooks";
54
import { useLogoutMutation } from "../../../redux/services/auth";
65
import { sendUserActionTrackingEvent } from "../../../utils/actions/sendUserActionTrackingEvent";
76
import { getThemeKind } from "../../common/App/styles";
@@ -15,7 +14,6 @@ export const NavSidebar = () => {
1514
const theme = useTheme();
1615
const themeKind = getThemeKind(theme);
1716
const [logout, result] = useLogoutMutation();
18-
const dispatch = useAdminDispatch();
1917
const posthog = usePostHog();
2018

2119
const handleLogoLinkClick = () => {
@@ -44,7 +42,7 @@ export const NavSidebar = () => {
4442
window.clearTimeout(timeoutId);
4543
}
4644
};
47-
}, [posthog, result.isSuccess, dispatch]);
45+
}, [posthog, result.isSuccess]);
4846

4947
return (
5048
<s.Sidebar>

src/components/Agentic/Sidebar/index.tsx

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
import { useMemo } from "react";
1+
import { usePostHog } from "posthog-js/react";
2+
import { useEffect, useMemo, useState } from "react";
23
import { useNavigate, useParams } from "react-router";
34
import { useTheme } from "styled-components";
45
import { useAgenticSelector } from "../../../containers/Agentic/hooks";
6+
import { useLogoutMutation } from "../../../redux/services/auth";
57
import { useGetIncidentsQuery } from "../../../redux/services/digma";
68
import type { IncidentResponseItem } from "../../../redux/services/types";
79
import { sendUserActionTrackingEvent } from "../../../utils/actions/sendUserActionTrackingEvent";
810
import { getThemeKind } from "../../common/App/styles";
11+
import { LogoutIcon } from "../../common/icons/16px/LogoutIcon";
12+
import { NewPopover } from "../../common/NewPopover";
913
import { Tooltip } from "../../common/v3/Tooltip";
14+
import { MenuList } from "../../Navigation/common/MenuList";
15+
import { Popup } from "../../Navigation/common/Popup";
1016
import { trackingEvents } from "../tracking";
1117
import * as s from "./styles";
1218

@@ -22,11 +28,15 @@ export const Sidebar = () => {
2228
const params = useParams();
2329
const incidentId = params.id;
2430
const navigate = useNavigate();
31+
const [isUserMenuOpen, setIsUserMenuOpen] = useState(false);
32+
const posthog = usePostHog();
2533

2634
const { data } = useGetIncidentsQuery(undefined, {
2735
pollingInterval: REFRESH_INTERVAL
2836
});
2937

38+
const [logout, result] = useLogoutMutation();
39+
3040
const handleLogoLinkClick = () => {
3141
sendUserActionTrackingEvent(trackingEvents.LOGO_LINK_CLICKED);
3242
};
@@ -39,6 +49,34 @@ export const Sidebar = () => {
3949
sendUserActionTrackingEvent(trackingEvents.TEMPLATE_BUTTON_CLICKED);
4050
};
4151

52+
const handleLogoutMenuItemClick = () => {
53+
sendUserActionTrackingEvent(trackingEvents.LOGOUT_MENU_ITEM_CLICKED);
54+
void logout();
55+
};
56+
57+
const handleUserMenuOpenChange = (isOpen: boolean) => {
58+
setIsUserMenuOpen(isOpen);
59+
};
60+
61+
useEffect(() => {
62+
let timeoutId: number | null = null;
63+
64+
if (result.isSuccess) {
65+
if (posthog.__loaded) {
66+
posthog.reset();
67+
}
68+
timeoutId = window.setTimeout(() => {
69+
window.location.reload();
70+
}, 500);
71+
}
72+
73+
return () => {
74+
if (timeoutId) {
75+
window.clearTimeout(timeoutId);
76+
}
77+
};
78+
}, [posthog, result.isSuccess]);
79+
4280
const sortedIncidents = useMemo(
4381
() =>
4482
[...(data?.items ?? [])].sort(
@@ -84,10 +122,30 @@ export const Sidebar = () => {
84122
onClick={handleTemplateButtonClick}
85123
isDisabled={true}
86124
/>
87-
<s.UserInfo>
88-
<s.Avatar>{userInitial}</s.Avatar>
89-
{user?.email ?? ""}
90-
</s.UserInfo>
125+
<NewPopover
126+
content={
127+
<Popup>
128+
<MenuList
129+
items={[
130+
{
131+
id: "logout",
132+
icon: <LogoutIcon size={16} color={"currentColor"} />,
133+
label: "Log out",
134+
onClick: handleLogoutMenuItemClick
135+
}
136+
]}
137+
/>
138+
</Popup>
139+
}
140+
onOpenChange={handleUserMenuOpenChange}
141+
isOpen={isUserMenuOpen}
142+
placement={"bottom-start"}
143+
>
144+
<s.UserInfo>
145+
<s.Avatar>{userInitial}</s.Avatar>
146+
{user?.email ?? ""}
147+
</s.UserInfo>
148+
</NewPopover>
91149
</s.Container>
92150
);
93151
};

src/components/Agentic/Sidebar/styles.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export const TemplateButton = styled(NewButton)`
105105
justify-content: center;
106106
`;
107107

108-
export const UserInfo = styled.div`
108+
export const UserInfo = styled.button`
109109
${subscriptRegularTypography};
110110
display: flex;
111111
align-items: center;
@@ -116,9 +116,12 @@ export const UserInfo = styled.div`
116116
text-overflow: ellipsis;
117117
white-space: nowrap;
118118
overflow: hidden;
119+
background: none;
120+
border: none;
121+
cursor: pointer;
119122
`;
120123

121-
export const Avatar = styled.div`
124+
export const Avatar = styled.span`
122125
width: 28px;
123126
height: 28px;
124127
border-radius: 6px;

src/components/Agentic/tracking.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export const trackingEvents = addPrefix(
55
APP_ID,
66
{
77
LOGO_LINK_CLICKED: "logo link clicked",
8-
TEMPLATE_BUTTON_CLICKED: "template button clicked"
8+
TEMPLATE_BUTTON_CLICKED: "template button clicked",
9+
LOGOUT_MENU_ITEM_CLICKED: "logout menu item clicked"
910
},
1011
" "
1112
);

src/components/Navigation/common/MenuList/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { MenuListProps } from "./types";
77

88
const UNGROUPED_GROUP_LABEL = "__ungrouped";
99

10+
// TODO: move to common
1011
export const MenuList = ({
1112
showGroupNames = true,
1213
items,

src/components/Navigation/common/Popup/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as s from "./styles";
22
import type { PopupProps } from "./types";
33

4+
// TODO: move to common
45
export const Popup = ({ className, height, header, children }: PopupProps) => (
56
<s.Container className={className} $height={height}>
67
{header && <div>{header}</div>}

0 commit comments

Comments
 (0)