import { FC, useEffect, useState } from 'react' import { Button, Input, Form, Modal, Listbox, IconPlus, IconDatabase } from '@supabase/ui' import { PostgresExtension, PostgresSchema } from '@supabase/postgres-meta' import { useStore } from 'hooks' import ShimmeringLoader from 'components/ui/ShimmeringLoader' interface Props { visible: boolean extension: PostgresExtension onCancel: () => void } const EnableExtensionModal: FC = ({ visible, extension, onCancel }) => { const { ui, meta } = useStore() const [defaultSchema, setDefaultSchema] = useState() const [fetchingSchemaInfo, setFetchingSchemaInfo] = useState(false) const schemas = meta.schemas.list() // [Joshen] Worth checking in with users - whether having this schema selection // might be confusing, and if we should have a tooltip to explain that schemas // are just concepts of namespace, you can use that extension no matter where it's // installed in useEffect(() => { let cancel = false if (visible) { const checkExtensionSchema = async () => { if (!cancel) { setFetchingSchemaInfo(true) setDefaultSchema(undefined) } const res = await meta.query( `select * from pg_available_extension_versions where name = '${extension.name}'` ) if (!res.error && !cancel) setDefaultSchema(res[0].schema) setFetchingSchemaInfo(false) } checkExtensionSchema() } return () => { cancel = true } }, [visible]) const validate = (values: any) => { const errors: any = {} if (values.schema === 'custom' && !values.name) errors.name = 'Required field' return errors } const onSubmit = async (values: any, { setSubmitting }: any) => { setSubmitting(true) if (values.schema === 'custom') { const { error } = await meta.query(`create schema if not exists ${values.name}`) if (error) { return ui.setNotification({ error, category: 'error', message: `Failed to create schema: ${error.message}`, }) } } const schema = defaultSchema ? defaultSchema : values.schema === 'custom' ? values.name : values.schema const { error } = await meta.extensions.create({ schema, name: extension.name, version: extension.default_version, cascade: true, }) if (error) { ui.setNotification({ error, category: 'error', message: `Failed to toggle ${extension.name.toUpperCase()}: ${error.message}`, }) } else { ui.setNotification({ category: 'success', message: `${extension.name.toUpperCase()} is on.`, }) } setSubmitting(false) onCancel() } return (
Confirm to enable
{extension.name} } >
{({ isSubmitting, values }: any) => { return (
{fetchingSchemaInfo ? (
) : defaultSchema ? ( ) : ( } > Create a new schema "{extension.name}" {/* @ts-ignore */} {schemas.map((schema: PostgresSchema) => { return ( } > {schema.name} ) })} )}
{values.schema === 'custom' && ( )}
) }}
) } export default EnableExtensionModal