Skip to content

Commit

Permalink
chore: simplify migration (#537)
Browse files Browse the repository at this point in the history
* chore: simplify migration

* tests: update snapshots
  • Loading branch information
dunglas authored Mar 7, 2024
1 parent 1710ffd commit a000f5d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 57 deletions.
76 changes: 31 additions & 45 deletions src/AdminGuesser.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import PropTypes from 'prop-types';
import {
Admin,
AdminContext,
AdminUI,
ComponentPropType,
I18nContextProvider,
Loading,
ThemeProvider,
ThemesContext,
defaultI18nProvider,
} from 'react-admin';
import { createHashHistory, createMemoryHistory } from 'history';
import { ErrorBoundary } from 'react-error-boundary';
import type { FallbackProps } from 'react-error-boundary';
import type { ComponentType, ErrorInfo } from 'react';
Expand All @@ -23,6 +20,7 @@ import {
Error as DefaultError,
Layout,
LoginPage,
darkTheme,
lightTheme,
} from './layout/index.js';
import getRoutesAndResourcesFromNodes, {
Expand Down Expand Up @@ -68,7 +66,7 @@ const getOverrideCode = (resources: Resource[]) => {
export const AdminResourcesGuesser = ({
// Admin props
loadingPage: LoadingPage = Loading,
admin: AdminEl = Admin,
admin: AdminEl = AdminUI,
// Props
children,
includeDeprecated,
Expand Down Expand Up @@ -116,7 +114,7 @@ const AdminGuesser = ({
includeDeprecated = false,
// Admin props
dataProvider,
history,
i18nProvider,
layout = Layout,
loginPage = LoginPage,
loading: loadingPage,
Expand All @@ -129,14 +127,6 @@ const AdminGuesser = ({
const [loading, setLoading] = useState(true);
const [, setError] = useState();
const [introspect, setIntrospect] = useState(true);
let adminHistory = history;

if (!adminHistory) {
adminHistory =
typeof window === 'undefined'
? createMemoryHistory()
: createHashHistory();
}

useEffect(() => {
if (typeof dataProvider.introspect !== 'function') {
Expand Down Expand Up @@ -175,23 +165,29 @@ const AdminGuesser = ({
);

return (
<IntrospectionContext.Provider value={introspectionContext}>
<SchemaAnalyzerContext.Provider value={schemaAnalyzer}>
<AdminResourcesGuesser
includeDeprecated={includeDeprecated}
resources={resources}
loading={loading}
dataProvider={dataProvider}
history={adminHistory}
layout={layout}
loginPage={loginPage}
loadingPage={loadingPage}
theme={theme}
{...rest}>
{children}
</AdminResourcesGuesser>
</SchemaAnalyzerContext.Provider>
</IntrospectionContext.Provider>
<AdminContext
i18nProvider={i18nProvider}
dataProvider={dataProvider}
theme={theme}
darkTheme={darkTheme}
lightTheme={lightTheme}>
<IntrospectionContext.Provider value={introspectionContext}>
<SchemaAnalyzerContext.Provider value={schemaAnalyzer}>
<AdminResourcesGuesser
includeDeprecated={includeDeprecated}
resources={resources}
loading={loading}
dataProvider={dataProvider}
layout={layout}
loginPage={loginPage}
loadingPage={loadingPage}
theme={theme}
{...rest}>
{children}
</AdminResourcesGuesser>
</SchemaAnalyzerContext.Provider>
</IntrospectionContext.Provider>
</AdminContext>
);
};

Expand Down Expand Up @@ -231,19 +227,9 @@ const AdminGuesserWithError = ({
);

return (
<I18nContextProvider value={i18nProvider}>
<ThemesContext.Provider value={theme}>
<ThemeProvider>
<ErrorBoundary onError={handleError} fallbackRender={renderError}>
<AdminGuesser
{...props}
i18nProvider={i18nProvider}
theme={theme}
/>
</ErrorBoundary>
</ThemeProvider>
</ThemesContext.Provider>
</I18nContextProvider>
<ErrorBoundary onError={handleError} fallbackRender={renderError}>
<AdminGuesser {...props} i18nProvider={i18nProvider} theme={theme} />
</ErrorBoundary>
);
};

Expand Down
10 changes: 5 additions & 5 deletions src/__snapshots__/AdminGuesser.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`<AdminGuesser /> renders loading 1`] = `<Loading />`;

exports[`<AdminGuesser /> renders with custom resources 1`] = `
<Admin
<AdminUI
dataProvider={
{
"create": [Function],
Expand All @@ -25,11 +25,11 @@ exports[`<AdminGuesser /> renders with custom resources 1`] = `
<ResourceGuesser
name="custom"
/>
</Admin>
</AdminUI>
`;

exports[`<AdminGuesser /> renders without custom resources 1`] = `
<Admin
<AdminUI
dataProvider={
{
"create": [Function],
Expand All @@ -51,11 +51,11 @@ exports[`<AdminGuesser /> renders without custom resources 1`] = `
<ResourceGuesser
name="books"
/>
</Admin>
</AdminUI>
`;

exports[`<AdminGuesser /> renders without data 1`] = `
<Admin
<AdminUI
dataProvider={
{
"create": [Function],
Expand Down
8 changes: 1 addition & 7 deletions src/layout/AppBar.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import React from 'react';
import {
AppBar,
AppBarClasses,
ToggleThemeButton,
useAuthProvider,
} from 'react-admin';
import { AppBar, AppBarClasses, useAuthProvider } from 'react-admin';
import type { AppBarProps } from 'react-admin';
import { Box, Typography } from '@mui/material';

Expand All @@ -23,7 +18,6 @@ const CustomAppBar = ({ classes, userMenu, ...props }: AppBarProps) => {
/>
<Logo />
<Box component="span" sx={{ flex: 1 }} />
<ToggleThemeButton />
</AppBar>
);
};
Expand Down

0 comments on commit a000f5d

Please sign in to comment.