-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(clerk-react): Improve error message and checks for routing strate…
…gies (#2543)
- Loading branch information
Showing
5 changed files
with
75 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
'@clerk/clerk-react': minor | ||
--- | ||
|
||
Apply the following changes to components with routing props: | ||
- default is `routing="path"` and `path` prop is required to be set via env or context | ||
- when `routing="hash"` or `routing="virtual"` is set the implicit (via env or context) `path` option is ignored | ||
- when `routing="hash"` or `routing="virtual"` then `path` prop is not allowed to be set | ||
|
||
Examples of components with routing props: | ||
- `<CreateOrganization />` | ||
- `<OrganizationProfile />` | ||
- `<SignIn />` | ||
- `<SignUp />` | ||
- `<UserProfile />` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 6 additions & 16 deletions
22
packages/react/src/hooks/__tests__/__snapshots__/useRoutingProps.test.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,35 @@ | ||
import type { RoutingOptions } from '@clerk/types'; | ||
|
||
import { errorThrower } from '../errors/errorThrower'; | ||
import { noPathProvidedError } from '../errors/messages'; | ||
import { incompatibleRoutingWithPathProvidedError, noPathProvidedError } from '../errors/messages'; | ||
|
||
export function useRoutingProps<T extends RoutingOptions>( | ||
componentName: string, | ||
props: T, | ||
routingOptions?: RoutingOptions, | ||
): T { | ||
const path = props.path || routingOptions?.path; | ||
if (!path && !props.routing) { | ||
errorThrower.throw(noPathProvidedError(componentName)); | ||
} | ||
const routing = props.routing || routingOptions?.routing || 'path'; | ||
|
||
if (props.routing && props.routing !== 'path' && routingOptions?.path && !props.path) { | ||
return { | ||
...routingOptions, | ||
...props, | ||
path: undefined, | ||
}; | ||
} | ||
if (routing === 'path') { | ||
if (!path) { | ||
return errorThrower.throw(noPathProvidedError(componentName)); | ||
} | ||
|
||
if (path && !props.routing) { | ||
return { | ||
...routingOptions, | ||
...props, | ||
routing: 'path', | ||
}; | ||
} | ||
|
||
if (props.path) { | ||
return errorThrower.throw(incompatibleRoutingWithPathProvidedError(componentName)); | ||
} | ||
|
||
return { | ||
...routingOptions, | ||
...props, | ||
path: undefined, | ||
}; | ||
} |