soapbox/src/features/ui/util/react-router-helpers.tsx

110 wiersze
2.8 KiB
TypeScript
Czysty Zwykły widok Historia

import React, { Suspense } from 'react';
2022-04-26 18:36:08 +00:00
import { Redirect, Route, useHistory, RouteProps, RouteComponentProps, match as MatchType } from 'react-router-dom';
2022-04-26 17:01:57 +00:00
import { Layout } from 'soapbox/components/ui';
2022-04-26 17:01:57 +00:00
import { useOwnAccount, useSettings } from 'soapbox/hooks';
2022-11-16 13:32:32 +00:00
import ColumnForbidden from '../components/column-forbidden';
import ColumnLoading from '../components/column-loading';
import ColumnsArea from '../components/columns-area';
2022-04-26 17:01:57 +00:00
type PageProps = {
params?: MatchType['params'];
layout?: any;
children: React.ReactNode;
2022-04-26 17:01:57 +00:00
};
2022-04-26 18:36:08 +00:00
interface IWrappedRoute extends RouteProps {
component: React.LazyExoticComponent<any>;
page?: React.ComponentType<PageProps>;
content?: React.ReactNode;
componentParams?: Record<string, any>;
layout?: any;
publicRoute?: boolean;
staffOnly?: boolean;
adminOnly?: boolean;
developerOnly?: boolean;
2022-04-26 17:01:57 +00:00
}
const WrappedRoute: React.FC<IWrappedRoute> = ({
component: Component,
2022-04-26 17:01:57 +00:00
page: Page,
content,
componentParams = {},
layout,
publicRoute = false,
staffOnly = false,
adminOnly = false,
developerOnly = false,
...rest
}) => {
const history = useHistory();
2023-06-25 17:35:09 +00:00
const { account } = useOwnAccount();
2022-04-26 17:01:57 +00:00
const settings = useSettings();
const renderComponent = ({ match }: RouteComponentProps) => {
if (Page) {
return (
<Suspense fallback={renderLoading()}>
<Page params={match.params} layout={layout} {...componentParams}>
<Component params={match.params} {...componentParams}>
{content}
</Component>
</Page>
</Suspense>
2022-04-26 17:01:57 +00:00
);
}
return (
<Suspense fallback={renderLoading()}>
<ColumnsArea layout={layout}>
<Component params={match.params} {...componentParams}>
{content}
</Component>
</ColumnsArea>
</Suspense>
2022-04-26 17:01:57 +00:00
);
};
const renderWithLayout = (children: JSX.Element) => (
<>
<Layout.Main>
{children}
</Layout.Main>
2022-04-26 17:01:57 +00:00
<Layout.Aside />
</>
);
2022-04-26 17:01:57 +00:00
const renderLoading = () => renderWithLayout(<ColumnLoading />);
const renderForbidden = () => renderWithLayout(<ColumnForbidden />);
2022-04-26 17:01:57 +00:00
const loginRedirect = () => {
const actualUrl = encodeURIComponent(`${history.location.pathname}${history.location.search}`);
localStorage.setItem('soapbox:redirect_uri', actualUrl);
return <Redirect to='/login' />;
2022-04-26 17:01:57 +00:00
};
const authorized = [
account || publicRoute,
developerOnly ? settings.get('isDeveloper') : true,
staffOnly ? account && account.staff : true,
adminOnly ? account && account.admin : true,
].every(c => c);
if (!authorized) {
if (!account) {
return loginRedirect();
} else {
return renderForbidden();
}
}
return <Route {...rest} render={renderComponent} />;
2022-04-26 17:01:57 +00:00
};
export {
WrappedRoute,
};