mirror of
https://github.com/supabase/supabase.git
synced 2026-09-08 19:08:44 +08:00
This PR disabled generating snippet titles when running and disables the "generate titles" buttons in Rename Snippet and Save Snippet dialogs (which is accessed through the side SQL Editor). How to test: 1. Disable AI for an org. 2. Try to run a new snippet, it shouldn't be renamed automatically. 3. Right click it, click Rename. The "generate title" in the dialog should be disabled with a reason in a tooltip. 4. Open the side SQL Editor, write "select 1", click Save snippet. The "generate title" in the dialog should be disabled. Testing the same flows for HIPAA projects should say `This feature is not available for HIPAA projects.` <img width="715" height="833" alt="Screenshot 2026-06-15 at 23 02 15" src="https://github.com/user-attachments/assets/f9b68f2f-5a5a-4a66-bd0d-9245f4e2f78e" /> <img width="948" height="845" alt="Screenshot 2026-06-15 at 23 02 01" src="https://github.com/user-attachments/assets/b0c9a90e-6cc1-4262-a246-88617cec41dc" /> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * AI feature availability is now gated by organization-level AI opt-in settings instead of subscription-based HIPAA add-ons. * **Bug Fixes** * Updated "Generate with AI" buttons to display disabled state with contextual messaging (missing API key, organization AI opt-out, HIPAA project restriction, or generation in progress). <!-- end of auto-generated comment: release notes by coderabbit.ai -->
63 lines
1.5 KiB
TypeScript
63 lines
1.5 KiB
TypeScript
import { useMutation } from '@tanstack/react-query'
|
|
import { toast } from 'sonner'
|
|
|
|
import { constructHeaders, fetchHandler } from '@/data/fetchers'
|
|
import { BASE_PATH } from '@/lib/constants'
|
|
import { ResponseError, UseCustomMutationOptions } from '@/types'
|
|
|
|
type SqlTitleGenerateResponse = {
|
|
title: string
|
|
description: string
|
|
}
|
|
|
|
type SqlTitleGenerateVariables = {
|
|
sql: string
|
|
}
|
|
|
|
async function generateSqlTitle({ sql }: SqlTitleGenerateVariables) {
|
|
const url = `${BASE_PATH}/api/ai/sql/title-v2`
|
|
|
|
const headers = await constructHeaders({ 'Content-Type': 'application/json' })
|
|
const response = await fetchHandler(url, {
|
|
headers,
|
|
method: 'POST',
|
|
body: JSON.stringify({
|
|
sql,
|
|
}),
|
|
})
|
|
let body: any
|
|
|
|
try {
|
|
body = await response.json()
|
|
} catch {}
|
|
|
|
if (!response.ok) {
|
|
throw new ResponseError(body?.message, response.status)
|
|
}
|
|
|
|
return body as SqlTitleGenerateResponse
|
|
}
|
|
|
|
type SqlTitleGenerateData = Awaited<ReturnType<typeof generateSqlTitle>>
|
|
|
|
export const useSqlTitleGenerateMutation = ({
|
|
onSuccess,
|
|
onError,
|
|
...options
|
|
}: Omit<
|
|
UseCustomMutationOptions<SqlTitleGenerateData, ResponseError, SqlTitleGenerateVariables>,
|
|
'mutationFn'
|
|
> = {}) => {
|
|
return useMutation<SqlTitleGenerateData, ResponseError, SqlTitleGenerateVariables>({
|
|
mutationFn: (vars) => generateSqlTitle(vars),
|
|
async onError(data, variables, context) {
|
|
if (onError === undefined) {
|
|
toast.error(`Failed to generate title: ${data.message}`)
|
|
} else {
|
|
onError(data, variables, context)
|
|
}
|
|
},
|
|
...options,
|
|
})
|
|
}
|