Skip to content

Commit

Permalink
extract util
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcarpenter committed Dec 11, 2024
1 parent 5dec045 commit e58dc76
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
10 changes: 3 additions & 7 deletions packages/clerk-js/src/ui/contexts/components/SignIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useEnvironment, useOptions } from '../../contexts';
import type { ParsedQueryString } from '../../router';
import { useRouter } from '../../router';
import type { SignInCtx } from '../../types';
import { getInitialValuesFromQueryParams } from '../utils';
import { getInitialValuesFromQueryParams, getRedirectUrlFromMode } from '../utils';

export type SignInContextType = SignInCtx & {
navigateAfterSignIn: () => any;
Expand Down Expand Up @@ -55,12 +55,8 @@ export const useSignInContext = (): SignInContextType => {
queryParams,
);

const getRedirectUrl = (url: string) => {
return mode === 'modal' && url === '/' ? window.location.href : clerk.buildUrlWithAuth(url);
};

const afterSignInUrl = getRedirectUrl(redirectUrls.getAfterSignInUrl());
const afterSignUpUrl = getRedirectUrl(redirectUrls.getAfterSignUpUrl());
const afterSignInUrl = getRedirectUrlFromMode({ mode, url: redirectUrls.getAfterSignInUrl(), clerk });
const afterSignUpUrl = getRedirectUrlFromMode({ mode, url: redirectUrls.getAfterSignUpUrl(), clerk });

const navigateAfterSignIn = () => navigate(afterSignInUrl);

Expand Down
10 changes: 3 additions & 7 deletions packages/clerk-js/src/ui/contexts/components/SignUp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useEnvironment, useOptions } from '../../contexts';
import type { ParsedQueryString } from '../../router';
import { useRouter } from '../../router';
import type { SignUpCtx } from '../../types';
import { getInitialValuesFromQueryParams } from '../utils';
import { getInitialValuesFromQueryParams, getRedirectUrlFromMode } from '../utils';

export type SignUpContextType = SignUpCtx & {
navigateAfterSignUp: () => any;
Expand Down Expand Up @@ -53,12 +53,8 @@ export const useSignUpContext = (): SignUpContextType => {
queryParams,
);

const getRedirectUrl = (url: string) => {
return mode === 'modal' && url === '/' ? window.location.href : clerk.buildUrlWithAuth(url);
};

const afterSignInUrl = getRedirectUrl(redirectUrls.getAfterSignInUrl());
const afterSignUpUrl = getRedirectUrl(redirectUrls.getAfterSignUpUrl());
const afterSignInUrl = getRedirectUrlFromMode({ mode, url: redirectUrls.getAfterSignInUrl(), clerk });
const afterSignUpUrl = getRedirectUrlFromMode({ mode, url: redirectUrls.getAfterSignUpUrl(), clerk });

const navigateAfterSignUp = () => navigate(afterSignUpUrl);

Expand Down
5 changes: 5 additions & 0 deletions packages/clerk-js/src/ui/contexts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ export function getInitialValuesFromQueryParams(queryString: string, params: str
}

export const populateParamFromObject = createDynamicParamParser({ regex: /:(\w+)/ });

type Mode = 'modal' | 'mounted' | undefined;
export const getRedirectUrlFromMode = ({ mode, url, clerk }: { mode: Mode; url: string; clerk: Clerk }) => {
return mode === 'modal' && url === '/' ? window.location.href : clerk.buildUrlWithAuth(url);
};

0 comments on commit e58dc76

Please sign in to comment.