11import { isClerkAPIResponseError } from '@clerk/shared/error' ;
22import { __experimental_useAPIKeys as useAPIKeys , useClerk , useOrganization , useUser } from '@clerk/shared/react' ;
3- import type { CreateAPIKeyParams } from '@clerk/shared/types' ;
3+ import type { APIKeyResource } from '@clerk/shared/types' ;
44import { lazy , useState } from 'react' ;
5- import useSWRMutation from 'swr/mutation' ;
65
76import { useProtect } from '@/ui/common' ;
87import { useAPIKeysContext , withCoreUserGuard } from '@/ui/contexts' ;
@@ -94,12 +93,9 @@ export const APIKeysPage = ({ subject, perPage, revokeModalRoot }: APIKeysPagePr
9493 } ;
9594 const card = useCardState ( ) ;
9695 const clerk = useClerk ( ) ;
97- // TODO: Replace useSWRMutation with the react-query equivalent.
98- const {
99- data : createdAPIKey ,
100- trigger : createAPIKey ,
101- isMutating,
102- } = useSWRMutation ( 'api-keys-create' , ( _key , { arg } : { arg : CreateAPIKeyParams } ) => clerk . apiKeys . create ( arg ) ) ;
96+
97+ const [ apiKey , setAPIKey ] = useState < APIKeyResource | null > ( null ) ;
98+
10399 const { t } = useLocalizations ( ) ;
104100 const [ isRevokeModalOpen , setIsRevokeModalOpen ] = useState ( false ) ;
105101 const [ selectedAPIKeyID , setSelectedAPIKeyID ] = useState ( '' ) ;
@@ -108,19 +104,23 @@ export const APIKeysPage = ({ subject, perPage, revokeModalRoot }: APIKeysPagePr
108104
109105 const handleCreateAPIKey = async ( params : OnCreateParams ) => {
110106 try {
111- await createAPIKey ( {
107+ card . setLoading ( ) ;
108+ const apiKey = await clerk . apiKeys . create ( {
112109 ...params ,
113110 subject,
114111 } ) ;
115112 invalidateAll ( ) ;
116113 card . setError ( undefined ) ;
117114 setIsCopyModalOpen ( true ) ;
115+ setAPIKey ( apiKey ) ;
118116 } catch ( err : any ) {
119117 if ( isClerkAPIResponseError ( err ) ) {
120118 if ( err . status === 409 ) {
121119 card . setError ( 'API Key name already exists' ) ;
122120 }
123121 }
122+ } finally {
123+ card . setIdle ( ) ;
124124 }
125125 } ;
126126
@@ -182,10 +182,7 @@ export const APIKeysPage = ({ subject, perPage, revokeModalRoot }: APIKeysPagePr
182182 < Action . Open value = 'add-api-key' >
183183 < Flex sx = { t => ( { paddingTop : t . space . $6 , paddingBottom : t . space . $6 } ) } >
184184 < Action . Card sx = { { width : '100%' } } >
185- < CreateAPIKeyForm
186- onCreate = { handleCreateAPIKey }
187- isSubmitting = { isMutating }
188- />
185+ < CreateAPIKeyForm onCreate = { handleCreateAPIKey } />
189186 </ Action . Card >
190187 </ Flex >
191188 </ Action . Open >
@@ -194,8 +191,8 @@ export const APIKeysPage = ({ subject, perPage, revokeModalRoot }: APIKeysPagePr
194191 isOpen = { isCopyModalOpen }
195192 onOpen = { ( ) => setIsCopyModalOpen ( true ) }
196193 onClose = { ( ) => setIsCopyModalOpen ( false ) }
197- apiKeyName = { createdAPIKey ?. name ?? '' }
198- apiKeySecret = { createdAPIKey ?. secret ?? '' }
194+ apiKeyName = { apiKey ?. name || '' }
195+ apiKeySecret = { apiKey ?. secret || '' }
199196 modalRoot = { revokeModalRoot }
200197 />
201198 </ Action . Root >
0 commit comments