refactor: rewrite useAutomatischInfo with RQ and REST API
This commit is contained in:
@@ -3,14 +3,17 @@ import * as React from 'react';
|
|||||||
import { FormattedMessage } from 'react-intl';
|
import { FormattedMessage } from 'react-intl';
|
||||||
import MationLogo from 'components/MationLogo';
|
import MationLogo from 'components/MationLogo';
|
||||||
import useAutomatischInfo from 'hooks/useAutomatischInfo';
|
import useAutomatischInfo from 'hooks/useAutomatischInfo';
|
||||||
const DefaultLogo = () => {
|
|
||||||
const { isMation, loading } = useAutomatischInfo();
|
export default function DefaultLogo() {
|
||||||
if (loading) return <React.Fragment />;
|
const { data: automatischInfo, isPending } = useAutomatischInfo();
|
||||||
|
const isMation = automatischInfo?.data.isMation;
|
||||||
|
|
||||||
|
if (isPending) return <React.Fragment />;
|
||||||
if (isMation) return <MationLogo />;
|
if (isMation) return <MationLogo />;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Typography variant="h6" component="h1" data-test="typography-logo" noWrap>
|
<Typography variant="h6" component="h1" data-test="typography-logo" noWrap>
|
||||||
<FormattedMessage id="brandText" />
|
<FormattedMessage id="brandText" />
|
||||||
</Typography>
|
</Typography>
|
||||||
);
|
);
|
||||||
};
|
}
|
||||||
export default DefaultLogo;
|
|
||||||
|
@@ -9,6 +9,7 @@ import useAuthentication from 'hooks/useAuthentication.js';
|
|||||||
const queryClient = new QueryClient({
|
const queryClient = new QueryClient({
|
||||||
defaultOptions: {
|
defaultOptions: {
|
||||||
queries: {
|
queries: {
|
||||||
|
staleTime: 1000,
|
||||||
refetchOnWindowFocus: false,
|
refetchOnWindowFocus: false,
|
||||||
// provides a convenient default while it should be overridden for other HTTP methods
|
// provides a convenient default while it should be overridden for other HTTP methods
|
||||||
queryFn: async ({ queryKey, signal }) => {
|
queryFn: async ({ queryKey, signal }) => {
|
||||||
|
@@ -29,7 +29,8 @@ function createDrawerLinks({ isCloud }) {
|
|||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
export default function SettingsLayout({ children }) {
|
export default function SettingsLayout({ children }) {
|
||||||
const { isCloud } = useAutomatischInfo();
|
const { data: automatischInfo } = useAutomatischInfo();
|
||||||
|
const isCloud = automatischInfo?.data.isCloud;
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const formatMessage = useFormatMessage();
|
const formatMessage = useFormatMessage();
|
||||||
const matchSmallScreens = useMediaQuery(theme.breakpoints.down('lg'));
|
const matchSmallScreens = useMediaQuery(theme.breakpoints.down('lg'));
|
||||||
|
@@ -4,32 +4,45 @@ import clone from 'lodash/clone';
|
|||||||
import get from 'lodash/get';
|
import get from 'lodash/get';
|
||||||
import set from 'lodash/set';
|
import set from 'lodash/set';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import useConfig from 'hooks/useConfig';
|
|
||||||
import useAutomatischInfo from 'hooks/useAutomatischInfo';
|
import useAutomatischInfo from 'hooks/useAutomatischInfo';
|
||||||
|
import useConfig from 'hooks/useConfig';
|
||||||
import { defaultTheme, mationTheme } from 'styles/theme';
|
import { defaultTheme, mationTheme } from 'styles/theme';
|
||||||
|
|
||||||
const customizeTheme = (theme, config) => {
|
const customizeTheme = (theme, config) => {
|
||||||
// `clone` is needed so that the new theme reference triggers re-render
|
// `clone` is needed so that the new theme reference triggers re-render
|
||||||
const shallowDefaultTheme = clone(theme);
|
const shallowDefaultTheme = clone(theme);
|
||||||
|
|
||||||
for (const key in config) {
|
for (const key in config) {
|
||||||
const value = config[key];
|
const value = config[key];
|
||||||
const exists = get(theme, key);
|
const exists = get(theme, key);
|
||||||
|
|
||||||
if (exists) {
|
if (exists) {
|
||||||
set(shallowDefaultTheme, key, value);
|
set(shallowDefaultTheme, key, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return shallowDefaultTheme;
|
return shallowDefaultTheme;
|
||||||
};
|
};
|
||||||
const ThemeProvider = ({ children, ...props }) => {
|
const ThemeProvider = ({ children, ...props }) => {
|
||||||
const { isMation, loading: automatischInfoLoading } = useAutomatischInfo();
|
const { data: automatischInfo, isPending: isAutomatischInfoPending } =
|
||||||
|
useAutomatischInfo();
|
||||||
|
const isMation = automatischInfo?.data.isMation;
|
||||||
const { config, loading: configLoading } = useConfig();
|
const { config, loading: configLoading } = useConfig();
|
||||||
|
|
||||||
const customTheme = React.useMemo(() => {
|
const customTheme = React.useMemo(() => {
|
||||||
const installationTheme = isMation ? mationTheme : defaultTheme;
|
const installationTheme = isMation ? mationTheme : defaultTheme;
|
||||||
if (configLoading || automatischInfoLoading) return installationTheme;
|
|
||||||
|
if (configLoading || isAutomatischInfoPending) return installationTheme;
|
||||||
|
|
||||||
const customTheme = customizeTheme(installationTheme, config || {});
|
const customTheme = customizeTheme(installationTheme, config || {});
|
||||||
|
|
||||||
return customTheme;
|
return customTheme;
|
||||||
}, [configLoading, config, isMation, automatischInfoLoading]);
|
}, [configLoading, config, isMation, isAutomatischInfoPending]);
|
||||||
|
|
||||||
// TODO: maybe a global loading state for the custom theme?
|
// TODO: maybe a global loading state for the custom theme?
|
||||||
if (automatischInfoLoading || configLoading) return <></>;
|
if (isAutomatischInfoPending || configLoading) return <></>;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<BaseThemeProvider theme={customTheme} {...props}>
|
<BaseThemeProvider theme={customTheme} {...props}>
|
||||||
<CssBaseline />
|
<CssBaseline />
|
||||||
|
@@ -1,7 +1,9 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { AuthenticationContext } from 'contexts/Authentication';
|
import { AuthenticationContext } from 'contexts/Authentication';
|
||||||
|
|
||||||
export default function useAuthentication() {
|
export default function useAuthentication() {
|
||||||
const authenticationContext = React.useContext(AuthenticationContext);
|
const authenticationContext = React.useContext(AuthenticationContext);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
token: authenticationContext.token,
|
token: authenticationContext.token,
|
||||||
updateToken: authenticationContext.updateToken,
|
updateToken: authenticationContext.updateToken,
|
||||||
|
@@ -1,10 +1,20 @@
|
|||||||
import * as React from 'react';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import { AutomatischInfoContext } from 'contexts/AutomatischInfo';
|
import api from 'helpers/api';
|
||||||
|
|
||||||
export default function useAutomatischInfo() {
|
export default function useAutomatischInfo() {
|
||||||
const automatischInfoContext = React.useContext(AutomatischInfoContext);
|
const query = useQuery({
|
||||||
return {
|
/**
|
||||||
isCloud: automatischInfoContext.isCloud,
|
* The data doesn't change by user actions, but only by server deployments.
|
||||||
isMation: automatischInfoContext.isMation,
|
* So we can set the `staleTime` to Infinity
|
||||||
loading: automatischInfoContext.loading,
|
**/
|
||||||
};
|
staleTime: Infinity,
|
||||||
|
queryKey: ['automatischInfo'],
|
||||||
|
queryFn: async (payload, signal) => {
|
||||||
|
const { data } = await api.get('/v1/automatisch/info', null, { signal });
|
||||||
|
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return query;
|
||||||
}
|
}
|
||||||
|
@@ -1,11 +1,17 @@
|
|||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
import useAutomatischInfo from './useAutomatischInfo';
|
import useAutomatischInfo from './useAutomatischInfo';
|
||||||
|
|
||||||
export default function useCloud(options) {
|
export default function useCloud(options) {
|
||||||
const redirect = options?.redirect || false;
|
const redirect = options?.redirect || false;
|
||||||
const { isCloud } = useAutomatischInfo();
|
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
const { data: automatischInfo } = useAutomatischInfo();
|
||||||
|
|
||||||
|
const isCloud = automatischInfo?.data.isCloud;
|
||||||
|
|
||||||
if (isCloud === false && redirect) {
|
if (isCloud === false && redirect) {
|
||||||
navigate('/');
|
navigate('/');
|
||||||
}
|
}
|
||||||
|
|
||||||
return isCloud;
|
return isCloud;
|
||||||
}
|
}
|
||||||
|
@@ -1,27 +1,34 @@
|
|||||||
import * as React from 'react';
|
|
||||||
import { useNavigate } from 'react-router-dom';
|
|
||||||
import Box from '@mui/material/Box';
|
import Box from '@mui/material/Box';
|
||||||
import Stack from '@mui/material/Stack';
|
import Stack from '@mui/material/Stack';
|
||||||
import useNotifications from 'hooks/useNotifications';
|
import * as React from 'react';
|
||||||
|
import { useNavigate } from 'react-router-dom';
|
||||||
|
|
||||||
import Container from 'components/Container';
|
import Container from 'components/Container';
|
||||||
import NotificationCard from 'components/NotificationCard';
|
import NotificationCard from 'components/NotificationCard';
|
||||||
import PageTitle from 'components/PageTitle';
|
import PageTitle from 'components/PageTitle';
|
||||||
import useFormatMessage from 'hooks/useFormatMessage';
|
|
||||||
import useAutomatischInfo from 'hooks/useAutomatischInfo';
|
|
||||||
import * as URLS from 'config/urls';
|
import * as URLS from 'config/urls';
|
||||||
|
import useAutomatischInfo from 'hooks/useAutomatischInfo';
|
||||||
|
import useFormatMessage from 'hooks/useFormatMessage';
|
||||||
|
import useNotifications from 'hooks/useNotifications';
|
||||||
|
|
||||||
export default function Updates() {
|
export default function Updates() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const formatMessage = useFormatMessage();
|
const formatMessage = useFormatMessage();
|
||||||
const { notifications } = useNotifications();
|
const { notifications } = useNotifications();
|
||||||
const { isMation, loading } = useAutomatischInfo();
|
const { data: automatischInfo, isPending } = useAutomatischInfo();
|
||||||
|
const isMation = automatischInfo?.data.isMation;
|
||||||
|
|
||||||
React.useEffect(
|
React.useEffect(
|
||||||
function redirectToHomepageInMation() {
|
function redirectToHomepageInMation() {
|
||||||
if (!loading && isMation) {
|
if (!navigate) return;
|
||||||
|
|
||||||
|
if (!isPending && isMation) {
|
||||||
navigate(URLS.DASHBOARD);
|
navigate(URLS.DASHBOARD);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[loading, isMation],
|
[isPending, isMation, navigate],
|
||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box sx={{ py: 3 }}>
|
<Box sx={{ py: 3 }}>
|
||||||
<Container>
|
<Container>
|
||||||
|
Reference in New Issue
Block a user