Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[frontend] Keep html body attribute 'data-theme' in sync when changing theme (#4693) #4906

Merged
merged 3 commits into from
Nov 17, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import React, { useEffect } from 'react';
import { Route, Switch } from 'react-router-dom';
import makeStyles from '@mui/styles/makeStyles';
import Box from '@mui/material/Box';
import CssBaseline from '@mui/material/CssBaseline';
import { useTheme } from '@mui/styles';
import { BoundaryRoute, NoMatch } from '@components/Error';
import Search from './components/Search';
import TopBar from './components/nav/TopBar';
import LeftBar from './components/nav/LeftBar';
Expand All @@ -23,21 +24,26 @@ import RootNotifications from './components/profile/Root';
import RootData from './components/data/Root';
import RootWorkspaces from './components/workspaces/Root';
import Message from '../components/Message';
import { BoundaryRoute, NoMatch } from './components/Error';
import StixObjectOrStixRelationship from './components/StixObjectOrStixRelationship';
import SearchBulk from './components/SearchBulk';
import RootCases from './components/cases/Root';
import SystemBanners from '../public/components/SystemBanners';
import TimeoutLock from './components/TimeoutLock';
import useAuth from '../utils/hooks/useAuth';
import SettingsMessagesBanner, { useSettingsMessagesBannerHeight } from './components/settings/settings_messages/SettingsMessagesBanner';
import { Theme } from '../components/Theme';
import { RootPrivateQuery$data } from './__generated__/RootPrivateQuery.graphql';

const useStyles = makeStyles((theme) => ({
const useStyles = makeStyles((theme: Theme) => ({
toolbar: theme.mixins.toolbar,
}));

const Index = ({ settings }) => {
const theme = useTheme();
interface IndexProps {
settings: RootPrivateQuery$data['settings']
}

const Index = ({ settings }: IndexProps) => {
const theme = useTheme<Theme>();
const classes = useStyles();
const {
bannerSettings: { bannerHeight },
Expand All @@ -52,10 +58,29 @@ const Index = ({ settings }) => {
overflowX: 'hidden',
};
const settingsMessagesBannerHeight = useSettingsMessagesBannerHeight();

// Change the theme body attribute when the mode changes in
// the palette because some components like CKEditor uses this
// body attribute to display correct styles.
useEffect(() => {
const body = document.querySelector('body');
if (body) {
const bodyMode = body.getAttribute('data-theme');
const themeMode = `${theme.palette.mode}`;
if (bodyMode !== themeMode) {
body.setAttribute('data-theme', themeMode);
}
}
}, [theme]);

return (
Comment on lines +62 to +75
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code about fixing the issue.
The rest is to be able to use TypeScript.

<>
<SystemBanners settings={settings} />
{settings.platform_session_idle_timeout > 0 && <TimeoutLock />}
{
settings.platform_session_idle_timeout
&& settings.platform_session_idle_timeout > 0
&& <TimeoutLock />
}
<SettingsMessagesBanner />
<Box
sx={{
Expand Down Expand Up @@ -100,6 +125,10 @@ const Index = ({ settings }) => {
<BoundaryRoute path="/dashboard/events" component={RootEvents} />
<Route
path="/dashboard/observations"
// Because mismatch of types between react-router v5 and v6.
// It uses types of v6, but we are using v5 here and compiler is lost.
/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */
/* @ts-ignore */
component={RootObservations}
/>
<BoundaryRoute path="/dashboard/threats" component={RootThreats} />
Expand Down Expand Up @@ -131,7 +160,13 @@ const Index = ({ settings }) => {
path="/dashboard/profile"
component={RootNotifications}
/>
<Route component={NoMatch} />
<Route
// Because mismatch of types between react-router v5 and v6.
// It uses types of v6, but we are using v5 here and compiler is lost.
/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */
/* @ts-ignore */
component={NoMatch}
/>
</Switch>
</Box>
</Box>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ export const BoundaryRoute = (props) => {
};

BoundaryRoute.propTypes = {
component: PropTypes.func,
display: PropTypes.object,
exact: PropTypes.bool,
path: PropTypes.string,
render: PropTypes.func,
};

// 404
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,7 @@ const timeoutReducer = (state: TimeoutState, action: Action): TimeoutState => {
return state;
};

interface TimeoutLockProps {
handleLogout: (url?: string) => void;
}

const TimeoutLock: React.FunctionComponent<TimeoutLockProps> = () => {
const TimeoutLock: React.FunctionComponent = () => {
const { t } = useFormatter();
const {
bannerSettings: { bannerHeightNumber, idleLimit, sessionLimit },
Expand Down