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 { useIsHighAvailability } from '@/hooks/misc/useSelectedProject' 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 isHighAvailability = useIsHighAvailability() 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 (
{!isForm && ( Source )}
{additionalOptions.length > 0 && ( {additionalOptions.map((option) => ( { if (!isForm) state.setSelectedDatabaseId(option.id) setOpen(false) onSelectId(option.id) }} onClick={() => { if (!isForm) state.setSelectedDatabaseId(option.id) setOpen(false) onSelectId(option.id) }} >

{option.name}

{option.id === selectedDatabaseId && }
))}
)} 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 (

Read replica ({region} - {id})

) } return ( { if (!isForm) state.setSelectedDatabaseId(database.identifier) setOpen(false) onSelectId(database.identifier) }} onClick={() => { if (!isForm) state.setSelectedDatabaseId(database.identifier) setOpen(false) onSelectId(database.identifier) }} >

{database.identifier === projectRef ? 'Primary database' : `Read replica (${region} - ${id})`}

{database.identifier === selectedDatabaseId && }
) })}
{IS_PLATFORM && infrastructureReadReplicas && !isHighAvailability && ( { setOpen(false) setShowConnect(false) }} >

Create a new read replica

)}
) }