2021-09-13 10:49:27 -05:00
|
|
|
import { Location } from 'history';
|
2021-11-02 06:19:18 -05:00
|
|
|
import { NavModelItem, NavSection } from '@grafana/data';
|
|
|
|
import { getConfig } from 'app/core/config';
|
|
|
|
import { contextSrv } from 'app/core/services/context_srv';
|
|
|
|
import { ShowModalReactEvent } from '../../../types/events';
|
|
|
|
import appEvents from '../../app_events';
|
|
|
|
import { getFooterLinks } from '../Footer/Footer';
|
|
|
|
import { HelpModal } from '../help/HelpModal';
|
2020-07-14 05:56:55 -05:00
|
|
|
|
|
|
|
export const getForcedLoginUrl = (url: string) => {
|
|
|
|
const queryParams = new URLSearchParams(url.split('?')[1]);
|
|
|
|
queryParams.append('forceLogin', 'true');
|
|
|
|
|
|
|
|
return `${getConfig().appSubUrl}${url.split('?')[0]}?${queryParams.toString()}`;
|
|
|
|
};
|
2021-09-13 10:49:27 -05:00
|
|
|
|
2021-11-02 06:19:18 -05:00
|
|
|
export const enrichConfigItems = (
|
|
|
|
items: NavModelItem[],
|
|
|
|
location: Location<unknown>,
|
|
|
|
toggleOrgSwitcher: () => void
|
|
|
|
) => {
|
|
|
|
const { isSignedIn, user } = contextSrv;
|
|
|
|
const onOpenShortcuts = () => {
|
|
|
|
appEvents.publish(new ShowModalReactEvent({ component: HelpModal }));
|
|
|
|
};
|
|
|
|
|
|
|
|
if (user && user.orgCount > 1) {
|
|
|
|
const profileNode = items.find((bottomNavItem) => bottomNavItem.id === 'profile');
|
|
|
|
if (profileNode) {
|
|
|
|
profileNode.showOrgSwitcher = true;
|
|
|
|
profileNode.subTitle = `Current Org.: ${user?.orgName}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isSignedIn) {
|
|
|
|
const forcedLoginUrl = getForcedLoginUrl(location.pathname + location.search);
|
|
|
|
|
|
|
|
items.unshift({
|
|
|
|
icon: 'signout',
|
|
|
|
id: 'signin',
|
|
|
|
section: NavSection.Config,
|
|
|
|
target: '_self',
|
|
|
|
text: 'Sign in',
|
|
|
|
url: forcedLoginUrl,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
items.forEach((link, index) => {
|
|
|
|
let menuItems = link.children || [];
|
|
|
|
|
|
|
|
if (link.id === 'help') {
|
|
|
|
link.children = [
|
|
|
|
...getFooterLinks(),
|
|
|
|
{
|
|
|
|
text: 'Keyboard shortcuts',
|
|
|
|
icon: 'keyboard',
|
|
|
|
onClick: onOpenShortcuts,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (link.showOrgSwitcher) {
|
|
|
|
link.children = [
|
|
|
|
...menuItems,
|
|
|
|
{
|
|
|
|
text: 'Switch organization',
|
|
|
|
icon: 'arrow-random',
|
|
|
|
onClick: toggleOrgSwitcher,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return items;
|
|
|
|
};
|
|
|
|
|
2021-09-13 10:49:27 -05:00
|
|
|
export const isLinkActive = (pathname: string, link: NavModelItem) => {
|
|
|
|
// strip out any query params
|
|
|
|
const linkPathname = link.url?.split('?')[0];
|
2021-11-02 06:19:18 -05:00
|
|
|
const newNavigationEnabled = getConfig().featureToggles.newNavigation;
|
2021-09-13 10:49:27 -05:00
|
|
|
if (linkPathname) {
|
2021-11-02 06:19:18 -05:00
|
|
|
const dashboardLinkMatch = newNavigationEnabled ? '/dashboards' : '/';
|
2021-09-13 10:49:27 -05:00
|
|
|
if (linkPathname === pathname) {
|
|
|
|
// exact match
|
|
|
|
return true;
|
|
|
|
} else if (linkPathname !== '/' && pathname.startsWith(linkPathname)) {
|
|
|
|
// partial match
|
|
|
|
return true;
|
|
|
|
} else if (linkPathname === '/alerting/list' && pathname.startsWith('/alerting/notification/')) {
|
|
|
|
// alert channel match
|
|
|
|
// TODO refactor routes such that we don't need this custom logic
|
|
|
|
return true;
|
2021-11-02 06:19:18 -05:00
|
|
|
} else if (linkPathname === dashboardLinkMatch && pathname.startsWith('/d/')) {
|
2021-09-13 10:49:27 -05:00
|
|
|
// dashboard match
|
|
|
|
// TODO refactor routes such that we don't need this custom logic
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// child match
|
|
|
|
if (link.children?.some((childLink) => isLinkActive(pathname, childLink))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const isSearchActive = (location: Location<unknown>) => {
|
|
|
|
const query = new URLSearchParams(location.search);
|
|
|
|
return query.get('search') === 'open';
|
|
|
|
};
|