The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat: Use URL query params in Auth Policies page (#22786)
Ivan Vasilov2dad02de62e1
diff --git a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/LockedQuerySection.tsx b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/LockedQuerySection.tsxindex 02dd1daac6b..224a8499203 100644--- a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/LockedQuerySection.tsx+++ b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/LockedQuerySection.tsx@@ -3,11 +3,11 @@ import { Lock } from 'lucide-react'import { useRouter } from 'next/router'import { useParams } from 'common'-import { useTableEditorStateSnapshot } from 'state/table-editor'import { Button } from 'ui'import { generateAlterPolicyQuery, generateCreatePolicyQuery } from './AIPolicyEditorPanel.utils'interface LockedCreateQuerySection {+ schema: stringselectedPolicy?: PostgresPolicyformFields: { name: string; table: string; behavior: string; command: string; roles: string }editorOneRef: any@@ -15,6 +15,7 @@ interface LockedCreateQuerySection {}export const LockedCreateQuerySection = ({+ schema,selectedPolicy,formFields,editorOneRef,@@ -22,7 +23,6 @@ export const LockedCreateQuerySection = ({}: LockedCreateQuerySection) => {const router = useRouter()const { ref } = useParams()- const state = useTableEditorStateSnapshot()const isEditing = selectedPolicy !== undefinedconst { name, table, behavior, command, roles } = formFields@@ -44,7 +44,7 @@ export const LockedCreateQuerySection = ({const query = isEditing? generateCreatePolicyQuery({name,- schema: state.selectedSchemaName,+ schema,table,behavior,command,@@ -58,7 +58,7 @@ export const LockedCreateQuerySection = ({: generateAlterPolicyQuery({name: '',newName: name,- schema: state.selectedSchemaName,+ schema,table,command,roles: roles.length === 0 ? 'public' : roles,@@ -84,7 +84,7 @@ export const LockedCreateQuerySection = ({<div className="flex items-start" style={{ fontSize: '14px' }}><p className="px-6 font-mono text-sm text-foreground-light select-none">2</p><p className="font-mono tracking-tighter">- <span className="text-[#569cd6]">on</span> "{state.selectedSchemaName}"."+ <span className="text-[#569cd6]">on</span> "{schema}"."{table}"</p></div>@@ -124,16 +124,16 @@ export const LockedCreateQuerySection = ({export const LockedRenameQuerySection = ({oldName,newName,+ schema,table,lineNumber,}: {oldName: stringnewName: string+ schema: stringtable: stringlineNumber: number}) => {- const state = useTableEditorStateSnapshot()-return (<div className="bg-surface-300 py-1"><div className="flex items-center" style={{ fontSize: '14px' }}>@@ -153,7 +153,7 @@ export const LockedRenameQuerySection = ({</p></div><p className="font-mono tracking-tighter">- <span className="text-[#569cd6]">on</span> "{state.selectedSchemaName}"."{table}"+ <span className="text-[#569cd6]">on</span> "{schema}"."{table}"</p></div><div className="flex items-center" style={{ fontSize: '14px' }}>diff --git a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyDetailsV2.tsx b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyDetailsV2.tsxindex 07fb57e485f..a7d1e848a92 100644--- a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyDetailsV2.tsx+++ b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyDetailsV2.tsx@@ -1,10 +1,8 @@import { useEffect } from 'react'-import { useParams } from 'common'import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'import { useDatabaseRolesQuery } from 'data/database-roles/database-roles-query'import { useTablesQuery } from 'data/tables/tables-query'-import { useTableEditorStateSnapshot } from 'state/table-editor'import {FormControl_Shadcn_,FormField_Shadcn_,@@ -24,6 +22,7 @@ import {import { MultiSelectV2 } from 'ui-patterns/MultiSelect/MultiSelectV2'interface PolicyDetailsV2Props {+ schema: stringsearchString?: stringisEditing: booleanform: any@@ -31,18 +30,18 @@ interface PolicyDetailsV2Props {}export const PolicyDetailsV2 = ({+ schema,searchString,isEditing,form,onUpdateCommand,}: PolicyDetailsV2Props) => {const { project } = useProjectContext()- const snap = useTableEditorStateSnapshot()const { data: tables, isSuccess: isSuccessTables } = useTablesQuery({projectRef: project?.ref,connectionString: project?.connectionString,- schema: snap.selectedSchemaName,+ schema: schema,sortByProperty: 'name',includeColumns: true,})@@ -66,7 +65,7 @@ export const PolicyDetailsV2 = ({if (!isEditing) {const table = tables?.find((table) =>- table.schema === snap.selectedSchemaName &&+ table.schema === schema &&(table.id.toString() === searchString || table.name === searchString))if (table) {@@ -117,7 +116,7 @@ export const PolicyDetailsV2 = ({onValueChange={(value) => form.setValue('table', value)}><SelectTrigger_Shadcn_ className="text-sm h-10">- {snap.selectedSchemaName}.{field.value}+ {schema}.{field.value}</SelectTrigger_Shadcn_><SelectContent_Shadcn_><SelectGroup_Shadcn_>diff --git a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyTemplates.tsx b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyTemplates.tsxindex 520534f9a50..73fefa60219 100644--- a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyTemplates.tsx+++ b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/PolicyTemplates.tsx@@ -9,9 +9,9 @@ import CardButton from 'components/ui/CardButton'import CopyButton from 'components/ui/CopyButton'import NoSearchResults from 'components/ui/NoSearchResults'import { getGeneralPolicyTemplates } from '../PolicyEditorModal/PolicyEditorModal.constants'-import { useTableEditorStateSnapshot } from 'state/table-editor'interface PolicyTemplatesProps {+ schema: stringtable: stringselectedPolicy?: PostgresPolicyselectedTemplate?: string@@ -19,17 +19,14 @@ interface PolicyTemplatesProps {}export const PolicyTemplates = ({+ schema,table,selectedPolicy,selectedTemplate,onSelectTemplate,}: PolicyTemplatesProps) => {const [search, setSearch] = useState('')- const state = useTableEditorStateSnapshot()- const templates = getGeneralPolicyTemplates(- state.selectedSchemaName,- table.length > 0 ? table : 'table_name'- )+ const templates = getGeneralPolicyTemplates(schema, table.length > 0 ? table : 'table_name')const baseTemplates =selectedPolicy !== undefineddiff --git a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsxindex fc78e0cbeba..730e5ce1e5e 100644--- a/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx+++ b/apps/studio/components/interfaces/Auth/Policies/AIPolicyEditorPanel/index.tsx@@ -15,7 +15,6 @@ import {Checkbox_Shadcn_,Form_Shadcn_,Label_Shadcn_,- Modal,ScrollArea,Sheet,SheetContent,@@ -45,7 +44,6 @@ import { useSelectedOrganization, useSelectedProject } from 'hooks'import { BASE_PATH, OPT_IN_TAGS } from 'lib/constants'import { uuidv4 } from 'lib/helpers'import Telemetry from 'lib/telemetry'-import { useTableEditorStateSnapshot } from 'state/table-editor'import ConfirmationModal from 'ui-patterns/Dialogs/ConfirmationModal'import { AIPolicyChat } from './AIPolicyChat'import {@@ -70,6 +68,7 @@ const DiffEditor = dynamic(interface AIPolicyEditorPanelProps {visible: boolean+ schema: stringsearchString?: stringselectedPolicy?: PostgresPolicyonSelectCancel: () => void@@ -80,6 +79,7 @@ interface AIPolicyEditorPanelProps {*/export const AIPolicyEditorPanel = memo(function ({visible,+ schema,searchString,selectedPolicy,onSelectCancel,@@ -91,7 +91,6 @@ export const AIPolicyEditorPanel = memo(function ({const selectedOrganization = useSelectedOrganization()const telemetryProps = useTelemetryProps()- const state = useTableEditorStateSnapshot()const isAiAssistantEnabled = useIsRLSAIAssistantEnabled()// [Joshen] Hyrid form fields, just spit balling to get a decent POC out@@ -330,7 +329,7 @@ export const AIPolicyEditorPanel = memo(function ({if (selectedPolicy === undefined) {const sql = generateCreatePolicyQuery({name: name,- schema: state.selectedSchemaName,+ schema,table,behavior,command,@@ -534,6 +533,7 @@ export const AIPolicyEditorPanel = memo(function ({) : (<><PolicyDetailsV2+ schema={schema}searchString={searchString}isEditing={selectedPolicy !== undefined}form={form}@@ -544,6 +544,7 @@ export const AIPolicyEditorPanel = memo(function ({/><div className="h-full"><LockedCreateQuerySection+ schema={schema}selectedPolicy={selectedPolicy}editorOneRef={editorOneRef}editorTwoRef={editorTwoRef}@@ -670,6 +671,7 @@ export const AIPolicyEditorPanel = memo(function ({<LockedRenameQuerySectionoldName={selectedPolicy.name}newName={name}+ schema={schema}table={table}lineNumber={8 + expOneLineCount + (showCheckBlock ? expTwoLineCount : 0)@@ -789,6 +791,7 @@ export const AIPolicyEditorPanel = memo(function ({><ScrollArea className="h-full w-full"><PolicyTemplates+ schema={schema}table={table}selectedPolicy={selectedPolicy}selectedTemplate={selectedDiff}diff --git a/apps/studio/components/interfaces/Auth/Policies/Policies.tsx b/apps/studio/components/interfaces/Auth/Policies/Policies.tsxindex 14b063ce0f6..62c9dfe12e6 100644--- a/apps/studio/components/interfaces/Auth/Policies/Policies.tsx+++ b/apps/studio/components/interfaces/Auth/Policies/Policies.tsx@@ -14,10 +14,10 @@ import ProductEmptyState from 'components/to-be-cleaned/ProductEmptyState'import InformationBox from 'components/ui/InformationBox'import { useDatabasePolicyDeleteMutation } from 'data/database-policies/database-policy-delete-mutation'import { useTableUpdateMutation } from 'data/tables/table-update-mutation'-import { useTableEditorStateSnapshot } from 'state/table-editor'import ConfirmModal from 'ui-patterns/Dialogs/ConfirmDialog'interface PoliciesProps {+ schema: stringtables: PostgresTable[]hasTables: booleanisLocked: boolean@@ -25,6 +25,7 @@ interface PoliciesProps {}const Policies = ({+ schema,tables,hasTables,isLocked,@@ -33,7 +34,6 @@ const Policies = ({const router = useRouter()const { ref } = useParams()const { project } = useProjectContext()- const snap = useTableEditorStateSnapshot()const [selectedTableToToggleRLS, setSelectedTableToToggleRLS] = useState<any>({})const [selectedPolicyToDelete, setSelectedPolicyToDelete] = useState<any>({})@@ -137,7 +137,7 @@ const Policies = ({return (<><div className="flex flex-col gap-y-4 pb-4">- {isLocked && <ProtectedSchemaWarning schema={snap.selectedSchemaName} entity="policies" />}+ {isLocked && <ProtectedSchemaWarning schema={schema} entity="policies" />}{tables.length > 0 ? (tables.map((table: any) => (<section key={table.id}>diff --git a/apps/studio/components/interfaces/TableGridEditor/GridHeaderActions.tsx b/apps/studio/components/interfaces/TableGridEditor/GridHeaderActions.tsxindex 0bd784d291c..14204939c98 100644--- a/apps/studio/components/interfaces/TableGridEditor/GridHeaderActions.tsx+++ b/apps/studio/components/interfaces/TableGridEditor/GridHeaderActions.tsx@@ -162,7 +162,7 @@ const GridHeaderActions = ({ table, isViewSelected, isTableSelected }: GridHeade><LinkpassHref- href={`/project/${projectRef}/auth/policies?search=${table.id}`}+ href={`/project/${projectRef}/auth/policies?search=${table.id}&schema=${table.schema}`}>Add RLS policy</Link>@@ -202,7 +202,10 @@ const GridHeaderActions = ({ table, isViewSelected, isTableSelected }: GridHeade)}>- <Link passHref href={`/project/${projectRef}/auth/policies?search=${table.id}`}>+ <Link+ passHref+ href={`/project/${projectRef}/auth/policies?search=${table.id}&schema=${table.schema}`}+ >Auth {policies.length > 1 ? 'policies' : 'policy'}</Link></Button>diff --git a/apps/studio/hooks/ui/useUrlState.ts b/apps/studio/hooks/ui/useUrlState.tsindex ef8bf5c0657..d08217df167 100644--- a/apps/studio/hooks/ui/useUrlState.ts+++ b/apps/studio/hooks/ui/useUrlState.ts@@ -1,11 +1,11 @@-import { useCallback } from 'react'import { useRouter } from 'next/router'+import { Dispatch, SetStateAction, useCallback, useMemo } from 'react'export type UrlStateParams = {[k: string]: string | string[] | undefined}-export function useUrlState({+export function useUrlState<ValueParams extends UrlStateParams>({replace = true,arrayKeys = [],}: {@@ -14,22 +14,26 @@ export function useUrlState({*/replace?: booleanarrayKeys?: string[]-} = {}) {- const arrayKeysSet = new Set(arrayKeys)+} = {}): [ValueParams, Dispatch<SetStateAction<ValueParams>>] {+ const stringifiedArrayKeys = JSON.stringify(arrayKeys)+ // eslint-disable-next-line react-hooks/exhaustive-deps+ const arrayKeysSet = useMemo(() => new Set(arrayKeys), [stringifiedArrayKeys])const router = useRouter()- const params: UrlStateParams = Object.fromEntries(- Object.entries(router.query).map(([key, value]) => {- if (arrayKeysSet.has(key)) {- return Array.isArray(value) ? [key, value] : [key, [value]]- }+ const params: ValueParams = useMemo(() => {+ return Object.fromEntries(+ Object.entries(router.query).map(([key, value]) => {+ if (arrayKeysSet.has(key)) {+ return Array.isArray(value) ? [key, value] : [key, [value]]+ }- return [key, value]- })- )+ return [key, value]+ })+ )+ }, [arrayKeysSet, router.query])- const setParams = useCallback(- (newParams: UrlStateParams | ((previousParams: UrlStateParams) => UrlStateParams)) => {+ const setParams: Dispatch<SetStateAction<ValueParams>> = useCallback(+ (newParams) => {const nextParams = typeof newParams === 'function' ? newParams(params) : newParamslet newQuery = Object.fromEntries(Object.entries({ ...params, ...nextParams }).filter(([, value]) => Boolean(value))diff --git a/apps/studio/pages/project/[ref]/auth/policies.tsx b/apps/studio/pages/project/[ref]/auth/policies.tsxindex 46b944bdb1a..48b0060ca3f 100644--- a/apps/studio/pages/project/[ref]/auth/policies.tsx+++ b/apps/studio/pages/project/[ref]/auth/policies.tsx@@ -1,10 +1,7 @@import type { PostgresPolicy, PostgresTable } from '@supabase/postgres-meta'import { PermissionAction } from '@supabase/shared-types/out/constants'-import { partition } from 'lodash'-import { useEffect, useState } from 'react'+import { useState } from 'react'-import { useParams } from 'common'-import { useIsRLSAIAssistantEnabled } from 'components/interfaces/App/FeaturePreview/FeaturePreviewContext'import { Policies } from 'components/interfaces/Auth/Policies'import { AIPolicyEditorPanel } from 'components/interfaces/Auth/Policies/AIPolicyEditorPanel'import { AuthLayout } from 'components/layouts'@@ -16,9 +13,9 @@ import { GenericSkeletonLoader } from 'components/ui/ShimmeringLoader'import { useDatabasePoliciesQuery } from 'data/database-policies/database-policies-query'import { useSchemasQuery } from 'data/database/schemas-query'import { useTablesQuery } from 'data/tables/tables-query'-import { useCheckPermissions, usePermissionsLoaded } from 'hooks'+import { useCheckPermissions, usePermissionsLoaded, useUrlState } from 'hooks'import { EXCLUDED_SCHEMAS } from 'lib/constants/schemas'-import { useTableEditorStateSnapshot } from 'state/table-editor'+import { partition } from 'lodash'import type { NextPageWithLayout } from 'types'import {Button,@@ -66,22 +63,16 @@ const onFilterTables = (}const AuthPoliciesPage: NextPageWithLayout = () => {- const { search, schema } = useParams()+ const [params, setParams] = useUrlState<{+ schema?: string+ search?: string+ }>()+ const { schema = 'public', search: searchString = '' } = paramsconst { project } = useProjectContext()- const snap = useTableEditorStateSnapshot()- const [searchString, setSearchString] = useState<string>('')const [showPolicyAiEditor, setShowPolicyAiEditor] = useState(false)const [selectedPolicyToEdit, setSelectedPolicyToEdit] = useState<PostgresPolicy>()- useEffect(() => {- if (search) setSearchString(search)- }, [search])-- useEffect(() => {- if (schema) snap.setSelectedSchemaName(schema)- }, [schema])-const { data: schemas } = useSchemasQuery({projectRef: project?.ref,connectionString: project?.connectionString,@@ -89,7 +80,7 @@ const AuthPoliciesPage: NextPageWithLayout = () => {const [protectedSchemas] = partition(schemas, (schema) =>EXCLUDED_SCHEMAS.includes(schema?.name ?? ''))- const selectedSchema = schemas?.find((schema) => schema.name === snap.selectedSchemaName)+ const selectedSchema = schemas?.find((s) => s.name === schema)const isLocked = protectedSchemas.some((s) => s.id === selectedSchema?.id)const { data: policies } = useDatabasePoliciesQuery({@@ -106,7 +97,7 @@ const AuthPoliciesPage: NextPageWithLayout = () => {} = useTablesQuery({projectRef: project?.ref,connectionString: project?.connectionString,- schema: snap.selectedSchemaName,+ schema: schema,})const filteredTables = onFilterTables(tables ?? [], policies ?? [], searchString)@@ -128,18 +119,20 @@ const AuthPoliciesPage: NextPageWithLayout = () => {className="w-[260px]"size="small"showError={false}- selectedSchemaName={snap.selectedSchemaName}- onSelectSchema={(schema: string) => {- snap.setSelectedSchemaName(schema)- setSearchString('')+ selectedSchemaName={schema}+ onSelectSchema={(schema) => {+ setParams({ ...params, search: undefined, schema })}}/><Inputsize="small"placeholder="Filter tables and policies"className="block w-64 text-sm placeholder-border-muted"- value={searchString}- onChange={(e) => setSearchString(e.target.value)}+ value={searchString || ''}+ onChange={(e) => {+ const str = e.target.value+ setParams({ ...params, search: str === '' ? undefined : str })+ }}icon={<IconSearch size="tiny" />}/></div>@@ -169,7 +162,7 @@ const AuthPoliciesPage: NextPageWithLayout = () => {{!canCreatePolicies? 'You need additional permissions to create RLS policies': schemaHasNoTables- ? `No table in schema ${snap.selectedSchemaName} to create policies on`+ ? `No table in schema ${schema} to create policies on`: null}</TooltipContent_Shadcn_>)}@@ -184,6 +177,7 @@ const AuthPoliciesPage: NextPageWithLayout = () => {{isSuccess && (<Policies+ schema={schema}tables={filteredTables}hasTables={tables.length > 0}isLocked={isLocked}@@ -196,6 +190,7 @@ const AuthPoliciesPage: NextPageWithLayout = () => {<AIPolicyEditorPanelvisible={showPolicyAiEditor}+ schema={schema}searchString={searchString}selectedPolicy={selectedPolicyToEdit}onSelectCancel={() => {