Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

enh(Header/userMenu):reduce spacing user menu #11393

Merged
merged 2 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions www/front_src/src/Header/Clock/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useRef, useState, useEffect } from 'react';
import { Typography } from '@mui/material';
import makeStyles from '@mui/styles/makeStyles';

import { useLocaleDateTimeFormat } from '@centreon/ui';
import { centreonUi } from '../helpers/index';

const useStyles = makeStyles((theme) => ({
dateTime: {
Expand All @@ -20,7 +20,7 @@ const Clock = (): JSX.Element => {
time: '',
});

const { format, toTime } = useLocaleDateTimeFormat();
const { format, toTime } = centreonUi.useLocaleDateTimeFormat();

const updateDateTime = (): void => {
const now = new Date();
Expand Down Expand Up @@ -48,7 +48,7 @@ const Clock = (): JSX.Element => {
const { date, time } = dateTime;

return (
<div className={classes.dateTime}>
<div className={classes.dateTime} data-cy="clock">
<Typography variant="body2">{date}</Typography>
<Typography variant="body1">{time}</Typography>
</div>
Expand Down
3 changes: 0 additions & 3 deletions www/front_src/src/Header/SwitchThemeMode/images/moon.svg

This file was deleted.

3 changes: 0 additions & 3 deletions www/front_src/src/Header/SwitchThemeMode/images/sun.svg

This file was deleted.

135 changes: 59 additions & 76 deletions www/front_src/src/Header/SwitchThemeMode/index.tsx
Original file line number Diff line number Diff line change
@@ -1,105 +1,70 @@
import { equals } from 'ramda';
import { useAtom } from 'jotai';
import { useState } from 'react';

import clsx from 'clsx';
import { useLocation } from 'react-router-dom';

import { styled } from '@mui/material/styles';
import Switch from '@mui/material/Switch';
import { ListItemText, Switch } from '@mui/material';
import makeStyles from '@mui/styles/makeStyles';

import { userAtom, ThemeMode } from '@centreon/ui-context';
import { patchData, useRequest } from '@centreon/ui';

import svgSun from './images/sun.svg';
import svgMoon from './images/moon.svg';

interface StyleProps {
darkModeSvg?: string;
lightModeSvg?: string;
}
import useSwitchThemeMode from './useSwitchThemeMode';

const ThemeModeSwitch = styled(Switch, {
shouldForwardProp: (prop) =>
!equals(prop, 'color') &&
!equals(prop, 'lightModeSvg') &&
!equals(prop, 'darkModeSvg'),
})<StyleProps>(({ theme, darkModeSvg, lightModeSvg }) => ({
'& .MuiSwitch-switchBase': {
const useStyles = makeStyles((theme) => ({
container: {
'& .MuiSwitch-thumb': {
backgroundColor: 'white',
},
'& .MuiSwitch-track': {
backgroundColor: '#aab4be',
jdelpierre marked this conversation as resolved.
Show resolved Hide resolved
opacity: 1,
},
alignItems: 'center',
display: 'flex',
},
containerMode: {
display: 'flex',
justifyContent: 'space-around',
},
containerSwitch: {
'& .MuiSwitch-switchBase': {
padding: theme.spacing(0.5, 0.5, 0.5, 0.75),
},
'&.Mui-checked': {
'& + .MuiSwitch-track': {
backgroundColor: '#aab4be',
opacity: 1,
},
'& .MuiSwitch-thumb:before': {
backgroundImage: `url(${darkModeSvg})`,
'&:hover': {
backgroundColor: 'unset',
},
color: 'transparent',
transform: 'translate(15px,-50%)',
},
'&:hover': {
backgroundColor: 'transparent',
backgroundColor: 'unset',
},
color: 'black',
margin: 0,
position: 'absolute',
top: '50%',
transform: 'translate(-0.5px,-50%)',
},
'& .MuiSwitch-thumb': {
'&:before': {
backgroundImage: `url(${lightModeSvg})`,
backgroundPosition: 'center',
backgroundRepeat: 'no-repeat',
content: "''",
height: '100%',
left: theme.spacing(0),
position: 'absolute',
top: theme.spacing(0),
width: '100%',
},
backgroundColor: 'white',
height: theme.spacing(3),
width: theme.spacing(3),
disabledMode: {
color: theme.palette.common.white,
opacity: 0.5,
},
'& .MuiSwitch-track': {
backgroundColor: '#aab4be',
borderRadius: theme.spacing(10 / 8),
opacity: 1,
},
height: theme.spacing(32 / 8),
padding: theme.spacing(11 / 8, 4 / 8, 11 / 8, 9 / 8),
width: theme.spacing(50 / 8),
}));

const useStyles = makeStyles(() => ({
container: {
alignItems: 'center',
display: 'flex',
mode: {
paddingLeft: theme.spacing(1),
},
}));

const SwitchThemeMode = (): JSX.Element => {
const props = {
darkModeSvg: svgMoon,
lightModeSvg: svgSun,
};
const classes = useStyles();
const { pathname } = useLocation();
const [isPending, isDarkMode, themeMode, updateUser] = useSwitchThemeMode();

const [isDark, setIsDark] = useState(isDarkMode);

const { sendRequest } = useRequest({
request: patchData,
});
const [user, setUser] = useAtom(userAtom);

const isDarkMode = equals(user.themeMode, ThemeMode.dark);
const switchEndPoint = './api/latest/configuration/users/current/parameters';

const switchThemeMode = (): void => {
const themeMode = isDarkMode ? ThemeMode.light : ThemeMode.dark;
const isCurrentPageLegacy = pathname.includes('php');
setUser({
...user,
themeMode,
});
setIsDark(!isDark);
updateUser();
sendRequest({
data: { theme: themeMode },
endpoint: switchEndPoint,
Expand All @@ -112,11 +77,29 @@ const SwitchThemeMode = (): JSX.Element => {

return (
<div className={classes.container}>
<ThemeModeSwitch
checked={isDarkMode}
{...props}
<Switch
checked={isDark}
className={classes.containerSwitch}
data-cy="themeSwitch"
disabled={isPending}
size="small"
onChange={switchThemeMode}
/>
<div className={classes.containerMode}>
<ListItemText
className={clsx(classes.mode, { [classes.disabledMode]: isDark })}
>
Light
</ListItemText>

<ListItemText
className={clsx(classes.mode, {
[classes.disabledMode]: !isDark,
})}
>
Dark
</ListItemText>
</div>
</div>
);
};
Expand Down
30 changes: 30 additions & 0 deletions www/front_src/src/Header/SwitchThemeMode/useSwitchThemeMode.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useTransition } from 'react';

import { useAtom } from 'jotai';
import { equals } from 'ramda';

import { userAtom, ThemeMode } from '@centreon/ui-context';

const useSwitchThemeMode = (): [
isDarkMode: boolean,
isPending: boolean,
themeMode: ThemeMode,
updateUser: () => void,
] => {
const [user, setUser] = useAtom(userAtom);
const isDarkMode = equals(user.themeMode, ThemeMode.dark);
const [isPending, startTransition] = useTransition();

const themeMode = isDarkMode ? ThemeMode.light : ThemeMode.dark;
const updateUser = (): void =>
startTransition(() => {
setUser({
...user,
themeMode,
});
});

return [isPending, isDarkMode, themeMode, updateUser];
};

export default useSwitchThemeMode;
5 changes: 5 additions & 0 deletions www/front_src/src/Header/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { useLocaleDateTimeFormat } from '@centreon/ui';

export const centreonUi = {
useLocaleDateTimeFormat,
};
17 changes: 8 additions & 9 deletions www/front_src/src/Header/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useRef } from 'react';

import { makeStyles } from '@mui/styles';

import Hook from '../components/Hook';
Expand All @@ -6,7 +8,6 @@ import PollerMenu from './PollerMenu';
import HostStatusCounter from './RessourceStatusCounter/Host';
import ServiceStatusCounter from './RessourceStatusCounter/Service';
import UserMenu from './userMenu';
import SwitchMode from './SwitchThemeMode';

const HookComponent = Hook as unknown as (props) => JSX.Element;

Expand All @@ -30,12 +31,12 @@ const useStyles = makeStyles((theme) => ({
justifyContent: 'center',
},
pollerContainer: {
flex: 0.5,
flex: 0.4,
},
rightContainer: {
alignItems: 'center',
display: 'flex',
flex: 1.1,
flex: 0.9,
},
serviceStatusContainer: {
display: 'flex',
Expand All @@ -49,16 +50,17 @@ const useStyles = makeStyles((theme) => ({
userMenuContainer: {
alignItems: 'center',
display: 'flex',
flex: 0.4,
flex: 0.3,
justifyContent: 'flex-end',
},
}));

const Header = (): JSX.Element => {
const classes = useStyles();
const headerRef = useRef<HTMLElement>(null);

return (
<header className={classes.header}>
<header className={classes.header} ref={headerRef}>
<div className={classes.container}>
<div className={classes.pollerContainer}>
<PollerMenu />
Expand All @@ -74,10 +76,7 @@ const Header = (): JSX.Element => {
<ServiceStatusCounter />
</div>
<div className={classes.userMenuContainer}>
<SwitchMode />
<div className={classes.userMenu}>
<UserMenu />
</div>
<UserMenu headerRef={headerRef} />
</div>
</div>
</div>
Expand Down
6 changes: 2 additions & 4 deletions www/front_src/src/Header/userMenu/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,9 @@ describe('User Menu', () => {
userEvent.click(screen.getByLabelText(labelProfile));

await waitFor(() => {
expect(screen.getByText('Admin admin')).toBeInTheDocument();
expect(screen.getByText('admin')).toBeInTheDocument();
});

expect(screen.getByText('as admin')).toBeInTheDocument();

await waitFor(() => {
expect(screen.getByText('1:20 PM')).toBeInTheDocument();
});
Expand All @@ -139,7 +137,7 @@ describe('User Menu', () => {
});

await waitFor(() => {
expect(screen.getByText('Admin admin')).toBeInTheDocument();
expect(screen.getByText('admin')).toBeInTheDocument();
});

userEvent.click(screen.getByText(labelCopyAutologinLink));
Expand Down
Loading