1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-06 00:07:44 +01:00
unleash.unleash/frontend/src/component/common/PageContent/PageContent.tsx
Jaanus Sellin 3acb3ad2c2
feat: upgrade from react v17 to v18 (#7265)
**Upgrade to React v18 for Unleash v6. Here's why I think it's a good
time to do it:**
- Command Bar project: We've begun work on the command bar project, and
there's a fantastic library we want to use. However, it requires React
v18 support.
- Straightforward Upgrade: I took a look at the upgrade guide
https://react.dev/blog/2022/03/08/react-18-upgrade-guide and it seems
fairly straightforward. In fact, I was able to get React v18 running
with minimal changes in just 10 minutes!
- Dropping IE Support: React v18 no longer supports Internet Explorer
(IE), which is no longer supported by Microsoft as of June 15, 2022.
Upgrading to v18 in v6 would be a good way to align with this change.

TS updates:
* FC children has to be explicit:
https://stackoverflow.com/questions/71788254/react-18-typescript-children-fc
* forcing version 18 types in resolutions:
https://sentry.io/answers/type-is-not-assignable-to-type-reactnode/

Test updates:
* fixing SWR issue that we have always had but it manifests more in new
React (https://github.com/vercel/swr/issues/2373)

---------

Co-authored-by: kwasniew <kwasniewski.mateusz@gmail.com>
2024-06-11 13:59:52 +03:00

120 lines
3.2 KiB
TypeScript

import type React from 'react';
import type { FC, ReactNode } from 'react';
import classnames from 'classnames';
import { PageHeader } from 'component/common/PageHeader/PageHeader';
import { Paper, type PaperProps, styled } from '@mui/material';
import { useStyles } from './PageContent.styles';
import useLoading from 'hooks/useLoading';
import { ConditionallyRender } from '../ConditionallyRender/ConditionallyRender';
interface IPageContentProps extends PaperProps {
header?: ReactNode;
isLoading?: boolean;
/**
* @deprecated fix feature event log and remove
*/
disablePadding?: boolean;
/**
* @deprecated fix feature event log and remove
*/
disableBorder?: boolean;
disableLoading?: boolean;
bodyClass?: string;
headerClass?: string;
withTabs?: boolean;
}
const StyledHeader = styled('div')(({ theme }) => ({
borderBottomStyle: 'solid',
borderBottomWidth: '1px',
borderBottomColor: theme.palette.divider,
[theme.breakpoints.down('md')]: {
padding: theme.spacing(3, 2),
},
}));
const StyledPaper = styled(Paper)(({ theme }) => ({
borderRadius: theme.shape.borderRadiusLarge,
boxShadow: 'none',
}));
const PageContentLoading: FC<{
isLoading: boolean;
children?: React.ReactNode;
}> = ({ children, isLoading }) => {
const ref = useLoading(isLoading);
return (
<div ref={ref} aria-busy={isLoading} aria-live='polite'>
{children}
</div>
);
};
export const PageContent: FC<IPageContentProps> = ({
children,
header,
disablePadding = false,
disableBorder = false,
bodyClass = '',
headerClass = '',
isLoading = false,
disableLoading = false,
className,
withTabs,
...rest
}) => {
const { classes: styles } = useStyles();
const headerClasses = classnames(
'header',
headerClass || styles.headerPadding,
{
[styles.paddingDisabled]: disablePadding,
[styles.borderDisabled]: disableBorder,
[styles.withTabs]: withTabs,
},
);
const bodyClasses = classnames(
'body',
bodyClass ? bodyClass : styles.bodyContainer,
{
[styles.paddingDisabled]: disablePadding,
[styles.borderDisabled]: disableBorder,
},
);
const paperProps = disableBorder ? { elevation: 0 } : {};
const content = (
<StyledPaper
{...rest}
{...paperProps}
className={classnames(className)}
>
<ConditionallyRender
condition={Boolean(header)}
show={
<StyledHeader className={headerClasses}>
<ConditionallyRender
condition={typeof header === 'string'}
show={<PageHeader title={header as string} />}
elseShow={header}
/>
</StyledHeader>
}
/>
<div className={bodyClasses}>{children}</div>
</StyledPaper>
);
if (disableLoading) {
return <div>{content}</div>;
}
return (
<PageContentLoading isLoading={isLoading}>{content}</PageContentLoading>
);
};