Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Show confirm dialog if text editor is edited when clicking on toggle feature preview button (#21251)

Joshen Lim64c6ad53ca11
diff --git a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx
index c71622413f5..e525696f33c 100644
--- a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx
+++ b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx
@@ -1,10 +1,11 @@
import { PostgresPolicy } from '@supabase/postgres-meta'
import { useQueryClient } from '@tanstack/react-query'
import { useChat } from 'ai/react'
-import { useParams } from 'common'
+import { useParams, useTelemetryProps } from 'common'
import { uniqBy } from 'lodash'
import { FileDiff } from 'lucide-react'
import dynamic from 'next/dynamic'
+import { useRouter } from 'next/router'
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import toast from 'react-hot-toast'
import { Button, Modal, SheetContent_Shadcn_, SheetFooter_Shadcn_, Sheet_Shadcn_, cn } from 'ui'
@@ -21,6 +22,8 @@ import { QueryResponseError, useExecuteSqlMutation } from 'data/sql/execute-sql-
import { useSelectedOrganization, useSelectedProject } from 'hooks'
import { BASE_PATH, LOCAL_STORAGE_KEYS, OPT_IN_TAGS } from 'lib/constants'
import { uuidv4 } from 'lib/helpers'
+import Telemetry from 'lib/telemetry'
+import { useAppStateSnapshot } from 'state/app-state'
import { AIPolicyChat } from './AIPolicyChat'
import {
MessageWithDebug,
@@ -32,10 +35,6 @@ import { AIPolicyHeader } from './AIPolicyHeader'
import PolicyDetails from './PolicyDetails'
import QueryError from './QueryError'
import RLSCodeEditor from './RLSCodeEditor'
-import Telemetry from 'lib/telemetry'
-import { useTelemetryProps } from 'common'
-import { useRouter } from 'next/router'
-import { useAppStateSnapshot } from 'state/app-state'
const DiffEditor = dynamic(
() => import('@monaco-editor/react').then(({ DiffEditor }) => DiffEditor),
@@ -68,6 +67,7 @@ export const AIPolicyEditorPanel = memo(function ({
const [chatId, setChatId] = useState(uuidv4())
const editorRef = useRef<IStandaloneCodeEditor | null>(null)
const diffEditorRef = useRef<IStandaloneDiffEditor | null>(null)
+ const isTogglingPreviewRef = useRef<boolean>(false)
const placeholder = generatePlaceholder(selectedPolicy)
const isOptedInToAI = selectedOrganization?.opt_in_tags?.includes(OPT_IN_TAGS.AI_SQL) ?? false
@@ -177,20 +177,23 @@ export const AIPolicyEditorPanel = memo(function ({
setIncomingChange(undefined)
}, [incomingChange])
- function toggleFeaturePreviewModal() {
- snap.setSelectedFeaturePreview(LOCAL_STORAGE_KEYS.UI_PREVIEW_RLS_AI_ASSISTANT)
- snap.setShowFeaturePreviewModal(!snap.showFeaturePreviewModal)
- onSelectCancel()
+ const toggleFeaturePreviewModal = () => {
+ isTogglingPreviewRef.current = true
+ onClosingPanel()
}
- const onClosingPanel = useCallback(() => {
+ const onClosingPanel = () => {
const policy = editorRef.current?.getValue()
if (policy || messages.length > 0 || !isAssistantChatInputEmpty) {
setIsClosingPolicyEditorPanel(true)
} else {
+ if (isTogglingPreviewRef.current) {
+ snap.setSelectedFeaturePreview(LOCAL_STORAGE_KEYS.UI_PREVIEW_RLS_AI_ASSISTANT)
+ snap.setShowFeaturePreviewModal(!snap.showFeaturePreviewModal)
+ }
onSelectCancel()
}
- }, [onSelectCancel, messages, isAssistantChatInputEmpty])
+ }
const onSelectDebug = async () => {
const policy = editorRef.current?.getValue().replaceAll('\n', ' ').replaceAll(' ', ' ')
@@ -398,9 +401,17 @@ export const AIPolicyEditorPanel = memo(function ({
visible={isClosingPolicyEditorPanel}
header="Discard changes"
buttonLabel="Discard"
- onSelectCancel={() => setIsClosingPolicyEditorPanel(false)}
+ onSelectCancel={() => {
+ isTogglingPreviewRef.current = false
+ setIsClosingPolicyEditorPanel(false)
+ }}
onSelectConfirm={() => {
+ if (isTogglingPreviewRef.current) {
+ snap.setSelectedFeaturePreview(LOCAL_STORAGE_KEYS.UI_PREVIEW_RLS_AI_ASSISTANT)
+ snap.setShowFeaturePreviewModal(!snap.showFeaturePreviewModal)
+ }
onSelectCancel()
+ isTogglingPreviewRef.current = false
setIsClosingPolicyEditorPanel(false)
}}
>