import { useParams } from 'common' import { useMemo, useState } from 'react' import { Button, Card, CardContent, CardFooter, CommandGroup } from 'ui' import { FormLayout } from 'ui-patterns/form/Layout/FormLayout' import { MultiSelector } from 'ui-patterns/multi-select' import { PageSection, PageSectionContent, PageSectionDescription, PageSectionMeta, PageSectionSummary, PageSectionTitle, } from 'ui-patterns/PageSection' import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader' import { buildSchemasWithTables, buildSelectionFromPublicationTables, buildWarehouseSetupTargets, getSchemaTableKey, hasSelectionChanged, type SchemaTableSelection, type SchemaWithTables, type WarehouseSetupTable, type WarehouseSetupTarget, } from './Warehouse.utils' import { AlertError } from '@/components/ui/AlertError' import { useSchemasQuery } from '@/data/database/schemas-query' import { useReplicationPublicationQuery } from '@/data/replication/publication-query' import { useReplicationSourcesQuery } from '@/data/replication/sources-query' import { useTablesQuery } from '@/data/tables/tables-query' import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject' import { WAREHOUSE_PUBLICATION_NAME } from '@/lib/warehouse' export interface WarehouseSchemaTablePickerProps { onSubmit: (targets: WarehouseSetupTarget[]) => void isSubmitting: boolean /** Set when editing an already-enabled Warehouse rather than setting one up for the first time. */ isEditing?: boolean /** Failure from the submit itself, rendered inline rather than as a toast the user can lose. */ error?: { message: string } | null initialTables?: Pick[] } export const WarehouseSchemaTablePicker = ({ onSubmit, isSubmitting, isEditing = false, error, initialTables, }: WarehouseSchemaTablePickerProps) => { const { ref: projectRef } = useParams() const { data: project } = useSelectedProjectQuery() // `null` until the user touches a checkbox, so the selection seeded from the existing // publication can arrive asynchronously without an effect syncing it into state. const [selectionOverride, setSelectionOverride] = useState(null) const { data: schemas, isPending: isSchemasPending, isError: isSchemasError, error: schemasError, } = useSchemasQuery({ projectRef, connectionString: project?.connectionString }) const { data: tables, isPending: isTablesPending, isError: isTablesError, error: tablesError, } = useTablesQuery({ projectRef, connectionString: project?.connectionString }) // The `supabase_warehouse` publication is the source of truth for what's currently replicated. // Reading the sources query directly (rather than via useReplicationSourceId) to get its // loading state: the publications query stays disabled until a source id exists, so without it // the list would render un-checked and then flash back to a loader once publications kick in. const { data: sourcesData, isLoading: isSourcesLoading, isError: isSourcesError, error: sourcesError, } = useReplicationSourcesQuery({ projectRef }, { enabled: isEditing }) const sourceId = sourcesData?.sources.find((source) => source.name === projectRef)?.id const { data: publication, isError: isPublicationsError, error: publicationsError, } = useReplicationPublicationQuery( { projectRef, sourceId, publicationName: WAREHOUSE_PUBLICATION_NAME, }, { enabled: isEditing } ) // Derived from data presence rather than fetch status, so there's no render gap between the // publication query becoming enabled and it actually starting to fetch. const isSelectionPending = isEditing && (isSourcesLoading || (sourceId !== undefined && publication === undefined && !isPublicationsError)) const initialSelection = useMemo( () => buildSelectionFromPublicationTables( isEditing ? (publication?.tables ?? []) : (initialTables ?? []) ), [isEditing, publication, initialTables] ) const schemasWithTables: SchemaWithTables[] = useMemo(() => { if (!schemas || !tables) return [] return buildSchemasWithTables(schemas, tables) }, [schemas, tables]) const tableKeys = schemasWithTables.flatMap(({ schema, tables }) => tables.map((table) => getSchemaTableKey(schema, table)) ) const selection = selectionOverride ?? initialSelection const selectedTableKeys = tableKeys.filter((key) => selection[key]) const selectedCount = selectedTableKeys.length const hasChanges = hasSelectionChanged(selection, initialSelection) const updateSelection = (updater: (current: SchemaTableSelection) => SchemaTableSelection) => { setSelectionOverride((prev) => updater(prev ?? initialSelection)) } const handleSchemaSelection = (schema: SchemaWithTables) => { const keys = schema.tables.map((table) => getSchemaTableKey(schema.schema, table)) const areAllSelected = keys.every((key) => selection[key]) updateSelection((current) => ({ ...current, ...Object.fromEntries(keys.map((key) => [key, !areAllSelected])), })) } const handleSubmit = () => { const targets = buildWarehouseSetupTargets(selection, schemasWithTables) if (targets.length === 0) return onSubmit(targets) } // When editing, wait for the publication so its selection is in place before the user can // start toggling. First-time setup always starts empty and does not need to wait for it. if (isSchemasPending || isTablesPending || isSelectionPending) { return ( Tables {isEditing ? 'Tables currently replicating are selected. Changes apply on save.' : 'Choose which schemas or tables to replicate. You can change this at any time.'} ) } if (isSchemasError) { return ( ) } if (isTablesError) { return ( ) } if (isEditing && isSourcesError) { return ( ) } // Only blocking when editing: a first-time setup starts from an empty selection anyway, so a // failed publication lookup shouldn't stop the user from enabling Warehouse at all. if (isEditing && isPublicationsError) { return ( ) } return ( Tables {isEditing ? 'Tables currently replicating are selected. Changes apply on save.' : 'Choose which schemas or tables to replicate. You can change this at any time.'} {!!error && ( )} {`${selectedCount} ${selectedCount === 1 ? 'table' : 'tables'} selected`} } > { const normalizedSearch = search.toLowerCase() return [value, ...(keywords ?? [])].some((candidate) => candidate.toLowerCase().includes(normalizedSearch) ) ? 1 : 0 }} onValuesChange={(values) => { const nextValues = new Set(values) updateSelection(() => Object.fromEntries(tableKeys.map((key) => [key, nextValues.has(key)])) ) }} className="w-full" > {value}} className="w-full" /> {schemasWithTables .filter((schema) => schema.tables.length > 0) .map((schema) => { const keys = schema.tables.map((table) => getSchemaTableKey(schema.schema, table) ) const areAllSelected = keys.every((key) => selection[key]) return ( {schema.schema} } > {schema.tables.map((table) => { const key = getSchemaTableKey(schema.schema, table) return ( {table} ) })} ) })} {/* An empty selection is a valid request that tears Warehouse down, so submitting one from here would destroy a project's Warehouse with no confirmation. Disabling keeps teardown on the dedicated action, which asks first. */} {isEditing && hasChanges && ( )} ) }