Files
supabase/apps/studio/components/interfaces/Settings/API/ExposedSchemaSelector.tsx
Joshen Lim ba39e9c387 Adjust Data API exposed schema (#46260)
## Context

Builds on top of the work done previously here:
https://github.com/supabase/supabase/pull/46169

In the Data API settings, only non-protected schemas are allowed to be
exposed - in which `pgmq_public` was labelled as protected, resulting in
an odd situation whereby if users expose the `pgmq_public` schema via
the queue settings, they'll see this UI message
<img width="762" height="238" alt="image"
src="https://github.com/user-attachments/assets/1ccac832-9524-40a9-956d-bbbda8a7e136"
/>

The `pgmq_public` schema was intended to be public, much like how
`graphql_public` schema was, hence we're exposing that schema to be
selectable in the Data API exposed schemas dropdown.

Am also making a couple of changes to adjust the UI a little
- Change missing schema text to be less alarming (reserve red for
destructive actions - otherwise the visual signal is inaccurate and
might cause unnecessary distress)
<img width="465" height="113" alt="image"
src="https://github.com/user-attachments/assets/bdc30d9c-7898-4b25-9c4b-bc5aafa22076"
/>
<img width="488" height="112" alt="image"
src="https://github.com/user-attachments/assets/f3e4459c-c670-4321-9f42-93e7abe69a00"
/>

- Highlight if a protected schema is being exposed with warning colors 
<img width="501" height="105" alt="image"
src="https://github.com/user-attachments/assets/b8bff3b8-9635-4e57-96d2-80b5ad33f53f"
/>

- Adjust text of missing schema in dropdown to text-foreground-lighter
instead of red (It's not necessarily a problem, just a clean up so again
just a visual signal thing)
<img width="461" height="249" alt="image"
src="https://github.com/user-attachments/assets/7f0632df-bee5-4911-bd3c-8a1cd6fb2f1f"
/>
<img width="442" height="207" alt="image"
src="https://github.com/user-attachments/assets/bad266aa-84bf-4de7-b62a-4362f85b9481"
/>



<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* Inline help text now surfaces schema exposure status directly in the
configuration UI.

* **Bug Fixes**
  * Improved filtering of internal schemas considered exposable.
* Messaging updated to show counts and distinct styling: protected
internal schemas render a warning and should be removed, while
missing/nonexistent schemas show a lighter "safe to remove" note.

<!-- review_stack_entry_start -->

[![Review Change
Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/46260?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack)

<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-05-22 16:05:24 +07:00

180 lines
6.2 KiB
TypeScript

import { Check, ChevronsUpDown } from 'lucide-react'
import { useMemo, useState } from 'react'
import {
Button,
cn,
Command,
CommandEmpty,
CommandGroup,
CommandInput,
CommandItem,
CommandList,
Popover,
PopoverContent,
PopoverTrigger,
ScrollArea,
} from 'ui'
import { ShimmeringLoader } from 'ui-patterns/ShimmeringLoader'
import { useSchemasQuery } from '@/data/database/schemas-query'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { INTERNAL_SCHEMAS } from '@/hooks/useProtectedSchemas'
import { pluralize } from '@/lib/helpers'
/**
* [Joshen] This would only affect graphql_public and pgmq_public, given that they're intended
* to be public, we can let users expose them via the API, but not let them adjust the schema via the dashboard
* */
export const internalSchemasCannotExpose = new Set(
INTERNAL_SCHEMAS.filter((x) => !x.endsWith('_public'))
)
interface ExposedSchemaSelectorProps {
disabled?: boolean
selectedSchemas: string[]
onToggleSchema: (schema: string) => void
}
export const ExposedSchemaSelector = ({
disabled = false,
selectedSchemas,
onToggleSchema,
}: ExposedSchemaSelectorProps) => {
const [open, setOpen] = useState(false)
const { data: project } = useSelectedProjectQuery()
const {
data: allSchemas,
isPending,
isError,
isSuccess,
} = useSchemasQuery({
projectRef: project?.ref,
connectionString: project?.connectionString,
})
const schemas = useMemo(
() =>
(allSchemas ?? [])
.filter((s) => !internalSchemasCannotExpose.has(s.name))
.sort((a, b) => a.name.localeCompare(b.name)),
[allSchemas]
)
const missingExposedSchema = useMemo(
() => selectedSchemas.filter((schema) => !schemas.some((s) => s.name === schema)),
[schemas, selectedSchemas]
)
const selectedSet = useMemo(() => new Set(selectedSchemas), [selectedSchemas])
const selectedCount = schemas.filter((s) => selectedSet.has(s.name)).length
return (
<Popover open={open} onOpenChange={setOpen} modal={false}>
<PopoverTrigger asChild>
<Button
size="small"
disabled={disabled}
type="default"
className="w-full [&>span]:w-full pr-1! space-x-1"
iconRight={<ChevronsUpDown className="text-foreground-muted" strokeWidth={2} size={14} />}
>
<div className="w-full flex gap-1">
<p className="text-foreground-lighter">
{isSuccess
? `${selectedCount} of ${schemas.length} ${pluralize(schemas.length, 'schema')} exposed`
: 'Loading schemas...'}
</p>
</div>
</Button>
</PopoverTrigger>
<PopoverContent
className="p-0 min-w-[200px] pointer-events-auto"
side="bottom"
align="start"
sameWidthAsTrigger
>
<Command>
<CommandInput className="text-xs" placeholder="Find schema..." />
<CommandList>
<CommandGroup>
{isPending ? (
<>
<div className="px-2 py-1">
<ShimmeringLoader className="py-2" />
</div>
<div className="px-2 py-1 w-4/5">
<ShimmeringLoader className="py-2" />
</div>
</>
) : isError ? (
<div className="flex items-center py-3 justify-center">
<p className="text-xs text-foreground-lighter">Failed to retrieve schemas</p>
</div>
) : (
<>
<CommandEmpty>
<p className="text-xs text-center text-foreground-lighter py-3">
No schemas found
</p>
</CommandEmpty>
<ScrollArea className={schemas.length > 7 ? 'h-[210px]' : ''}>
{missingExposedSchema.map((schema) => (
<CommandItem
key={schema}
value={schema}
className="cursor-pointer w-full"
onSelect={() => {
onToggleSchema(schema)
}}
>
<div className="w-full flex flex-col">
<div className="w-full flex items-center gap-x-2">
<Check size={16} className="text-brand shrink-0" />
<span className="truncate">{schema}</span>
</div>
{internalSchemasCannotExpose.has(schema) ? (
<span className="pl-6 text-warning text-xs tracking-tight">
This schema is protected and should not be exposed
</span>
) : (
<span className="pl-6 text-foreground-lighter text-xs tracking-tight">
This schema does not exist and can be safely removed
</span>
)}
</div>
</CommandItem>
))}
{schemas.map((schema) => {
const isExposed = selectedSet.has(schema.name)
return (
<CommandItem
key={schema.id}
value={schema.name}
className="cursor-pointer w-full"
onSelect={() => {
onToggleSchema(schema.name)
}}
>
<div
className={cn('w-full flex items-center gap-x-2', !isExposed && 'ml-6')}
>
{isExposed && <Check size={16} className="text-brand shrink-0" />}
<span className="truncate">{schema.name}</span>
</div>
</CommandItem>
)
})}
</ScrollArea>
</>
)}
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
)
}