findFirstRouteWithoutRouteParam.tsx 737 B

123456789101112131415161718192021
  1. import {PlainRoute} from 'react-router';
  2. /**
  3. * For all routes with a `path`, find the first route without a route param (e.g. :apiKey)
  4. *
  5. * @param routes A list of react-router route objects
  6. * @param route If given, will only take into account routes between `route` and end of the routes list
  7. * @return Object Returns a react-router route object
  8. */
  9. export default function findFirstRouteWithoutRouteParam(
  10. routes: PlainRoute[],
  11. route?: PlainRoute
  12. ) {
  13. const routeIndex = route !== undefined ? routes.indexOf(route) : -1;
  14. const routesToSearch = route && routeIndex > -1 ? routes.slice(routeIndex) : routes;
  15. return (
  16. routesToSearch.filter(({path}) => !!path).find(({path}) => !path?.includes(':')) ||
  17. route
  18. );
  19. }