import { noop } from 'lodash' import { Check, ChevronDown, Loader2, Plus } from 'lucide-react' import Link from 'next/link' import { useRouter } from 'next/router' import { parseAsBoolean, useQueryState } from 'nuqs' import { useEffect, useState } from 'react' import { useParams } from 'common' import { Markdown } from 'components/interfaces/Markdown' import { REPLICA_STATUS } from 'components/interfaces/Settings/Infrastructure/InfrastructureConfiguration/InstanceConfiguration.constants' 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' import { Button, ButtonProps, CommandGroup_Shadcn_, CommandItem_Shadcn_, CommandList_Shadcn_, Command_Shadcn_, PopoverContent_Shadcn_, PopoverTrigger_Shadcn_, Popover_Shadcn_, ScrollArea, Tooltip, TooltipContent, TooltipTrigger, cn, } from 'ui' 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 onCreateReplicaClick?: () => void portal?: boolean className?: string } const DatabaseSelector = ({ selectedDatabaseId: _selectedDatabaseId, variant = 'regular', additionalOptions = [], onSelectId = noop, buttonProps, onCreateReplicaClick = noop, portal = true, className, }: DatabaseSelectorProps) => { const router = useRouter() 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, 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) useEffect(() => { if (_selectedDatabaseId) state.setSelectedDatabaseId(_selectedDatabaseId) // eslint-disable-next-line react-hooks/exhaustive-deps }, [_selectedDatabaseId]) return (
Source
{additionalOptions.length > 0 && ( {additionalOptions.map((option) => ( { state.setSelectedDatabaseId(option.id) setOpen(false) onSelectId(option.id) }} onClick={() => { 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 ( { state.setSelectedDatabaseId(database.identifier) setOpen(false) onSelectId(database.identifier) }} onClick={() => { state.setSelectedDatabaseId(database.identifier) setOpen(false) onSelectId(database.identifier) }} >

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

{database.identifier === selectedDatabaseId && }
) })}
{IS_PLATFORM && infrastructureReadReplicas && ( { setOpen(false) router.push(`/project/${projectRef}/settings/infrastructure`) }} onClick={() => setOpen(false)} > { setOpen(false) // [Joshen] This is used in the Connect UI which is available across all pages setShowConnect(null) onCreateReplicaClick?.() }} className="w-full flex items-center gap-2" >

Create a new read replica

)}
) } export default DatabaseSelector