Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow sql language functions #25986

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { zodResolver } from '@hookform/resolvers/zod'
import { PostgresFunction } from '@supabase/postgres-meta'
import { isEmpty, isNull, keyBy, mapValues, partition } from 'lodash'
import { useEffect, useState } from 'react'
import { Plus, Trash } from 'lucide-react'
import { useEffect, useMemo, useState } from 'react'
import { SubmitHandler, useFieldArray, useForm } from 'react-hook-form'
import toast from 'react-hot-toast'
import z from 'zod'
import { Plus, Trash } from 'lucide-react'

import { POSTGRES_DATA_TYPES } from 'components/interfaces/TableGridEditor/SidePanelEditor/SidePanelEditor.constants'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
Expand All @@ -25,21 +25,21 @@ import {
FormMessage_Shadcn_,
Form_Shadcn_,
Input_Shadcn_,
Modal,
Radio,
ScrollArea,
SelectContent_Shadcn_,
SelectItem_Shadcn_,
SelectTrigger_Shadcn_,
SelectValue_Shadcn_,
Select_Shadcn_,
Separator,
Sheet,
ScrollArea,
SheetContent,
SheetFooter,
SheetSection,
Toggle,
cn,
useWatch_Shadcn_,
} from 'ui'
import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
Expand Down Expand Up @@ -256,7 +256,11 @@ const CreateFunction = ({ func, visible, setVisible }: CreateFunctionProps) => {
</FormLabel_Shadcn_>
<FormDescription_Shadcn_ className="text-sm text-foreground-light">
<p>
The language below should be written in <code>plpgsql</code>.
The language below should be written in{' '}
<code>
<FormLanguage />
</code>
.
</p>
{!isEditing && <p>Change the language in the Advanced Settings below.</p>}
</FormDescription_Shadcn_>
Expand Down Expand Up @@ -593,15 +597,32 @@ const FormFieldConfigParams = ({ readonly }: FormFieldConfigParamsProps) => {
)
}

const ALL_ALLOWED_LANGUAGES = ['plpgsql', 'sql', 'plcoffee', 'plv8', 'plls']

const FormFieldLanguage = () => {
const { project } = useProjectContext()

const { data } = useDatabaseExtensionsQuery({
projectRef: project?.ref,
connectionString: project?.connectionString,
})
const { data: enabledExtensions } = useDatabaseExtensionsQuery(
{
projectRef: project?.ref,
connectionString: project?.connectionString,
},
{
select(data) {
return partition(data, (ext) => !isNull(ext.installed_version))[0]
},
}
)

const allowedLanguages = useMemo(() => {
return ALL_ALLOWED_LANGUAGES.filter((lang) => {
if (lang.startsWith('pl')) {
return enabledExtensions?.find((ex) => ex.name === lang) !== undefined
}

const [enabledExtensions] = partition(data ?? [], (ext) => !isNull(ext.installed_version))
return true
})
}, [enabledExtensions])

return (
<FormField_Shadcn_
Expand All @@ -614,19 +635,21 @@ const FormFieldLanguage = () => {
<SelectValue_Shadcn_ />
</SelectTrigger_Shadcn_>
<SelectContent_Shadcn_>
{enabledExtensions
.filter((ex) => {
return ex.name.startsWith('pl')
})
.map((option) => (
<SelectItem_Shadcn_ value={option.name} key={option.name}>
{option.name}
</SelectItem_Shadcn_>
))}
{allowedLanguages.map((option) => (
<SelectItem_Shadcn_ value={option} key={option}>
{option}
</SelectItem_Shadcn_>
))}
</SelectContent_Shadcn_>
</Select_Shadcn_>
</FormItemLayout>
)}
/>
)
}

const FormLanguage = () => {
const language = useWatch_Shadcn_({ name: 'language' })

return language
}
1 change: 1 addition & 0 deletions packages/ui/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export {
FormDescription as FormDescription_Shadcn_,
FormMessage as FormMessage_Shadcn_,
FormField as FormField_Shadcn_,
useWatch as useWatch_Shadcn_,
} from './src/components/shadcn/ui/form'

export {
Expand Down
2 changes: 2 additions & 0 deletions packages/ui/src/components/shadcn/ui/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
FieldValues,
FormProvider,
useFormContext,
useWatch,
} from 'react-hook-form'

import { cn } from '../../../lib/utils/cn'
Expand Down Expand Up @@ -172,4 +173,5 @@ export {
FormLabel,
FormMessage,
useFormField,
useWatch,
}
Loading