From bf862a52a75ce2551d75410d15b09559f71796f4 Mon Sep 17 00:00:00 2001 From: Sven Czarnian Date: Sat, 5 Nov 2022 02:14:49 +0100 Subject: [PATCH] handle the active theme and use MenuItem for the tree --- src/components/navbar.tsx | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/components/navbar.tsx b/src/components/navbar.tsx index 41116d4..94a672f 100644 --- a/src/components/navbar.tsx +++ b/src/components/navbar.tsx @@ -1,17 +1,21 @@ import { Menubar } from 'primereact/menubar'; +import { MenuItem } from 'primereact/menuitem'; import React, { useContext, useEffect, useRef, useState } from 'react'; import { useNavigate } from 'react-router-dom'; -import { AuthContext } from '../contexts'; +import { AuthContext, ThemeContext } from '../contexts'; import { Configuration } from '../services'; import { Airport } from '../services'; import { AirportOverview, BackendReturnStatus, IAuthState } from '../types'; export const NavBar: React.FC = () => { const [menuTree, setMenuTree] = useState(undefined); + const themeContext = useContext(ThemeContext); const authContext = useContext(AuthContext); const currentAuth = useRef(); + const currentTheme = useRef(); const navigate = useNavigate(); + currentTheme.current = themeContext.darkMode; currentAuth.current = authContext.auth; const firBasedSubMenu = (airports: AirportOverview[], endpoint: string): any[] => { @@ -63,10 +67,10 @@ export const NavBar: React.FC = () => { Airport.all().then((response) => { if (response.status !== BackendReturnStatus.Ok) return []; - const newMenuTree: { label: string; items?: any[]; command?: () => void }[] = [ + const newMenuTree: MenuItem[] = [ { label: 'Airports', - items: [] as any[], + items: [], } ]; @@ -103,6 +107,14 @@ export const NavBar: React.FC = () => { }); } + newMenuTree.push({ + label: currentTheme.current ? 'Light mode' : 'Dark mode', + command: () => { + themeContext.setDarkMode(!currentTheme.current); + updateMenuItems(); + }, + }); + newMenuTree.push({ label: 'Logout', command: () => navigate('/logout'), @@ -135,5 +147,9 @@ export const NavBar: React.FC = () => { if (menuTree === undefined || !currentAuth.current.valid) return <>; - return (menuTree.length !== 0 ? : <>); + return ( + menuTree.length !== 0 ? + + : <> + ); };