Skip to content

Commit c258f7a

Browse files
committed
remove useSWRMutation from APIKeys.tsx
1 parent 5c4823d commit c258f7a

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

packages/clerk-js/src/ui/components/APIKeys/APIKeys.tsx

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { isClerkAPIResponseError } from '@clerk/shared/error';
22
import { __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';
44
import { lazy, useState } from 'react';
5-
import useSWRMutation from 'swr/mutation';
65

76
import { useProtect } from '@/ui/common';
87
import { 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>

packages/clerk-js/src/ui/components/APIKeys/CreateAPIKeyForm.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React, { useMemo, useRef, useState } from 'react';
33
import { useAPIKeysContext } from '@/ui/contexts';
44
import { Box, Col, descriptors, FormLabel, localizationKeys, Text, useLocalizations } from '@/ui/customizables';
55
import { useActionContext } from '@/ui/elements/Action/ActionRoot';
6+
import { useCardState } from '@/ui/elements/contexts';
67
import { Form } from '@/ui/elements/Form';
78
import { FormButtons } from '@/ui/elements/FormButtons';
89
import { FormContainer } from '@/ui/elements/FormContainer';
@@ -28,7 +29,6 @@ export type OnCreateParams = {
2829

2930
interface CreateAPIKeyFormProps {
3031
onCreate: (params: OnCreateParams) => void;
31-
isSubmitting: boolean;
3232
}
3333

3434
const EXPIRATION_DURATIONS: Record<Exclude<Expiration, 'never'>, (date: Date) => void> = {
@@ -117,10 +117,11 @@ const ExpirationSelector: React.FC<ExpirationSelectorProps> = ({ selectedExpirat
117117
);
118118
};
119119

120-
export const CreateAPIKeyForm: React.FC<CreateAPIKeyFormProps> = ({ onCreate, isSubmitting }) => {
120+
export const CreateAPIKeyForm: React.FC<CreateAPIKeyFormProps> = ({ onCreate }) => {
121121
const [selectedExpiration, setSelectedExpiration] = useState<ExpirationOption | null>(null);
122122
const { close: closeCardFn } = useActionContext();
123123
const { showDescription = false } = useAPIKeysContext();
124+
const card = useCardState();
124125
const { t } = useLocalizations();
125126

126127
const nameField = useFormControl('name', '', {
@@ -251,7 +252,7 @@ export const CreateAPIKeyForm: React.FC<CreateAPIKeyFormProps> = ({ onCreate, is
251252
submitLabel={localizationKeys('apiKeys.formButtonPrimary__add')}
252253
isDisabled={!canSubmit}
253254
onReset={closeCardFn}
254-
isLoading={isSubmitting}
255+
isLoading={card.isLoading}
255256
elementDescriptor={descriptors.apiKeysCreateFormSubmitButton}
256257
/>
257258
</Form.Root>

0 commit comments

Comments
 (0)