import { FC, useState } from 'react' import ReactMarkdown from 'react-markdown' import * as Tooltip from '@radix-ui/react-tooltip' import { PermissionAction } from '@supabase/shared-types/out/constants' import { Alert, Button, Collapsible, Form, IconCheck, IconChevronUp, Input } from '@supabase/ui' import { useStore, checkPermissions } from 'hooks' import { Provider } from './AuthProvidersForm.types' import { ProviderCollapsibleClasses } from './AuthProvidersForm.constants' import FormField from './FormField' interface Props { provider: Provider } const ProviderForm: FC = ({ provider }) => { const [open, setOpen] = useState(false) const { authConfig, ui } = useStore() const doubleNegativeKeys = ['MAILER_AUTOCONFIRM', 'SMS_AUTOCONFIRM'] const canUpdateConfig = checkPermissions(PermissionAction.UPDATE, 'custom_config_gotrue') const generateInitialValues = () => { const initialValues: { [x: string]: string } = {} Object.keys(provider.properties).forEach((key) => { // When the key is a 'double negative' key, we must reverse the boolean before adding it to the form const isDoubleNegative = doubleNegativeKeys.includes(key) initialValues[key] = isDoubleNegative ? !authConfig.config[key] : authConfig.config[key] ?? '' }) return initialValues } const isActive = authConfig.config[`EXTERNAL_${provider?.title?.toUpperCase()}_ENABLED`] const INITIAL_VALUES = generateInitialValues() const onSubmit = async (values: any, { setSubmitting, resetForm }: any) => { const payload = { ...values } // When the key is a 'double negative' key, we must reverse the boolean before the payload can be sent Object.keys(values).map((x: string) => { if (doubleNegativeKeys.includes(x)) { payload[x] = !values[x] } }) const { error } = await authConfig.update(payload) if (!error) { resetForm({ values: payload, initialValues: payload }) setOpen(false) ui.setNotification({ category: 'success', message: 'Successfully updated settings' }) } else { ui.setNotification({ error, category: 'error', message: `Failed to update settings: ${error?.message}`, }) } setSubmitting(false) } return (
{({ isSubmitting, handleReset, initialValues, values }: any) => { const noChanges = JSON.stringify(initialValues) === JSON.stringify(values) return (
{Object.keys(provider.properties).map((x: string) => ( ))} {provider?.misc?.alert && ( {provider.misc.alert.description} )} {provider.misc.requiresRedirect && ( <> {provider.misc.helper} )}
{!canUpdateConfig && (
You need additional permissions to update provider settings
)}
) }}
) } export default ProviderForm