diff --git a/apps/studio/components/interfaces/Functions/EdgeFunctionDetails/EdgeFunctionDetails.tsx b/apps/studio/components/interfaces/Functions/EdgeFunctionDetails/EdgeFunctionDetails.tsx
index a88a5a16ebf..fdbab04ad27 100644
--- a/apps/studio/components/interfaces/Functions/EdgeFunctionDetails/EdgeFunctionDetails.tsx
+++ b/apps/studio/components/interfaces/Functions/EdgeFunctionDetails/EdgeFunctionDetails.tsx
@@ -52,7 +52,7 @@ const FormSchema = z.object({
verify_jwt: z.boolean(),
})
-const EdgeFunctionDetails = () => {
+export const EdgeFunctionDetails = () => {
const router = useRouter()
const { ref: projectRef, functionSlug } = useParams()
const [showDeleteModal, setShowDeleteModal] = useState(false)
@@ -365,5 +365,3 @@ const EdgeFunctionDetails = () => {
)
}
-
-export default EdgeFunctionDetails
diff --git a/apps/studio/components/interfaces/Functions/EdgeFunctionsListItem.tsx b/apps/studio/components/interfaces/Functions/EdgeFunctionsListItem.tsx
index 3df7336638d..a0951552654 100644
--- a/apps/studio/components/interfaces/Functions/EdgeFunctionsListItem.tsx
+++ b/apps/studio/components/interfaces/Functions/EdgeFunctionsListItem.tsx
@@ -14,7 +14,7 @@ interface EdgeFunctionsListItemProps {
function: EdgeFunctionsResponse
}
-const EdgeFunctionsListItem = ({ function: item }: EdgeFunctionsListItemProps) => {
+export const EdgeFunctionsListItem = ({ function: item }: EdgeFunctionsListItemProps) => {
const router = useRouter()
const { ref } = useParams()
const { project } = useProjectContext()
@@ -46,7 +46,9 @@ const EdgeFunctionsListItem = ({ function: item }: EdgeFunctionsListItemProps) =
-
{endpoint}
+
+ {endpoint}
+
,
- ]
- : []),
,
+
}>
+
+ Examples
+
+ ,
{
secondaryActions={secondaryActions}
>
- {isLoading && (
-
-
-
- )}
+
+ {isLoading && }
- {isError && }
+ {isError && }
- {isSuccess && (
- <>
- {hasFunctions ? (
-
+ {isSuccess && (
+ <>
+ {hasFunctions ? (
@@ -176,12 +166,12 @@ const EdgeFunctionsPage: NextPageWithLayout = () => {
>
}
/>
-
- ) : (
-
- )}
- >
- )}
+ ) : (
+
+ )}
+ >
+ )}
+
)
diff --git a/apps/studio/pages/project/[ref]/functions/new.tsx b/apps/studio/pages/project/[ref]/functions/new.tsx
index ffa685c6475..9c1bd3d7c51 100644
--- a/apps/studio/pages/project/[ref]/functions/new.tsx
+++ b/apps/studio/pages/project/[ref]/functions/new.tsx
@@ -1,10 +1,10 @@
+import { zodResolver } from '@hookform/resolvers/zod'
import { AlertCircle, Book, Check } from 'lucide-react'
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'
-import { toast } from 'sonner'
import { useForm } from 'react-hook-form'
-import { yupResolver } from '@hookform/resolvers/yup'
-import * as yup from 'yup'
+import { toast } from 'sonner'
+import * as z from 'zod'
import { useParams } from 'common'
import { EDGE_FUNCTION_TEMPLATES } from 'components/interfaces/Functions/Functions.templates'
@@ -41,7 +41,6 @@ import {
TooltipContent,
TooltipTrigger,
} from 'ui'
-import { InfoTooltip } from 'ui-patterns/info-tooltip'
// Array of adjectives and nouns for random function name generation
const ADJECTIVES = [
@@ -73,11 +72,11 @@ const NOUNS = [
const FUNCTION_NAME_REGEX = /^[A-Za-z0-9_-]+$/
// Define form schema with yup
-const formSchema = yup.object({
- functionName: yup
+const FormSchema = z.object({
+ functionName: z
.string()
- .required('Function name is required')
- .matches(FUNCTION_NAME_REGEX, 'Only letters, numbers, hyphens, and underscores allowed'),
+ .min(1, 'Function name is required')
+ .regex(FUNCTION_NAME_REGEX, 'Only letters, numbers, hyphens, and underscores allowed'),
})
// Generate a random function name
@@ -94,11 +93,11 @@ const sanitizeFunctionName = (name: string): string => {
}
// Type for the form values
-type FormValues = yup.InferType
+type FormValues = z.infer
const NewFunctionPage = () => {
const router = useRouter()
- const { ref } = useParams()
+ const { ref, template } = useParams()
const project = useSelectedProject()
const isOptedInToAI = useOrgOptedIntoAi()
const includeSchemaMetadata = isOptedInToAI || !IS_PLATFORM
@@ -119,15 +118,15 @@ const NewFunctionPage = () => {
const [isPreviewingTemplate, setIsPreviewingTemplate] = useState(false)
const [savedCode, setSavedCode] = useState('')
- // Setup form with react-hook-form and yup resolver
const form = useForm({
- resolver: yupResolver(formSchema),
+ resolver: zodResolver(FormSchema),
defaultValues: {
functionName: generateRandomFunctionName(),
},
})
const { mutate: deployFunction, isLoading: isDeploying } = useEdgeFunctionDeployMutation({
+ // [Joshen] To investigate: For some reason, the invalidation for list of edge functions isn't triggering
onSuccess: () => {
toast.success('Successfully deployed edge function')
const functionName = form.getValues('functionName')
@@ -218,6 +217,24 @@ const NewFunctionPage = () => {
}
}, [edgeFunctionCreate, ref, router])
+ useEffect(() => {
+ if (template) {
+ const templateMeta = EDGE_FUNCTION_TEMPLATES.find((x) => x.value === template)
+ if (templateMeta) {
+ form.reset({ functionName: template })
+ setFiles([
+ {
+ id: 1,
+ name: 'index.ts',
+ selected: true,
+ content: templateMeta.content,
+ },
+ ])
+ }
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [template])
+
return (
{
Templates
-
+