import { Location } from 'history' import { memo, ReactNode } from 'react' import { Redirect, Route, RouteProps } from 'react-router-dom' import { join } from 'path' import { getUserId } from '@utils/storage' import { isURLAvailable } from '@utils/permissions' export type PrivateRouteProps = RouteProps & { root?: string path: string children?: ReactNode redirect?: (location?: Location) => ReactNode } export const defaultRedirect = (location?: Location) => ( ) export const PrivateRoute = memo(({ root = '', path, component, children, redirect = defaultRedirect, ...other }) => { const available = isURLAvailable(join(root, path)) return ( available ? children : redirect(location)} /> ) }) export default PrivateRoute