import React from 'react'; import { Redirect, Route, useHistory, RouteProps, RouteComponentProps, match as MatchType } from 'react-router-dom'; import { Layout } from 'soapbox/components/ui'; import { useOwnAccount, useSettings } from 'soapbox/hooks'; import BundleColumnError from '../components/bundle-column-error'; import ColumnForbidden from '../components/column-forbidden'; import ColumnLoading from '../components/column-loading'; import ColumnsArea from '../components/columns-area'; import BundleContainer from '../containers/bundle-container'; type PageProps = { params?: MatchType['params'], layout?: any, children: React.ReactNode, }; interface IWrappedRoute extends RouteProps { component: (...args: any[]) => any, page?: React.ComponentType, content?: React.ReactNode, componentParams?: Record, layout?: any, publicRoute?: boolean, staffOnly?: boolean, adminOnly?: boolean, developerOnly?: boolean, } const WrappedRoute: React.FC = ({ component, page: Page, content, componentParams = {}, layout, publicRoute = false, staffOnly = false, adminOnly = false, developerOnly = false, ...rest }) => { const history = useHistory(); const account = useOwnAccount(); const settings = useSettings(); const renderComponent = ({ match }: RouteComponentProps) => { if (Page) { return ( {Component => ( {content} ) } ); } return ( {Component => ( {content} ) } ); }; const renderWithLayout = (children: JSX.Element) => ( <> {children} ); const renderLoading = () => renderWithLayout(); const renderForbidden = () => renderWithLayout(); const renderError = (props: any) => renderWithLayout(); const loginRedirect = () => { const actualUrl = encodeURIComponent(`${history.location.pathname}${history.location.search}`); localStorage.setItem('soapbox:redirect_uri', actualUrl); return ; }; 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 ; }; export { WrappedRoute, };