forked from openshift/console
/
NavLinkRoot.tsx
46 lines (41 loc) · 1.32 KB
/
NavLinkRoot.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import * as React from 'react';
import { connect } from 'react-redux';
import { RootState } from '@console/internal/redux';
import { getActiveNamespace } from '@console/internal/reducers/ui';
import { stripNS } from './utils';
import { NavLinkComponent, NavLinkProps } from './NavLink';
// TODO [tech debt] Remove this component and use hooks in NavLinkHref, NavLinkResourceNS,
// and NavLinkResourceCluster
const NavLinkRoot_: React.FC<NavLinkRootProps & NavLinkRootStateProps> = ({
component: Component,
isActive,
className,
children,
dragRef,
...props
}) => {
return (
<Component className={className} {...props} isActive={isActive} dragRef={dragRef}>
{children}
</Component>
);
};
const navLinkRootMapStateToProps = (
state: RootState,
{ component: Component, ...props }: NavLinkRootProps,
): NavLinkRootStateProps => {
const activeNamespace = getActiveNamespace(state);
const location = stripNS(state.UI.get('location'));
return {
activeNamespace,
isActive: Component.isActive(props, location, activeNamespace),
};
};
export const NavLinkRoot = connect(navLinkRootMapStateToProps)(NavLinkRoot_);
type NavLinkRootStateProps = {
isActive: boolean;
activeNamespace: string;
};
type NavLinkRootProps<T extends NavLinkProps = NavLinkProps> = NavLinkProps & {
component: NavLinkComponent<T>;
};