-
Notifications
You must be signed in to change notification settings - Fork 160
/
index.tsx
146 lines (141 loc) · 3.69 KB
/
index.tsx
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
import { Badge, Tabs, TabsProps } from "antd";
import styled from "@emotion/styled";
import { FC, useMemo } from "react";
import { useAntdTheme } from "@lepton-dashboard/hooks/use-antd-theme";
import { css } from "@emotion/react";
import { useLocation, useNavigate } from "react-router-dom";
import { useInject } from "@lepton-libs/di";
import { PhotonService } from "@lepton-dashboard/services/photon.service";
import { DeploymentService } from "@lepton-dashboard/services/deployment.service";
import { useStateFromObservable } from "@lepton-libs/hooks/use-state-from-observable";
import {
CarbonIcon,
DeploymentIcon,
PhotonIcon,
} from "@lepton-dashboard/components/icons";
import { NotificationService } from "@lepton-dashboard/services/notification.service";
import { Workspace } from "@carbon/icons-react";
const Container = styled.div`
position: sticky;
padding: 0 24px;
z-index: 2;
flex: 0 0 46px;
top: 0;
.ant-tabs-nav::before {
display: none;
}
.ant-tabs-nav-operations {
display: none !important;
}
`;
const StyledBadge = styled(Badge)`
margin-left: 12px;
top: -1px;
min-width: 20px;
`;
const PhotonLabel: FC = () => {
const theme = useAntdTheme();
const photonService = useInject(PhotonService);
const notificationService = useInject(NotificationService);
const notify = useStateFromObservable(
() => notificationService.photonNotify$,
false
);
const photons = useStateFromObservable(() => photonService.listGroups(), []);
return (
<>
<PhotonIcon />
Photons
<StyledBadge
showZero
size="small"
color={notify ? theme.colorLink : theme.colorTextTertiary}
count={photons.length}
/>
</>
);
};
const DeploymentLabel: FC = () => {
const theme = useAntdTheme();
const notificationService = useInject(NotificationService);
const notify = useStateFromObservable(
() => notificationService.deploymentNotify$,
false
);
const deploymentService = useInject(DeploymentService);
const deployments = useStateFromObservable(
() => deploymentService.list(),
[]
);
return (
<>
<DeploymentIcon />
Deployments
<StyledBadge
showZero
size="small"
color={notify ? theme.colorLink : theme.colorTextTertiary}
count={deployments.length}
/>
</>
);
};
const menuItems: TabsProps["items"] = [
{
label: (
<>
<CarbonIcon icon={<Workspace />} />
Dashboard
</>
),
key: "/dashboard",
},
{
label: <PhotonLabel />,
key: "/photons",
},
{
label: <DeploymentLabel />,
key: "/deployments",
},
];
export const Nav: FC = () => {
const theme = useAntdTheme();
const notificationService = useInject(NotificationService);
const location = useLocation();
const navigate = useNavigate();
const selectedKey = useMemo(() => {
return menuItems.find((item) =>
location.pathname.startsWith(`${item?.key}`)
)?.key;
}, [location.pathname]);
const navigateTo = useMemo(
() => (key: string) => {
if (key === "/deployments") {
notificationService.updateDeploymentNotify();
} else if (key === "/photons") {
notificationService.updatePhotonNotify();
}
navigate(key);
},
[navigate, notificationService]
);
return (
<Container
css={css`
background: ${theme.colorBgContainer};
border-bottom: 1px solid ${theme.colorBorder};
box-shadow: ${theme.boxShadowTertiary};
`}
>
<Tabs
moreIcon={null}
tabBarGutter={32}
tabBarStyle={{ marginBottom: 0 }}
activeKey={selectedKey}
items={menuItems}
onTabClick={(key) => navigateTo(key)}
/>
</Container>
);
};