EditSecretSheet.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { zodResolver } from '@hookform/resolvers/zod'
  2. import { useParams } from 'common'
  3. import { Eye, EyeOff } from 'lucide-react'
  4. import { useEffect, useState } from 'react'
  5. import { SubmitHandler, useForm } from 'react-hook-form'
  6. import { useLatest } from 'react-use'
  7. import { toast } from 'sonner'
  8. import {
  9. Button,
  10. Form,
  11. FormControl,
  12. FormField,
  13. Input,
  14. Sheet,
  15. SheetContent,
  16. SheetFooter,
  17. SheetHeader,
  18. SheetSection,
  19. SheetTitle,
  20. } from 'ui'
  21. import { Input as PasswordInput } from 'ui-patterns/DataInputs/Input'
  22. import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
  23. import z from 'zod'
  24. import { DiscardChangesConfirmationDialog } from '@/components/ui-patterns/Dialogs/DiscardChangesConfirmationDialog'
  25. import { useSecretsCreateMutation } from '@/data/secrets/secrets-create-mutation'
  26. import { ProjectSecret } from '@/data/secrets/secrets-query'
  27. import { useConfirmOnClose } from '@/hooks/ui/useConfirmOnClose'
  28. const FORM_ID = 'edit-secret-sidepanel'
  29. const FormSchema = z.object({
  30. name: z.string().min(1, 'Please provide a name for your secret'),
  31. value: z.string().min(1, 'Please provide a value for your secret'),
  32. })
  33. type FormSchemaType = z.infer<typeof FormSchema>
  34. interface EditSecretSheetProps {
  35. secret?: ProjectSecret
  36. visible: boolean
  37. onClose: () => void
  38. }
  39. export function EditSecretSheet({ secret, visible, onClose }: EditSecretSheetProps) {
  40. const { ref: projectRef } = useParams()
  41. const secretName = useLatest(secret?.name)
  42. const [showSecretValue, setShowSecretValue] = useState(false)
  43. const form = useForm<FormSchemaType>({
  44. resolver: zodResolver(FormSchema as any),
  45. })
  46. const isValid = form.formState.isValid
  47. const isDirty = form.formState.isDirty
  48. const { mutate: updateSecret, isPending: isUpdating } = useSecretsCreateMutation({
  49. onSuccess: (_, variables) => {
  50. toast.success(`Successfully updated secret "${variables.secrets[0].name}"`)
  51. onClose()
  52. },
  53. })
  54. const onSubmit: SubmitHandler<FormSchemaType> = async ({ name, value }) => {
  55. updateSecret({
  56. projectRef,
  57. secrets: [{ name, value }],
  58. })
  59. }
  60. const { confirmOnClose, handleOpenChange, modalProps } = useConfirmOnClose({
  61. checkIsDirty: () => isDirty,
  62. onClose,
  63. })
  64. useEffect(() => {
  65. if (visible) {
  66. form.reset({ name: secretName.current ?? '', value: '' })
  67. }
  68. }, [form, secretName, visible])
  69. return (
  70. <Sheet open={visible} onOpenChange={handleOpenChange}>
  71. <SheetContent size="default" className={'min-w-screen! lg:min-w-[600px]! flex flex-col'}>
  72. <SheetHeader className="py-3 flex flex-row gap-3 items-center">
  73. <SheetTitle>Edit secret</SheetTitle>
  74. </SheetHeader>
  75. <SheetSection className="h-full">
  76. <Form {...form}>
  77. <form
  78. id={FORM_ID}
  79. className="flex flex-col gap-y-4"
  80. onSubmit={form.handleSubmit(onSubmit)}
  81. >
  82. <FormField
  83. control={form.control}
  84. name="name"
  85. render={({ field }) => (
  86. <FormItemLayout label="Name" layout="horizontal">
  87. <FormControl>
  88. <Input
  89. {...field}
  90. readOnly
  91. className="text-foreground-light! cursor-not-allowed"
  92. />
  93. </FormControl>
  94. </FormItemLayout>
  95. )}
  96. />
  97. <FormField
  98. control={form.control}
  99. name="value"
  100. render={({ field }) => (
  101. <FormItemLayout
  102. label="Value"
  103. layout="horizontal"
  104. description="Secrets can’t be retrieved once saved. Enter a new value to overwrite the existing value."
  105. >
  106. <FormControl>
  107. <PasswordInput
  108. {...field}
  109. type={showSecretValue ? 'text' : 'password'}
  110. placeholder="my-secret-value"
  111. data-1p-ignore
  112. data-lpignore="true"
  113. data-form-type="other"
  114. data-bwignore
  115. actions={
  116. <div className="mr-1">
  117. <Button
  118. type="text"
  119. className="px-1"
  120. icon={showSecretValue ? <EyeOff /> : <Eye />}
  121. onClick={() => setShowSecretValue(!showSecretValue)}
  122. />
  123. </div>
  124. }
  125. />
  126. </FormControl>
  127. </FormItemLayout>
  128. )}
  129. />
  130. </form>
  131. </Form>
  132. </SheetSection>
  133. <SheetFooter>
  134. <Button disabled={isUpdating} type="default" onClick={confirmOnClose}>
  135. Cancel
  136. </Button>
  137. <Button form={FORM_ID} htmlType="submit" disabled={!isValid} loading={isUpdating}>
  138. Save
  139. </Button>
  140. </SheetFooter>
  141. </SheetContent>
  142. <DiscardChangesConfirmationDialog {...modalProps} />
  143. </Sheet>
  144. )
  145. }