diff --git a/clients/ui/frontend/src/app/App.tsx b/clients/ui/frontend/src/app/App.tsx index d3fdb404..f20b4743 100644 --- a/clients/ui/frontend/src/app/App.tsx +++ b/clients/ui/frontend/src/app/App.tsx @@ -5,23 +5,11 @@ import { Alert, Bullseye, Button, - Dropdown, - DropdownItem, - DropdownList, - Masthead, - MastheadContent, - MastheadMain, - MenuToggle, - MenuToggleElement, Page, PageSection, Spinner, Stack, StackItem, - Toolbar, - ToolbarContent, - ToolbarGroup, - ToolbarItem, } from '@patternfly/react-core'; import ToastNotifications from '~/shared/components/ToastNotifications'; import { useSettings } from '~/shared/hooks/useSettings'; @@ -30,8 +18,7 @@ import NavSidebar from './NavSidebar'; import AppRoutes from './AppRoutes'; import { AppContext } from './AppContext'; import { ModelRegistrySelectorContextProvider } from './context/ModelRegistrySelectorContext'; -import { Select } from '@mui/material'; -import { SimpleSelect, SimpleSelectOption } from '@patternfly/react-templates'; +import NavBar from './NavBar'; const App: React.FC = () => { const { @@ -54,7 +41,7 @@ const App: React.FC = () => { React.useEffect(() => { // Add the user to localStorage if in PoC - // TODO: [Env Handling] Remove this when auth is enabled + // TODO: [Env Handling] Just add this logic in PoC mode if (username) { localStorage.setItem(USER_ID, username); } else { @@ -73,19 +60,6 @@ const App: React.FC = () => { [configSettings, userSettings], ); - const handleLogout = () => { - setUserMenuOpen(false); - // TODO: [Auth-enablement] Logout when auth is enabled - }; - - - const [userMenuOpen, setUserMenuOpen] = React.useState(false); - const userMenuItems = [ - - Log out - , - ]; - // We lack the critical data to startup the app if (configError) { // There was an error fetching critical data @@ -118,62 +92,6 @@ const App: React.FC = () => { // Waiting on the API to finish const loading = !configLoaded || !userSettings || !configSettings || !contextValue; - const Options: SimpleSelectOption[] = [ - { content: 'All Namespaces', value: 'All' }, - ]; - - const [selected, setSelected] = React.useState('All'); - - const initialOptions = React.useMemo( - () => Options.map((o) => ({ ...o, selected: o.value === selected })), - [selected] - ); - - const masthead = ( - - - - - - - - setSelected(String(selection))} - > - - - - - - {/* TODO: [Auth-enablement] Add logout button */} - setUserMenuOpen(isOpen)} - toggle={(toggleRef: React.Ref) => ( - setUserMenuOpen(!userMenuOpen)} - isExpanded={userMenuOpen} - > - {username} - - )} - isOpen={userMenuOpen} - > - {userMenuItems} - - - - - - - - ); - return loading ? ( @@ -182,7 +100,14 @@ const App: React.FC = () => { { + //TODO: [Auth-enablement] Logout when auth is enabled + }} + /> + } isManagedSidebar sidebar={} > diff --git a/clients/ui/frontend/src/app/AppRoutes.tsx b/clients/ui/frontend/src/app/AppRoutes.tsx index 7dbf30b9..122102c8 100644 --- a/clients/ui/frontend/src/app/AppRoutes.tsx +++ b/clients/ui/frontend/src/app/AppRoutes.tsx @@ -3,6 +3,7 @@ import { Navigate, Route, Routes } from 'react-router-dom'; import { NotFound } from './pages/notFound/NotFound'; import ModelRegistrySettingsRoutes from './pages/settings/ModelRegistrySettingsRoutes'; import ModelRegistryRoutes from './pages/modelRegistry/ModelRegistryRoutes'; +import { useAppContext } from './AppContext'; export const isNavDataGroup = (navItem: NavDataItem): navItem is NavDataGroup => 'children' in navItem; @@ -22,11 +23,8 @@ export type NavDataGroup = NavDataCommon & { type NavDataItem = NavDataHref | NavDataGroup; export const useAdminSettings = (): NavDataItem[] => { - // get auth access for example set admin as true - const isAdmin = true; //this should be a call to getting auth / role access + const { isAdmin } = useAppContext().user; - // TODO: [Auth-enablement] Remove the linter skip when we implement authentication - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition if (!isAdmin) { return []; } @@ -48,20 +46,16 @@ export const useNavData = (): NavDataItem[] => [ ]; const AppRoutes: React.FC = () => { - const isAdmin = true; + const { isAdmin } = useAppContext().user; return ( } /> } /> } /> - { - // TODO: [Auth-enablement] Remove the linter skip when we implement authentication - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition - isAdmin && ( - } /> - ) - } + {isAdmin && ( + } /> + )} ); }; diff --git a/clients/ui/frontend/src/app/NavBar.tsx b/clients/ui/frontend/src/app/NavBar.tsx new file mode 100644 index 00000000..62876c85 --- /dev/null +++ b/clients/ui/frontend/src/app/NavBar.tsx @@ -0,0 +1,91 @@ +import React from 'react'; +import { + Dropdown, + DropdownItem, + DropdownList, + Masthead, + MastheadContent, + MastheadMain, + MenuToggle, + MenuToggleElement, + Toolbar, + ToolbarContent, + ToolbarGroup, + ToolbarItem, +} from '@patternfly/react-core'; +import { SimpleSelect, SimpleSelectOption } from '@patternfly/react-templates'; + +interface NavBarProps { + username?: string; + onLogout: () => void; +} + +const Options: SimpleSelectOption[] = [{ content: 'All Namespaces', value: 'All' }]; + +const NavBar: React.FC = ({ username, onLogout }) => { + const [selected, setSelected] = React.useState('All'); + const [userMenuOpen, setUserMenuOpen] = React.useState(false); + + const initialOptions = React.useMemo( + () => Options.map((o) => ({ ...o, selected: o.value === selected })), + [selected], + ); + + const handleLogout = () => { + setUserMenuOpen(false); + onLogout(); + }; + + const userMenuItems = [ + + Log out + , + ]; + + return ( + + + + + + + + setSelected(String(selection))} + /> + + + {username && ( + + + setUserMenuOpen(isOpen)} + toggle={(toggleRef: React.Ref) => ( + setUserMenuOpen(!userMenuOpen)} + isExpanded={userMenuOpen} + > + {username} + + )} + isOpen={userMenuOpen} + > + {userMenuItems} + + + + )} + + + + + ); +}; + +export default NavBar; diff --git a/clients/ui/frontend/src/shared/hooks/useSettings.tsx b/clients/ui/frontend/src/shared/hooks/useSettings.tsx index 196f1ac2..648ef119 100644 --- a/clients/ui/frontend/src/shared/hooks/useSettings.tsx +++ b/clients/ui/frontend/src/shared/hooks/useSettings.tsx @@ -75,4 +75,5 @@ export const fetchConfig = async (): Promise => ({ // TODO: [Auth-enablement] replace with the actual call once we have the endpoint export const fetchUser = async (): Promise => ({ username: 'user@example.com', + isAdmin: true, }); diff --git a/clients/ui/frontend/src/shared/types.ts b/clients/ui/frontend/src/shared/types.ts index f442960f..6ef4c3c9 100644 --- a/clients/ui/frontend/src/shared/types.ts +++ b/clients/ui/frontend/src/shared/types.ts @@ -3,6 +3,7 @@ import { ValueOf } from '~/shared/typeHelpers'; // TODO: [Data Flow] Get the status config params export type UserSettings = { username: string; + isAdmin?: boolean; }; // TODO: [Data Flow] Add more config parameters