/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import React, {useEffect, useState} from 'react'; import {Button, Drawer, Dropdown, Grid, Layout, Menu, Space} from 'antd'; import {BgColorsOutlined, DownOutlined, GlobalOutlined, MenuOutlined, UserOutlined} from '@ant-design/icons'; import {useLocation, useNavigate} from 'react-router-dom'; import {useLanguage} from '../i18n/LanguageContext'; import {useTheme} from "../store/context/ThemeContext"; import {remoteApi} from "../api/remoteApi/remoteApi"; const {Header} = Layout; const {useBreakpoint} = Grid; // Used to determine screen breakpoints const Navbar = ({rmqVersion = true, showAcl = true}) => { const location = useLocation(); const navigate = useNavigate(); const {lang, setLang, t} = useLanguage(); const screens = useBreakpoint(); // Get current screen size breakpoints const {currentThemeName, setCurrentThemeName} = useTheme(); const [userName, setUserName] = useState(null); const [drawerVisible, setDrawerVisible] = useState(false); // Controls drawer visibility // Get selected menu item key based on current route path const getPath = () => location.pathname.replace('/', ''); const handleMenuClick = ({key}) => { navigate(`/${key}`); setDrawerVisible(false); // Close drawer after clicking a menu item }; const onLogout = () => { remoteApi.logout().then(res => { if (res.status === 0) { window.localStorage.removeItem("username"); window.localStorage.removeItem("userRole"); window.localStorage.removeItem("token"); window.localStorage.removeItem("rmqVersion"); navigate('/login'); } else { console.error('Logout failed:', res.message) navigate('/login'); } }) }; useEffect(() => { const storedUsername = window.localStorage.getItem("username"); if (storedUsername) { setUserName(storedUsername); } else { setUserName(null); } }, []); const langMenu = ( setLang(key)}> {t.ENGLISH} {t.CHINESE} ); const userMenu = ( {t.LOGOUT} ); const themeMenu = ( setCurrentThemeName(key)}> {t.BLUE} ({t.DEFAULT}) {t.PINK} {t.GREEN} ); // Menu item configuration const menuItems = [ {key: 'ops', label: t.OPS}, ...(rmqVersion ? [{key: 'proxy', label: t.PROXY}] : []), {key: '', label: t.DASHBOARD}, // Dashboard corresponds to root path {key: 'cluster', label: t.CLUSTER}, {key: 'topic', label: t.TOPIC}, {key: 'consumer', label: t.CONSUMER}, {key: 'producer', label: t.PRODUCER}, {key: 'message', label: t.MESSAGE}, {key: 'dlqMessage', label: t.DLQ_MESSAGE}, {key: 'messageTrace', label: t.MESSAGETRACE}, ...(showAcl ? [{key: 'acl', label: t.ACL_MANAGEMENT}] : []), ]; // Determine if it's a small screen (e.g., less than md) const isSmallScreen = !screens.md; // Determine if it's an extra small screen (e.g., less than sm) const isExtraSmallScreen = !screens.sm; return (
{t.TITLE}
{!isSmallScreen && ( // Display full menu on large screens )}
{/* Adjust spacing for buttons */} {/* Theme switch button */} {userName && ( {/* 使用一个可点击的元素作为 Dropdown 的唯一子元素 */} e.preventDefault()} style={{display: 'flex', alignItems: 'center'}}> {/* 添加一些间距 */} {userName} )} {isSmallScreen && ( // Display hamburger icon on small screens
); }; export default Navbar;