Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions packages/react/src/reactrouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,26 @@ function computeRootMatch(pathname: string): Match {
return { path: '/', url: '/', params: {}, isExact: pathname === '/' };
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function withSentryRouting<P extends Record<string, any>>(Route: React.ComponentType<P>): React.FC<P> {
const componentDisplayName = Route.displayName || Route.name;
/* eslint-disable @typescript-eslint/no-explicit-any */
export function withSentryRouting<P extends Record<string, any>, R extends React.ComponentType<P>>(Route: R): R {
const componentDisplayName = (Route as any).displayName || (Route as any).name;

const WrappedRoute: React.FC<P> = (props: P) => {
if (activeTransaction && props && props.computedMatch && props.computedMatch.isExact) {
activeTransaction.setName(props.computedMatch.path);
}

// @ts-ignore Setting more specific React Component typing for `R` generic above
// will break advanced type inference done by react router params:
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/13dc4235c069e25fe7ee16e11f529d909f9f3ff8/types/react-router/index.d.ts#L154-L164
return <Route {...props} />;
};

WrappedRoute.displayName = `sentryRoute(${componentDisplayName})`;
hoistNonReactStatics(WrappedRoute, Route);
// @ts-ignore Setting more specific React Component typing for `R` generic above
// will break advanced type inference done by react router params:
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/13dc4235c069e25fe7ee16e11f529d909f9f3ff8/types/react-router/index.d.ts#L154-L164
return WrappedRoute;
}
/* eslint-enable @typescript-eslint/no-explicit-any */