Skip to content

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

fix: Fix the Edit policy panel work (#40660)

Ivan Vasilov2f337882fa0e
diff --git a/apps/studio/components/interfaces/Auth/Policies/PolicyEditorPanel/index.tsx b/apps/studio/components/interfaces/Auth/Policies/PolicyEditorPanel/index.tsx
index 5e9c7a45edc..27ff753378a 100644
--- a/apps/studio/components/interfaces/Auth/Policies/PolicyEditorPanel/index.tsx
+++ b/apps/studio/components/interfaces/Auth/Policies/PolicyEditorPanel/index.tsx
@@ -52,6 +52,22 @@ interface PolicyEditorPanelProps {
authContext: 'database' | 'realtime'
}
+const FORM_ID = 'rls-editor'
+const FormSchema = z.object({
+ name: z.string().min(1, 'Please provide a name'),
+ table: z.string(),
+ behavior: z.string(),
+ command: z.string(),
+ roles: z.string(),
+})
+const defaultValues = {
+ name: '',
+ table: '',
+ behavior: 'permissive',
+ command: 'select',
+ roles: '',
+}
+
/**
* Using memo for this component because everything rerenders on window focus because of outside fetches
*/
@@ -96,21 +112,6 @@ export const PolicyEditorPanel = memo(function ({
const [showTools, setShowTools] = useState<boolean>(false)
- const formId = 'rls-editor'
- const FormSchema = z.object({
- name: z.string().min(1, 'Please provide a name'),
- table: z.string(),
- behavior: z.string(),
- command: z.string(),
- roles: z.string(),
- })
- const defaultValues = {
- name: '',
- table: '',
- behavior: 'permissive',
- command: 'select',
- roles: '',
- }
const form = useForm<z.infer<typeof FormSchema>>({
mode: 'onBlur',
reValidateMode: 'onBlur',
@@ -290,7 +291,7 @@ export const PolicyEditorPanel = memo(function ({
return (
<>
<Form_Shadcn_ {...form}>
- <form id={formId} onSubmit={form.handleSubmit(onSubmit)}>
+ <form id={FORM_ID} onSubmit={form.handleSubmit(onSubmit)}>
<Sheet open={visible} onOpenChange={confirmOnClose}>
<SheetContent
showClose={false}
@@ -490,7 +491,7 @@ export const PolicyEditorPanel = memo(function ({
</Button>
<ButtonTooltip
- form={formId}
+ form={FORM_ID}
htmlType="submit"
loading={isExecuting || isUpdating}
disabled={!canUpdatePolicies || isExecuting || isUpdating}
diff --git a/apps/studio/pages/project/[ref]/auth/policies.tsx b/apps/studio/pages/project/[ref]/auth/policies.tsx
index 222c75969b4..e017abc8a68 100644
--- a/apps/studio/pages/project/[ref]/auth/policies.tsx
+++ b/apps/studio/pages/project/[ref]/auth/policies.tsx
@@ -25,9 +25,9 @@ import { useProjectPostgrestConfigQuery } from 'data/config/project-postgrest-co
import { useDatabasePoliciesQuery } from 'data/database-policies/database-policies-query'
import { useTablesQuery } from 'data/tables/tables-query'
import { useAsyncCheckPermissions } from 'hooks/misc/useCheckPermissions'
+import { useQueryStateWithSelect } from 'hooks/misc/useQueryStateWithSelect'
import { useSelectedProjectQuery } from 'hooks/misc/useSelectedProject'
import { useIsProtectedSchema } from 'hooks/useProtectedSchemas'
-import { useQueryStateWithSelect } from 'hooks/misc/useQueryStateWithSelect'
import { DOCS_URL } from 'lib/constants'
import { useEditorPanelStateSnapshot } from 'state/editor-panel-state'
import { useSidebarManagerSnapshot } from 'state/sidebar-manager-state'
@@ -206,6 +206,8 @@ const AuthPoliciesPage: NextPageWithLayout = () => {
const handleResetSearch = useCallback(() => setSearchString(''), [setSearchString])
+ const isUpdatingPolicy = !!selectedPolicyIdToEdit
+
if (isPermissionsLoaded && !canReadPolicies) {
return <NoPermission isFullPage resourceText="view this project's RLS policies" />
}
@@ -276,28 +278,20 @@ const AuthPoliciesPage: NextPageWithLayout = () => {
</PoliciesDataProvider>
)}
- {/* Create Policy */}
+ {/* Create or Edit Policy */}
<PolicyEditorPanel
- visible={showCreatePolicy}
+ visible={showCreatePolicy || isUpdatingPolicy}
schema={schema}
searchString={searchString}
- selectedTable={selectedTable}
+ selectedTable={isUpdatingPolicy ? undefined : selectedTable}
+ selectedPolicy={isUpdatingPolicy ? selectedPolicyIdToEdit : undefined}
onSelectCancel={() => {
setSelectedTable(undefined)
- setShowCreatePolicy(false)
- }}
- authContext="database"
- />
-
- {/* Edit Policy */}
- <PolicyEditorPanel
- visible={!!selectedPolicyIdToEdit}
- schema={schema}
- searchString={searchString}
- selectedPolicy={selectedPolicyIdToEdit}
- onSelectCancel={() => {
- setSelectedTable(undefined)
- setSelectedPolicyIdToEdit(null)
+ if (isUpdatingPolicy) {
+ setSelectedPolicyIdToEdit(null)
+ } else {
+ setShowCreatePolicy(false)
+ }
}}
authContext="database"
/>