@@ -15,7 +15,6 @@ import { createRoot } from 'react-dom/client';
15
15
16
16
import { PRESERVED_QUERYSTRING_PARAMS } from '../core/constants' ;
17
17
import { clerkUIErrorDOMElementNotFound } from '../core/errors' ;
18
- import { getClerkQueryParam , removeClerkQueryParam } from '../utils' ;
19
18
import { CreateOrganization , CreateOrganizationModal } from './components/CreateOrganization' ;
20
19
import { ImpersonationFab } from './components/ImpersonationFab' ;
21
20
import { OrganizationProfile , OrganizationProfileModal } from './components/OrganizationProfile' ;
@@ -28,7 +27,7 @@ import { EnvironmentProvider, OptionsProvider } from './contexts';
28
27
import { CoreClerkContextWrapper } from './contexts/CoreClerkContextWrapper' ;
29
28
import { AppearanceProvider } from './customizables' ;
30
29
import { FlowMetadataProvider , Modal } from './elements' ;
31
- import { useSafeLayoutEffect } from './hooks' ;
30
+ import { useReadParamState , useSafeLayoutEffect } from './hooks' ;
32
31
import Portal from './portal' ;
33
32
import { VirtualRouter } from './router' ;
34
33
import { InternalThemeProvider } from './styledSystem' ;
@@ -135,14 +134,13 @@ export type MountComponentRenderer = typeof mountComponentRenderer;
135
134
136
135
const componentsControls = { } as ComponentControls ;
137
136
138
- // TODO: move this elsewhere
139
137
const componentNodes = Object . freeze ( {
140
138
SignUp : 'signUpModal' ,
141
139
SignIn : 'signInModal' ,
142
140
UserProfile : 'userProfileModal' ,
143
141
OrganizationProfile : 'organizationProfileModal' ,
144
142
CreateOrganization : 'createOrganizationModal' ,
145
- } ) ;
143
+ } ) as any ;
146
144
147
145
const Components = ( props : ComponentsProps ) => {
148
146
const [ state , setState ] = React . useState < ComponentsState > ( {
@@ -158,19 +156,10 @@ const Components = (props: ComponentsProps) => {
158
156
const { signInModal, signUpModal, userProfileModal, organizationProfileModal, createOrganizationModal, nodes } =
159
157
state ;
160
158
161
- const [ urlState , setUrlState ] = React . useState ( null ) ;
162
-
163
- const readAndRemoveStateParam = ( ) => {
164
- const urlClerkState = getClerkQueryParam ( '__clerk_state' ) ?? '' ;
165
- removeClerkQueryParam ( '__clerk_state' ) ;
166
- return urlClerkState ? JSON . parse ( atob ( urlClerkState ) ) : null ;
167
- } ;
168
-
169
- const decodedRedirectParams = readAndRemoveStateParam ( ) ?? '' ;
159
+ const { urlStateParam, clearUrlStateParam, decodedRedirectParams } = useReadParamState ( ) ;
170
160
171
161
useSafeLayoutEffect ( ( ) => {
172
162
if ( decodedRedirectParams ) {
173
- setUrlState ( decodedRedirectParams ) ;
174
163
setState ( s => ( {
175
164
...s ,
176
165
[ componentNodes [ decodedRedirectParams . componentName ] ] : true ,
@@ -207,7 +196,7 @@ const Components = (props: ComponentsProps) => {
207
196
} ;
208
197
209
198
componentsControls . closeModal = name => {
210
- setUrlState ( null ) ;
199
+ clearUrlStateParam ( ) ;
211
200
setState ( s => ( { ...s , [ name + 'Modal' ] : null } ) ) ;
212
201
} ;
213
202
@@ -230,7 +219,7 @@ const Components = (props: ComponentsProps) => {
230
219
< VirtualRouter
231
220
preservedParams = { PRESERVED_QUERYSTRING_PARAMS }
232
221
onExternalNavigate = { ( ) => componentsControls . closeModal ( 'signIn' ) }
233
- startPath = { state . options ?. startPath || '/sign-in' }
222
+ startPath = { '/sign-in' + urlStateParam ?. path }
234
223
>
235
224
< SignInModal { ...signInModal } />
236
225
< SignUpModal
@@ -257,7 +246,7 @@ const Components = (props: ComponentsProps) => {
257
246
< VirtualRouter
258
247
preservedParams = { PRESERVED_QUERYSTRING_PARAMS }
259
248
onExternalNavigate = { ( ) => componentsControls . closeModal ( 'signUp' ) }
260
- startPath = { state . options ?. startPath || '/sign-up' }
249
+ startPath = { '/sign-up' + urlStateParam ?. path }
261
250
>
262
251
< SignInModal
263
252
afterSignInUrl = { signUpModal ?. afterSignInUrl }
@@ -288,8 +277,7 @@ const Components = (props: ComponentsProps) => {
288
277
< VirtualRouter
289
278
preservedParams = { PRESERVED_QUERYSTRING_PARAMS }
290
279
onExternalNavigate = { ( ) => componentsControls . closeModal ( 'userProfile' ) }
291
- // startPath={state.options?.startPath || '/user'}
292
- startPath = { `/user${ urlState ?. path || '' } ` }
280
+ startPath = { '/user' + urlStateParam ?. path }
293
281
>
294
282
< UserProfileModal />
295
283
</ VirtualRouter >
@@ -315,7 +303,7 @@ const Components = (props: ComponentsProps) => {
315
303
< VirtualRouter
316
304
preservedParams = { PRESERVED_QUERYSTRING_PARAMS }
317
305
onExternalNavigate = { ( ) => componentsControls . closeModal ( 'organizationProfile' ) }
318
- startPath = { state . options ?. startPath || '/organization' }
306
+ startPath = { '/organization' + urlStateParam ?. path }
319
307
>
320
308
< OrganizationProfileModal { ...organizationProfileModal } />
321
309
</ VirtualRouter >
@@ -341,7 +329,7 @@ const Components = (props: ComponentsProps) => {
341
329
< VirtualRouter
342
330
preservedParams = { PRESERVED_QUERYSTRING_PARAMS }
343
331
onExternalNavigate = { ( ) => componentsControls . closeModal ( 'createOrganization' ) }
344
- startPath = { state . options ?. startPath || '/create-organization' }
332
+ startPath = { '/create-organization' + urlStateParam ?. path }
345
333
>
346
334
< CreateOrganizationModal { ...createOrganizationModal } />
347
335
</ VirtualRouter >
0 commit comments