CopyEnvButton.tsx 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { Copy } from 'lucide-react'
  2. import { useCallback, useState } from 'react'
  3. import { toast } from 'sonner'
  4. import { Button, copyToClipboard } from 'ui'
  5. import { getDecryptedValue } from '@/data/vault/vault-secret-decrypted-value-query'
  6. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  7. export const CopyEnvButton = ({
  8. serverOptions,
  9. values,
  10. }: {
  11. serverOptions: { name: string; secureEntry: boolean }[]
  12. values: Record<string, string>
  13. }) => {
  14. const { data: project } = useSelectedProjectQuery()
  15. const [isLoading, setIsLoading] = useState(false)
  16. const onCopy = useCallback(async () => {
  17. setIsLoading(true)
  18. const envFile = Promise.all(
  19. serverOptions.map(async (option) => {
  20. if (option.secureEntry) {
  21. const decryptedValue = await getDecryptedValue({
  22. projectRef: project?.ref,
  23. connectionString: project?.connectionString,
  24. id: values[option.name],
  25. })
  26. return `${option.name.toUpperCase().replace('VAULT_', '')}=${decryptedValue[0].decrypted_secret}`
  27. }
  28. return `${option.name.toUpperCase().replace('.', '_')}=${values[option.name]}`
  29. })
  30. ).then((values) => values.join('\n'))
  31. copyToClipboard(envFile, () => {
  32. toast.success('Copied to clipboard as environment variables')
  33. setIsLoading(false)
  34. })
  35. }, [serverOptions, values])
  36. return (
  37. <Button type="default" loading={isLoading} icon={<Copy />} onClick={onCopy}>
  38. Copy all
  39. </Button>
  40. )
  41. }