{"version":3,"file":"startReactRouterView.mjs","names":[],"sources":["../../../src/domain/reactRouter/startReactRouterView.ts"],"sourcesContent":["import { display } from '@datadog/browser-core'\nimport { onRumInit } from '../reactPlugin'\nimport type { AnyRouteMatch } from './types'\n\nexport function startReactRouterView(routeMatches: AnyRouteMatch[]) {\n  onRumInit((configuration, rumPublicApi) => {\n    if (!configuration.router) {\n      display.warn('`router: true` is missing from the react plugin configuration, the view will not be tracked.')\n      return\n    }\n    rumPublicApi.startView(computeViewName(routeMatches))\n  })\n}\n\nexport function computeViewName(routeMatches: AnyRouteMatch[]) {\n  if (!routeMatches || routeMatches.length === 0) {\n    return ''\n  }\n\n  let viewName = '/'\n\n  for (const routeMatch of routeMatches) {\n    let path = routeMatch.route.path\n    if (!path) {\n      continue\n    }\n\n    path = substitutePathSplats(path, routeMatch.params, routeMatch === routeMatches[routeMatches.length - 1])\n\n    if (path.startsWith('/')) {\n      // Absolute path, replace the current view name\n      viewName = path\n    } else {\n      // Relative path, append to the current view name\n      if (!viewName.endsWith('/')) {\n        viewName += '/'\n      }\n      viewName += path\n    }\n  }\n\n  return viewName\n}\n\n/**\n * React-Router allows to define routes with \"splats\" (or \"catchall\" or \"star\") segments[1],\n * example: /files/*. It has been noticed that keeping those splats in the view name isn't helpful\n * as it \"hides\" too much information. This function replaces the splats with the actual URL path\n * name that they are matching.\n *\n * [1]: https://reactrouter.com/en/main/route/route#splats\n *\n * @example\n * substitutePathSplats('/files/*', { '*': 'path/to/file' }, true) // => '/files/path/to/file'\n */\nfunction substitutePathSplats(path: string, params: Record<string, string | undefined>, isLastMatchingRoute: boolean) {\n  if (\n    !path.includes('*') ||\n    // In some edge cases, react-router does not provide the `*` parameter, so we don't know what to\n    // replace it with. In this case, we keep the asterisk.\n    params['*'] === undefined\n  ) {\n    return path\n  }\n\n  // The `*` parameter is only related to the last matching route path.\n  if (isLastMatchingRoute) {\n    return path.replace(/\\*/, params['*'])\n  }\n\n  // Intermediary route paths with a `*` are kind of edge cases, and the `*` parameter is not\n  // relevant for them. We remove it from the path (along with a potential slash preceeding it) to\n  // have a coherent view name once everything is concatenated (see examples in spec file).\n  return path.replace(/\\/?\\*/, '')\n}\n"],"mappings":";;;AAIA,SAAgB,qBAAqB,cAA+B;CAClE,WAAW,eAAe,iBAAiB;EACzC,IAAI,CAAC,cAAc,QAAQ;GACzB,QAAQ,KAAK,8FAA8F;GAC3G;EACF;EACA,aAAa,UAAU,gBAAgB,YAAY,CAAC;CACtD,CAAC;AACH;AAEA,SAAgB,gBAAgB,cAA+B;CAC7D,IAAI,CAAC,gBAAgB,aAAa,WAAW,GAC3C,OAAO;CAGT,IAAI,WAAW;CAEf,KAAK,MAAM,cAAc,cAAc;EACrC,IAAI,OAAO,WAAW,MAAM;EAC5B,IAAI,CAAC,MACH;EAGF,OAAO,qBAAqB,MAAM,WAAW,QAAQ,eAAe,aAAa,aAAa,SAAS,EAAE;EAEzG,IAAI,KAAK,WAAW,GAAG,GAErB,WAAW;OACN;GAEL,IAAI,CAAC,SAAS,SAAS,GAAG,GACxB,YAAY;GAEd,YAAY;EACd;CACF;CAEA,OAAO;AACT;;;;;;;;;;;;AAaA,SAAS,qBAAqB,MAAc,QAA4C,qBAA8B;CACpH,IACE,CAAC,KAAK,SAAS,GAAG,KAGlB,OAAO,SAAS,KAAA,GAEhB,OAAO;CAIT,IAAI,qBACF,OAAO,KAAK,QAAQ,MAAM,OAAO,IAAI;CAMvC,OAAO,KAAK,QAAQ,SAAS,EAAE;AACjC"}