import { format } from 'date-fns'
import { CalendarIcon, ExternalLink } from 'lucide-react'
import { useEffect } from 'react'
import { useFormContext, type Control } from 'react-hook-form'
import ReactMarkdown from 'react-markdown'
import {
Button,
Calendar,
FormControl,
FormInputGroupInput,
Input,
InputGroup,
InputGroupAddon,
Popover,
PopoverContent,
PopoverTrigger,
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
Separator,
SheetSection,
Switch,
Textarea,
FormField as UIFormField,
useWatch,
} from 'ui'
import { Input as DataInput } from 'ui-patterns/DataInputs/Input'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import type { Enum } from './AuthProvidersForm.types'
import { Markdown } from '@/components/interfaces/Markdown'
import { BASE_PATH } from '@/lib/constants'
interface FormFieldProps {
projectRef: string | undefined
organizationSlug: string | undefined
name: string
properties: any
control: Control
hasAccess: boolean
disabled?: boolean
readOnly?: boolean
}
const FormField = ({
projectRef,
name,
properties,
organizationSlug,
control,
hasAccess,
disabled: disabledProp,
readOnly,
}: FormFieldProps) => {
const { setValue } = useFormContext()
const { description: originalDescription } = properties
let description = originalDescription
if (originalDescription && projectRef) {
description = originalDescription.replace(
/\(\.\.\/auth\/(.*?)\)/g,
`(/project/${projectRef}/auth/$1)`
)
}
const fieldValue = useWatch({ control, name })
if (!hasAccess) {
const planMessage = organizationSlug
? `Only available on [Pro plan](/org/${organizationSlug}/billing?panel=subscriptionPlan) and above.`
: ''
description = originalDescription ? `${originalDescription} ${planMessage}` : planMessage
}
const disabled =
disabledProp || (properties.type === 'boolean' ? !hasAccess && !fieldValue : !hasAccess)
const showValue = useWatch({
control,
name: properties.show?.key,
disabled: properties.show == null,
})
useEffect(() => {
if (properties.show?.key != null && !showValue && fieldValue !== '') {
setValue(name, '', { shouldDirty: true })
}
}, [fieldValue, name, properties.show?.key, setValue, showValue])
if (properties.show) {
if (properties.show.matches) {
if (!properties.show.matches.includes(showValue)) {
return null
}
} else if (!showValue) {
return null
}
}
switch (properties.type) {
case 'datetime':
return (
<>
(
{description}
) : null
}
>
}
size="small"
>
{field.value ? format(new Date(field.value), 'PPP') : 'Pick a date'}
{
field.onChange(date?.toISOString())
}}
initialFocus
/>
)}
/>
>
)
case 'string':
return (
<>
(
) : null
}
>
{properties.isSecret ? (
) : (
)}
)}
/>
>
)
case 'multiline-string':
return (
<>
(
) : null
}
>
)}
/>
>
)
case 'number':
return (
<>
(
) : null
}
>
{properties.units ? (
field.onChange(e.target.value === '' ? '' : Number(e.target.value))
}
readOnly={readOnly}
/>
{properties.units}
) : (
field.onChange(e.target.value === '' ? '' : Number(e.target.value))
}
readOnly={readOnly}
/>
)}
)}
/>
>
)
case 'boolean':
return (
<>
(
{description ? : null}
{properties.link && (
}>
Documentation
)}
}
>
)}
/>
>
)
case 'select':
return (
<>
(
{description}
) : null
}
>
)}
/>
>
)
default:
return null
}
}
export default FormField