1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-24 15:59:41 +02:00
portainer/app/react/sidebar/SidebarItem/useSidebarSrefActive.tsx
Steven Kang d32b0f8b7e feat(kubernetes): support for jobs and cron jobs - r8s-182 (#260)
Co-authored-by: James Carppe <85850129+jamescarppe@users.noreply.github.com>
Co-authored-by: Anthony Lapenna <anthony.lapenna@portainer.io>
Co-authored-by: andres-portainer <91705312+andres-portainer@users.noreply.github.com>
Co-authored-by: Oscar Zhou <100548325+oscarzhou-portainer@users.noreply.github.com>
Co-authored-by: Yajith Dayarathna <yajith.dayarathna@portainer.io>
Co-authored-by: LP B <xAt0mZ@users.noreply.github.com>
Co-authored-by: oscarzhou <oscar.zhou@portainer.io>
Co-authored-by: testA113 <aliharriss1995@gmail.com>
2025-01-10 13:21:27 +13:00

53 lines
1.8 KiB
TypeScript

import {
TransitionOptions,
useCurrentStateAndParams,
useSrefActive,
} from '@uirouter/react';
export type PathOptions = {
/** ignorePaths ignores highlighting the sidebar parent when the URL of a sidebar child matches the current URL */
ignorePaths?: string[];
/** includePaths help to highlight the sidebar parent when the URL of a sidebar child matches the current URL */
includePaths?: string[];
};
/**
* Extends useSrefActive by ignoring or including paths and updating the classNames field returned when a child route is active.
* @param to The route to match
* @param activeClassName The active class names to return
* @param params The route params
* @param options The transition options
* @param pathOptions The paths to ignore/include
*/
export function useSidebarSrefActive(
to: string,
// default values are the classes used in the sidebar for an active item
activeClassName: string = 'bg-blue-5/25 be:bg-gray-5/25 th-dark:bg-gray-true-5/25',
params: Partial<Record<string, string>> = {},
options: TransitionOptions = {},
pathOptions: PathOptions = {
ignorePaths: [],
includePaths: [],
}
) {
const { state: { name: stateName = '' } = {} } = useCurrentStateAndParams();
const anchorProps = useSrefActive(to, params || {}, activeClassName, options);
// overwrite the className to '' if the the current route is in ignorePaths
const isIgnorePathInRoute = pathOptions.ignorePaths?.some((path) =>
stateName.includes(path)
);
if (isIgnorePathInRoute) {
return { ...anchorProps, className: '' };
}
// overwrite the className to activeClassName if the the current route is in includePaths
const isIncludePathInRoute = pathOptions.includePaths?.some((path) =>
stateName.includes(path)
);
if (isIncludePathInRoute) {
return { ...anchorProps, className: activeClassName };
}
return anchorProps;
}