From 0271ea983f60c0b6fcffe68e8f1eef58ecc8ac0f Mon Sep 17 00:00:00 2001 From: Vincent T Date: Tue, 10 Dec 2024 10:32:20 -0500 Subject: [PATCH] frontend: Link: Add drawer logic to links Signed-off-by: Vincent T --- .../src/components/common/Link.stories.tsx | 14 ++++-- frontend/src/components/common/Link.tsx | 50 ++++++++++++++++++- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/frontend/src/components/common/Link.stories.tsx b/frontend/src/components/common/Link.stories.tsx index 54dfdf80b5..905fa67338 100644 --- a/frontend/src/components/common/Link.stories.tsx +++ b/frontend/src/components/common/Link.stories.tsx @@ -1,15 +1,23 @@ import { Meta, StoryFn } from '@storybook/react'; +import React from 'react'; +import { Provider } from 'react-redux'; import { MemoryRouter } from 'react-router-dom'; +import { createStore } from 'redux'; +import reducers from '../../redux/reducers/reducers'; import Link, { LinkProps } from './Link'; +const store = createStore(reducers); + export default { title: 'Link', component: Link, decorators: [ Story => ( - - - + + + + + ), ], } as Meta; diff --git a/frontend/src/components/common/Link.tsx b/frontend/src/components/common/Link.tsx index 69f1155a73..abcc0fcf82 100644 --- a/frontend/src/components/common/Link.tsx +++ b/frontend/src/components/common/Link.tsx @@ -1,10 +1,14 @@ import MuiLink from '@mui/material/Link'; import { useQueryClient } from '@tanstack/react-query'; import React from 'react'; +import { useDispatch } from 'react-redux'; import { Link as RouterLink } from 'react-router-dom'; +import helpers from '../../helpers'; import { kubeObjectQueryKey, useEndpoints } from '../../lib/k8s/api/v2/hooks'; import { KubeObject } from '../../lib/k8s/KubeObject'; import { createRouteURL, RouteURLProps } from '../../lib/router'; +import { setSelectedResource } from '../../redux/drawerModeSlice'; +import { useTypedSelector } from '../../redux/reducers/reducers'; import { LightTooltip } from './Tooltip'; export interface LinkBaseProps { @@ -23,6 +27,7 @@ export interface LinkProps extends LinkBaseProps { state?: { [prop: string]: any; }; + drawerEnabled?: boolean; } export interface LinkObjectProps extends LinkBaseProps { @@ -66,8 +71,8 @@ function PureLink(props: React.PropsWithChildren) { const { kubeObject, ...otherProps } = props as LinkObjectProps; return ; } + const { routeName, params = {}, search, state, ...otherProps } = props as LinkObjectProps; - const { routeName, params = {}, search, state, ...otherProps } = props as LinkProps; return ( ) { } export default function Link(props: React.PropsWithChildren) { - const { tooltip, ...otherProps } = props; + const drawerEnabled = useTypedSelector(state => state.drawerMode.isDetailDrawerEnabled); + const dispatch = useDispatch(); + + const { tooltip, kubeObject, ...otherProps } = props as LinkObjectProps; + if (tooltip) { let tooltipText = ''; if (typeof tooltip === 'string') { @@ -106,5 +115,42 @@ export default function Link(props: React.PropsWithChildren { + if (drawerEnabled) { + dispatch(setSelectedResource(kubeJSON!)); + /** + * NOTE: we are using window.history.pushState to update the URL without causing a page reload. + * currently there is no way to update the URL without navigation to the details page which would make the drawer redundant. + * + * also note that this currently only works in the browser, not in electron. + */ + if (!helpers.isElectron()) { + window.history.pushState( + { path: kubeObject.getDetailsLink() }, + '', + kubeObject.getDetailsLink() + ); + } + } + }} + > + {props.children || kubeObject.getName()} + + ); + } else { + return ( + + {props.children || kubeObject.getName()} + + ); + } + } + return ; }