2022-04-05 18:21:25 +05:30
|
|
|
import { CheckCircleTwoTone, WarningOutlined } from '@ant-design/icons';
|
2023-06-08 10:49:33 +03:00
|
|
|
import { Menu, MenuProps } from 'antd';
|
2022-03-22 16:22:02 +05:30
|
|
|
import getLocalStorageKey from 'api/browser/localstorage/get';
|
|
|
|
|
import { IS_SIDEBAR_COLLAPSED } from 'constants/app';
|
2021-09-23 15:43:43 +05:30
|
|
|
import ROUTES from 'constants/routes';
|
2021-09-28 18:50:10 +05:30
|
|
|
import history from 'lib/history';
|
2023-05-19 13:14:32 +05:30
|
|
|
import {
|
|
|
|
|
ReactNode,
|
|
|
|
|
useCallback,
|
|
|
|
|
useLayoutEffect,
|
|
|
|
|
useMemo,
|
|
|
|
|
useState,
|
|
|
|
|
} from 'react';
|
2022-04-05 18:21:25 +05:30
|
|
|
import { useTranslation } from 'react-i18next';
|
2022-05-03 15:27:09 +05:30
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
|
import { useLocation } from 'react-router-dom';
|
2022-03-22 16:22:02 +05:30
|
|
|
import { SideBarCollapse } from 'store/actions/app';
|
2021-09-28 18:50:10 +05:30
|
|
|
import { AppState } from 'store/reducers';
|
|
|
|
|
import AppReducer from 'types/reducer/app';
|
2021-09-23 15:43:43 +05:30
|
|
|
|
2023-04-20 18:14:32 +05:30
|
|
|
import { routeConfig, styles } from './config';
|
|
|
|
|
import { getQueryString } from './helper';
|
2021-09-23 15:43:43 +05:30
|
|
|
import menus from './menuItems';
|
2022-02-12 15:01:38 +05:30
|
|
|
import Slack from './Slack';
|
2022-03-22 12:10:31 +05:30
|
|
|
import {
|
2022-04-05 18:21:25 +05:30
|
|
|
RedDot,
|
2022-03-22 12:10:31 +05:30
|
|
|
Sider,
|
|
|
|
|
SlackButton,
|
|
|
|
|
SlackMenuItemContainer,
|
2022-04-05 18:21:25 +05:30
|
|
|
VersionContainer,
|
2022-03-22 12:10:31 +05:30
|
|
|
} from './styles';
|
2021-08-27 12:13:32 +05:30
|
|
|
|
2022-05-03 15:27:09 +05:30
|
|
|
function SideNav(): JSX.Element {
|
2022-03-22 16:22:02 +05:30
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const [collapsed, setCollapsed] = useState<boolean>(
|
|
|
|
|
getLocalStorageKey(IS_SIDEBAR_COLLAPSED) === 'true',
|
|
|
|
|
);
|
2022-05-03 15:27:09 +05:30
|
|
|
const { currentVersion, latestVersion, isCurrentVersionError } = useSelector<
|
|
|
|
|
AppState,
|
|
|
|
|
AppReducer
|
|
|
|
|
>((state) => state.app);
|
2021-08-27 12:13:32 +05:30
|
|
|
|
2023-04-20 18:14:32 +05:30
|
|
|
const { pathname, search } = useLocation();
|
2023-06-08 10:49:33 +03:00
|
|
|
|
2022-04-05 18:21:25 +05:30
|
|
|
const { t } = useTranslation('');
|
2022-03-22 21:56:12 +05:30
|
|
|
|
2021-08-27 12:13:32 +05:30
|
|
|
const onCollapse = useCallback(() => {
|
|
|
|
|
setCollapsed((collapsed) => !collapsed);
|
|
|
|
|
}, []);
|
|
|
|
|
|
2022-03-22 16:22:02 +05:30
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
dispatch(SideBarCollapse(collapsed));
|
2022-03-22 21:56:12 +05:30
|
|
|
}, [collapsed, dispatch]);
|
2022-03-22 16:22:02 +05:30
|
|
|
|
2021-10-20 09:24:55 +05:30
|
|
|
const onClickHandler = useCallback(
|
2023-08-02 11:22:24 +05:30
|
|
|
(key: string) => {
|
2023-04-20 18:14:32 +05:30
|
|
|
const params = new URLSearchParams(search);
|
2023-08-02 11:22:24 +05:30
|
|
|
const availableParams = routeConfig[key];
|
2023-04-20 18:14:32 +05:30
|
|
|
|
2023-06-08 10:49:33 +03:00
|
|
|
const queryString = getQueryString(availableParams || [], params);
|
2023-04-20 18:14:32 +05:30
|
|
|
|
2023-08-02 11:22:24 +05:30
|
|
|
if (pathname !== key) {
|
|
|
|
|
history.push(`${key}?${queryString.join('&')}`);
|
2021-10-20 09:24:55 +05:30
|
|
|
}
|
|
|
|
|
},
|
2023-04-20 18:14:32 +05:30
|
|
|
[pathname, search],
|
2021-10-20 09:24:55 +05:30
|
|
|
);
|
2021-09-23 15:43:43 +05:30
|
|
|
|
2023-06-08 10:49:33 +03:00
|
|
|
const onClickMenuHandler: MenuProps['onClick'] = (e) => {
|
|
|
|
|
onClickHandler(e.key);
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-22 16:22:02 +05:30
|
|
|
const onClickSlackHandler = (): void => {
|
2022-02-12 15:01:38 +05:30
|
|
|
window.open('https://signoz.io/slack', '_blank');
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-05 18:21:25 +05:30
|
|
|
const onClickVersionHandler = (): void => {
|
|
|
|
|
history.push(ROUTES.VERSION);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const isNotCurrentVersion = currentVersion !== latestVersion;
|
|
|
|
|
|
2023-03-03 12:09:24 +01:00
|
|
|
const sidebar: SidebarItem[] = [
|
2022-04-05 18:21:25 +05:30
|
|
|
{
|
|
|
|
|
onClick: onClickSlackHandler,
|
|
|
|
|
icon: <Slack />,
|
|
|
|
|
text: <SlackButton>Support</SlackButton>,
|
2023-03-03 12:09:24 +01:00
|
|
|
key: 'slack',
|
2022-04-05 18:21:25 +05:30
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
onClick: onClickVersionHandler,
|
2023-03-03 12:09:24 +01:00
|
|
|
key: 'version',
|
2022-04-05 18:21:25 +05:30
|
|
|
icon: isNotCurrentVersion ? (
|
|
|
|
|
<WarningOutlined style={{ color: '#E87040' }} />
|
|
|
|
|
) : (
|
|
|
|
|
<CheckCircleTwoTone twoToneColor={['#D5F2BB', '#1f1f1f']} />
|
|
|
|
|
),
|
|
|
|
|
text: (
|
|
|
|
|
<VersionContainer>
|
|
|
|
|
{!isCurrentVersionError ? (
|
|
|
|
|
<SlackButton>{currentVersion}</SlackButton>
|
|
|
|
|
) : (
|
|
|
|
|
<SlackButton>{t('n_a')}</SlackButton>
|
|
|
|
|
)}
|
|
|
|
|
{isNotCurrentVersion && <RedDot />}
|
|
|
|
|
</VersionContainer>
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
2023-06-08 10:49:33 +03:00
|
|
|
const currentMenu = useMemo(() => {
|
|
|
|
|
const routeKeys = Object.keys(ROUTES) as (keyof typeof ROUTES)[];
|
|
|
|
|
const currentRouteKey = routeKeys.find((key) => {
|
|
|
|
|
const route = ROUTES[key];
|
|
|
|
|
return pathname === route;
|
|
|
|
|
});
|
2022-12-30 01:10:02 +05:30
|
|
|
|
2023-06-08 10:49:33 +03:00
|
|
|
if (!currentRouteKey) return null;
|
|
|
|
|
|
|
|
|
|
return ROUTES[currentRouteKey];
|
|
|
|
|
}, [pathname]);
|
2023-03-03 12:09:24 +01:00
|
|
|
|
|
|
|
|
const sidebarItems = (props: SidebarItem, index: number): SidebarItem => ({
|
|
|
|
|
key: `${index}`,
|
|
|
|
|
icon: props.icon,
|
|
|
|
|
onClick: props.onClick,
|
|
|
|
|
label: props.text,
|
|
|
|
|
});
|
|
|
|
|
|
2021-08-27 12:13:32 +05:30
|
|
|
return (
|
2021-09-28 18:50:10 +05:30
|
|
|
<Sider collapsible collapsed={collapsed} onCollapse={onCollapse} width={200}>
|
2021-08-27 12:13:32 +05:30
|
|
|
<Menu
|
|
|
|
|
theme="dark"
|
|
|
|
|
defaultSelectedKeys={[ROUTES.APPLICATION]}
|
2023-06-08 10:49:33 +03:00
|
|
|
selectedKeys={currentMenu ? [currentMenu] : []}
|
2023-03-23 14:50:17 +05:30
|
|
|
mode="vertical"
|
2023-01-11 14:39:06 +05:30
|
|
|
style={styles}
|
2023-06-08 10:49:33 +03:00
|
|
|
items={menus}
|
|
|
|
|
onClick={onClickMenuHandler}
|
2023-03-03 12:09:24 +01:00
|
|
|
/>
|
|
|
|
|
{sidebar.map((props, index) => (
|
|
|
|
|
<SlackMenuItemContainer
|
|
|
|
|
index={index + 1}
|
|
|
|
|
key={`${index + 1}`}
|
|
|
|
|
collapsed={collapsed}
|
|
|
|
|
>
|
|
|
|
|
<Menu
|
|
|
|
|
theme="dark"
|
|
|
|
|
defaultSelectedKeys={[ROUTES.APPLICATION]}
|
2023-06-08 10:49:33 +03:00
|
|
|
selectedKeys={currentMenu ? [currentMenu] : []}
|
2023-03-03 12:09:24 +01:00
|
|
|
mode="inline"
|
|
|
|
|
style={styles}
|
|
|
|
|
items={[sidebarItems(props, index)]}
|
|
|
|
|
/>
|
|
|
|
|
</SlackMenuItemContainer>
|
|
|
|
|
))}
|
2021-08-27 12:13:32 +05:30
|
|
|
</Sider>
|
|
|
|
|
);
|
2022-03-22 12:10:31 +05:30
|
|
|
}
|
2021-08-27 12:13:32 +05:30
|
|
|
|
2023-03-03 12:09:24 +01:00
|
|
|
interface SidebarItem {
|
|
|
|
|
onClick: VoidFunction;
|
2023-05-19 13:14:32 +05:30
|
|
|
icon?: ReactNode;
|
|
|
|
|
text?: ReactNode;
|
2023-03-03 12:09:24 +01:00
|
|
|
key: string;
|
2023-05-19 13:14:32 +05:30
|
|
|
label?: ReactNode;
|
2023-03-03 12:09:24 +01:00
|
|
|
}
|
|
|
|
|
|
2022-05-03 15:27:09 +05:30
|
|
|
export default SideNav;
|