import { Link } from '@tanstack/react-router'
import { useParams } from 'common'
import { EyeIcon, EyeOffIcon, KeyRoundIcon, Loader2Icon } from 'lucide-react'
import { useRef, useState } from 'react'
import { toast } from 'sonner'
import {
Button,
Card,
CardContent,
cn,
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
Separator,
Switch,
Tooltip,
TooltipContent,
TooltipTrigger,
} from 'ui'
import { Admonition } from 'ui-patterns/Admonition'
import { CodeBlock } from 'ui-patterns/CodeBlock'
import { Input } from 'ui-patterns/DataInputs/Input'
import { FormLayout } from 'ui-patterns/form/Layout/FormLayout'
import {
PageSection,
PageSectionContent,
PageSectionDescription,
PageSectionMeta,
PageSectionSummary,
PageSectionTitle,
} from 'ui-patterns/PageSection'
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
import { ConnectSheetStep } from '../../ConnectSheet/ConnectSheetStep'
import { EnvRow } from '../../ConnectSheet/content/server/common/EnvRow'
import { CopyPromptButton } from '../../ConnectSheet/CopyPromptAdmonition'
import { useConnectSheetParams } from '../../ConnectSheet/useConnectSheetParams'
import type { WarehouseCatalogCredentials } from './Warehouse.utils'
import { AlertError } from '@/components/ui/AlertError'
import CopyButton from '@/components/ui/CopyButton'
import { useUpdateWarehouseCatalogMutation } from '@/data/warehouse/warehouse-catalog-mutation'
import { useWarehouseCatalogQuery } from '@/data/warehouse/warehouse-catalog-query'
import {
DUCKLAKE_METADATA_PASSWORD_ENV_VAR,
DUCKLAKE_S3_SECRET_ENV_VAR,
getDuckLakeSetupScript,
getWarehouseFlightSqlConnectionString,
getWarehouseFlightSqlEndpoint,
getWarehouseDatabowCommand,
parseWarehouseCatalogUrl,
} from '@/lib/warehouse'
/**
* Only engines we can produce real configuration for. DuckLake exposes a Postgres catalog over S3
* rather than an Iceberg REST catalog, so Trino, Spark and PyIceberg need API support first.
*/
const QUERY_ENGINES = [
{ value: 'flightsql', label: 'FlightSQL' },
{ value: 'duckdb', label: 'DuckDB' },
] as const
type QueryEngine = (typeof QUERY_ENGINES)[number]['value']
function FieldRow({
id,
label,
children,
}: {
id?: string
label: React.ReactNode
children: React.ReactNode
}) {
return (
{children}
)
}
const FlightSqlContent = ({ projectRef }: { projectRef: string }) => (
}>
Reset database password
)
const DuckLakeSecretRow = ({ name, value }: { name: string; value: string }) => {
const [isRevealed, setIsRevealed] = useState(false)
return (
: }
onClick={() => setIsRevealed((value) => !value)}
/>
{isRevealed ? 'Hide environment variable' : 'Reveal environment variable'}
)
}
/**
* The DuckDB setup script inlines everything except the two passwords, which it reads via
* `getenv()` -- so those are the only credential values surfaced as their own rows here.
*/
const DuckLakeEnvironmentVariables = ({
credentials,
password,
}: {
credentials: WarehouseCatalogCredentials
password: string
}) => {
const environmentVariables = [
`${DUCKLAKE_S3_SECRET_ENV_VAR}=${credentials.s3_secret_access_key}`,
`${DUCKLAKE_METADATA_PASSWORD_ENV_VAR}=${password}`,
].join('\n')
return (
)
}
const DuckLakeSetup = ({
credentials,
variant,
}: {
credentials: WarehouseCatalogCredentials
variant: 'default' | 'sheet'
}) => {
const connection = parseWarehouseCatalogUrl(credentials.catalog_url)
const stepsContainerRef = useRef(null)
if (connection === null) {
const errorContent = (
<>
>
)
return variant === 'sheet' ? (
{errorContent}
) : (
{errorContent}
)
}
const script = (
)
if (variant === 'default') {
return (
{script}
)
}
return (
Follow these steps
{script}
)
}
const CatalogAccessToggle = ({
projectRef,
isEnabled,
}: {
projectRef: string
isEnabled: boolean
}) => {
const catalogMutation = useUpdateWarehouseCatalogMutation({
onSuccess: (catalog) =>
toast.success(
catalog?.enabled ? 'DuckDB catalog access enabled' : 'DuckDB catalog access disabled'
),
})
return (
{catalogMutation.isPending ? 'Updating DuckDB catalog access' : ''}
{catalogMutation.isPending && (
)}
catalogMutation.mutate({ projectRef, body: { enabled } })}
/>
)
}
interface WarehouseConnectionCardProps {
variant?: 'default' | 'sheet'
}
export const WarehouseConnectionCard = ({ variant = 'default' }: WarehouseConnectionCardProps) => {
const { ref: projectRef } = useParams()
const { params, setConnectParams } = useConnectSheetParams()
const engine: QueryEngine = params.warehouseQueryEngine === 'duckdb' ? 'duckdb' : 'flightsql'
const {
data: catalog,
isPending: isCatalogPending,
isError: isCatalogError,
error: catalogError,
} = useWarehouseCatalogQuery({ projectRef }, { enabled: engine === 'duckdb' })
if (!projectRef) return null
let catalogStatus = ''
if (engine === 'duckdb' && isCatalogPending) catalogStatus = 'Loading DuckDB catalog access'
if (engine === 'duckdb' && !isCatalogPending && !isCatalogError) {
catalogStatus = 'DuckDB catalog access loaded'
}
const isSheet = variant === 'sheet'
const connectionCard = (
div]:border-0 [&>div]:p-0'
)}
>
{!isSheet && }
{engine === 'flightsql' && }
{engine === 'duckdb' && (
<>
{isCatalogPending && (
)}
{isCatalogError && (
)}
{catalog && }
{!isSheet && catalog?.enabled && catalog.credentials && (
)}
>
)}
{catalogStatus}
)
if (!isSheet) return connectionCard
return (
{connectionCard}
{engine === 'duckdb' && catalog?.enabled && catalog.credentials && (
)}
)
}
export const WarehouseConnectSection = () => {
return (
Connect
Point an analytical tool at Warehouse without querying your primary database.
)
}