Skip to content

Commit

Permalink
frontend Detail Drawer: Add detail drawer mode
Browse files Browse the repository at this point in the history
DONE: Added drawer static on refresh ability
WIP: Reload component on name change working
WIP: Pods drawer open close button working
WIP: links no longer route to different drawer path

Signed-off-by: Vincent T <[email protected]>
  • Loading branch information
vyncent-t committed Mar 22, 2024
1 parent 93fda6c commit 32ddbdf
Show file tree
Hide file tree
Showing 21 changed files with 696 additions and 185 deletions.
10 changes: 10 additions & 0 deletions frontend/src/components/App/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { styled } from '@mui/material/styles';
import { useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { useDispatch } from 'react-redux';
import { useParams } from 'react-router-dom';
import { useClustersConf } from '../../lib/k8s';
import { request } from '../../lib/k8s/apiProxy';
import { Cluster } from '../../lib/k8s/cluster';
Expand All @@ -18,6 +19,7 @@ import store from '../../redux/stores/store';
import { fetchStatelessClusterKubeConfigs, processClusterComparison } from '../../stateless/';
import ActionsNotifier from '../common/ActionsNotifier';
import AlertNotification from '../common/AlertNotification';
import DetailsDrawer from '../DetailsDrawer/DetailsDrawer';
import Sidebar, { NavigationTabs } from '../Sidebar';
import RouteSwitcher from './RouteSwitcher';
import TopBar from './TopBar';
Expand Down Expand Up @@ -70,6 +72,13 @@ export default function Layout({}: LayoutProps) {
* indexDB and then sends the backend to parse it and then updates the parsed value into redux
* store on an interval.
* */
// DETAIL DRAWER MODE
const isDetailDrawerEnabled = useTypedSelector(state => state.drawerMode.isDetailDrawerEnabled);
console.log('LAY - isDetailDrawerEnabled', isDetailDrawerEnabled);

const params = useParams();
console.log('LAY - params: ', params);

useEffect(() => {
window.clusterConfigFetchHandler = setInterval(
() => {
Expand Down Expand Up @@ -181,6 +190,7 @@ export default function Layout({}: LayoutProps) {
</Container>
</Box>
</Main>
{isDetailDrawerEnabled && <DetailsDrawer />}
<ActionsNotifier />
</Box>
</>
Expand Down
59 changes: 59 additions & 0 deletions frontend/src/components/App/Settings/DrawerModeButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { FormControlLabel, Switch } from '@mui/material';
import React from 'react';
import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useDispatch } from 'react-redux';
import { setDetailDrawerEnabled } from '../../../redux/drawerModeSlice';
import { useTypedSelector } from '../../../redux/reducers/reducers';

export default function DrawerModeButton() {
const dispatch = useDispatch();
const { t } = useTranslation('translation');
// This will fix the problem of the project refreshing the state away from needed position

// DETAIL DRAWER MODE
const isDetailDrawerEnabled = useTypedSelector(state => state.drawerMode.isDetailDrawerEnabled);
// const localDetailDrawerEnabled = localStorage.getItem('detailDrawerEnabled');
const [isDrawerEnabled, changeDetailDrawerEnabled] = useState<any>(isDetailDrawerEnabled);

if (isDetailDrawerEnabled) {
console.log('THE LOCAL STORAGE IS TRUE');
dispatch(setDetailDrawerEnabled(true));
console.log('READING FROM DISPATCHED STATE', isDetailDrawerEnabled);
} else {
console.log(" THE LOCAL STORAGE IS NULL, UNDEFINED, OR 'FALSE' ");
dispatch(setDetailDrawerEnabled(false));
console.log('READING FROM DISPATCHED STATE', isDetailDrawerEnabled);
}

console.log('BUTTON - isDetailDrawerEnabled', isDetailDrawerEnabled);

// the useEffect will run everytime the isDrawerEnabled state changes, which is everytime the user clicks the switch button because the switch button changes the state of isDrawerEnabled
useEffect(() => {
dispatch(setDetailDrawerEnabled(isDrawerEnabled));
console.log('ON SETTINGS');
console.log(localStorage.getItem('detailDrawerEnabled'));
}, [isDrawerEnabled]);

// this function takes in the current changes and updates it, this kicks off the useEffect that is listening for changes to newDrawerEnabled
function drawerModeToggle() {
console.log('drawerModeToggle');
changeDetailDrawerEnabled(!isDrawerEnabled);
}

// NOTICE THAT WE DO NOT USE isDrawerEnabled TO DETERMINE HOW THE SWITCH IS RENDERED UNDER THE CHECKED PROP, THIS IS BECAUSE THE USEEFFECT WILL RERENDER THE COMPONENT WITH THE NEW STATE
return (
<FormControlLabel
control={
<Switch
checked={isDetailDrawerEnabled}
onClick={drawerModeToggle}
name="drawerMode"
color="primary"
/>
}
// will need to replace label
label={t('translation|Drawer Mode')}
/>
);
}
5 changes: 5 additions & 0 deletions frontend/src/components/App/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { setAppSettings } from '../../../redux/configSlice';
import { defaultTableRowsPerPageOptions } from '../../../redux/configSlice';
import { ActionButton, NameValueTable, SectionBox } from '../../common';
import TimezoneSelect from '../../common/TimezoneSelect';
import DrawerModeButton from './DrawerModeButton';
import { useSettings } from './hook';
import NumRowsInput from './NumRowsInput';
import ThemeChangeButton from './ThemeChangeButton';
Expand Down Expand Up @@ -69,6 +70,10 @@ export default function Settings() {
name: t('translation|Theme'),
value: <ThemeChangeButton showBothIcons />,
},
{
name: t('translation|Details on list view'),
value: <DrawerModeButton />,
},
{
name: t('translation|Number of rows for tables'),
value: (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,52 @@ exports[`Storyshots Settings General 1`] = `
</button>
</div>
</dd>
<dt
class="MuiGrid-root MuiGrid-item MuiGrid-grid-xs-12 MuiGrid-grid-sm-4 makeStyles-metadataNameCell css-1r6indk-MuiGrid-root"
>
Drawer Mode
</dt>
<dd
class="MuiGrid-root MuiGrid-item MuiGrid-grid-xs-12 MuiGrid-grid-sm-8 makeStyles-metadataCell makeStyles-valueCol css-r0umuq-MuiGrid-root"
>
<label
class="MuiFormControlLabel-root"
>
<span
class="MuiSwitch-root"
>
<span
aria-disabled="false"
class="MuiButtonBase-root MuiIconButton-root PrivateSwitchBase-root-1 MuiSwitch-switchBase MuiSwitch-colorPrimary"
>
<span
class="MuiIconButton-label"
>
<input
class="PrivateSwitchBase-input-4 MuiSwitch-input"
name="drawerMode"
type="checkbox"
value=""
/>
<span
class="MuiSwitch-thumb"
/>
</span>
<span
class="MuiTouchRipple-root"
/>
</span>
<span
class="MuiSwitch-track"
/>
</span>
<span
class="MuiTypography-root MuiFormControlLabel-label MuiTypography-body1"
>
Drawer Mode
</span>
</label>
</dd>
<dt
class="MuiGrid-root MuiGrid-item MuiGrid-grid-xs-12 MuiGrid-grid-sm-4 makeStyles-metadataNameCell css-1r6indk-MuiGrid-root"
>
Expand Down
123 changes: 123 additions & 0 deletions frontend/src/components/DetailsDrawer/DetailsDrawer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { Box, Button, Drawer, useMediaQuery } from '@mui/material';
import React, { useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { useParams } from 'react-router-dom';
import Deployment from '../../lib/k8s/deployment';
import Pod from '../../lib/k8s/pod';
import ReplicaSet from '../../lib/k8s/replicaSet';
import { setDetailDrawerOpen } from '../../redux/drawerModeSlice';
import { useTypedSelector } from '../../redux/reducers/reducers';
import WorkloadDetails from '../workload/Details';

export default function DetailsDrawer() {
const drawerOpen = useTypedSelector(state => state.drawerMode.isDetailDrawerOpen);
const dispatch = useDispatch();

const params = useParams();

console.log('EXAMPLE params: ', params);

const mediaQuerySize = useMediaQuery('(min-width: 1200px)');

// EXPERIMENTAL -------------
//
// const drawerNamespace = useTypedSelector(state => state.drawerMode.currentDrawerNamespace);
// const drawerName = useTypedSelector(state => state.drawerMode.currentDrawerName);
// const drawerResource = useTypedSelector(state => state.drawerMode.currentDrawerResource);
// const drawerCluster = useTypedSelector(state => state.drawerMode.currentDrawerCluster);

// const newNamespace = useState(drawerNamespace);
// const newName = useState(drawerName);
// const newResource = useState(drawerResource);
// const newCluster = useState(drawerCluster);
// const newDrawerOpen = useState(drawerOpen);

// --------------------------

// const detailView = {
// pods: <PodDetails />,
// };

const drawerName = useTypedSelector(state => state.drawerMode.currentDrawerName);

const localDrawerName = localStorage.getItem('currentDrawerName') as string;
const drawerNamespace = localStorage.getItem('currentDrawerNamespace') as string;
const drawerResource = localStorage.getItem('currentDrawerResource') as string;

console.log('drawerNamespace: ', drawerNamespace);
console.log('drawerName: ', drawerName);
console.log('drawerResource: ', drawerResource);

function closeDrawer() {
dispatch(setDetailDrawerOpen(false));
}

// This is used to reload the drawer, currently the most viable solution
const [currentName, setCurrentName] = React.useState('');

useEffect(() => {
// refresh?
console.log('refresh');
console.log('CLOSING DRAWER');
console.log('DRAWER OPEN', drawerOpen);
console.log('IS DRAWER OPEN', drawerOpen);
console.log('drawerNamespace: ', drawerNamespace);
console.log('drawerName: ', drawerName);
console.log('drawerResource: ', drawerResource);
setCurrentName(drawerName);
}, [drawerName]);

// NOTE: for the refresh to remember the drawer we need to use the local drawer name first then the redux if not available
return (
<>
{mediaQuerySize && (
<Drawer variant="persistent" anchor="right" open={drawerOpen} onClose={() => closeDrawer()}>
<Box width={800}>
<Box style={{ marginTop: '5rem', marginBottom: '2rem' }}>
<Button variant="outlined" color="primary" onClick={() => closeDrawer()}>
Close
</Button>
</Box>
<Box>
{/* {detailView['pods']} */}
{/* {<WorkloadDetails workloadKind={Deployment} />} */}
{drawerResource === 'pods' && drawerName === currentName ? (
<WorkloadDetails
workloadKind={Pod}
drawerName={localDrawerName ? localDrawerName : localDrawerName}
drawerNamespace={drawerNamespace}
/>
) : null}

{drawerResource === 'deployments' && drawerName === currentName ? (
<WorkloadDetails
workloadKind={Deployment}
drawerName={localDrawerName ? localDrawerName : localDrawerName}
drawerNamespace={drawerNamespace}
/>
) : null}

{drawerResource === 'replicasets' && drawerName === currentName ? (
<WorkloadDetails
workloadKind={ReplicaSet}
drawerName={localDrawerName ? localDrawerName : localDrawerName}
drawerNamespace={drawerNamespace}
/>
) : null}

{/*
NEED A WAY TO RENDER NON WORKLOAD DETAILS VIEWS
{drawerResource === 'services' && drawerName === currentName ? (
<WorkloadDetails
workloadKind={Service}
drawerName={localDrawerName ? localDrawerName : localDrawerName}
drawerNamespace={drawerNamespace}
/>
) : null} */}
</Box>
</Box>
</Drawer>
)}
</>
);
}
56 changes: 52 additions & 4 deletions frontend/src/components/common/Link.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import MuiLink from '@mui/material/Link';
import React from 'react';
import { useDispatch } from 'react-redux';
import { Link as RouterLink } from 'react-router-dom';
import { makeKubeObject } from '../../lib/k8s/cluster';
import { createRouteURL, RouteURLProps } from '../../lib/router';
import {
setDetailDrawerOpen,
updateDrawerCluster,
updateDrawerName,
updateDrawerNamespace,
updateDrawerResource,
} from '../../redux/drawerModeSlice';
import { LightTooltip } from './Tooltip';

export interface LinkBaseProps {
Expand All @@ -21,6 +29,7 @@ export interface LinkProps extends LinkBaseProps {
state?: {
[prop: string]: any;
};
drawerEnabled?: boolean;
}

export interface LinkObjectProps extends LinkBaseProps {
Expand All @@ -30,11 +39,50 @@ export interface LinkObjectProps extends LinkBaseProps {

function PureLink(props: React.PropsWithChildren<LinkProps | LinkObjectProps>) {
if ((props as LinkObjectProps).kubeObject) {
const { kubeObject, ...otherProps } = props as LinkObjectProps;
const { kubeObject, drawerEnabled, ...otherProps } = props as LinkObjectProps;
const dispatch = useDispatch();

function drawerLinkHandler(event: any) {
// this will be use to determine if the drawer needs to open when the link is clicked or change pages
const drawerResourcesAllowed = ['pods', 'deployments', 'replicasets'];

console.log('LINK STRING: ', kubeObject.getDetailsLink());
const url = kubeObject.getDetailsLink();
const urlPieces = url.split('/');
console.log('URL PIECES: ', urlPieces);
const linkCluster = urlPieces[2];
const linkResource = urlPieces[3];
const linkNamespace = urlPieces[4];
const linkName = urlPieces[5];

// if the resource bit from the url matches the resources allowed, then open the drawer
if (drawerResourcesAllowed.includes(linkResource)) {
event.preventDefault();
dispatch(updateDrawerCluster(linkCluster));
dispatch(updateDrawerResource(linkResource));
dispatch(updateDrawerNamespace(linkNamespace));
dispatch(updateDrawerName(linkName));
dispatch(setDetailDrawerOpen(true));
}
}

return (
<MuiLink component={RouterLink} to={kubeObject.getDetailsLink()} {...otherProps}>
{props.children || kubeObject.getName()}
</MuiLink>
<>
{drawerEnabled === true ? (
<MuiLink
component={RouterLink}
to={kubeObject.getDetailsLink()}
{...otherProps}
onClick={drawerLinkHandler}
>
{props.children || kubeObject.getName()}
</MuiLink>
) : (
<MuiLink component={RouterLink} to={kubeObject.getDetailsLink()} {...otherProps}>
{props.children || kubeObject.getName()}
</MuiLink>
)}
</>
);
}

Expand Down
Loading

0 comments on commit 32ddbdf

Please sign in to comment.