Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI make navbar options configurable and filter menu items according to user groups #3154

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions salt/metalk8s/addons/ui/deployed/ui.sls.in
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ Create shell-ui ConfigMap:
"clientId": "metalk8s-ui",
"responseType": "id_token",
"scopes": "openid profile email groups offline_access audience:server:client_id:oidc-auth-client"
},
"options": {
"main": {
"https://{{ ingress_control_plane }}/":{ "en": "Platform", "fr": "Plateforme" },
"https://{{ ingress_control_plane }}/alerts":{ "en": "Alerts", "fr": "Alertes" }
},
"subLogin": {
"https://{{ ingress_control_plane }}/docs":{ "en": "Documentation", "fr": "Documentation" }
}
}
}

Expand Down
31 changes: 29 additions & 2 deletions shell-ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion shell-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
},
"dependencies": {
"@fortawesome/fontawesome-free": "^5.15.1",
"@scality/core-ui": "github:scality/core-ui.git#v0.7.1",
"@scality/core-ui": "github:scality/core-ui.git#v0.13.0",
"oidc-client": "^1.11.3",
"oidc-react": "^1.1.5",
"polished": "^4.0.5",
"quicklink": "^2.1.0",
"react": "^17.0.1",
"react-debounce-input": "^3.2.3",
"react-dom": "^17.0.1",
Expand Down
7 changes: 7 additions & 0 deletions shell-ui/public/shell/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,12 @@
"clientId": "metalk8s-ui",
"responseType": "id_token",
"scopes": "openid profile email groups offline_access audience:server:client_id:oidc-auth-client"
},
"options": {
"main": {
"http://localhost:8082/":{ "en": "Platform", "fr": "Plateforme" },
"http://localhost:8082/test":{ "en": "Test", "fr": "Test" }
},
"subLogin": {}
}
}
130 changes: 84 additions & 46 deletions shell-ui/src/NavBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,101 @@
import CoreUINavbar from '@scality/core-ui/dist/components/navbar/Navbar.component';
import { useAuth } from 'oidc-react';
import { useLayoutEffect, useState } from 'react';
import type { SolutionsNavbarProps } from './index';
import type {
Options,
SolutionsNavbarProps,
TranslationAndGroups,
} from './index';
import type { Node } from 'react';
import { logOut } from './auth/logout';
import {
getAccessiblePathsFromOptions,
isEntryAccessibleByTheUser,
normalizePath,
} from './auth/permissionUtils';
import { prefetch } from 'quicklink';

export const LoadingNavbar = (): Node => <CoreUINavbar role="navigation" tabs={[{title: 'loading'}]} />;
export const LoadingNavbar = (): Node => (
<CoreUINavbar role="navigation" tabs={[{ title: 'loading' }]} />
);

export const Navbar = ({
options,
}: {
options: $PropertyType<SolutionsNavbarProps, 'options'>,
}): Node => {
const translateOptionsToMenu = (
options: Options,
section: 'main' | 'subLogin',
renderer: (
path: string,
translationAndGroup: TranslationAndGroups,
) => { link: Node } | { label: string, onClick: () => void },
userGroups: string[],
) => {
const normalizedLocation = normalizePath(location.href);
return (
Object.entries(options[section])
//$FlowIssue - flow typing for Object.entries incorrectly typing values as [string, mixed] instead of [string, TranslationAndGroups]
.filter((entry: [string, TranslationAndGroups]) =>
isEntryAccessibleByTheUser(entry, userGroups),
)
.map(
//$FlowIssue - flow typing for Object.entries incorrectly typing values as [string, mixed] instead of [string, TranslationAndGroups]
([path, translationAndGroup]: [string, TranslationAndGroups], i) => {
try {
return {
...renderer(path, translationAndGroup),
selected: translationAndGroup.activeIfMatches
? new RegExp(translationAndGroup.activeIfMatches).test(
location.href,
)
: normalizedLocation === normalizePath(path),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid this default isn't really useful, since it will never match on "sub-paths"...
Maybe normalizedLocation.startsWith(normalizePath(path)) instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also thought about switching to startsWith but IMO it is not a correct approach because we can for example have a menu entry for / and for /alerts on the same UI where this default will make both entries selected. I think an exact match is the strongest default to avoid duplicated selected entries that could mislead the user.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmh, right. So we'd need to sort by specificity of configured paths to know "which Ingress is catching the current page", I guess.

};
} catch (e) {
throw new Error(
`[navbar][config] Invalid path specified in "options.${section}": "${path}" ` +
'(keys must be defined as fully qualified URLs, ' +
'such as "{protocol}://{host}{path}?{queryParams}")',
);
}
},
)
);
};

export const Navbar = ({ options }: { options: Options }): Node => {
const auth = useAuth();

const tabs = [
{
selected: true,
title: 'Overview',
onClick: () => console.log('Overview'),
},
];
const userGroups: string[] = auth.userData?.profile?.groups || [];
const accessiblePaths = getAccessiblePathsFromOptions(options, userGroups);
useLayoutEffect(() => {
accessiblePaths.forEach((accessiblePath) => {
prefetch(accessiblePath);
});
}, [JSON.stringify(accessiblePaths)]);

const tabs = translateOptionsToMenu(
options,
'main',
(path, translationAndGroup) => ({
link: <a href={path}>{translationAndGroup.en}</a>,
}),
userGroups,
);

const rightActions = [
{
type: 'dropdown',
text: 'FR',
icon: <i className="fas fa-globe" />,
items: [
{
label: 'English',
name: 'EN',
onClick: () => console.log('en'),
},
],
},
{
type: 'dropdown',
icon: <i className="fas fa-question-circle" />,
items: [
{ label: 'About', onClick: () => console.log('About clicked') },
{
label: 'Documentation',
onClick: () => console.log('Documentation clicked'),
},
{
label: 'Onboarding',
onClick: () => console.log('Onboarding clicked'),
},
],
},
{
type: 'button',
icon: <i className="fas fa-sun" />,
onClick: () => console.log('Theme toggle clicked'),
},
{
type: 'dropdown',
text: auth.userData?.profile.name || '',
icon: <i className="fas fa-user" />,
items: [
...translateOptionsToMenu(
options,
'subLogin',
(path, translationAndGroup) => ({
label: translationAndGroup.en,
onClick: () => {
location.href = path;
},
}),
userGroups,
),
{
label: 'Log out',
gdemonet marked this conversation as resolved.
Show resolved Hide resolved
onClick: () => {
Expand All @@ -69,6 +106,7 @@ export const Navbar = ({
],
},
];

return (
<CoreUINavbar rightActions={rightActions} tabs={tabs} role="navigation" />
);
Expand Down
Loading