Files
Joshen Lim cab0585533 Fe 1799/consolidate to useselectedprojectquery and (#37684)
* Replace all usage of useProjectContext with useSelectedProjectQuery

* Replace all usage of useSelectedProject with useSelectedProjectQuery

* Replace all usage of useProjectByRef with useProjectByRefQuery

* Replace all usage of useSelectedOrganization with useSelectedOrganizationQuery

* Deprecate useSelectedProject, useSelectedOrganization, and useProjectByRef hooks

* Deprecate ProjecContext
2025-08-06 10:53:10 +07:00

63 lines
1.8 KiB
TypeScript

import { Copy } from 'lucide-react'
import { useCallback, useState } from 'react'
import { toast } from 'sonner'
import { ButtonTooltip } from 'components/ui/ButtonTooltip'
import { getDecryptedValue } from 'data/vault/vault-secret-decrypted-value-query'
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
import { copyToClipboard } from 'ui'
export const CopyEnvButton = ({
serverOptions,
values,
}: {
serverOptions: { name: string; secureEntry: boolean }[]
values: Record<string, string>
}) => {
const { data: project } = useSelectedProjectQuery()
const [isLoading, setIsLoading] = useState(false)
const onCopy = useCallback(async () => {
setIsLoading(true)
const envFile = Promise.all(
serverOptions.map(async (option) => {
if (option.secureEntry) {
const decryptedValue = await getDecryptedValue({
projectRef: project?.ref,
connectionString: project?.connectionString,
id: values[option.name],
})
return `${option.name.toUpperCase().replace('VAULT_', '')}=${decryptedValue[0].decrypted_secret}`
}
return `${option.name.toUpperCase().replace('.', '_')}=${values[option.name]}`
})
).then((values) => values.join('\n'))
copyToClipboard(envFile, () => {
toast.success('Copied to clipboard')
setIsLoading(false)
})
}, [serverOptions, values])
return (
<ButtonTooltip
type="primary"
loading={isLoading}
icon={<Copy />}
onClick={onCopy}
tooltip={{
content: {
text: (
<span>
Copies an <span className="text-brand">.env file</span> with the configuration details
to your clipboard.
</span>
),
},
}}
>
Copy env values
</ButtonTooltip>
)
}