mirror of
https://github.com/supabase/supabase.git
synced 2026-09-08 19:08:44 +08:00
## What kind of change does this PR introduce? Bug fix. Resolves [FE-4192](https://linear.app/supabase/issue/FE-4192/org-and-project-selectors-sometimes-dont-register-selections). ## What is the current behavior? Navigation actions sometimes nest links inside command or dropdown menu items. Closing the menu during selection can prevent the nested link navigation from registering. ## What is the new behavior? - Adds a documented Studio CommandItemLink composition that wraps command items with their navigation link. - Migrates all Studio command-item links, including organisation, project, function, database, branch, and integration actions. - Uses the dropdown menu asChild composition for both infrastructure-diagram Manage replica actions. - Preserves native link behaviour and leaves disabled command items non-navigable. ## To test - [ ] [Organisation and project selectors](https://studio-staging-git-dnywh-fe-4192-selector-links-supabase.vercel.app/dashboard/org): open the organisation selector and try an organisation, All Organizations, and New organization. Open a project, then use the project selector to switch projects and open New project. Confirm every action navigates on the first click. - [ ] [Branch selector](https://studio-staging-git-dnywh-fe-4192-selector-links-supabase.vercel.app/dashboard/project/_): in a project with branching enabled, open the branch selector. Switch branches and select Manage branches. Confirm both navigate on the first click. - [ ] [Database selector](https://studio-staging-git-dnywh-fe-4192-selector-links-supabase.vercel.app/dashboard/project/_/observability/query-performance): open the Source selector. Switch between the primary database and a read replica if available, then select Create a new read replica. Confirm selections apply and the footer action navigates on the first click. - [ ] [Function selector](https://studio-staging-git-dnywh-fe-4192-selector-links-supabase.vercel.app/dashboard/project/_/auth/hooks): select Add a new hook, choose a hook, select Postgres, then open the Postgres function selector and select New function. Confirm it navigates on the first click. - [ ] [Infrastructure diagram](https://studio-staging-git-dnywh-fe-4192-selector-links-supabase.vercel.app/dashboard/project/_/settings/infrastructure): for a project with a read replica, select Manage replica from both diagram variants. Confirm the replica settings open on the first click. - [ ] On any navigational row above, modifier-click and confirm native link behaviour is preserved. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added consistent link navigation across organization, project, branch, function, replica, and integration menus. * Added project-specific destinations to organization and project selectors. * Preserved disabled-item behavior while improving accessible command-menu link semantics. * **Bug Fixes** * Improved navigation and menu-closing behavior for command items and dropdown actions. * **Tests** * Added coverage for link destinations, accessibility roles, disabled states, and route preservation. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
240 lines
9.3 KiB
TypeScript
240 lines
9.3 KiB
TypeScript
import { useParams } from 'common'
|
|
import { noop } from 'lodash'
|
|
import { Check, ChevronDown, Loader2, Plus } from 'lucide-react'
|
|
import { parseAsBoolean, useQueryState } from 'nuqs'
|
|
import { useEffect, useState } from 'react'
|
|
import {
|
|
Button,
|
|
ButtonProps,
|
|
cn,
|
|
Command,
|
|
CommandGroup,
|
|
CommandItem,
|
|
CommandList,
|
|
Popover,
|
|
PopoverContent,
|
|
PopoverTrigger,
|
|
ScrollArea,
|
|
Tooltip,
|
|
TooltipContent,
|
|
TooltipTrigger,
|
|
} from 'ui'
|
|
|
|
import { Markdown } from '@/components/interfaces/Markdown'
|
|
import {
|
|
getAddReadReplicaPath,
|
|
getInfrastructurePath,
|
|
} from '@/components/interfaces/Settings/Infrastructure/Infrastructure.utils'
|
|
import { REPLICA_STATUS } from '@/components/interfaces/Settings/Infrastructure/ReadReplicas/ReadReplicas.constants'
|
|
import { CommandItemLink } from '@/components/ui/CommandItemLink'
|
|
import { useReadReplicasQuery } from '@/data/read-replicas/replicas-query'
|
|
import { formatDatabaseID, formatDatabaseRegion } from '@/data/read-replicas/replicas.utils'
|
|
import { useIsFeatureEnabled } from '@/hooks/misc/useIsFeatureEnabled'
|
|
import { IS_PLATFORM } from '@/lib/constants'
|
|
import { useDatabaseSelectorStateSnapshot } from '@/state/database-selector'
|
|
|
|
interface DatabaseSelectorProps {
|
|
selectedDatabaseId?: string // To override initial state
|
|
variant?: 'regular' | 'connected-on-right' | 'connected-on-left' | 'connected-on-both'
|
|
additionalOptions?: { id: string; name: string }[]
|
|
buttonProps?: ButtonProps
|
|
onSelectId?: (id: string) => void // Optional callback
|
|
className?: string
|
|
align?: 'start' | 'end'
|
|
isForm?: boolean
|
|
}
|
|
|
|
export const DatabaseSelector = ({
|
|
selectedDatabaseId: _selectedDatabaseId,
|
|
variant = 'regular',
|
|
additionalOptions = [],
|
|
onSelectId = noop,
|
|
buttonProps,
|
|
align = 'end',
|
|
className,
|
|
isForm = false,
|
|
}: DatabaseSelectorProps) => {
|
|
const { ref: projectRef } = useParams()
|
|
const [open, setOpen] = useState(false)
|
|
const [, setShowConnect] = useQueryState('showConnect', parseAsBoolean.withDefault(false))
|
|
|
|
const { infrastructureReadReplicas } = useIsFeatureEnabled(['infrastructure:read_replicas'])
|
|
|
|
const state = useDatabaseSelectorStateSnapshot()
|
|
const selectedDatabaseId = _selectedDatabaseId ?? state.selectedDatabaseId
|
|
|
|
const { data, isPending: isLoading, isSuccess } = useReadReplicasQuery({ projectRef })
|
|
const databases = data ?? []
|
|
const sortedDatabases = databases
|
|
.sort((a, b) => (a.inserted_at > b.inserted_at ? 1 : 0))
|
|
.sort((database) => (database.identifier === projectRef ? -1 : 0))
|
|
|
|
const selectedDatabase = databases.find((db) => db.identifier === selectedDatabaseId)
|
|
const selectedDatabaseRegion = formatDatabaseRegion(selectedDatabase?.region ?? '')
|
|
const formattedDatabaseId = formatDatabaseID(selectedDatabaseId ?? '')
|
|
|
|
const selectedAdditionalOption = additionalOptions.find((x) => x.id === selectedDatabaseId)
|
|
|
|
const newReplicaURL = getAddReadReplicaPath(projectRef)
|
|
|
|
useEffect(() => {
|
|
if (_selectedDatabaseId && !isForm) state.setSelectedDatabaseId(_selectedDatabaseId)
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, [_selectedDatabaseId])
|
|
|
|
return (
|
|
<Popover open={open} onOpenChange={setOpen} modal={false}>
|
|
<PopoverTrigger asChild>
|
|
<div className={cn('flex cursor-pointer', className)}>
|
|
{!isForm && (
|
|
<span className="flex items-center text-foreground-lighter px-3 rounded-lg rounded-r-none text-xs border border-button border-r-0">
|
|
Source
|
|
</span>
|
|
)}
|
|
<Button
|
|
variant="default"
|
|
icon={isLoading && <Loader2 className="animate-spin" />}
|
|
iconRight={<ChevronDown strokeWidth={1.5} size={12} />}
|
|
{...buttonProps}
|
|
className={cn(
|
|
'justify-start',
|
|
!isForm && 'rounded-l-none',
|
|
variant === 'connected-on-right' && 'rounded-r-none',
|
|
variant === 'connected-on-left' && 'rounded-l-none border-l-0',
|
|
variant === 'connected-on-both' && 'rounded-none border-x-0',
|
|
buttonProps?.className
|
|
)}
|
|
>
|
|
{selectedAdditionalOption ? (
|
|
<span>{selectedAdditionalOption.name}</span>
|
|
) : (
|
|
<>
|
|
<span className="capitalize">
|
|
{isLoading || selectedDatabase?.identifier === projectRef
|
|
? 'Primary database'
|
|
: 'Read replica'}
|
|
</span>{' '}
|
|
{isSuccess && selectedDatabase?.identifier !== projectRef && (
|
|
<span>
|
|
({selectedDatabaseRegion} - {formattedDatabaseId})
|
|
</span>
|
|
)}
|
|
</>
|
|
)}
|
|
</Button>
|
|
</div>
|
|
</PopoverTrigger>
|
|
<PopoverContent className="p-0 w-64" side="bottom" align={align}>
|
|
<Command>
|
|
<CommandList>
|
|
{additionalOptions.length > 0 && (
|
|
<CommandGroup className="border-b">
|
|
{additionalOptions.map((option) => (
|
|
<CommandItem
|
|
key={option.id}
|
|
value={option.id}
|
|
className="cursor-pointer w-full"
|
|
onSelect={() => {
|
|
if (!isForm) state.setSelectedDatabaseId(option.id)
|
|
setOpen(false)
|
|
onSelectId(option.id)
|
|
}}
|
|
onClick={() => {
|
|
if (!isForm) state.setSelectedDatabaseId(option.id)
|
|
setOpen(false)
|
|
onSelectId(option.id)
|
|
}}
|
|
>
|
|
<div className="w-full flex items-center justify-between">
|
|
<p>{option.name}</p>
|
|
{option.id === selectedDatabaseId && <Check size={14} />}
|
|
</div>
|
|
</CommandItem>
|
|
))}
|
|
</CommandGroup>
|
|
)}
|
|
<CommandGroup>
|
|
<ScrollArea className={(databases || []).length > 7 ? 'h-[210px]' : ''}>
|
|
{sortedDatabases?.map((database) => {
|
|
const region = formatDatabaseRegion(database.region)
|
|
const id = formatDatabaseID(database.identifier)
|
|
|
|
if (database.status !== 'ACTIVE_HEALTHY') {
|
|
const status = [
|
|
REPLICA_STATUS.INIT_READ_REPLICA,
|
|
REPLICA_STATUS.COMING_UP,
|
|
].includes(database.status)
|
|
? 'coming up'
|
|
: 'not healthy'
|
|
|
|
return (
|
|
<Tooltip key={database.identifier}>
|
|
<TooltipTrigger asChild>
|
|
<div className="px-2 py-1.5 w-full flex items-center justify-between">
|
|
<p className="text-xs text-foreground-lighter">
|
|
Read replica ({region} - {id})
|
|
</p>
|
|
</div>
|
|
</TooltipTrigger>
|
|
<TooltipContent side="right" className="w-80">
|
|
<Markdown
|
|
className="text-xs text-foreground"
|
|
content={`Replica unable to accept requests as its ${status}. [View infrastructure settings](${getInfrastructurePath(projectRef)}) for more information.`}
|
|
/>
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
)
|
|
}
|
|
|
|
return (
|
|
<CommandItem
|
|
key={database.identifier}
|
|
value={database.identifier}
|
|
className="cursor-pointer w-full"
|
|
onSelect={() => {
|
|
if (!isForm) state.setSelectedDatabaseId(database.identifier)
|
|
setOpen(false)
|
|
onSelectId(database.identifier)
|
|
}}
|
|
onClick={() => {
|
|
if (!isForm) state.setSelectedDatabaseId(database.identifier)
|
|
setOpen(false)
|
|
onSelectId(database.identifier)
|
|
}}
|
|
>
|
|
<div className="w-full flex items-center justify-between">
|
|
<p>
|
|
{database.identifier === projectRef
|
|
? 'Primary database'
|
|
: `Read replica (${region} - ${id})`}
|
|
</p>
|
|
{database.identifier === selectedDatabaseId && <Check size={16} />}
|
|
</div>
|
|
</CommandItem>
|
|
)
|
|
})}
|
|
</ScrollArea>
|
|
</CommandGroup>
|
|
|
|
{IS_PLATFORM && infrastructureReadReplicas && (
|
|
<CommandGroup className="border-t">
|
|
<CommandItemLink
|
|
href={newReplicaURL}
|
|
className="cursor-pointer w-full gap-2"
|
|
onSelect={() => {
|
|
setOpen(false)
|
|
setShowConnect(false)
|
|
}}
|
|
>
|
|
<Plus size={14} strokeWidth={1.5} />
|
|
<p>Create a new read replica</p>
|
|
</CommandItemLink>
|
|
</CommandGroup>
|
|
)}
|
|
</CommandList>
|
|
</Command>
|
|
</PopoverContent>
|
|
</Popover>
|
|
)
|
|
}
|