Skip to content

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

Set different colors for RLS templates UPDATE and ALL, and add placeholders with using and with check monaco fields (#22401)

Joshen Lim1f388cc98ea1
diff --git a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyTemplates.tsx b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyTemplates.tsx
index aa4d199eed2..520534f9a50 100644
--- a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyTemplates.tsx
+++ b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyTemplates.tsx
@@ -83,10 +83,15 @@ export const PolicyTemplates = ({
icon={
<div className="min-w-16">
<Badge
- className="!rounded font-mono"
+ className={cn(
+ '!rounded font-mono',
+ template.command === 'UPDATE'
+ ? 'bg-blue-400 text-blue-900 border border-blue-800'
+ : ''
+ )}
variant={
template.command === 'ALL'
- ? 'default'
+ ? 'outline'
: template.command === 'SELECT'
? 'brand'
: template.command === 'UPDATE'
diff --git a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/RLSCodeEditor.tsx b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/RLSCodeEditor.tsx
index 4e43237932b..e51b2284519 100644
--- a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/RLSCodeEditor.tsx
+++ b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/RLSCodeEditor.tsx
@@ -22,6 +22,7 @@ interface RLSCodeEditorProps {
placeholder?: string
readOnly?: boolean
+ disableTabToUsePlaceholder?: boolean
lineNumberStart?: number
onChange?: () => void
onMount?: () => void
@@ -39,6 +40,7 @@ const RLSCodeEditor = ({
placeholder,
readOnly = false,
+ disableTabToUsePlaceholder = false,
lineNumberStart,
onChange = noop,
onMount: _onMount = noop,
@@ -50,6 +52,7 @@ const RLSCodeEditor = ({
const monaco = useMonaco()
const { project } = useProjectContext()
+ const placeholderId = `monaco-placeholder-${id}`
const options: editor.IStandaloneEditorConstructionOptions = {
tabSize: 2,
fontSize: 13,
@@ -70,27 +73,31 @@ const RLSCodeEditor = ({
if (monacoRef !== undefined) monacoRef.current = monaco
hasValue.current = editor.createContextKey('hasValue', false)
- const placeholderEl = document.querySelector('.monaco-placeholder') as HTMLElement | null
- if (placeholderEl && placeholder !== undefined) placeholderEl.style.display = 'block'
-
- editor.addCommand(
- monaco.KeyCode.Tab,
- () => {
- editor.executeEdits('source', [
- {
- // @ts-ignore
- identifier: 'add-placeholder',
- range: new monaco.Range(1, 1, 1, 1),
- text: (placeholder ?? '')
- .split('\n\n')
- .join('\n')
- .replaceAll('*', '')
- .replaceAll('&nbsp;', ''),
- },
- ])
- },
- '!hasValue'
- )
+ const placeholderEl = document.getElementById(placeholderId) as HTMLElement | null
+ if (placeholderEl && placeholder !== undefined && (value ?? '').trim().length === 0) {
+ placeholderEl.style.display = 'block'
+ }
+
+ if (!disableTabToUsePlaceholder) {
+ editor.addCommand(
+ monaco.KeyCode.Tab,
+ () => {
+ editor.executeEdits('source', [
+ {
+ // @ts-ignore
+ identifier: 'add-placeholder',
+ range: new monaco.Range(1, 1, 1, 1),
+ text: (placeholder ?? '')
+ .split('\n\n')
+ .join('\n')
+ .replaceAll('*', '')
+ .replaceAll('&nbsp;', ''),
+ },
+ ])
+ },
+ '!hasValue'
+ )
+ }
editor.focus()
@@ -100,7 +107,7 @@ const RLSCodeEditor = ({
const onChangeContent: OnChange = (value) => {
hasValue.current.set((value ?? '').length > 0)
- const placeholderEl = document.querySelector('.monaco-placeholder') as HTMLElement | null
+ const placeholderEl = document.getElementById(placeholderId) as HTMLElement | null
if (placeholderEl) {
if (!value) {
placeholderEl.style.display = 'block'
@@ -148,6 +155,13 @@ const RLSCodeEditor = ({
}
}, [monaco])
+ useEffect(() => {
+ if (value !== undefined && value.trim().length > 0) {
+ const placeholderEl = document.getElementById(placeholderId) as HTMLElement | null
+ if (placeholderEl) placeholderEl.style.display = 'none'
+ }
+ }, [value])
+
return (
<>
<Editor
@@ -164,6 +178,7 @@ const RLSCodeEditor = ({
/>
{placeholder !== undefined && (
<div
+ id={placeholderId}
className="monaco-placeholder absolute top-[0px] left-[57px] text-sm pointer-events-none font-mono [&>div>p]:text-foreground-lighter [&>div>p]:!m-0 tracking-tighter"
style={{ display: 'none' }}
>
diff --git a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx
index 616c8d14015..fc78e0cbeba 100644
--- a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx
+++ b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx
@@ -551,14 +551,20 @@ export const AIPolicyEditorPanel = memo(function ({
/>
<div
- className={`py-1 relative ${incomingChange ? 'hidden' : 'block'}`}
+ className={`mt-1 relative ${incomingChange ? 'hidden' : 'block'}`}
style={{
height:
expOneContentHeight <= 100 ? `${8 + expOneContentHeight}px` : '108px',
}}
>
<RLSCodeEditor
+ disableTabToUsePlaceholder
id="rls-exp-one-editor"
+ placeholder={
+ command === 'insert'
+ ? '-- Provide a SQL expression for the with check statement'
+ : '-- Provide a SQL expression for the using statement'
+ }
defaultValue={command === 'insert' ? check : using}
value={command === 'insert' ? check : using}
editorRef={editorOneRef}
@@ -608,7 +614,7 @@ export const AIPolicyEditorPanel = memo(function ({
{showCheckBlock && (
<>
<div
- className={`py-1 relative ${incomingChange ? 'hidden' : 'block'}`}
+ className={`mt-1 relative ${incomingChange ? 'hidden' : 'block'}`}
style={{
height:
expTwoContentHeight <= 100
@@ -617,7 +623,9 @@ export const AIPolicyEditorPanel = memo(function ({
}}
>
<RLSCodeEditor
+ disableTabToUsePlaceholder
id="rls-exp-two-editor"
+ placeholder="-- Provide a SQL expression for the with check statement"
defaultValue={check}
value={check}
editorRef={editorTwoRef}
diff --git a/apps/studio/data/database/schemas-query.ts b/apps/studio/data/database/schemas-query.ts
index f986fdf5702..b9bfaee5713 100644
--- a/apps/studio/data/database/schemas-query.ts
+++ b/apps/studio/data/database/schemas-query.ts
@@ -1,10 +1,9 @@
-import { QueryClient, UseQueryOptions } from '@tanstack/react-query'
import pgMeta from '@supabase/pg-meta'
+import { QueryClient, UseQueryOptions } from '@tanstack/react-query'
import { z } from 'zod'
-import { databaseKeys } from './keys'
import { ExecuteSqlData, useExecuteSqlQuery } from 'data/sql/execute-sql-query'
-import { ResponseError } from 'types'
+import { databaseKeys } from './keys'
export type SchemasVariables = {
projectRef?: string