CreateNewAPIKeysButton.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { useParams } from 'common'
  2. import { useState } from 'react'
  3. import { toast } from 'sonner'
  4. import {
  5. AlertDialog,
  6. AlertDialogAction,
  7. AlertDialogCancel,
  8. AlertDialogContent,
  9. AlertDialogDescription,
  10. AlertDialogFooter,
  11. AlertDialogHeader,
  12. AlertDialogTitle,
  13. Button,
  14. } from 'ui'
  15. import { useAPIKeyCreateMutation } from '@/data/api-keys/api-key-create-mutation'
  16. export const CreateNewAPIKeysButton = () => {
  17. const { ref: projectRef } = useParams()
  18. const [isCreatingKeys, setIsCreatingKeys] = useState(false)
  19. const [createKeysDialogOpen, setCreateKeysDialogOpen] = useState(false)
  20. const { mutateAsync: createAPIKey } = useAPIKeyCreateMutation()
  21. const handleCreateNewApiKeys = async () => {
  22. if (!projectRef) return
  23. setIsCreatingKeys(true)
  24. try {
  25. // Create publishable key
  26. await createAPIKey({ projectRef, type: 'publishable', name: 'default' })
  27. // Create secret key
  28. await createAPIKey({ projectRef, type: 'secret', name: 'default' })
  29. setCreateKeysDialogOpen(false)
  30. toast.success('Successfully created a new set of API keys!')
  31. } catch (error) {
  32. console.error('Failed to create API keys:', error)
  33. } finally {
  34. setIsCreatingKeys(false)
  35. }
  36. }
  37. return (
  38. <AlertDialog open={createKeysDialogOpen} onOpenChange={setCreateKeysDialogOpen}>
  39. <Button onClick={() => setCreateKeysDialogOpen(true)}>Create new API keys</Button>
  40. <AlertDialogContent>
  41. <AlertDialogHeader>
  42. <AlertDialogTitle>Create new API keys</AlertDialogTitle>
  43. <AlertDialogDescription>
  44. This will create a default publishable key and a default secret key both named{' '}
  45. <code className="break-keep! text-code-inline">default</code>. These keys are required
  46. to connect your application to your Briven project.
  47. </AlertDialogDescription>
  48. </AlertDialogHeader>
  49. <AlertDialogFooter>
  50. <AlertDialogCancel>Cancel</AlertDialogCancel>
  51. <AlertDialogAction onClick={handleCreateNewApiKeys} disabled={isCreatingKeys}>
  52. {isCreatingKeys ? 'Creating...' : 'Create keys'}
  53. </AlertDialogAction>
  54. </AlertDialogFooter>
  55. </AlertDialogContent>
  56. </AlertDialog>
  57. )
  58. }