Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(remix): Automatically infer path to where component is mounted #3104

Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/wicked-worms-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/remix': patch
---

Automatically infer the path for where the component is mounted.
19 changes: 11 additions & 8 deletions packages/remix/src/client/uiComponents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ import type {
} from '@clerk/types';
import React from 'react';

import { useClerkRemixOptions } from './RemixOptionsContext';
import { usePathnameWithoutSplatRouteParams } from './usePathnameWithoutSplatRouteParams';

// The assignment of UserProfile with BaseUserProfile props is used
// to support the CustomPage functionality (eg UserProfile.Page)
// Also the `typeof BaseUserProfile` is used to resolved the following error:
// "The inferred type of 'UserProfile' cannot be named without a reference to ..."
export const UserProfile: typeof BaseUserProfile = Object.assign(
(props: UserProfileProps) => {
return <BaseUserProfile {...useRoutingProps('UserProfile', props)} />;
const path = usePathnameWithoutSplatRouteParams();
return <BaseUserProfile {...useRoutingProps('UserProfile', props, { path })} />;
},
{ ...BaseUserProfile },
);

export const CreateOrganization = (props: CreateOrganizationProps) => {
return <BaseCreateOrganization {...useRoutingProps('CreateOrganization', props)} />;
const path = usePathnameWithoutSplatRouteParams();
return <BaseCreateOrganization {...useRoutingProps('CreateOrganization', props, { path })} />;
};

// The assignment of OrganizationProfile with BaseOrganizationProfile props is used
Expand All @@ -38,17 +40,18 @@ export const CreateOrganization = (props: CreateOrganizationProps) => {
// "The inferred type of 'OrganizationProfile' cannot be named without a reference to ..."
export const OrganizationProfile: typeof BaseOrganizationProfile = Object.assign(
(props: OrganizationProfileProps) => {
return <BaseOrganizationProfile {...useRoutingProps('OrganizationProfile', props)} />;
const path = usePathnameWithoutSplatRouteParams();
return <BaseOrganizationProfile {...useRoutingProps('OrganizationProfile', props, { path })} />;
},
{ ...BaseOrganizationProfile },
);

export const SignIn = (props: SignInProps) => {
const { signInUrl } = useClerkRemixOptions();
return <BaseSignIn {...useRoutingProps('SignIn', props, { path: signInUrl })} />;
const path = usePathnameWithoutSplatRouteParams();
return <BaseSignIn {...useRoutingProps('SignIn', props, { path })} />;
};

export const SignUp = (props: SignUpProps) => {
const { signUpUrl } = useClerkRemixOptions();
return <BaseSignUp {...useRoutingProps('SignUp', props, { path: signUpUrl })} />;
const path = usePathnameWithoutSplatRouteParams();
return <BaseSignUp {...useRoutingProps('SignUp', props, { path })} />;
};
18 changes: 18 additions & 0 deletions packages/remix/src/client/usePathnameWithoutSplatRouteParams.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useLocation, useParams } from '@remix-run/react';

export const usePathnameWithoutSplatRouteParams = () => {
const params = useParams();
const { pathname } = useLocation();

// Get the splat route params
// Remix store splat route params in an object with a key of '*'
// If there are no splat route params, we fallback to an empty string
const splatRouteParam = params['*'] || '';

// Remove the splat route param from the pathname
// so we end up with the pathname where the components are mounted at
// eg /user/123/profile/security will return /user/123/profile as the path
const path = pathname.replace(splatRouteParam, '').replace(/\/$/, '').replace(/^\//, '').trim();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a couple of tests for this? Maybe turn it into a util.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we need to test something here as the test here is going to test if replace works correctly, the only thing we do here is we are removing a specific part of the pathname, and also remove slashes from the start and the end of the string only if they exist, also we don't have unit tests specifically for Remix right now, and this part is very specific to Remix to make it a general util. The only thing I see here we can extract in a util and test it is the part of removing slashes.


return `/${path}`;
};
Loading